| 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 "chrome/browser/content_settings/content_settings_pref_provider.h" | 5 #include "chrome/browser/content_settings/content_settings_pref_provider.h" |
| 6 | 6 |
| 7 #include "base/auto_reset.h" | 7 #include "base/auto_reset.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "chrome/browser/content_settings/content_settings_mock_observer.h" |
| 9 #include "chrome/browser/content_settings/mock_settings_observer.h" | 10 #include "chrome/browser/content_settings/mock_settings_observer.h" |
| 10 #include "chrome/browser/prefs/browser_prefs.h" | 11 #include "chrome/browser/prefs/browser_prefs.h" |
| 11 #include "chrome/browser/prefs/default_pref_store.h" | 12 #include "chrome/browser/prefs/default_pref_store.h" |
| 12 #include "chrome/browser/prefs/overlay_persistent_pref_store.h" | 13 #include "chrome/browser/prefs/overlay_persistent_pref_store.h" |
| 13 #include "chrome/browser/prefs/pref_service.h" | 14 #include "chrome/browser/prefs/pref_service.h" |
| 14 #include "chrome/browser/prefs/pref_service_mock_builder.h" | 15 #include "chrome/browser/prefs/pref_service_mock_builder.h" |
| 15 #include "chrome/browser/prefs/testing_pref_store.h" | 16 #include "chrome/browser/prefs/testing_pref_store.h" |
| 16 #include "chrome/common/chrome_switches.h" | 17 #include "chrome/common/chrome_switches.h" |
| 17 #include "chrome/common/pref_names.h" | 18 #include "chrome/common/pref_names.h" |
| 18 #include "chrome/common/url_constants.h" | 19 #include "chrome/common/url_constants.h" |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 BrowserThread::UI, &message_loop_) { | 153 BrowserThread::UI, &message_loop_) { |
| 153 } | 154 } |
| 154 | 155 |
| 155 protected: | 156 protected: |
| 156 MessageLoop message_loop_; | 157 MessageLoop message_loop_; |
| 157 BrowserThread ui_thread_; | 158 BrowserThread ui_thread_; |
| 158 }; | 159 }; |
| 159 | 160 |
| 160 TEST_F(PrefProviderTest, Observer) { | 161 TEST_F(PrefProviderTest, Observer) { |
| 161 TestingProfile profile; | 162 TestingProfile profile; |
| 163 content_settings::MockObserver mock_observer; |
| 164 |
| 162 PrefProvider pref_content_settings_provider( | 165 PrefProvider pref_content_settings_provider( |
| 163 profile.GetHostContentSettingsMap(), profile.GetPrefs(), false); | 166 &mock_observer, profile.GetPrefs(), false); |
| 164 MockSettingsObserver observer; | 167 |
| 165 ContentSettingsPattern pattern = | 168 ContentSettingsPattern pattern = |
| 166 ContentSettingsPattern::FromString("[*.]example.com"); | 169 ContentSettingsPattern::FromString("[*.]example.com"); |
| 170 EXPECT_CALL(mock_observer, |
| 171 OnContentSettingChanged(pattern, |
| 172 ContentSettingsPattern::Wildcard(), |
| 173 CONTENT_SETTINGS_TYPE_IMAGES, |
| 174 "")); |
| 167 | 175 |
| 168 // There are two PrefProvider instances, one in the host content settings map | |
| 169 // and one instance that is created here. Setting a content setting will fire | |
| 170 // one notification. This will change the underlying preferences which will | |
| 171 // cause the second PrefProvider instance to reload these and to | |
| 172 // sync the obsolete prefences. Hence we get two more notifications which we | |
| 173 // will not get in the real world. | |
| 174 EXPECT_CALL(observer, | |
| 175 OnContentSettingsChanged(profile.GetHostContentSettingsMap(), | |
| 176 CONTENT_SETTINGS_TYPE_IMAGES, | |
| 177 false, | |
| 178 pattern, | |
| 179 ContentSettingsPattern::Wildcard(), | |
| 180 false)); | |
| 181 EXPECT_CALL(observer, | |
| 182 OnContentSettingsChanged(profile.GetHostContentSettingsMap(), | |
| 183 CONTENT_SETTINGS_TYPE_DEFAULT, true, | |
| 184 _, _, true)).Times(2); | |
| 185 pref_content_settings_provider.SetContentSetting( | 176 pref_content_settings_provider.SetContentSetting( |
| 186 pattern, | 177 pattern, |
| 187 ContentSettingsPattern::Wildcard(), | 178 ContentSettingsPattern::Wildcard(), |
| 188 CONTENT_SETTINGS_TYPE_IMAGES, | 179 CONTENT_SETTINGS_TYPE_IMAGES, |
| 189 "", | 180 "", |
| 190 CONTENT_SETTING_ALLOW); | 181 CONTENT_SETTING_ALLOW); |
| 191 | 182 |
| 192 pref_content_settings_provider.ShutdownOnUIThread(); | 183 pref_content_settings_provider.ShutdownOnUIThread(); |
| 193 } | 184 } |
| 194 | 185 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 210 Profile::RegisterUserPrefs(otr_prefs); | 201 Profile::RegisterUserPrefs(otr_prefs); |
| 211 browser::RegisterUserPrefs(otr_prefs); | 202 browser::RegisterUserPrefs(otr_prefs); |
| 212 | 203 |
| 213 TestingProfile profile; | 204 TestingProfile profile; |
| 214 TestingProfile* otr_profile = new TestingProfile; | 205 TestingProfile* otr_profile = new TestingProfile; |
| 215 profile.SetOffTheRecordProfile(otr_profile); | 206 profile.SetOffTheRecordProfile(otr_profile); |
| 216 profile.SetPrefService(regular_prefs); | 207 profile.SetPrefService(regular_prefs); |
| 217 otr_profile->set_incognito(true); | 208 otr_profile->set_incognito(true); |
| 218 otr_profile->SetPrefService(otr_prefs); | 209 otr_profile->SetPrefService(otr_prefs); |
| 219 | 210 |
| 211 testing::NiceMock<content_settings::MockObserver> mock_observer; |
| 220 PrefProvider pref_content_settings_provider( | 212 PrefProvider pref_content_settings_provider( |
| 221 profile.GetHostContentSettingsMap(), regular_prefs, false); | 213 &mock_observer, regular_prefs, false); |
| 222 PrefProvider pref_content_settings_provider_incognito( | 214 PrefProvider pref_content_settings_provider_incognito( |
| 223 otr_profile->GetHostContentSettingsMap(), otr_prefs, true); | 215 &mock_observer, otr_prefs, true); |
| 224 ContentSettingsPattern pattern = | 216 ContentSettingsPattern pattern = |
| 225 ContentSettingsPattern::FromString("[*.]example.com"); | 217 ContentSettingsPattern::FromString("[*.]example.com"); |
| 226 pref_content_settings_provider.SetContentSetting( | 218 pref_content_settings_provider.SetContentSetting( |
| 227 pattern, | 219 pattern, |
| 228 pattern, | 220 pattern, |
| 229 CONTENT_SETTINGS_TYPE_IMAGES, | 221 CONTENT_SETTINGS_TYPE_IMAGES, |
| 230 "", | 222 "", |
| 231 CONTENT_SETTING_ALLOW); | 223 CONTENT_SETTING_ALLOW); |
| 232 | 224 |
| 233 GURL host("http://example.com/"); | 225 GURL host("http://example.com/"); |
| 234 // The value should of course be visible in the regular PrefProvider. | 226 // The value should of course be visible in the regular PrefProvider. |
| 235 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 227 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 236 pref_content_settings_provider.GetContentSetting( | 228 pref_content_settings_provider.GetContentSetting( |
| 237 host, host, CONTENT_SETTINGS_TYPE_IMAGES, "")); | 229 host, host, CONTENT_SETTINGS_TYPE_IMAGES, "")); |
| 238 // And also in the OTR version. | 230 // And also in the OTR version. |
| 239 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 231 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 240 pref_content_settings_provider_incognito.GetContentSetting( | 232 pref_content_settings_provider_incognito.GetContentSetting( |
| 241 host, host, CONTENT_SETTINGS_TYPE_IMAGES, "")); | 233 host, host, CONTENT_SETTINGS_TYPE_IMAGES, "")); |
| 242 // But the value should not be overridden in the OTR user prefs accidentally. | 234 // But the value should not be overridden in the OTR user prefs accidentally. |
| 243 EXPECT_FALSE(otr_user_prefs->IsSetInOverlay(prefs::kContentSettingsPatterns)); | 235 EXPECT_FALSE(otr_user_prefs->IsSetInOverlay(prefs::kContentSettingsPatterns)); |
| 244 | 236 |
| 245 pref_content_settings_provider.ShutdownOnUIThread(); | 237 pref_content_settings_provider.ShutdownOnUIThread(); |
| 246 pref_content_settings_provider_incognito.ShutdownOnUIThread(); | 238 pref_content_settings_provider_incognito.ShutdownOnUIThread(); |
| 247 } | 239 } |
| 248 | 240 |
| 249 TEST_F(PrefProviderTest, Patterns) { | 241 TEST_F(PrefProviderTest, Patterns) { |
| 250 TestingProfile testing_profile; | 242 TestingProfile testing_profile; |
| 243 testing::NiceMock<content_settings::MockObserver> mock_observer; |
| 251 PrefProvider pref_content_settings_provider( | 244 PrefProvider pref_content_settings_provider( |
| 252 testing_profile.GetHostContentSettingsMap(), | 245 &mock_observer, testing_profile.GetPrefs(), false); |
| 253 testing_profile.GetPrefs(), false); | |
| 254 | 246 |
| 255 GURL host1("http://example.com/"); | 247 GURL host1("http://example.com/"); |
| 256 GURL host2("http://www.example.com/"); | 248 GURL host2("http://www.example.com/"); |
| 257 GURL host3("http://example.org/"); | 249 GURL host3("http://example.org/"); |
| 258 GURL host4("file:///tmp/test.html"); | 250 GURL host4("file:///tmp/test.html"); |
| 259 ContentSettingsPattern pattern1 = | 251 ContentSettingsPattern pattern1 = |
| 260 ContentSettingsPattern::FromString("[*.]example.com"); | 252 ContentSettingsPattern::FromString("[*.]example.com"); |
| 261 ContentSettingsPattern pattern2 = | 253 ContentSettingsPattern pattern2 = |
| 262 ContentSettingsPattern::FromString("example.org"); | 254 ContentSettingsPattern::FromString("example.org"); |
| 263 ContentSettingsPattern pattern3 = | 255 ContentSettingsPattern pattern3 = |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 308 pref_content_settings_provider.ShutdownOnUIThread(); | 300 pref_content_settings_provider.ShutdownOnUIThread(); |
| 309 } | 301 } |
| 310 | 302 |
| 311 TEST_F(PrefProviderTest, ResourceIdentifier) { | 303 TEST_F(PrefProviderTest, ResourceIdentifier) { |
| 312 // This feature is currently behind a flag. | 304 // This feature is currently behind a flag. |
| 313 CommandLine* cmd = CommandLine::ForCurrentProcess(); | 305 CommandLine* cmd = CommandLine::ForCurrentProcess(); |
| 314 AutoReset<CommandLine> auto_reset(cmd, *cmd); | 306 AutoReset<CommandLine> auto_reset(cmd, *cmd); |
| 315 cmd->AppendSwitch(switches::kEnableResourceContentSettings); | 307 cmd->AppendSwitch(switches::kEnableResourceContentSettings); |
| 316 | 308 |
| 317 TestingProfile testing_profile; | 309 TestingProfile testing_profile; |
| 310 testing::NiceMock<content_settings::MockObserver> mock_observer; |
| 318 PrefProvider pref_content_settings_provider( | 311 PrefProvider pref_content_settings_provider( |
| 319 testing_profile.GetHostContentSettingsMap(), | 312 &mock_observer, testing_profile.GetPrefs(), false); |
| 320 testing_profile.GetPrefs(), | |
| 321 false); | |
| 322 | 313 |
| 323 GURL host("http://example.com/"); | 314 GURL host("http://example.com/"); |
| 324 ContentSettingsPattern pattern = | 315 ContentSettingsPattern pattern = |
| 325 ContentSettingsPattern::FromString("[*.]example.com"); | 316 ContentSettingsPattern::FromString("[*.]example.com"); |
| 326 std::string resource1("someplugin"); | 317 std::string resource1("someplugin"); |
| 327 std::string resource2("otherplugin"); | 318 std::string resource2("otherplugin"); |
| 328 | 319 |
| 329 EXPECT_EQ(CONTENT_SETTING_DEFAULT, | 320 EXPECT_EQ(CONTENT_SETTING_DEFAULT, |
| 330 pref_content_settings_provider.GetContentSetting( | 321 pref_content_settings_provider.GetContentSetting( |
| 331 host, host, CONTENT_SETTINGS_TYPE_PLUGINS, resource1)); | 322 host, host, CONTENT_SETTINGS_TYPE_PLUGINS, resource1)); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 358 ContentSettingsPattern pattern = | 349 ContentSettingsPattern pattern = |
| 359 ContentSettingsPattern::FromString("http://www.example.com"); | 350 ContentSettingsPattern::FromString("http://www.example.com"); |
| 360 scoped_ptr<DictionaryValue> all_settings_dictionary(new DictionaryValue()); | 351 scoped_ptr<DictionaryValue> all_settings_dictionary(new DictionaryValue()); |
| 361 all_settings_dictionary->SetWithoutPathExpansion( | 352 all_settings_dictionary->SetWithoutPathExpansion( |
| 362 pattern.ToString(), settings_dictionary); | 353 pattern.ToString(), settings_dictionary); |
| 363 | 354 |
| 364 // Set Obsolete preference. | 355 // Set Obsolete preference. |
| 365 prefs->Set(prefs::kContentSettingsPatterns, *all_settings_dictionary); | 356 prefs->Set(prefs::kContentSettingsPatterns, *all_settings_dictionary); |
| 366 | 357 |
| 367 // Test if single pattern settings are properly migrated. | 358 // Test if single pattern settings are properly migrated. |
| 368 content_settings::PrefProvider provider(profile.GetHostContentSettingsMap(), | 359 testing::NiceMock<content_settings::MockObserver> mock_observer; |
| 369 prefs, false); | 360 content_settings::PrefProvider provider(&mock_observer, prefs, false); |
| 370 | 361 |
| 371 // Validate migrated preferences | 362 // Validate migrated preferences |
| 372 const DictionaryValue* const_all_settings_dictionary = | 363 const DictionaryValue* const_all_settings_dictionary = |
| 373 prefs->GetDictionary(prefs::kContentSettingsPatternPairs); | 364 prefs->GetDictionary(prefs::kContentSettingsPatternPairs); |
| 374 EXPECT_EQ(1U, const_all_settings_dictionary->size()); | 365 EXPECT_EQ(1U, const_all_settings_dictionary->size()); |
| 375 EXPECT_FALSE(const_all_settings_dictionary->HasKey(pattern.ToString())); | 366 EXPECT_FALSE(const_all_settings_dictionary->HasKey(pattern.ToString())); |
| 376 EXPECT_TRUE(const_all_settings_dictionary->HasKey( | 367 EXPECT_TRUE(const_all_settings_dictionary->HasKey( |
| 377 pattern.ToString() + "," + | 368 pattern.ToString() + "," + |
| 378 ContentSettingsPattern::Wildcard().ToString())); | 369 ContentSettingsPattern::Wildcard().ToString())); |
| 379 | 370 |
| 380 EXPECT_EQ(CONTENT_SETTING_BLOCK, provider.GetContentSetting( | 371 EXPECT_EQ(CONTENT_SETTING_BLOCK, provider.GetContentSetting( |
| 381 GURL("http://www.example.com"), | 372 GURL("http://www.example.com"), |
| 382 GURL("http://www.example.com"), | 373 GURL("http://www.example.com"), |
| 383 CONTENT_SETTINGS_TYPE_IMAGES, | 374 CONTENT_SETTINGS_TYPE_IMAGES, |
| 384 "")); | 375 "")); |
| 385 | 376 |
| 386 EXPECT_EQ(CONTENT_SETTING_BLOCK, provider.GetContentSetting( | 377 EXPECT_EQ(CONTENT_SETTING_BLOCK, provider.GetContentSetting( |
| 387 GURL("http://www.example.com"), | 378 GURL("http://www.example.com"), |
| 388 GURL("http://www.example.com"), | 379 GURL("http://www.example.com"), |
| 389 CONTENT_SETTINGS_TYPE_POPUPS, | 380 CONTENT_SETTINGS_TYPE_POPUPS, |
| 390 "")); | 381 "")); |
| 391 | 382 |
| 392 provider.ShutdownOnUIThread(); | 383 provider.ShutdownOnUIThread(); |
| 393 } | 384 } |
| 394 | 385 |
| 395 } // namespace content_settings | 386 } // namespace content_settings |
| OLD | NEW |