| 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 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 host_content_settings_map->GetDefaultContentSetting( | 84 host_content_settings_map->GetDefaultContentSetting( |
| 84 CONTENT_SETTINGS_TYPE_JAVASCRIPT)); | 85 CONTENT_SETTINGS_TYPE_JAVASCRIPT)); |
| 85 host_content_settings_map->SetDefaultContentSetting( | 86 host_content_settings_map->SetDefaultContentSetting( |
| 86 CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK); | 87 CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK); |
| 87 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 88 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 88 host_content_settings_map->GetDefaultContentSetting( | 89 host_content_settings_map->GetDefaultContentSetting( |
| 89 CONTENT_SETTINGS_TYPE_IMAGES)); | 90 CONTENT_SETTINGS_TYPE_IMAGES)); |
| 90 EXPECT_EQ(CONTENT_SETTING_ALLOW, host_content_settings_map->GetContentSetting( | 91 EXPECT_EQ(CONTENT_SETTING_ALLOW, host_content_settings_map->GetContentSetting( |
| 91 GURL(chrome::kChromeUINewTabURL), | 92 GURL(chrome::kChromeUINewTabURL), |
| 92 CONTENT_SETTINGS_TYPE_IMAGES, "")); | 93 CONTENT_SETTINGS_TYPE_IMAGES, "")); |
| 93 host_content_settings_map->SetDefaultContentSetting( | 94 { |
| 94 CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_ASK); | 95 // Click-to-play needs to be enabled to set the content setting to ASK. |
| 95 EXPECT_EQ(CONTENT_SETTING_ASK, | 96 CommandLine* cmd = CommandLine::ForCurrentProcess(); |
| 96 host_content_settings_map->GetDefaultContentSetting( | 97 AutoReset<CommandLine> auto_reset(cmd, *cmd); |
| 97 CONTENT_SETTINGS_TYPE_PLUGINS)); | 98 cmd->AppendSwitch(switches::kEnableClickToPlay); |
| 99 |
| 100 host_content_settings_map->SetDefaultContentSetting( |
| 101 CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_ASK); |
| 102 EXPECT_EQ(CONTENT_SETTING_ASK, |
| 103 host_content_settings_map->GetDefaultContentSetting( |
| 104 CONTENT_SETTINGS_TYPE_PLUGINS)); |
| 105 } |
| 98 host_content_settings_map->SetDefaultContentSetting( | 106 host_content_settings_map->SetDefaultContentSetting( |
| 99 CONTENT_SETTINGS_TYPE_POPUPS, CONTENT_SETTING_ALLOW); | 107 CONTENT_SETTINGS_TYPE_POPUPS, CONTENT_SETTING_ALLOW); |
| 100 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 108 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 101 host_content_settings_map->GetDefaultContentSetting( | 109 host_content_settings_map->GetDefaultContentSetting( |
| 102 CONTENT_SETTINGS_TYPE_POPUPS)); | 110 CONTENT_SETTINGS_TYPE_POPUPS)); |
| 103 host_content_settings_map->ResetToDefaults(); | 111 host_content_settings_map->ResetToDefaults(); |
| 104 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 112 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 105 host_content_settings_map->GetDefaultContentSetting( | 113 host_content_settings_map->GetDefaultContentSetting( |
| 106 CONTENT_SETTINGS_TYPE_PLUGINS)); | 114 CONTENT_SETTINGS_TYPE_PLUGINS)); |
| 107 | 115 |
| (...skipping 451 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 559 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 567 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 560 host_content_settings_map->GetContentSetting( | 568 host_content_settings_map->GetContentSetting( |
| 561 host, CONTENT_SETTINGS_TYPE_IMAGES, "")); | 569 host, CONTENT_SETTINGS_TYPE_IMAGES, "")); |
| 562 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 570 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 563 otr_map->GetContentSetting( | 571 otr_map->GetContentSetting( |
| 564 host, CONTENT_SETTINGS_TYPE_IMAGES, "")); | 572 host, CONTENT_SETTINGS_TYPE_IMAGES, "")); |
| 565 } | 573 } |
| 566 | 574 |
| 567 TEST_F(HostContentSettingsMapTest, MigrateObsoletePrefs) { | 575 TEST_F(HostContentSettingsMapTest, MigrateObsoletePrefs) { |
| 568 // This feature is currently behind a flag. | 576 // This feature is currently behind a flag. |
| 569 CommandLine cl(*CommandLine::ForCurrentProcess()); | 577 CommandLine* cmd = CommandLine::ForCurrentProcess(); |
| 570 CommandLine::ForCurrentProcess()->AppendSwitch( | 578 AutoReset<CommandLine> auto_reset(cmd, *cmd); |
| 571 switches::kEnableResourceContentSettings); | 579 cmd->AppendSwitch(switches::kEnableResourceContentSettings); |
| 572 | 580 |
| 573 TestingProfile profile; | 581 TestingProfile profile; |
| 574 PrefService* prefs = profile.GetPrefs(); | 582 PrefService* prefs = profile.GetPrefs(); |
| 575 | 583 |
| 576 // Set obsolete data. | 584 // Set obsolete data. |
| 577 prefs->SetInteger(prefs::kCookieBehavior, | 585 prefs->SetInteger(prefs::kCookieBehavior, |
| 578 net::StaticCookiePolicy::BLOCK_ALL_COOKIES); | 586 net::StaticCookiePolicy::BLOCK_ALL_COOKIES); |
| 579 | 587 |
| 580 ListValue popup_hosts; | 588 ListValue popup_hosts; |
| 581 popup_hosts.Append(new StringValue("[*.]example.com")); | 589 popup_hosts.Append(new StringValue("[*.]example.com")); |
| 582 prefs->Set(prefs::kPopupWhitelistedHosts, popup_hosts); | 590 prefs->Set(prefs::kPopupWhitelistedHosts, popup_hosts); |
| 583 | 591 |
| 584 HostContentSettingsMap* host_content_settings_map = | 592 HostContentSettingsMap* host_content_settings_map = |
| 585 profile.GetHostContentSettingsMap(); | 593 profile.GetHostContentSettingsMap(); |
| 586 | 594 |
| 587 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 595 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 588 host_content_settings_map->GetDefaultContentSetting( | 596 host_content_settings_map->GetDefaultContentSetting( |
| 589 CONTENT_SETTINGS_TYPE_COOKIES)); | 597 CONTENT_SETTINGS_TYPE_COOKIES)); |
| 590 | 598 |
| 591 GURL host("http://example.com"); | 599 GURL host("http://example.com"); |
| 592 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 600 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 593 host_content_settings_map->GetContentSetting( | 601 host_content_settings_map->GetContentSetting( |
| 594 host, CONTENT_SETTINGS_TYPE_POPUPS, "")); | 602 host, CONTENT_SETTINGS_TYPE_POPUPS, "")); |
| 595 *CommandLine::ForCurrentProcess() = cl; | |
| 596 } | 603 } |
| 597 | 604 |
| 598 // For a single Unicode encoded pattern, check if it gets converted to punycode | 605 // For a single Unicode encoded pattern, check if it gets converted to punycode |
| 599 // and old pattern gets deleted. | 606 // and old pattern gets deleted. |
| 600 TEST_F(HostContentSettingsMapTest, CanonicalizeExceptionsUnicodeOnly) { | 607 TEST_F(HostContentSettingsMapTest, CanonicalizeExceptionsUnicodeOnly) { |
| 601 TestingProfile profile; | 608 TestingProfile profile; |
| 602 PrefService* prefs = profile.GetPrefs(); | 609 PrefService* prefs = profile.GetPrefs(); |
| 603 | 610 |
| 604 // Set utf-8 data. | 611 // Set utf-8 data. |
| 605 DictionaryValue* all_settings_dictionary = | 612 DictionaryValue* all_settings_dictionary = |
| (...skipping 11 matching lines...) Expand all Loading... |
| 617 EXPECT_FALSE(all_settings_dictionary->GetDictionaryWithoutPathExpansion( | 624 EXPECT_FALSE(all_settings_dictionary->GetDictionaryWithoutPathExpansion( |
| 618 "[*.]\xC4\x87ira.com", &result)); | 625 "[*.]\xC4\x87ira.com", &result)); |
| 619 EXPECT_TRUE(all_settings_dictionary->GetDictionaryWithoutPathExpansion( | 626 EXPECT_TRUE(all_settings_dictionary->GetDictionaryWithoutPathExpansion( |
| 620 "[*.]xn--ira-ppa.com", &result)); | 627 "[*.]xn--ira-ppa.com", &result)); |
| 621 } | 628 } |
| 622 | 629 |
| 623 // If both Unicode and its punycode pattern exist, make sure we don't touch the | 630 // If both Unicode and its punycode pattern exist, make sure we don't touch the |
| 624 // settings for the punycode, and that Unicode pattern gets deleted. | 631 // settings for the punycode, and that Unicode pattern gets deleted. |
| 625 TEST_F(HostContentSettingsMapTest, CanonicalizeExceptionsUnicodeAndPunycode) { | 632 TEST_F(HostContentSettingsMapTest, CanonicalizeExceptionsUnicodeAndPunycode) { |
| 626 // This feature is currently behind a flag. | 633 // This feature is currently behind a flag. |
| 627 CommandLine cl(*CommandLine::ForCurrentProcess()); | 634 CommandLine* cmd = CommandLine::ForCurrentProcess(); |
| 628 CommandLine::ForCurrentProcess()->AppendSwitch( | 635 AutoReset<CommandLine> auto_reset(cmd, *cmd); |
| 629 switches::kEnableResourceContentSettings); | 636 cmd->AppendSwitch(switches::kEnableResourceContentSettings); |
| 630 | 637 |
| 631 TestingProfile profile; | 638 TestingProfile profile; |
| 632 | 639 |
| 633 scoped_ptr<Value> value(base::JSONReader::Read( | 640 scoped_ptr<Value> value(base::JSONReader::Read( |
| 634 "{\"[*.]\\xC4\\x87ira.com\":{\"per_plugin\":{\"pluginx\":2}}}", false)); | 641 "{\"[*.]\\xC4\\x87ira.com\":{\"per_plugin\":{\"pluginx\":2}}}", false)); |
| 635 profile.GetPrefs()->Set(prefs::kContentSettingsPatterns, *value); | 642 profile.GetPrefs()->Set(prefs::kContentSettingsPatterns, *value); |
| 636 | 643 |
| 637 // Set punycode equivalent, with different setting. | 644 // Set punycode equivalent, with different setting. |
| 638 scoped_ptr<Value> puny_value(base::JSONReader::Read( | 645 scoped_ptr<Value> puny_value(base::JSONReader::Read( |
| 639 "{\"[*.]xn--ira-ppa.com\":{\"per_plugin\":{\"pluginy\":2}}}", false)); | 646 "{\"[*.]xn--ira-ppa.com\":{\"per_plugin\":{\"pluginy\":2}}}", false)); |
| 640 profile.GetPrefs()->Set(prefs::kContentSettingsPatterns, *puny_value); | 647 profile.GetPrefs()->Set(prefs::kContentSettingsPatterns, *puny_value); |
| 641 | 648 |
| 642 // Initialize the content map. | 649 // Initialize the content map. |
| 643 profile.GetHostContentSettingsMap(); | 650 profile.GetHostContentSettingsMap(); |
| 644 | 651 |
| 645 const DictionaryValue* content_setting_prefs = | 652 const DictionaryValue* content_setting_prefs = |
| 646 profile.GetPrefs()->GetDictionary(prefs::kContentSettingsPatterns); | 653 profile.GetPrefs()->GetDictionary(prefs::kContentSettingsPatterns); |
| 647 std::string prefs_as_json; | 654 std::string prefs_as_json; |
| 648 base::JSONWriter::Write(content_setting_prefs, false, &prefs_as_json); | 655 base::JSONWriter::Write(content_setting_prefs, false, &prefs_as_json); |
| 649 EXPECT_STREQ("{\"[*.]xn--ira-ppa.com\":{\"per_plugin\":{\"pluginy\":2}}}", | 656 EXPECT_STREQ("{\"[*.]xn--ira-ppa.com\":{\"per_plugin\":{\"pluginy\":2}}}", |
| 650 prefs_as_json.c_str()); | 657 prefs_as_json.c_str()); |
| 651 | |
| 652 *CommandLine::ForCurrentProcess() = cl; | |
| 653 } | 658 } |
| 654 | 659 |
| 655 TEST_F(HostContentSettingsMapTest, NonDefaultSettings) { | 660 TEST_F(HostContentSettingsMapTest, NonDefaultSettings) { |
| 656 TestingProfile profile; | 661 TestingProfile profile; |
| 657 HostContentSettingsMap* host_content_settings_map = | 662 HostContentSettingsMap* host_content_settings_map = |
| 658 profile.GetHostContentSettingsMap(); | 663 profile.GetHostContentSettingsMap(); |
| 659 | 664 |
| 660 GURL host("http://example.com/"); | 665 GURL host("http://example.com/"); |
| 661 HostContentSettingsMap::Pattern pattern("[*.]example.com"); | 666 HostContentSettingsMap::Pattern pattern("[*.]example.com"); |
| 662 | 667 |
| 663 ContentSettings desired_settings(CONTENT_SETTING_DEFAULT); | 668 ContentSettings desired_settings(CONTENT_SETTING_DEFAULT); |
| 664 ContentSettings settings = | 669 ContentSettings settings = |
| 665 host_content_settings_map->GetNonDefaultContentSettings(host); | 670 host_content_settings_map->GetNonDefaultContentSettings(host); |
| 666 EXPECT_TRUE(SettingsEqual(desired_settings, settings)); | 671 EXPECT_TRUE(SettingsEqual(desired_settings, settings)); |
| 667 | 672 |
| 668 host_content_settings_map->SetContentSetting(pattern, | 673 host_content_settings_map->SetContentSetting(pattern, |
| 669 CONTENT_SETTINGS_TYPE_IMAGES, "", CONTENT_SETTING_BLOCK); | 674 CONTENT_SETTINGS_TYPE_IMAGES, "", CONTENT_SETTING_BLOCK); |
| 670 desired_settings.settings[CONTENT_SETTINGS_TYPE_IMAGES] = | 675 desired_settings.settings[CONTENT_SETTINGS_TYPE_IMAGES] = |
| 671 CONTENT_SETTING_BLOCK; | 676 CONTENT_SETTING_BLOCK; |
| 672 settings = | 677 settings = |
| 673 host_content_settings_map->GetNonDefaultContentSettings(host); | 678 host_content_settings_map->GetNonDefaultContentSettings(host); |
| 674 EXPECT_TRUE(SettingsEqual(desired_settings, settings)); | 679 EXPECT_TRUE(SettingsEqual(desired_settings, settings)); |
| 675 } | 680 } |
| 676 | 681 |
| 677 TEST_F(HostContentSettingsMapTest, ResourceIdentifier) { | 682 TEST_F(HostContentSettingsMapTest, ResourceIdentifier) { |
| 678 // This feature is currently behind a flag. | 683 // This feature is currently behind a flag. |
| 679 CommandLine cl(*CommandLine::ForCurrentProcess()); | 684 CommandLine* cmd = CommandLine::ForCurrentProcess(); |
| 680 CommandLine::ForCurrentProcess()->AppendSwitch( | 685 AutoReset<CommandLine> auto_reset(cmd, *cmd); |
| 681 switches::kEnableResourceContentSettings); | 686 cmd->AppendSwitch(switches::kEnableResourceContentSettings); |
| 682 | 687 |
| 683 TestingProfile profile; | 688 TestingProfile profile; |
| 684 HostContentSettingsMap* host_content_settings_map = | 689 HostContentSettingsMap* host_content_settings_map = |
| 685 profile.GetHostContentSettingsMap(); | 690 profile.GetHostContentSettingsMap(); |
| 686 | 691 |
| 687 GURL host("http://example.com/"); | 692 GURL host("http://example.com/"); |
| 688 HostContentSettingsMap::Pattern pattern("[*.]example.com"); | 693 HostContentSettingsMap::Pattern pattern("[*.]example.com"); |
| 689 std::string resource1("someplugin"); | 694 std::string resource1("someplugin"); |
| 690 std::string resource2("otherplugin"); | 695 std::string resource2("otherplugin"); |
| 691 | 696 |
| 692 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 697 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 693 host_content_settings_map->GetContentSetting( | 698 host_content_settings_map->GetContentSetting( |
| 694 host, CONTENT_SETTINGS_TYPE_PLUGINS, resource1)); | 699 host, CONTENT_SETTINGS_TYPE_PLUGINS, resource1)); |
| 695 host_content_settings_map->SetContentSetting(pattern, | 700 host_content_settings_map->SetContentSetting(pattern, |
| 696 CONTENT_SETTINGS_TYPE_PLUGINS, resource1, CONTENT_SETTING_BLOCK); | 701 CONTENT_SETTINGS_TYPE_PLUGINS, resource1, CONTENT_SETTING_BLOCK); |
| 697 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 702 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 698 host_content_settings_map->GetContentSetting( | 703 host_content_settings_map->GetContentSetting( |
| 699 host, CONTENT_SETTINGS_TYPE_PLUGINS, resource1)); | 704 host, CONTENT_SETTINGS_TYPE_PLUGINS, resource1)); |
| 700 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 705 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 701 host_content_settings_map->GetContentSetting( | 706 host_content_settings_map->GetContentSetting( |
| 702 host, CONTENT_SETTINGS_TYPE_PLUGINS, resource2)); | 707 host, CONTENT_SETTINGS_TYPE_PLUGINS, resource2)); |
| 703 *CommandLine::ForCurrentProcess() = cl; | |
| 704 } | 708 } |
| 705 | 709 |
| 706 TEST_F(HostContentSettingsMapTest, ResourceIdentifierPrefs) { | 710 TEST_F(HostContentSettingsMapTest, ResourceIdentifierPrefs) { |
| 707 // This feature is currently behind a flag. | 711 // This feature is currently behind a flag. |
| 708 CommandLine cl(*CommandLine::ForCurrentProcess()); | 712 CommandLine* cmd = CommandLine::ForCurrentProcess(); |
| 709 CommandLine::ForCurrentProcess()->AppendSwitch( | 713 AutoReset<CommandLine> auto_reset(cmd, *cmd); |
| 710 switches::kEnableResourceContentSettings); | 714 cmd->AppendSwitch(switches::kEnableResourceContentSettings); |
| 711 | 715 |
| 712 TestingProfile profile; | 716 TestingProfile profile; |
| 713 scoped_ptr<Value> value(base::JSONReader::Read( | 717 scoped_ptr<Value> value(base::JSONReader::Read( |
| 714 "{\"[*.]example.com\":{\"per_plugin\":{\"someplugin\":2}}}", false)); | 718 "{\"[*.]example.com\":{\"per_plugin\":{\"someplugin\":2}}}", false)); |
| 715 profile.GetPrefs()->Set(prefs::kContentSettingsPatterns, *value); | 719 profile.GetPrefs()->Set(prefs::kContentSettingsPatterns, *value); |
| 716 HostContentSettingsMap* host_content_settings_map = | 720 HostContentSettingsMap* host_content_settings_map = |
| 717 profile.GetHostContentSettingsMap(); | 721 profile.GetHostContentSettingsMap(); |
| 718 | 722 |
| 719 GURL host("http://example.com/"); | 723 GURL host("http://example.com/"); |
| 720 HostContentSettingsMap::Pattern pattern("[*.]example.com"); | 724 HostContentSettingsMap::Pattern pattern("[*.]example.com"); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 735 EXPECT_STREQ("{}", prefs_as_json.c_str()); | 739 EXPECT_STREQ("{}", prefs_as_json.c_str()); |
| 736 | 740 |
| 737 host_content_settings_map->SetContentSetting(pattern, | 741 host_content_settings_map->SetContentSetting(pattern, |
| 738 CONTENT_SETTINGS_TYPE_PLUGINS, resource2, CONTENT_SETTING_BLOCK); | 742 CONTENT_SETTINGS_TYPE_PLUGINS, resource2, CONTENT_SETTING_BLOCK); |
| 739 | 743 |
| 740 content_setting_prefs = | 744 content_setting_prefs = |
| 741 profile.GetPrefs()->GetDictionary(prefs::kContentSettingsPatterns); | 745 profile.GetPrefs()->GetDictionary(prefs::kContentSettingsPatterns); |
| 742 base::JSONWriter::Write(content_setting_prefs, false, &prefs_as_json); | 746 base::JSONWriter::Write(content_setting_prefs, false, &prefs_as_json); |
| 743 EXPECT_STREQ("{\"[*.]example.com\":{\"per_plugin\":{\"otherplugin\":2}}}", | 747 EXPECT_STREQ("{\"[*.]example.com\":{\"per_plugin\":{\"otherplugin\":2}}}", |
| 744 prefs_as_json.c_str()); | 748 prefs_as_json.c_str()); |
| 745 *CommandLine::ForCurrentProcess() = cl; | |
| 746 } | 749 } |
| 747 | 750 |
| 748 } // namespace | 751 } // namespace |
| OLD | NEW |