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/content_settings_mock_observer.h" |
10 #include "chrome/browser/prefs/browser_prefs.h" | 10 #include "chrome/browser/prefs/browser_prefs.h" |
11 #include "chrome/browser/prefs/default_pref_store.h" | 11 #include "chrome/browser/prefs/default_pref_store.h" |
12 #include "chrome/browser/prefs/incognito_user_pref_store.h" | 12 #include "chrome/browser/prefs/incognito_user_pref_store.h" |
13 #include "chrome/browser/prefs/pref_service.h" | 13 #include "chrome/browser/prefs/pref_service.h" |
14 #include "chrome/browser/prefs/pref_service_mock_builder.h" | 14 #include "chrome/browser/prefs/pref_service_mock_builder.h" |
15 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 15 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
16 #include "chrome/browser/prefs/testing_pref_store.h" | 16 #include "chrome/browser/prefs/testing_pref_store.h" |
17 #include "chrome/common/chrome_switches.h" | 17 #include "chrome/common/chrome_switches.h" |
18 #include "chrome/common/pref_names.h" | 18 #include "chrome/common/pref_names.h" |
19 #include "chrome/common/url_constants.h" | 19 #include "chrome/common/url_constants.h" |
20 #include "chrome/test/base/testing_browser_process_test.h" | 20 #include "chrome/test/base/testing_browser_process_test.h" |
21 #include "chrome/test/base/testing_pref_service.h" | 21 #include "chrome/test/base/testing_pref_service.h" |
22 #include "chrome/test/base/testing_profile.h" | 22 #include "chrome/test/base/testing_profile.h" |
23 #include "content/browser/browser_thread.h" | 23 #include "content/browser/browser_thread.h" |
24 #include "googleurl/src/gurl.h" | 24 #include "googleurl/src/gurl.h" |
25 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
26 | 26 |
27 using ::testing::_; | 27 using ::testing::_; |
28 | 28 |
| 29 namespace { |
| 30 |
| 31 void ExpectObsoleteGeolocationSetting( |
| 32 const DictionaryValue& geo_settings_dictionary, |
| 33 const GURL& primary_origin, |
| 34 const GURL& secondary_origin, |
| 35 ContentSetting expected_setting) { |
| 36 |
| 37 DictionaryValue* one_origin_settings; |
| 38 ASSERT_TRUE(geo_settings_dictionary.GetDictionaryWithoutPathExpansion( |
| 39 std::string(primary_origin.spec()), &one_origin_settings)); |
| 40 int setting_value; |
| 41 ASSERT_TRUE(one_origin_settings->GetIntegerWithoutPathExpansion( |
| 42 std::string(secondary_origin.spec()), &setting_value)); |
| 43 EXPECT_EQ(expected_setting, setting_value); |
| 44 } |
| 45 |
| 46 } // namespace |
| 47 |
29 namespace content_settings { | 48 namespace content_settings { |
30 | 49 |
31 class PrefDefaultProviderTest : public TestingBrowserProcessTest { | 50 class PrefDefaultProviderTest : public TestingBrowserProcessTest { |
32 public: | 51 public: |
33 PrefDefaultProviderTest() | 52 PrefDefaultProviderTest() |
34 : ui_thread_(BrowserThread::UI, &message_loop_), | 53 : ui_thread_(BrowserThread::UI, &message_loop_), |
35 provider_(profile_.GetPrefs(), false) { | 54 provider_(profile_.GetPrefs(), false) { |
36 } | 55 } |
37 ~PrefDefaultProviderTest() { | 56 ~PrefDefaultProviderTest() { |
38 provider_.ShutdownOnUIThread(); | 57 provider_.ShutdownOnUIThread(); |
(...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
394 EXPECT_EQ(CONTENT_SETTING_BLOCK, provider.GetContentSetting( | 413 EXPECT_EQ(CONTENT_SETTING_BLOCK, provider.GetContentSetting( |
395 GURL("http://www.example.com"), | 414 GURL("http://www.example.com"), |
396 GURL("http://www.example.com"), | 415 GURL("http://www.example.com"), |
397 CONTENT_SETTINGS_TYPE_IMAGES, | 416 CONTENT_SETTINGS_TYPE_IMAGES, |
398 "")); | 417 "")); |
399 EXPECT_EQ(CONTENT_SETTING_BLOCK, provider.GetContentSetting( | 418 EXPECT_EQ(CONTENT_SETTING_BLOCK, provider.GetContentSetting( |
400 GURL("http://www.example.com"), | 419 GURL("http://www.example.com"), |
401 GURL("http://www.example.com"), | 420 GURL("http://www.example.com"), |
402 CONTENT_SETTINGS_TYPE_POPUPS, | 421 CONTENT_SETTINGS_TYPE_POPUPS, |
403 "")); | 422 "")); |
404 | 423 // Test if single pattern settings are properly migrated. |
405 // Change obsolete preference. This can happen if a user has enabled sync | 424 const_all_settings_dictionary = prefs->GetDictionary( |
406 // while using an old version of chrome. | 425 prefs::kContentSettingsPatternPairs); |
407 { | 426 EXPECT_EQ(1U, const_all_settings_dictionary->size()); |
408 DictionaryPrefUpdate update(prefs, prefs::kContentSettingsPatterns); | 427 EXPECT_FALSE(const_all_settings_dictionary->HasKey(pattern.ToString())); |
409 DictionaryValue* mutable_patterns = update.Get(); | 428 EXPECT_TRUE(const_all_settings_dictionary->HasKey( |
410 DictionaryValue* mutable_settings = NULL; | 429 pattern.ToString() + "," + |
411 std::string key = pattern.ToString(); | 430 ContentSettingsPattern::Wildcard().ToString())); |
412 mutable_patterns->GetDictionaryWithoutPathExpansion(key, &mutable_settings); | |
413 ASSERT_TRUE(mutable_settings != NULL) << "Dictionary has no key: " << key; | |
414 mutable_settings->SetInteger("javascript", CONTENT_SETTING_BLOCK); | |
415 } | |
416 | |
417 // Test if the changed single pattern setting was migrated correctly. | |
418 EXPECT_EQ(CONTENT_SETTING_BLOCK, provider.GetContentSetting( | |
419 GURL("http://www.example.com"), | |
420 GURL("http://www.foo.com"), | |
421 CONTENT_SETTINGS_TYPE_JAVASCRIPT, | |
422 "")); | |
423 | 431 |
424 provider.ShutdownOnUIThread(); | 432 provider.ShutdownOnUIThread(); |
425 } | 433 } |
426 | 434 |
427 TEST_F(PrefProviderTest, SyncObsoletePref) { | 435 TEST_F(PrefProviderTest, SyncObsoletePref) { |
428 TestingProfile profile; | 436 TestingProfile profile; |
429 PrefService* prefs = profile.GetPrefs(); | 437 PrefService* prefs = profile.GetPrefs(); |
430 content_settings::PrefProvider provider(prefs, false); | 438 content_settings::PrefProvider provider(prefs, false); |
431 | 439 |
432 // Assert pre-condition. | 440 // Assert pre-condition. |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
548 EXPECT_FALSE(patterns_dictionary->HasKey("http://www.foo.com,error*")); | 556 EXPECT_FALSE(patterns_dictionary->HasKey("http://www.foo.com,error*")); |
549 EXPECT_FALSE(patterns_dictionary->HasKey( | 557 EXPECT_FALSE(patterns_dictionary->HasKey( |
550 "http://www.foo.com,[*.]bar.com")); | 558 "http://www.foo.com,[*.]bar.com")); |
551 EXPECT_FALSE(patterns_dictionary->HasKey("http://www.example2.com,*")); | 559 EXPECT_FALSE(patterns_dictionary->HasKey("http://www.example2.com,*")); |
552 | 560 |
553 EXPECT_FALSE(patterns_dictionary->HasKey("http://www.broken.com*")); | 561 EXPECT_FALSE(patterns_dictionary->HasKey("http://www.broken.com*")); |
554 | 562 |
555 provider.ShutdownOnUIThread(); | 563 provider.ShutdownOnUIThread(); |
556 } | 564 } |
557 | 565 |
| 566 TEST_F(PrefProviderTest, MigrateObsoleteGeolocationPref) { |
| 567 TestingProfile profile; |
| 568 PrefService* prefs = profile.GetPrefs(); |
| 569 GURL secondary_url("http://www.foo.com"); |
| 570 GURL primary_url("http://www.bar.com"); |
| 571 |
| 572 // Set obsolete preference. |
| 573 DictionaryValue* secondary_patterns_dictionary = new DictionaryValue(); |
| 574 secondary_patterns_dictionary->SetWithoutPathExpansion( |
| 575 secondary_url.spec(), |
| 576 Value::CreateIntegerValue(CONTENT_SETTING_BLOCK)); |
| 577 scoped_ptr<DictionaryValue> geolocation_settings_dictionary( |
| 578 new DictionaryValue()); |
| 579 geolocation_settings_dictionary->SetWithoutPathExpansion( |
| 580 primary_url.spec(), secondary_patterns_dictionary); |
| 581 prefs->Set(prefs::kGeolocationContentSettings, |
| 582 *geolocation_settings_dictionary); |
| 583 |
| 584 |
| 585 content_settings::PrefProvider provider(prefs, false); |
| 586 |
| 587 // Test if the migrated settings are loaded and available. |
| 588 EXPECT_EQ(CONTENT_SETTING_BLOCK, provider.GetContentSetting( |
| 589 primary_url, |
| 590 secondary_url, |
| 591 CONTENT_SETTINGS_TYPE_GEOLOCATION, |
| 592 "")); |
| 593 EXPECT_EQ(CONTENT_SETTING_DEFAULT, provider.GetContentSetting( |
| 594 GURL("http://www.example.com"), |
| 595 secondary_url, |
| 596 CONTENT_SETTINGS_TYPE_GEOLOCATION, |
| 597 "")); |
| 598 // Check if the settings where migrated correctly. |
| 599 const DictionaryValue* const_all_settings_dictionary = |
| 600 prefs->GetDictionary(prefs::kContentSettingsPatternPairs); |
| 601 EXPECT_EQ(1U, const_all_settings_dictionary->size()); |
| 602 EXPECT_TRUE(const_all_settings_dictionary->HasKey( |
| 603 ContentSettingsPattern::FromURLNoWildcard(primary_url).ToString() + "," + |
| 604 ContentSettingsPattern::FromURLNoWildcard(secondary_url).ToString())); |
| 605 // Check that geolocation settings were not synced to the obsolete content |
| 606 // settings pattern preference. |
| 607 const DictionaryValue* const_obsolete_patterns_dictionary = |
| 608 prefs->GetDictionary(prefs::kContentSettingsPatterns); |
| 609 EXPECT_TRUE(const_obsolete_patterns_dictionary->empty()); |
| 610 |
| 611 // Change obsolete preference. This could be triggered by sync if sync is used |
| 612 // with an old version of chrome. |
| 613 secondary_patterns_dictionary = new DictionaryValue(); |
| 614 secondary_patterns_dictionary->SetWithoutPathExpansion( |
| 615 secondary_url.spec(), |
| 616 Value::CreateIntegerValue(CONTENT_SETTING_ALLOW)); |
| 617 geolocation_settings_dictionary.reset(new DictionaryValue()); |
| 618 geolocation_settings_dictionary->SetWithoutPathExpansion( |
| 619 primary_url.spec(), secondary_patterns_dictionary); |
| 620 prefs->Set(prefs::kGeolocationContentSettings, |
| 621 *geolocation_settings_dictionary); |
| 622 |
| 623 // Test if the changed obsolete preference was migrated correctly. |
| 624 EXPECT_EQ(CONTENT_SETTING_ALLOW, provider.GetContentSetting( |
| 625 primary_url, |
| 626 secondary_url, |
| 627 CONTENT_SETTINGS_TYPE_GEOLOCATION, |
| 628 "")); |
| 629 // Check that geolocation settings were not synced to the obsolete content |
| 630 // settings pattern preference. |
| 631 const_obsolete_patterns_dictionary = |
| 632 prefs->GetDictionary(prefs::kContentSettingsPatterns); |
| 633 EXPECT_TRUE(const_obsolete_patterns_dictionary->empty()); |
| 634 |
| 635 provider.ShutdownOnUIThread(); |
| 636 } |
| 637 |
| 638 TEST_F(PrefProviderTest, SyncObsoleteGeolocationPref) { |
| 639 TestingProfile profile; |
| 640 PrefService* prefs = profile.GetPrefs(); |
| 641 |
| 642 content_settings::PrefProvider provider(prefs, false); |
| 643 |
| 644 // Changing the preferences prefs::kContentSettingsPatternPairs. |
| 645 ContentSettingsPattern primary_pattern= |
| 646 ContentSettingsPattern::FromString("http://www.bar.com"); |
| 647 ContentSettingsPattern primary_pattern_2 = |
| 648 ContentSettingsPattern::FromString("http://www.example.com"); |
| 649 ContentSettingsPattern secondary_pattern = |
| 650 ContentSettingsPattern::FromString("http://www.foo.com"); |
| 651 scoped_ptr<DictionaryValue> settings_dictionary(new DictionaryValue()); |
| 652 settings_dictionary->SetInteger("geolocation", CONTENT_SETTING_BLOCK); |
| 653 { |
| 654 DictionaryPrefUpdate update(prefs, |
| 655 prefs::kContentSettingsPatternPairs); |
| 656 DictionaryValue* all_settings_dictionary = update.Get(); |
| 657 std::string key( |
| 658 primary_pattern.ToString()+ "," + |
| 659 secondary_pattern.ToString()); |
| 660 all_settings_dictionary->SetWithoutPathExpansion( |
| 661 key, settings_dictionary->DeepCopy()); |
| 662 |
| 663 key = std::string( |
| 664 primary_pattern_2.ToString()+ "," + |
| 665 secondary_pattern.ToString()); |
| 666 all_settings_dictionary->SetWithoutPathExpansion( |
| 667 key, settings_dictionary->DeepCopy()); |
| 668 } |
| 669 |
| 670 // Test if the obsolete geolocation preference is kept in sync if the new |
| 671 // preference is changed by a sync. |
| 672 GURL primary_url("http://www.bar.com"); |
| 673 GURL primary_url_2("http://www.example.com"); |
| 674 GURL secondary_url("http://www.foo.com"); |
| 675 |
| 676 const DictionaryValue* geo_settings_dictionary = |
| 677 prefs->GetDictionary(prefs::kGeolocationContentSettings); |
| 678 EXPECT_EQ(2U, geo_settings_dictionary->size()); |
| 679 ExpectObsoleteGeolocationSetting(*geo_settings_dictionary, |
| 680 primary_url, |
| 681 secondary_url, |
| 682 CONTENT_SETTING_BLOCK); |
| 683 ExpectObsoleteGeolocationSetting(*geo_settings_dictionary, |
| 684 primary_url_2, |
| 685 secondary_url, |
| 686 CONTENT_SETTING_BLOCK); |
| 687 |
| 688 provider.ShutdownOnUIThread(); |
| 689 } |
| 690 |
558 } // namespace content_settings | 691 } // namespace content_settings |
OLD | NEW |