| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/host_content_settings_map.h" | 5 #include "chrome/browser/host_content_settings_map.h" |
| 6 | 6 |
| 7 #include "base/auto_reset.h" |
| 7 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 8 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
| 9 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
| 10 #include "chrome/browser/prefs/pref_service.h" | 11 #include "chrome/browser/prefs/pref_service.h" |
| 11 #include "chrome/common/chrome_switches.h" | 12 #include "chrome/common/chrome_switches.h" |
| 12 #include "chrome/common/notification_registrar.h" | 13 #include "chrome/common/notification_registrar.h" |
| 13 #include "chrome/common/notification_service.h" | 14 #include "chrome/common/notification_service.h" |
| 14 #include "chrome/common/pref_names.h" | 15 #include "chrome/common/pref_names.h" |
| 15 #include "chrome/common/url_constants.h" | 16 #include "chrome/common/url_constants.h" |
| 16 #include "chrome/test/testing_profile.h" | 17 #include "chrome/test/testing_profile.h" |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 host_content_settings_map->GetDefaultContentSetting( | 83 host_content_settings_map->GetDefaultContentSetting( |
| 83 CONTENT_SETTINGS_TYPE_JAVASCRIPT)); | 84 CONTENT_SETTINGS_TYPE_JAVASCRIPT)); |
| 84 host_content_settings_map->SetDefaultContentSetting( | 85 host_content_settings_map->SetDefaultContentSetting( |
| 85 CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK); | 86 CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK); |
| 86 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 87 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 87 host_content_settings_map->GetDefaultContentSetting( | 88 host_content_settings_map->GetDefaultContentSetting( |
| 88 CONTENT_SETTINGS_TYPE_IMAGES)); | 89 CONTENT_SETTINGS_TYPE_IMAGES)); |
| 89 EXPECT_EQ(CONTENT_SETTING_ALLOW, host_content_settings_map->GetContentSetting( | 90 EXPECT_EQ(CONTENT_SETTING_ALLOW, host_content_settings_map->GetContentSetting( |
| 90 GURL(chrome::kChromeUINewTabURL), | 91 GURL(chrome::kChromeUINewTabURL), |
| 91 CONTENT_SETTINGS_TYPE_IMAGES, "")); | 92 CONTENT_SETTINGS_TYPE_IMAGES, "")); |
| 92 host_content_settings_map->SetDefaultContentSetting( | 93 { |
| 93 CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_ASK); | 94 // Click-to-play needs to be enabled to set the content setting to ASK. |
| 94 EXPECT_EQ(CONTENT_SETTING_ASK, | 95 CommandLine* cmd = CommandLine::ForCurrentProcess(); |
| 95 host_content_settings_map->GetDefaultContentSetting( | 96 AutoReset<CommandLine> auto_reset(cmd, *cmd); |
| 96 CONTENT_SETTINGS_TYPE_PLUGINS)); | 97 cmd->AppendSwitch(switches::kEnableClickToPlay); |
| 98 |
| 99 host_content_settings_map->SetDefaultContentSetting( |
| 100 CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_ASK); |
| 101 EXPECT_EQ(CONTENT_SETTING_ASK, |
| 102 host_content_settings_map->GetDefaultContentSetting( |
| 103 CONTENT_SETTINGS_TYPE_PLUGINS)); |
| 104 } |
| 97 host_content_settings_map->SetDefaultContentSetting( | 105 host_content_settings_map->SetDefaultContentSetting( |
| 98 CONTENT_SETTINGS_TYPE_POPUPS, CONTENT_SETTING_ALLOW); | 106 CONTENT_SETTINGS_TYPE_POPUPS, CONTENT_SETTING_ALLOW); |
| 99 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 107 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 100 host_content_settings_map->GetDefaultContentSetting( | 108 host_content_settings_map->GetDefaultContentSetting( |
| 101 CONTENT_SETTINGS_TYPE_POPUPS)); | 109 CONTENT_SETTINGS_TYPE_POPUPS)); |
| 102 host_content_settings_map->ResetToDefaults(); | 110 host_content_settings_map->ResetToDefaults(); |
| 103 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 111 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 104 host_content_settings_map->GetDefaultContentSetting( | 112 host_content_settings_map->GetDefaultContentSetting( |
| 105 CONTENT_SETTINGS_TYPE_PLUGINS)); | 113 CONTENT_SETTINGS_TYPE_PLUGINS)); |
| 106 | 114 |
| (...skipping 439 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 546 CONTENT_SETTINGS_TYPE_IMAGES, "", CONTENT_SETTING_BLOCK); | 554 CONTENT_SETTINGS_TYPE_IMAGES, "", CONTENT_SETTING_BLOCK); |
| 547 desired_settings.settings[CONTENT_SETTINGS_TYPE_IMAGES] = | 555 desired_settings.settings[CONTENT_SETTINGS_TYPE_IMAGES] = |
| 548 CONTENT_SETTING_BLOCK; | 556 CONTENT_SETTING_BLOCK; |
| 549 settings = | 557 settings = |
| 550 host_content_settings_map->GetNonDefaultContentSettings(host); | 558 host_content_settings_map->GetNonDefaultContentSettings(host); |
| 551 EXPECT_TRUE(SettingsEqual(desired_settings, settings)); | 559 EXPECT_TRUE(SettingsEqual(desired_settings, settings)); |
| 552 } | 560 } |
| 553 | 561 |
| 554 TEST_F(HostContentSettingsMapTest, ResourceIdentifier) { | 562 TEST_F(HostContentSettingsMapTest, ResourceIdentifier) { |
| 555 // This feature is currently behind a flag. | 563 // This feature is currently behind a flag. |
| 556 CommandLine cl(*CommandLine::ForCurrentProcess()); | 564 CommandLine* cmd = CommandLine::ForCurrentProcess(); |
| 557 CommandLine::ForCurrentProcess()->AppendSwitch( | 565 AutoReset<CommandLine> auto_reset(cmd, *cmd); |
| 558 switches::kEnableResourceContentSettings); | 566 cmd->AppendSwitch(switches::kEnableResourceContentSettings); |
| 559 | 567 |
| 560 TestingProfile profile; | 568 TestingProfile profile; |
| 561 HostContentSettingsMap* host_content_settings_map = | 569 HostContentSettingsMap* host_content_settings_map = |
| 562 profile.GetHostContentSettingsMap(); | 570 profile.GetHostContentSettingsMap(); |
| 563 | 571 |
| 564 GURL host("http://example.com/"); | 572 GURL host("http://example.com/"); |
| 565 HostContentSettingsMap::Pattern pattern("[*.]example.com"); | 573 HostContentSettingsMap::Pattern pattern("[*.]example.com"); |
| 566 std::string resource1("someplugin"); | 574 std::string resource1("someplugin"); |
| 567 std::string resource2("otherplugin"); | 575 std::string resource2("otherplugin"); |
| 568 | 576 |
| 569 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 577 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 570 host_content_settings_map->GetContentSetting( | 578 host_content_settings_map->GetContentSetting( |
| 571 host, CONTENT_SETTINGS_TYPE_PLUGINS, resource1)); | 579 host, CONTENT_SETTINGS_TYPE_PLUGINS, resource1)); |
| 572 host_content_settings_map->SetContentSetting(pattern, | 580 host_content_settings_map->SetContentSetting(pattern, |
| 573 CONTENT_SETTINGS_TYPE_PLUGINS, resource1, CONTENT_SETTING_BLOCK); | 581 CONTENT_SETTINGS_TYPE_PLUGINS, resource1, CONTENT_SETTING_BLOCK); |
| 574 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 582 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 575 host_content_settings_map->GetContentSetting( | 583 host_content_settings_map->GetContentSetting( |
| 576 host, CONTENT_SETTINGS_TYPE_PLUGINS, resource1)); | 584 host, CONTENT_SETTINGS_TYPE_PLUGINS, resource1)); |
| 577 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 585 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 578 host_content_settings_map->GetContentSetting( | 586 host_content_settings_map->GetContentSetting( |
| 579 host, CONTENT_SETTINGS_TYPE_PLUGINS, resource2)); | 587 host, CONTENT_SETTINGS_TYPE_PLUGINS, resource2)); |
| 580 *CommandLine::ForCurrentProcess() = cl; | |
| 581 } | 588 } |
| 582 | 589 |
| 583 TEST_F(HostContentSettingsMapTest, ResourceIdentifierPrefs) { | 590 TEST_F(HostContentSettingsMapTest, ResourceIdentifierPrefs) { |
| 584 // This feature is currently behind a flag. | 591 // This feature is currently behind a flag. |
| 585 CommandLine cl(*CommandLine::ForCurrentProcess()); | 592 CommandLine* cmd = CommandLine::ForCurrentProcess(); |
| 586 CommandLine::ForCurrentProcess()->AppendSwitch( | 593 AutoReset<CommandLine> auto_reset(cmd, *cmd); |
| 587 switches::kEnableResourceContentSettings); | 594 cmd->AppendSwitch(switches::kEnableResourceContentSettings); |
| 588 | 595 |
| 589 TestingProfile profile; | 596 TestingProfile profile; |
| 590 scoped_ptr<Value> value(base::JSONReader::Read( | 597 scoped_ptr<Value> value(base::JSONReader::Read( |
| 591 "{\"[*.]example.com\":{\"per_plugin\":{\"someplugin\":2}}}", false)); | 598 "{\"[*.]example.com\":{\"per_plugin\":{\"someplugin\":2}}}", false)); |
| 592 profile.GetPrefs()->Set(prefs::kContentSettingsPatterns, *value); | 599 profile.GetPrefs()->Set(prefs::kContentSettingsPatterns, *value); |
| 593 HostContentSettingsMap* host_content_settings_map = | 600 HostContentSettingsMap* host_content_settings_map = |
| 594 profile.GetHostContentSettingsMap(); | 601 profile.GetHostContentSettingsMap(); |
| 595 | 602 |
| 596 GURL host("http://example.com/"); | 603 GURL host("http://example.com/"); |
| 597 HostContentSettingsMap::Pattern pattern("[*.]example.com"); | 604 HostContentSettingsMap::Pattern pattern("[*.]example.com"); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 612 EXPECT_STREQ("{}", prefs_as_json.c_str()); | 619 EXPECT_STREQ("{}", prefs_as_json.c_str()); |
| 613 | 620 |
| 614 host_content_settings_map->SetContentSetting(pattern, | 621 host_content_settings_map->SetContentSetting(pattern, |
| 615 CONTENT_SETTINGS_TYPE_PLUGINS, resource2, CONTENT_SETTING_BLOCK); | 622 CONTENT_SETTINGS_TYPE_PLUGINS, resource2, CONTENT_SETTING_BLOCK); |
| 616 | 623 |
| 617 content_setting_prefs = | 624 content_setting_prefs = |
| 618 profile.GetPrefs()->GetDictionary(prefs::kContentSettingsPatterns); | 625 profile.GetPrefs()->GetDictionary(prefs::kContentSettingsPatterns); |
| 619 base::JSONWriter::Write(content_setting_prefs, false, &prefs_as_json); | 626 base::JSONWriter::Write(content_setting_prefs, false, &prefs_as_json); |
| 620 EXPECT_STREQ("{\"[*.]example.com\":{\"per_plugin\":{\"otherplugin\":2}}}", | 627 EXPECT_STREQ("{\"[*.]example.com\":{\"per_plugin\":{\"otherplugin\":2}}}", |
| 621 prefs_as_json.c_str()); | 628 prefs_as_json.c_str()); |
| 622 *CommandLine::ForCurrentProcess() = cl; | |
| 623 } | 629 } |
| 624 | 630 |
| 625 } // namespace | 631 } // namespace |
| OLD | NEW |