| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "components/content_settings/core/browser/content_settings_policy_provi
der.h" | 5 #include "components/content_settings/core/browser/content_settings_policy_provi
der.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/auto_reset.h" | 10 #include "base/auto_reset.h" |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/memory/ptr_util.h" |
| 12 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 13 #include "base/test/scoped_feature_list.h" | 14 #include "base/test/scoped_feature_list.h" |
| 14 #include "chrome/browser/content_settings/content_settings_mock_observer.h" | 15 #include "chrome/browser/content_settings/content_settings_mock_observer.h" |
| 15 #include "chrome/common/chrome_switches.h" | 16 #include "chrome/common/chrome_switches.h" |
| 16 #include "chrome/common/url_constants.h" | 17 #include "chrome/common/url_constants.h" |
| 17 #include "chrome/test/base/testing_profile.h" | 18 #include "chrome/test/base/testing_profile.h" |
| 18 #include "components/content_settings/core/browser/content_settings_rule.h" | 19 #include "components/content_settings/core/browser/content_settings_rule.h" |
| 19 #include "components/content_settings/core/browser/content_settings_utils.h" | 20 #include "components/content_settings/core/browser/content_settings_utils.h" |
| 20 #include "components/content_settings/core/common/content_settings_pattern.h" | 21 #include "components/content_settings/core/common/content_settings_pattern.h" |
| 21 #include "components/content_settings/core/common/pref_names.h" | 22 #include "components/content_settings/core/common/pref_names.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 41 sync_preferences::TestingPrefServiceSyncable* prefs = | 42 sync_preferences::TestingPrefServiceSyncable* prefs = |
| 42 profile.GetTestingPrefService(); | 43 profile.GetTestingPrefService(); |
| 43 PolicyProvider provider(prefs); | 44 PolicyProvider provider(prefs); |
| 44 | 45 |
| 45 std::unique_ptr<RuleIterator> rule_iterator(provider.GetRuleIterator( | 46 std::unique_ptr<RuleIterator> rule_iterator(provider.GetRuleIterator( |
| 46 CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string(), false)); | 47 CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string(), false)); |
| 47 EXPECT_FALSE(rule_iterator); | 48 EXPECT_FALSE(rule_iterator); |
| 48 | 49 |
| 49 // Change the managed value of the default geolocation setting | 50 // Change the managed value of the default geolocation setting |
| 50 prefs->SetManagedPref(prefs::kManagedDefaultGeolocationSetting, | 51 prefs->SetManagedPref(prefs::kManagedDefaultGeolocationSetting, |
| 51 new base::Value(CONTENT_SETTING_BLOCK)); | 52 base::MakeUnique<base::Value>(CONTENT_SETTING_BLOCK)); |
| 52 | 53 |
| 53 rule_iterator = provider.GetRuleIterator(CONTENT_SETTINGS_TYPE_GEOLOCATION, | 54 rule_iterator = provider.GetRuleIterator(CONTENT_SETTINGS_TYPE_GEOLOCATION, |
| 54 std::string(), false); | 55 std::string(), false); |
| 55 ASSERT_TRUE(rule_iterator); | 56 ASSERT_TRUE(rule_iterator); |
| 56 EXPECT_TRUE(rule_iterator->HasNext()); | 57 EXPECT_TRUE(rule_iterator->HasNext()); |
| 57 Rule rule = rule_iterator->Next(); | 58 Rule rule = rule_iterator->Next(); |
| 58 EXPECT_FALSE(rule_iterator->HasNext()); | 59 EXPECT_FALSE(rule_iterator->HasNext()); |
| 59 | 60 |
| 60 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule.primary_pattern); | 61 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule.primary_pattern); |
| 61 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule.secondary_pattern); | 62 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule.secondary_pattern); |
| 62 EXPECT_EQ(CONTENT_SETTING_BLOCK, ValueToContentSetting(rule.value.get())); | 63 EXPECT_EQ(CONTENT_SETTING_BLOCK, ValueToContentSetting(rule.value.get())); |
| 63 | 64 |
| 64 provider.ShutdownOnUIThread(); | 65 provider.ShutdownOnUIThread(); |
| 65 } | 66 } |
| 66 | 67 |
| 67 TEST_F(PolicyProviderTest, ManagedDefaultContentSettings) { | 68 TEST_F(PolicyProviderTest, ManagedDefaultContentSettings) { |
| 68 TestingProfile profile; | 69 TestingProfile profile; |
| 69 sync_preferences::TestingPrefServiceSyncable* prefs = | 70 sync_preferences::TestingPrefServiceSyncable* prefs = |
| 70 profile.GetTestingPrefService(); | 71 profile.GetTestingPrefService(); |
| 71 PolicyProvider provider(prefs); | 72 PolicyProvider provider(prefs); |
| 72 | 73 |
| 73 prefs->SetManagedPref(prefs::kManagedDefaultPluginsSetting, | 74 prefs->SetManagedPref(prefs::kManagedDefaultPluginsSetting, |
| 74 new base::Value(CONTENT_SETTING_BLOCK)); | 75 base::MakeUnique<base::Value>(CONTENT_SETTING_BLOCK)); |
| 75 | 76 |
| 76 std::unique_ptr<RuleIterator> rule_iterator(provider.GetRuleIterator( | 77 std::unique_ptr<RuleIterator> rule_iterator(provider.GetRuleIterator( |
| 77 CONTENT_SETTINGS_TYPE_PLUGINS, std::string(), false)); | 78 CONTENT_SETTINGS_TYPE_PLUGINS, std::string(), false)); |
| 78 EXPECT_TRUE(rule_iterator->HasNext()); | 79 EXPECT_TRUE(rule_iterator->HasNext()); |
| 79 Rule rule = rule_iterator->Next(); | 80 Rule rule = rule_iterator->Next(); |
| 80 EXPECT_FALSE(rule_iterator->HasNext()); | 81 EXPECT_FALSE(rule_iterator->HasNext()); |
| 81 | 82 |
| 82 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule.primary_pattern); | 83 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule.primary_pattern); |
| 83 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule.secondary_pattern); | 84 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule.secondary_pattern); |
| 84 EXPECT_EQ(CONTENT_SETTING_BLOCK, ValueToContentSetting(rule.value.get())); | 85 EXPECT_EQ(CONTENT_SETTING_BLOCK, ValueToContentSetting(rule.value.get())); |
| 85 | 86 |
| 86 provider.ShutdownOnUIThread(); | 87 provider.ShutdownOnUIThread(); |
| 87 } | 88 } |
| 88 | 89 |
| 89 TEST_F(PolicyProviderTest, ManagedDefaultPluginSettingsExperiment) { | 90 TEST_F(PolicyProviderTest, ManagedDefaultPluginSettingsExperiment) { |
| 90 base::test::ScopedFeatureList scoped_feature_list; | 91 base::test::ScopedFeatureList scoped_feature_list; |
| 91 scoped_feature_list.InitFromCommandLine("IgnoreDefaultPluginsSetting", | 92 scoped_feature_list.InitFromCommandLine("IgnoreDefaultPluginsSetting", |
| 92 std::string()); | 93 std::string()); |
| 93 | 94 |
| 94 TestingProfile profile; | 95 TestingProfile profile; |
| 95 sync_preferences::TestingPrefServiceSyncable* prefs = | 96 sync_preferences::TestingPrefServiceSyncable* prefs = |
| 96 profile.GetTestingPrefService(); | 97 profile.GetTestingPrefService(); |
| 97 PolicyProvider provider(prefs); | 98 PolicyProvider provider(prefs); |
| 98 | 99 |
| 99 // ForceDefaultPluginsSettingAsk overrides this to ASK. | 100 // ForceDefaultPluginsSettingAsk overrides this to ASK. |
| 100 prefs->SetManagedPref(prefs::kManagedDefaultPluginsSetting, | 101 prefs->SetManagedPref(prefs::kManagedDefaultPluginsSetting, |
| 101 new base::Value(CONTENT_SETTING_BLOCK)); | 102 base::MakeUnique<base::Value>(CONTENT_SETTING_BLOCK)); |
| 102 prefs->SetManagedPref(prefs::kManagedDefaultJavaScriptSetting, | 103 prefs->SetManagedPref(prefs::kManagedDefaultJavaScriptSetting, |
| 103 new base::Value(CONTENT_SETTING_BLOCK)); | 104 base::MakeUnique<base::Value>(CONTENT_SETTING_BLOCK)); |
| 104 | 105 |
| 105 std::unique_ptr<RuleIterator> plugin_rule_iterator(provider.GetRuleIterator( | 106 std::unique_ptr<RuleIterator> plugin_rule_iterator(provider.GetRuleIterator( |
| 106 CONTENT_SETTINGS_TYPE_PLUGINS, std::string(), false)); | 107 CONTENT_SETTINGS_TYPE_PLUGINS, std::string(), false)); |
| 107 // Policy should be removed when running under experiment. | 108 // Policy should be removed when running under experiment. |
| 108 EXPECT_FALSE(plugin_rule_iterator); | 109 EXPECT_FALSE(plugin_rule_iterator); |
| 109 | 110 |
| 110 std::unique_ptr<RuleIterator> js_rule_iterator(provider.GetRuleIterator( | 111 std::unique_ptr<RuleIterator> js_rule_iterator(provider.GetRuleIterator( |
| 111 CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string(), false)); | 112 CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string(), false)); |
| 112 // Other policies should be left alone. | 113 // Other policies should be left alone. |
| 113 EXPECT_TRUE(js_rule_iterator->HasNext()); | 114 EXPECT_TRUE(js_rule_iterator->HasNext()); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 133 MockObserver mock_observer; | 134 MockObserver mock_observer; |
| 134 EXPECT_CALL(mock_observer, | 135 EXPECT_CALL(mock_observer, |
| 135 OnContentSettingChanged(_, | 136 OnContentSettingChanged(_, |
| 136 _, | 137 _, |
| 137 CONTENT_SETTINGS_TYPE_DEFAULT, | 138 CONTENT_SETTINGS_TYPE_DEFAULT, |
| 138 "")); | 139 "")); |
| 139 provider.AddObserver(&mock_observer); | 140 provider.AddObserver(&mock_observer); |
| 140 | 141 |
| 141 // Set the managed default-content-setting. | 142 // Set the managed default-content-setting. |
| 142 prefs->SetManagedPref(prefs::kManagedDefaultImagesSetting, | 143 prefs->SetManagedPref(prefs::kManagedDefaultImagesSetting, |
| 143 new base::Value(CONTENT_SETTING_BLOCK)); | 144 base::MakeUnique<base::Value>(CONTENT_SETTING_BLOCK)); |
| 144 ::testing::Mock::VerifyAndClearExpectations(&mock_observer); | 145 ::testing::Mock::VerifyAndClearExpectations(&mock_observer); |
| 145 EXPECT_CALL(mock_observer, | 146 EXPECT_CALL(mock_observer, |
| 146 OnContentSettingChanged(_, | 147 OnContentSettingChanged(_, |
| 147 _, | 148 _, |
| 148 CONTENT_SETTINGS_TYPE_DEFAULT, | 149 CONTENT_SETTINGS_TYPE_DEFAULT, |
| 149 "")); | 150 "")); |
| 150 // Remove the managed default-content-setting. | 151 // Remove the managed default-content-setting. |
| 151 prefs->RemoveManagedPref(prefs::kManagedDefaultImagesSetting); | 152 prefs->RemoveManagedPref(prefs::kManagedDefaultImagesSetting); |
| 152 provider.ShutdownOnUIThread(); | 153 provider.ShutdownOnUIThread(); |
| 153 } | 154 } |
| 154 | 155 |
| 155 TEST_F(PolicyProviderTest, GettingManagedContentSettings) { | 156 TEST_F(PolicyProviderTest, GettingManagedContentSettings) { |
| 156 TestingProfile profile; | 157 TestingProfile profile; |
| 157 sync_preferences::TestingPrefServiceSyncable* prefs = | 158 sync_preferences::TestingPrefServiceSyncable* prefs = |
| 158 profile.GetTestingPrefService(); | 159 profile.GetTestingPrefService(); |
| 159 | 160 |
| 160 base::ListValue* value = new base::ListValue(); | 161 auto value = base::MakeUnique<base::ListValue>(); |
| 161 value->AppendString("[*.]google.com"); | 162 value->AppendString("[*.]google.com"); |
| 162 prefs->SetManagedPref(prefs::kManagedImagesBlockedForUrls, | 163 prefs->SetManagedPref(prefs::kManagedImagesBlockedForUrls, std::move(value)); |
| 163 value); | |
| 164 | 164 |
| 165 PolicyProvider provider(prefs); | 165 PolicyProvider provider(prefs); |
| 166 | 166 |
| 167 ContentSettingsPattern yt_url_pattern = | 167 ContentSettingsPattern yt_url_pattern = |
| 168 ContentSettingsPattern::FromString("www.youtube.com"); | 168 ContentSettingsPattern::FromString("www.youtube.com"); |
| 169 GURL youtube_url("http://www.youtube.com"); | 169 GURL youtube_url("http://www.youtube.com"); |
| 170 GURL google_url("http://mail.google.com"); | 170 GURL google_url("http://mail.google.com"); |
| 171 | 171 |
| 172 EXPECT_EQ(CONTENT_SETTING_DEFAULT, | 172 EXPECT_EQ(CONTENT_SETTING_DEFAULT, |
| 173 TestUtils::GetContentSetting(&provider, youtube_url, youtube_url, | 173 TestUtils::GetContentSetting(&provider, youtube_url, youtube_url, |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 std::string(), false)); | 206 std::string(), false)); |
| 207 | 207 |
| 208 provider.ShutdownOnUIThread(); | 208 provider.ShutdownOnUIThread(); |
| 209 } | 209 } |
| 210 | 210 |
| 211 TEST_F(PolicyProviderTest, ResourceIdentifier) { | 211 TEST_F(PolicyProviderTest, ResourceIdentifier) { |
| 212 TestingProfile profile; | 212 TestingProfile profile; |
| 213 sync_preferences::TestingPrefServiceSyncable* prefs = | 213 sync_preferences::TestingPrefServiceSyncable* prefs = |
| 214 profile.GetTestingPrefService(); | 214 profile.GetTestingPrefService(); |
| 215 | 215 |
| 216 base::ListValue* value = new base::ListValue(); | 216 auto value = base::MakeUnique<base::ListValue>(); |
| 217 value->AppendString("[*.]google.com"); | 217 value->AppendString("[*.]google.com"); |
| 218 prefs->SetManagedPref(prefs::kManagedPluginsAllowedForUrls, | 218 prefs->SetManagedPref(prefs::kManagedPluginsAllowedForUrls, std::move(value)); |
| 219 value); | |
| 220 | 219 |
| 221 PolicyProvider provider(prefs); | 220 PolicyProvider provider(prefs); |
| 222 | 221 |
| 223 GURL youtube_url("http://www.youtube.com"); | 222 GURL youtube_url("http://www.youtube.com"); |
| 224 GURL google_url("http://mail.google.com"); | 223 GURL google_url("http://mail.google.com"); |
| 225 | 224 |
| 226 EXPECT_EQ(CONTENT_SETTING_DEFAULT, | 225 EXPECT_EQ(CONTENT_SETTING_DEFAULT, |
| 227 TestUtils::GetContentSetting(&provider, youtube_url, youtube_url, | 226 TestUtils::GetContentSetting(&provider, youtube_url, youtube_url, |
| 228 CONTENT_SETTINGS_TYPE_PLUGINS, | 227 CONTENT_SETTINGS_TYPE_PLUGINS, |
| 229 "someplugin", false)); | 228 "someplugin", false)); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 253 // Tests the default setting for auto selecting certificates | 252 // Tests the default setting for auto selecting certificates |
| 254 EXPECT_EQ(NULL, TestUtils::GetContentSettingValue( | 253 EXPECT_EQ(NULL, TestUtils::GetContentSettingValue( |
| 255 &provider, google_url, google_url, | 254 &provider, google_url, google_url, |
| 256 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE, | 255 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE, |
| 257 std::string(), false)); | 256 std::string(), false)); |
| 258 | 257 |
| 259 // Set the content settings pattern list for origins to auto select | 258 // Set the content settings pattern list for origins to auto select |
| 260 // certificates. | 259 // certificates. |
| 261 std::string pattern_str("\"pattern\":\"[*.]google.com\""); | 260 std::string pattern_str("\"pattern\":\"[*.]google.com\""); |
| 262 std::string filter_str("\"filter\":{\"ISSUER\":{\"CN\":\"issuer name\"}}"); | 261 std::string filter_str("\"filter\":{\"ISSUER\":{\"CN\":\"issuer name\"}}"); |
| 263 base::ListValue* value = new base::ListValue(); | 262 auto value = base::MakeUnique<base::ListValue>(); |
| 264 value->AppendString("{" + pattern_str + "," + filter_str + "}"); | 263 value->AppendString("{" + pattern_str + "," + filter_str + "}"); |
| 265 prefs->SetManagedPref(prefs::kManagedAutoSelectCertificateForUrls, | 264 prefs->SetManagedPref(prefs::kManagedAutoSelectCertificateForUrls, |
| 266 value); | 265 std::move(value)); |
| 267 GURL youtube_url("https://www.youtube.com"); | 266 GURL youtube_url("https://www.youtube.com"); |
| 268 EXPECT_EQ(NULL, TestUtils::GetContentSettingValue( | 267 EXPECT_EQ(NULL, TestUtils::GetContentSettingValue( |
| 269 &provider, youtube_url, youtube_url, | 268 &provider, youtube_url, youtube_url, |
| 270 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE, | 269 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE, |
| 271 std::string(), false)); | 270 std::string(), false)); |
| 272 std::unique_ptr<base::Value> cert_filter(TestUtils::GetContentSettingValue( | 271 std::unique_ptr<base::Value> cert_filter(TestUtils::GetContentSettingValue( |
| 273 &provider, google_url, google_url, | 272 &provider, google_url, google_url, |
| 274 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE, std::string(), false)); | 273 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE, std::string(), false)); |
| 275 | 274 |
| 276 ASSERT_EQ(base::Value::Type::DICTIONARY, cert_filter->GetType()); | 275 ASSERT_EQ(base::Value::Type::DICTIONARY, cert_filter->GetType()); |
| 277 base::DictionaryValue* dict_value = | 276 base::DictionaryValue* dict_value = |
| 278 static_cast<base::DictionaryValue*>(cert_filter.get()); | 277 static_cast<base::DictionaryValue*>(cert_filter.get()); |
| 279 std::string actual_common_name; | 278 std::string actual_common_name; |
| 280 ASSERT_TRUE(dict_value->GetString("ISSUER.CN", &actual_common_name)); | 279 ASSERT_TRUE(dict_value->GetString("ISSUER.CN", &actual_common_name)); |
| 281 EXPECT_EQ("issuer name", actual_common_name); | 280 EXPECT_EQ("issuer name", actual_common_name); |
| 282 provider.ShutdownOnUIThread(); | 281 provider.ShutdownOnUIThread(); |
| 283 } | 282 } |
| 284 | 283 |
| 285 } // namespace content_settings | 284 } // namespace content_settings |
| OLD | NEW |