Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_pref_provide r.h" | 5 #include "components/content_settings/core/browser/content_settings_pref_provide r.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/auto_reset.h" | 9 #include "base/auto_reset.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 14 #include "base/test/simple_test_clock.h" | |
| 15 #include "base/threading/platform_thread.h" | 14 #include "base/threading/platform_thread.h" |
| 16 #include "base/values.h" | 15 #include "base/values.h" |
| 17 #include "chrome/browser/content_settings/content_settings_mock_observer.h" | 16 #include "chrome/browser/content_settings/content_settings_mock_observer.h" |
| 18 #include "chrome/browser/prefs/browser_prefs.h" | 17 #include "chrome/browser/prefs/browser_prefs.h" |
| 19 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" |
| 20 #include "chrome/common/pref_names.h" | 19 #include "chrome/common/pref_names.h" |
| 21 #include "chrome/common/url_constants.h" | 20 #include "chrome/common/url_constants.h" |
| 22 #include "chrome/test/base/testing_profile.h" | 21 #include "chrome/test/base/testing_profile.h" |
| 23 #include "components/content_settings/core/browser/content_settings_pref.h" | 22 #include "components/content_settings/core/browser/content_settings_pref.h" |
| 24 #include "components/content_settings/core/browser/content_settings_registry.h" | 23 #include "components/content_settings/core/browser/content_settings_registry.h" |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 128 | 127 |
| 129 pref_content_settings_provider.SetWebsiteSetting( | 128 pref_content_settings_provider.SetWebsiteSetting( |
| 130 pattern, ContentSettingsPattern::Wildcard(), | 129 pattern, ContentSettingsPattern::Wildcard(), |
| 131 CONTENT_SETTINGS_TYPE_COOKIES, std::string(), | 130 CONTENT_SETTINGS_TYPE_COOKIES, std::string(), |
| 132 new base::FundamentalValue(CONTENT_SETTING_ALLOW)); | 131 new base::FundamentalValue(CONTENT_SETTING_ALLOW)); |
| 133 | 132 |
| 134 pref_content_settings_provider.ShutdownOnUIThread(); | 133 pref_content_settings_provider.ShutdownOnUIThread(); |
| 135 } | 134 } |
| 136 | 135 |
| 137 // Tests that fullscreen and mouselock content settings are cleared. | 136 // Tests that fullscreen and mouselock content settings are cleared. |
| 138 TEST_F(PrefProviderTest, DiscardObsoletePreferences) { | 137 TEST_F(PrefProviderTest, DiscardObsoleteFullscreenAndMouselockPreferences) { |
| 139 static const char kFullscreenPrefPath[] = | 138 static const char kFullscreenPrefPath[] = |
| 140 "profile.content_settings.exceptions.fullscreen"; | 139 "profile.content_settings.exceptions.fullscreen"; |
| 141 #if !defined(OS_ANDROID) | 140 #if !defined(OS_ANDROID) |
| 142 static const char kMouselockPrefPath[] = | 141 static const char kMouselockPrefPath[] = |
| 143 "profile.content_settings.exceptions.mouselock"; | 142 "profile.content_settings.exceptions.mouselock"; |
| 144 #endif | 143 #endif |
| 145 static const char kGeolocationPrefPath[] = | 144 static const char kGeolocationPrefPath[] = |
| 146 "profile.content_settings.exceptions.geolocation"; | 145 "profile.content_settings.exceptions.geolocation"; |
| 147 static const char kPattern[] = "[*.]example.com"; | 146 static const char kPattern[] = "[*.]example.com"; |
| 148 | 147 |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 172 EXPECT_FALSE(prefs->HasPrefPath(kMouselockPrefPath)); | 171 EXPECT_FALSE(prefs->HasPrefPath(kMouselockPrefPath)); |
| 173 #endif | 172 #endif |
| 174 EXPECT_TRUE(prefs->HasPrefPath(kGeolocationPrefPath)); | 173 EXPECT_TRUE(prefs->HasPrefPath(kGeolocationPrefPath)); |
| 175 GURL primary_url("http://example.com/"); | 174 GURL primary_url("http://example.com/"); |
| 176 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 175 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 177 TestUtils::GetContentSetting(&provider, primary_url, primary_url, | 176 TestUtils::GetContentSetting(&provider, primary_url, primary_url, |
| 178 CONTENT_SETTINGS_TYPE_GEOLOCATION, | 177 CONTENT_SETTINGS_TYPE_GEOLOCATION, |
| 179 std::string(), false)); | 178 std::string(), false)); |
| 180 } | 179 } |
| 181 | 180 |
| 181 // Tests that last usage content settings are cleared. | |
| 182 TEST_F(PrefProviderTest, DiscardObsoleteLastUsagePreferences) { | |
| 183 std::string kGeolocationPrefPath = | |
| 184 ContentSettingsRegistry::GetInstance() | |
| 185 ->Get(CONTENT_SETTINGS_TYPE_GEOLOCATION) | |
| 186 ->website_settings_info() | |
| 187 ->pref_name(); | |
| 188 std::string kMicPrefPath = | |
| 189 ContentSettingsRegistry::GetInstance() | |
| 190 ->Get(CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC) | |
| 191 ->website_settings_info() | |
| 192 ->pref_name(); | |
| 193 const char kObsoleteLastUsed[] = "last_used"; | |
| 194 | |
| 195 TestingProfile profile; | |
| 196 PrefService* prefs = profile.GetPrefs(); | |
| 197 | |
| 198 // Content settings prefs are structured as follows: | |
| 199 // "media_stream_mic": { | |
| 200 // "https://example.com:443,*": { | |
| 201 // "last_used": 1486968992.758971, | |
| 202 // "setting": 1 | |
| 203 // } | |
| 204 // } | |
| 205 const char kPattern[] = "https://example.com:443,*"; | |
| 206 GURL host("https://example.com/"); | |
| 207 | |
| 208 auto geolocation_pattern_data = base::MakeUnique<base::DictionaryValue>(); | |
| 209 geolocation_pattern_data->SetDouble(kObsoleteLastUsed, 1485000000.0); | |
| 210 base::DictionaryValue geolocation_pref_data; | |
| 211 geolocation_pref_data.SetWithoutPathExpansion( | |
| 212 kPattern, std::move(geolocation_pattern_data)); | |
| 213 prefs->Set(kGeolocationPrefPath, geolocation_pref_data); | |
| 214 | |
| 215 auto mic_pattern_data = base::MakeUnique<base::DictionaryValue>(); | |
| 216 mic_pattern_data->SetInteger("setting", CONTENT_SETTING_ALLOW); | |
| 217 mic_pattern_data->SetDouble(kObsoleteLastUsed, 1480000000.0); | |
| 218 base::DictionaryValue mic_pref_data; | |
| 219 mic_pref_data.SetWithoutPathExpansion(kPattern, std::move(mic_pattern_data)); | |
| 220 prefs->Set(kMicPrefPath, mic_pref_data); | |
| 221 | |
| 222 // Instantiate a new PrefProvider here, because we want to test the | |
| 223 // constructor's behavior after setting the above. | |
| 224 PrefProvider provider(prefs, false); | |
| 225 provider.ShutdownOnUIThread(); | |
|
raymes
2017/02/19 23:57:54
nit: should we avoid calling this until after we f
Timothy Loh
2017/02/21 00:14:08
Yep, fixed.
| |
| 226 | |
| 227 // Check that last_used data has been deleted. | |
| 228 EXPECT_TRUE(prefs->GetDictionary(kGeolocationPrefPath)->empty()); | |
| 229 auto mic_prefs = prefs->GetDictionary(kMicPrefPath); | |
| 230 const base::DictionaryValue* mic_result_pattern_data; | |
| 231 ASSERT_TRUE(mic_prefs->GetDictionaryWithoutPathExpansion( | |
| 232 kPattern, &mic_result_pattern_data)); | |
| 233 EXPECT_EQ(mic_result_pattern_data->size(), static_cast<size_t>(1)); | |
| 234 int mic_result_setting; | |
| 235 EXPECT_TRUE( | |
| 236 mic_result_pattern_data->GetInteger("setting", &mic_result_setting)); | |
| 237 EXPECT_EQ(mic_result_setting, CONTENT_SETTING_ALLOW); | |
| 238 EXPECT_EQ(TestUtils::GetContentSetting(&provider, host, host, | |
| 239 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC, | |
| 240 std::string(), false), | |
| 241 CONTENT_SETTING_ALLOW); | |
|
raymes
2017/02/19 23:57:54
nit: reverse the order of the params so expected i
Timothy Loh
2017/02/21 00:14:08
Fixed.
| |
| 242 } | |
| 243 | |
| 182 // Test for regression in which the PrefProvider modified the user pref store | 244 // Test for regression in which the PrefProvider modified the user pref store |
| 183 // of the OTR unintentionally: http://crbug.com/74466. | 245 // of the OTR unintentionally: http://crbug.com/74466. |
| 184 TEST_F(PrefProviderTest, Incognito) { | 246 TEST_F(PrefProviderTest, Incognito) { |
| 185 PersistentPrefStore* user_prefs = new TestingPrefStore(); | 247 PersistentPrefStore* user_prefs = new TestingPrefStore(); |
| 186 OverlayUserPrefStore* otr_user_prefs = | 248 OverlayUserPrefStore* otr_user_prefs = |
| 187 new OverlayUserPrefStore(user_prefs); | 249 new OverlayUserPrefStore(user_prefs); |
| 188 | 250 |
| 189 sync_preferences::PrefServiceMockFactory factory; | 251 sync_preferences::PrefServiceMockFactory factory; |
| 190 factory.set_user_prefs(make_scoped_refptr(user_prefs)); | 252 factory.set_user_prefs(make_scoped_refptr(user_prefs)); |
| 191 scoped_refptr<user_prefs::PrefRegistrySyncable> registry( | 253 scoped_refptr<user_prefs::PrefRegistrySyncable> registry( |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 392 DictionaryPrefUpdate update(&prefs, info->pref_name()); | 454 DictionaryPrefUpdate update(&prefs, info->pref_name()); |
| 393 base::DictionaryValue* mutable_settings = update.Get(); | 455 base::DictionaryValue* mutable_settings = update.Get(); |
| 394 mutable_settings->SetWithoutPathExpansion("www.example.com,*", | 456 mutable_settings->SetWithoutPathExpansion("www.example.com,*", |
| 395 new base::DictionaryValue()); | 457 new base::DictionaryValue()); |
| 396 } | 458 } |
| 397 EXPECT_TRUE(observer.notification_received()); | 459 EXPECT_TRUE(observer.notification_received()); |
| 398 | 460 |
| 399 provider.ShutdownOnUIThread(); | 461 provider.ShutdownOnUIThread(); |
| 400 } | 462 } |
| 401 | 463 |
| 402 TEST_F(PrefProviderTest, LastUsage) { | |
| 403 TestingProfile testing_profile; | |
| 404 PrefProvider pref_content_settings_provider(testing_profile.GetPrefs(), | |
| 405 false); | |
| 406 base::SimpleTestClock* test_clock = new base::SimpleTestClock; | |
| 407 test_clock->SetNow(base::Time::Now()); | |
| 408 | |
| 409 pref_content_settings_provider.SetClockForTesting( | |
| 410 std::unique_ptr<base::Clock>(test_clock)); | |
| 411 GURL host("http://example.com/"); | |
| 412 ContentSettingsPattern pattern = | |
| 413 ContentSettingsPattern::FromString("[*.]example.com"); | |
| 414 | |
| 415 base::Time no_usage = pref_content_settings_provider.GetLastUsage( | |
| 416 pattern, pattern, CONTENT_SETTINGS_TYPE_GEOLOCATION); | |
| 417 EXPECT_EQ(no_usage.ToDoubleT(), 0); | |
| 418 | |
| 419 pref_content_settings_provider.UpdateLastUsage( | |
| 420 pattern, pattern, CONTENT_SETTINGS_TYPE_GEOLOCATION); | |
| 421 base::Time first = pref_content_settings_provider.GetLastUsage( | |
| 422 pattern, pattern, CONTENT_SETTINGS_TYPE_GEOLOCATION); | |
| 423 | |
| 424 test_clock->Advance(base::TimeDelta::FromSeconds(10)); | |
| 425 | |
| 426 pref_content_settings_provider.UpdateLastUsage( | |
| 427 pattern, pattern, CONTENT_SETTINGS_TYPE_GEOLOCATION); | |
| 428 base::Time second = pref_content_settings_provider.GetLastUsage( | |
| 429 pattern, pattern, CONTENT_SETTINGS_TYPE_GEOLOCATION); | |
| 430 | |
| 431 base::TimeDelta delta = second - first; | |
| 432 EXPECT_EQ(delta.InSeconds(), 10); | |
| 433 | |
| 434 pref_content_settings_provider.ShutdownOnUIThread(); | |
| 435 } | |
| 436 | |
| 437 TEST_F(PrefProviderTest, IncognitoInheritsValueMap) { | 464 TEST_F(PrefProviderTest, IncognitoInheritsValueMap) { |
| 438 sync_preferences::TestingPrefServiceSyncable prefs; | 465 sync_preferences::TestingPrefServiceSyncable prefs; |
| 439 PrefProvider::RegisterProfilePrefs(prefs.registry()); | 466 PrefProvider::RegisterProfilePrefs(prefs.registry()); |
| 440 | 467 |
| 441 ContentSettingsPattern pattern_1 = | 468 ContentSettingsPattern pattern_1 = |
| 442 ContentSettingsPattern::FromString("google.com"); | 469 ContentSettingsPattern::FromString("google.com"); |
| 443 ContentSettingsPattern pattern_2 = | 470 ContentSettingsPattern pattern_2 = |
| 444 ContentSettingsPattern::FromString("www.google.com"); | 471 ContentSettingsPattern::FromString("www.google.com"); |
| 445 ContentSettingsPattern wildcard = | 472 ContentSettingsPattern wildcard = |
| 446 ContentSettingsPattern::FromString("*"); | 473 ContentSettingsPattern::FromString("*"); |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 569 for (const char* pref : nonempty_prefs) { | 596 for (const char* pref : nonempty_prefs) { |
| 570 DictionaryPrefUpdate update(&prefs, pref); | 597 DictionaryPrefUpdate update(&prefs, pref); |
| 571 const base::DictionaryValue* dictionary = update.Get(); | 598 const base::DictionaryValue* dictionary = update.Get(); |
| 572 EXPECT_EQ(1u, dictionary->size()); | 599 EXPECT_EQ(1u, dictionary->size()); |
| 573 } | 600 } |
| 574 | 601 |
| 575 provider.ShutdownOnUIThread(); | 602 provider.ShutdownOnUIThread(); |
| 576 } | 603 } |
| 577 | 604 |
| 578 } // namespace content_settings | 605 } // namespace content_settings |
| OLD | NEW |