| 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 "base/auto_reset.h" | 7 #include "base/auto_reset.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 #include "content/public/test/test_browser_thread_bundle.h" | 34 #include "content/public/test/test_browser_thread_bundle.h" |
| 35 #include "testing/gtest/include/gtest/gtest.h" | 35 #include "testing/gtest/include/gtest/gtest.h" |
| 36 #include "url/gurl.h" | 36 #include "url/gurl.h" |
| 37 | 37 |
| 38 using ::testing::_; | 38 using ::testing::_; |
| 39 | 39 |
| 40 namespace content_settings { | 40 namespace content_settings { |
| 41 | 41 |
| 42 class DeadlockCheckerThread : public base::PlatformThread::Delegate { | 42 class DeadlockCheckerThread : public base::PlatformThread::Delegate { |
| 43 public: | 43 public: |
| 44 explicit DeadlockCheckerThread(PrefProvider* provider) | 44 explicit DeadlockCheckerThread(const ContentSettingsPref* pref) |
| 45 : provider_(provider) {} | 45 : pref_(pref) {} |
| 46 | 46 |
| 47 void ThreadMain() override { | 47 void ThreadMain() override { |
| 48 EXPECT_TRUE(provider_->TestAllLocks()); | 48 EXPECT_TRUE(pref_->TryLockForTesting()); |
| 49 } | 49 } |
| 50 private: | 50 private: |
| 51 PrefProvider* provider_; | 51 const ContentSettingsPref* pref_; |
| 52 DISALLOW_COPY_AND_ASSIGN(DeadlockCheckerThread); | 52 DISALLOW_COPY_AND_ASSIGN(DeadlockCheckerThread); |
| 53 }; | 53 }; |
| 54 | 54 |
| 55 // A helper for observing an preference changes and testing whether | 55 // A helper for observing an preference changes and testing whether |
| 56 // |PrefProvider| holds a lock when the preferences change. | 56 // |PrefProvider| holds a lock when the preferences change. |
| 57 class DeadlockCheckerObserver { | 57 class DeadlockCheckerObserver { |
| 58 public: | 58 public: |
| 59 // |DeadlockCheckerObserver| doesn't take the ownership of |prefs| or | 59 // |DeadlockCheckerObserver| doesn't take the ownership of |prefs| or |
| 60 // |provider|. | 60 // |provider|. |
| 61 DeadlockCheckerObserver(PrefService* prefs, PrefProvider* provider) | 61 DeadlockCheckerObserver(PrefService* prefs, PrefProvider* provider) |
| 62 : provider_(provider), | 62 : provider_(provider), |
| 63 notification_received_(false) { | 63 notification_received_(false) { |
| 64 pref_change_registrar_.Init(prefs); | 64 pref_change_registrar_.Init(prefs); |
| 65 pref_change_registrar_.Add( | 65 for (size_t i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) { |
| 66 prefs::kContentSettingsPatternPairs, | 66 pref_change_registrar_.Add( |
| 67 base::Bind( | 67 provider_->content_settings_prefs_[i]->pref_name_, |
| 68 &DeadlockCheckerObserver::OnContentSettingsPatternPairsChanged, | 68 base::Bind( |
| 69 base::Unretained(this))); | 69 &DeadlockCheckerObserver::OnContentSettingsPatternPairsChanged, |
| 70 base::Unretained(this), |
| 71 base::Unretained(provider_->content_settings_prefs_[i]))); |
| 72 } |
| 70 } | 73 } |
| 71 virtual ~DeadlockCheckerObserver() {} | 74 virtual ~DeadlockCheckerObserver() {} |
| 72 | 75 |
| 73 bool notification_received() const { | 76 bool notification_received() const { |
| 74 return notification_received_; | 77 return notification_received_; |
| 75 } | 78 } |
| 76 | 79 |
| 77 private: | 80 private: |
| 78 void OnContentSettingsPatternPairsChanged() { | 81 void OnContentSettingsPatternPairsChanged(const ContentSettingsPref* pref) { |
| 79 // Check whether |provider_| holds its lock. For this, we need a | 82 // Check whether |provider_| holds its lock. For this, we need a |
| 80 // separate thread. | 83 // separate thread. |
| 81 DeadlockCheckerThread thread(provider_); | 84 DeadlockCheckerThread thread(pref); |
| 82 base::PlatformThreadHandle handle; | 85 base::PlatformThreadHandle handle; |
| 83 ASSERT_TRUE(base::PlatformThread::Create(0, &thread, &handle)); | 86 ASSERT_TRUE(base::PlatformThread::Create(0, &thread, &handle)); |
| 84 base::PlatformThread::Join(handle); | 87 base::PlatformThread::Join(handle); |
| 85 notification_received_ = true; | 88 notification_received_ = true; |
| 86 } | 89 } |
| 87 | 90 |
| 88 PrefProvider* provider_; | 91 PrefProvider* provider_; |
| 89 PrefChangeRegistrar pref_change_registrar_; | 92 PrefChangeRegistrar pref_change_registrar_; |
| 90 bool notification_received_; | 93 bool notification_received_; |
| 91 DISALLOW_COPY_AND_ASSIGN(DeadlockCheckerObserver); | 94 DISALLOW_COPY_AND_ASSIGN(DeadlockCheckerObserver); |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 // And also in the OTR version. | 179 // And also in the OTR version. |
| 177 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 180 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 178 GetContentSetting(&pref_content_settings_provider_incognito, | 181 GetContentSetting(&pref_content_settings_provider_incognito, |
| 179 host, | 182 host, |
| 180 host, | 183 host, |
| 181 CONTENT_SETTINGS_TYPE_IMAGES, | 184 CONTENT_SETTINGS_TYPE_IMAGES, |
| 182 std::string(), | 185 std::string(), |
| 183 false)); | 186 false)); |
| 184 // But the value should not be overridden in the OTR user prefs accidentally. | 187 // But the value should not be overridden in the OTR user prefs accidentally. |
| 185 EXPECT_FALSE(otr_user_prefs->IsSetInOverlay( | 188 EXPECT_FALSE(otr_user_prefs->IsSetInOverlay( |
| 186 prefs::kContentSettingsPatternPairs)); | 189 prefs::kContentSettingsImagesPatternPairs)); |
| 187 | 190 |
| 188 pref_content_settings_provider.ShutdownOnUIThread(); | 191 pref_content_settings_provider.ShutdownOnUIThread(); |
| 189 pref_content_settings_provider_incognito.ShutdownOnUIThread(); | 192 pref_content_settings_provider_incognito.ShutdownOnUIThread(); |
| 190 } | 193 } |
| 191 | 194 |
| 192 TEST_F(PrefProviderTest, GetContentSettingsValue) { | 195 TEST_F(PrefProviderTest, GetContentSettingsValue) { |
| 193 TestingProfile testing_profile; | 196 TestingProfile testing_profile; |
| 194 PrefProvider provider(testing_profile.GetPrefs(), false); | 197 PrefProvider provider(testing_profile.GetPrefs(), false); |
| 195 | 198 |
| 196 GURL primary_url("http://example.com/"); | 199 GURL primary_url("http://example.com/"); |
| (...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 416 | 419 |
| 417 // Chain of events: a preference changes, |PrefProvider| notices it, and reads | 420 // Chain of events: a preference changes, |PrefProvider| notices it, and reads |
| 418 // and writes the preference. When the preference is written, a notification | 421 // and writes the preference. When the preference is written, a notification |
| 419 // is sent, and this used to happen when |PrefProvider| was still holding its | 422 // is sent, and this used to happen when |PrefProvider| was still holding its |
| 420 // lock. | 423 // lock. |
| 421 | 424 |
| 422 PrefProvider provider(&prefs, false); | 425 PrefProvider provider(&prefs, false); |
| 423 DeadlockCheckerObserver observer(&prefs, &provider); | 426 DeadlockCheckerObserver observer(&prefs, &provider); |
| 424 { | 427 { |
| 425 DictionaryPrefUpdate update(&prefs, | 428 DictionaryPrefUpdate update(&prefs, |
| 426 prefs::kContentSettingsPatternPairs); | 429 prefs::kContentSettingsImagesPatternPairs); |
| 427 base::DictionaryValue* mutable_settings = update.Get(); | 430 base::DictionaryValue* mutable_settings = update.Get(); |
| 428 mutable_settings->SetWithoutPathExpansion("www.example.com,*", | 431 mutable_settings->SetWithoutPathExpansion("www.example.com,*", |
| 429 new base::DictionaryValue()); | 432 new base::DictionaryValue()); |
| 430 } | 433 } |
| 431 EXPECT_TRUE(observer.notification_received()); | 434 EXPECT_TRUE(observer.notification_received()); |
| 432 | 435 |
| 433 provider.ShutdownOnUIThread(); | 436 provider.ShutdownOnUIThread(); |
| 434 } | 437 } |
| 435 | 438 |
| 436 TEST_F(PrefProviderTest, LastUsage) { | 439 TEST_F(PrefProviderTest, LastUsage) { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 461 pattern, pattern, CONTENT_SETTINGS_TYPE_GEOLOCATION); | 464 pattern, pattern, CONTENT_SETTINGS_TYPE_GEOLOCATION); |
| 462 base::Time second = pref_content_settings_provider.GetLastUsage( | 465 base::Time second = pref_content_settings_provider.GetLastUsage( |
| 463 pattern, pattern, CONTENT_SETTINGS_TYPE_GEOLOCATION); | 466 pattern, pattern, CONTENT_SETTINGS_TYPE_GEOLOCATION); |
| 464 | 467 |
| 465 base::TimeDelta delta = second - first; | 468 base::TimeDelta delta = second - first; |
| 466 EXPECT_EQ(delta.InSeconds(), 10); | 469 EXPECT_EQ(delta.InSeconds(), 10); |
| 467 | 470 |
| 468 pref_content_settings_provider.ShutdownOnUIThread(); | 471 pref_content_settings_provider.ShutdownOnUIThread(); |
| 469 } | 472 } |
| 470 | 473 |
| 471 | |
| 472 // TODO(msramek): This tests the correct migration behavior between the old | |
| 473 // aggregate dictionary preferences for all content settings types and the new | |
| 474 // dictionary preferences for individual types. Remove this when the migration | |
| 475 // period is over. | |
| 476 TEST_F(PrefProviderTest, SyncingOldToNew) { | |
| 477 TestingPrefServiceSyncable prefs; | |
| 478 PrefProvider::RegisterProfilePrefs(prefs.registry()); | |
| 479 PrefProvider provider(&prefs, false); | |
| 480 | |
| 481 const std::string pattern = "google.com,*"; | |
| 482 const std::string resource_id = "abcde12345"; | |
| 483 base::DictionaryValue* exceptions = new base::DictionaryValue(); | |
| 484 base::DictionaryValue* plugin_resources = new base::DictionaryValue(); | |
| 485 | |
| 486 // Add exceptions for images and app banner which did not need to be migrated. | |
| 487 exceptions->SetIntegerWithoutPathExpansion( | |
| 488 GetTypeName(CONTENT_SETTINGS_TYPE_IMAGES), CONTENT_SETTING_ALLOW); | |
| 489 exceptions->SetIntegerWithoutPathExpansion( | |
| 490 GetTypeName(CONTENT_SETTINGS_TYPE_APP_BANNER), CONTENT_SETTING_ALLOW); | |
| 491 | |
| 492 // Add exceptions for new content settings types added after the migration. | |
| 493 exceptions->SetIntegerWithoutPathExpansion( | |
| 494 GetTypeName(CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT), | |
| 495 CONTENT_SETTING_ALLOW); | |
| 496 | |
| 497 // Add a regular exception for plugins, then one with a resource identifier. | |
| 498 exceptions->SetIntegerWithoutPathExpansion( | |
| 499 GetTypeName(CONTENT_SETTINGS_TYPE_PLUGINS), CONTENT_SETTING_ALLOW); | |
| 500 plugin_resources->SetIntegerWithoutPathExpansion( | |
| 501 resource_id, CONTENT_SETTING_BLOCK); | |
| 502 exceptions->SetWithoutPathExpansion( | |
| 503 "per_plugin", plugin_resources); | |
| 504 | |
| 505 // Change the old dictionary preference and observe changes | |
| 506 // in the new preferences. | |
| 507 { | |
| 508 DictionaryPrefUpdate update(&prefs, prefs::kContentSettingsPatternPairs); | |
| 509 base::DictionaryValue* old_dictionary = update.Get(); | |
| 510 old_dictionary->SetWithoutPathExpansion(pattern, exceptions); | |
| 511 } | |
| 512 | |
| 513 // The images exception was synced. | |
| 514 { | |
| 515 DictionaryPrefUpdate update( | |
| 516 &prefs, prefs::kContentSettingsImagesPatternPairs); | |
| 517 const base::DictionaryValue* images_dictionary = update.Get(); | |
| 518 | |
| 519 EXPECT_EQ(1u, images_dictionary->size()); | |
| 520 const base::DictionaryValue* images_exception; | |
| 521 EXPECT_TRUE(images_dictionary->GetDictionaryWithoutPathExpansion( | |
| 522 pattern, &images_exception)); | |
| 523 | |
| 524 // And it has a correct value. | |
| 525 int images_exception_value = CONTENT_SETTING_DEFAULT; | |
| 526 EXPECT_TRUE(images_exception->GetIntegerWithoutPathExpansion( | |
| 527 "setting", &images_exception_value)); | |
| 528 EXPECT_EQ(CONTENT_SETTING_ALLOW, images_exception_value); | |
| 529 } | |
| 530 | |
| 531 // The app banner exception was not synced. | |
| 532 { | |
| 533 DictionaryPrefUpdate update( | |
| 534 &prefs, prefs::kContentSettingsAppBannerPatternPairs); | |
| 535 const base::DictionaryValue* app_banner_dictionary = update.Get(); | |
| 536 EXPECT_TRUE(app_banner_dictionary->empty()); | |
| 537 } | |
| 538 | |
| 539 // The plugins exception was synced, together with the resource identifiers. | |
| 540 { | |
| 541 DictionaryPrefUpdate update( | |
| 542 &prefs, prefs::kContentSettingsPluginsPatternPairs); | |
| 543 const base::DictionaryValue* plugins_dictionary = update.Get(); | |
| 544 EXPECT_EQ(1u, plugins_dictionary->size()); | |
| 545 | |
| 546 const base::DictionaryValue* plugins_exception; | |
| 547 EXPECT_TRUE(plugins_dictionary->GetDictionaryWithoutPathExpansion( | |
| 548 pattern, &plugins_exception)); | |
| 549 | |
| 550 int plugins_exception_value = CONTENT_SETTING_DEFAULT; | |
| 551 EXPECT_TRUE(plugins_exception->GetIntegerWithoutPathExpansion( | |
| 552 "setting", &plugins_exception_value)); | |
| 553 EXPECT_EQ(CONTENT_SETTING_ALLOW, plugins_exception_value); | |
| 554 | |
| 555 int resource_exception_value = CONTENT_SETTING_DEFAULT; | |
| 556 const base::DictionaryValue* resource_exception; | |
| 557 EXPECT_TRUE(plugins_exception->GetDictionaryWithoutPathExpansion( | |
| 558 "per_resource", &resource_exception)); | |
| 559 EXPECT_TRUE(resource_exception->GetIntegerWithoutPathExpansion( | |
| 560 resource_id, &resource_exception_value)); | |
| 561 EXPECT_EQ(CONTENT_SETTING_BLOCK, resource_exception_value); | |
| 562 } | |
| 563 | |
| 564 provider.ShutdownOnUIThread(); | |
| 565 } | |
| 566 | |
| 567 TEST_F(PrefProviderTest, SyncingNewToOld) { | |
| 568 TestingPrefServiceSyncable prefs; | |
| 569 PrefProvider::RegisterProfilePrefs(prefs.registry()); | |
| 570 PrefProvider provider(&prefs, false); | |
| 571 | |
| 572 const std::string pattern = "google.com,*"; | |
| 573 const std::string resource_id = "abcde12345"; | |
| 574 base::DictionaryValue block_exception; | |
| 575 block_exception.SetIntegerWithoutPathExpansion( | |
| 576 "setting", CONTENT_SETTING_BLOCK); | |
| 577 | |
| 578 // Add a mouselock exception. | |
| 579 { | |
| 580 DictionaryPrefUpdate update( | |
| 581 &prefs, prefs::kContentSettingsMouseLockPatternPairs); | |
| 582 base::DictionaryValue* mouselock_dictionary = update.Get(); | |
| 583 | |
| 584 mouselock_dictionary->SetWithoutPathExpansion( | |
| 585 pattern, block_exception.DeepCopy()); | |
| 586 } | |
| 587 | |
| 588 // Add a microphone exception. | |
| 589 { | |
| 590 DictionaryPrefUpdate update( | |
| 591 &prefs, prefs::kContentSettingsMediaStreamMicPatternPairs); | |
| 592 base::DictionaryValue* microphone_dictionary = update.Get(); | |
| 593 | |
| 594 microphone_dictionary->SetWithoutPathExpansion( | |
| 595 pattern, block_exception.DeepCopy()); | |
| 596 } | |
| 597 | |
| 598 // Add a plugin exception with resource identifiers. | |
| 599 { | |
| 600 DictionaryPrefUpdate update( | |
| 601 &prefs, prefs::kContentSettingsPluginsPatternPairs); | |
| 602 base::DictionaryValue* plugins_dictionary = update.Get(); | |
| 603 | |
| 604 base::DictionaryValue* plugin_exception = block_exception.DeepCopy(); | |
| 605 plugins_dictionary->SetWithoutPathExpansion( | |
| 606 pattern, plugin_exception); | |
| 607 | |
| 608 base::DictionaryValue* resource_exception = new base::DictionaryValue(); | |
| 609 resource_exception->SetIntegerWithoutPathExpansion( | |
| 610 resource_id, CONTENT_SETTING_ALLOW); | |
| 611 | |
| 612 plugin_exception->SetWithoutPathExpansion( | |
| 613 "per_resource", resource_exception); | |
| 614 } | |
| 615 | |
| 616 // Only the notifications and plugin exceptions should appear in the | |
| 617 // old dictionary. We should also have a resource identifiers section | |
| 618 // for plugins. | |
| 619 { | |
| 620 DictionaryPrefUpdate update( | |
| 621 &prefs, prefs::kContentSettingsPatternPairs); | |
| 622 const base::DictionaryValue* old_dictionary = update.Get(); | |
| 623 | |
| 624 const base::DictionaryValue* exception; | |
| 625 EXPECT_TRUE(old_dictionary->GetDictionaryWithoutPathExpansion( | |
| 626 pattern, &exception)); | |
| 627 EXPECT_EQ(3u, exception->size()); | |
| 628 EXPECT_FALSE(exception->HasKey( | |
| 629 GetTypeName(CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC))); | |
| 630 | |
| 631 int mouselock_exception_value = CONTENT_SETTING_DEFAULT; | |
| 632 exception->GetIntegerWithoutPathExpansion( | |
| 633 GetTypeName(CONTENT_SETTINGS_TYPE_MOUSELOCK), | |
| 634 &mouselock_exception_value); | |
| 635 DCHECK_EQ(CONTENT_SETTING_BLOCK, mouselock_exception_value); | |
| 636 | |
| 637 int plugins_exception_value = CONTENT_SETTING_DEFAULT; | |
| 638 exception->GetIntegerWithoutPathExpansion( | |
| 639 GetTypeName(CONTENT_SETTINGS_TYPE_PLUGINS), | |
| 640 &plugins_exception_value); | |
| 641 DCHECK_EQ(CONTENT_SETTING_BLOCK, plugins_exception_value); | |
| 642 | |
| 643 int resource_exception_value = CONTENT_SETTING_DEFAULT; | |
| 644 const base::DictionaryValue* resource_values; | |
| 645 exception->GetDictionaryWithoutPathExpansion( | |
| 646 "per_plugin", &resource_values); | |
| 647 resource_values->GetIntegerWithoutPathExpansion( | |
| 648 resource_id, &resource_exception_value); | |
| 649 DCHECK_EQ(CONTENT_SETTING_ALLOW, resource_exception_value); | |
| 650 } | |
| 651 | |
| 652 provider.ShutdownOnUIThread(); | |
| 653 } | |
| 654 | |
| 655 #if defined(OS_CHROMEOS) || defined(OS_ANDROID) | |
| 656 TEST_F(PrefProviderTest, PMIMigrateOnlyAllow) { | |
| 657 TestingPrefServiceSyncable prefs; | |
| 658 PrefProvider::RegisterProfilePrefs(prefs.registry()); | |
| 659 | |
| 660 const std::string pattern_1 = "google.com,*"; | |
| 661 const std::string pattern_2 = "www.google.com,*"; | |
| 662 base::DictionaryValue* exception_1 = new base::DictionaryValue(); | |
| 663 base::DictionaryValue* exception_2 = new base::DictionaryValue(); | |
| 664 | |
| 665 // Add both an "allow" and "block" exception for PMI. | |
| 666 exception_1->SetIntegerWithoutPathExpansion( | |
| 667 GetTypeName(CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER), | |
| 668 CONTENT_SETTING_ALLOW); | |
| 669 exception_2->SetIntegerWithoutPathExpansion( | |
| 670 GetTypeName(CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER), | |
| 671 CONTENT_SETTING_BLOCK); | |
| 672 | |
| 673 // Change the old dictionary preference. | |
| 674 { | |
| 675 DictionaryPrefUpdate update(&prefs, prefs::kContentSettingsPatternPairs); | |
| 676 base::DictionaryValue* old_dictionary = update.Get(); | |
| 677 old_dictionary->SetWithoutPathExpansion(pattern_1, exception_1); | |
| 678 old_dictionary->SetWithoutPathExpansion(pattern_2, exception_2); | |
| 679 } | |
| 680 | |
| 681 // Create the PrefProvider. It should migrate the settings. | |
| 682 PrefProvider provider(&prefs, false); | |
| 683 | |
| 684 // The "block" exception for PMI was migrated, but "allow" was not. | |
| 685 { | |
| 686 DictionaryPrefUpdate update( | |
| 687 &prefs, prefs::kContentSettingsProtectedMediaIdentifierPatternPairs); | |
| 688 const base::DictionaryValue* pmi_dictionary = update.Get(); | |
| 689 EXPECT_FALSE(pmi_dictionary->HasKey(pattern_1)); | |
| 690 EXPECT_TRUE(pmi_dictionary->HasKey(pattern_2)); | |
| 691 } | |
| 692 | |
| 693 provider.ShutdownOnUIThread(); | |
| 694 } | |
| 695 #endif | |
| 696 | |
| 697 TEST_F(PrefProviderTest, PrefsMigrateVerbatim) { | |
| 698 TestingPrefServiceSyncable prefs; | |
| 699 PrefProvider::RegisterProfilePrefs(prefs.registry()); | |
| 700 | |
| 701 const std::string pattern_1 = "google.com,*"; | |
| 702 const std::string pattern_2 = "www.google.com,*"; | |
| 703 base::DictionaryValue* exception_1 = new base::DictionaryValue(); | |
| 704 base::DictionaryValue* exception_2 = new base::DictionaryValue(); | |
| 705 scoped_ptr<base::DictionaryValue> old_dictionary; | |
| 706 | |
| 707 // Add two exceptions. | |
| 708 exception_1->SetIntegerWithoutPathExpansion( | |
| 709 GetTypeName(CONTENT_SETTINGS_TYPE_COOKIES), | |
| 710 CONTENT_SETTING_ALLOW); | |
| 711 exception_2->SetIntegerWithoutPathExpansion( | |
| 712 GetTypeName(CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS), | |
| 713 CONTENT_SETTING_BLOCK); | |
| 714 | |
| 715 // Change the old dictionary preference. | |
| 716 { | |
| 717 DictionaryPrefUpdate update(&prefs, prefs::kContentSettingsPatternPairs); | |
| 718 base::DictionaryValue* dictionary = update.Get(); | |
| 719 dictionary->SetWithoutPathExpansion(pattern_1, exception_1); | |
| 720 dictionary->SetWithoutPathExpansion(pattern_2, exception_2); | |
| 721 old_dictionary.reset(dictionary->DeepCopy()); | |
| 722 } | |
| 723 | |
| 724 // Create the PrefProvider. It should copy the settings from the old | |
| 725 // preference to the new ones. | |
| 726 PrefProvider provider(&prefs, false); | |
| 727 | |
| 728 // Force copying back from the new preferences to the old one. | |
| 729 { | |
| 730 DictionaryPrefUpdate update( | |
| 731 &prefs, prefs::kContentSettingsCookiesPatternPairs); | |
| 732 } | |
| 733 { | |
| 734 DictionaryPrefUpdate update( | |
| 735 &prefs, prefs::kContentSettingsAutomaticDownloadsPatternPairs); | |
| 736 } | |
| 737 | |
| 738 // Test if the value after copying there and back is the same. | |
| 739 { | |
| 740 DictionaryPrefUpdate update(&prefs, prefs::kContentSettingsPatternPairs); | |
| 741 base::DictionaryValue* new_dictionary = update.Get(); | |
| 742 EXPECT_TRUE(old_dictionary->Equals(new_dictionary)); | |
| 743 } | |
| 744 | |
| 745 provider.ShutdownOnUIThread(); | |
| 746 } | |
| 747 | |
| 748 TEST_F(PrefProviderTest, IncognitoInheritsValueMap) { | 474 TEST_F(PrefProviderTest, IncognitoInheritsValueMap) { |
| 749 TestingPrefServiceSyncable prefs; | 475 TestingPrefServiceSyncable prefs; |
| 750 PrefProvider::RegisterProfilePrefs(prefs.registry()); | 476 PrefProvider::RegisterProfilePrefs(prefs.registry()); |
| 751 | 477 |
| 752 ContentSettingsPattern pattern_1 = | 478 ContentSettingsPattern pattern_1 = |
| 753 ContentSettingsPattern::FromString("google.com"); | 479 ContentSettingsPattern::FromString("google.com"); |
| 754 ContentSettingsPattern pattern_2 = | 480 ContentSettingsPattern pattern_2 = |
| 755 ContentSettingsPattern::FromString("www.google.com"); | 481 ContentSettingsPattern::FromString("www.google.com"); |
| 756 ContentSettingsPattern wildcard = | 482 ContentSettingsPattern wildcard = |
| 757 ContentSettingsPattern::FromString("*"); | 483 ContentSettingsPattern::FromString("*"); |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 849 | 575 |
| 850 // Non-empty pattern, non-syncable, empty resource identifier. | 576 // Non-empty pattern, non-syncable, empty resource identifier. |
| 851 provider.SetWebsiteSetting(pattern, wildcard, | 577 provider.SetWebsiteSetting(pattern, wildcard, |
| 852 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | 578 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, |
| 853 ResourceIdentifier(), value->DeepCopy()); | 579 ResourceIdentifier(), value->DeepCopy()); |
| 854 | 580 |
| 855 provider.ClearAllContentSettingsRules(CONTENT_SETTINGS_TYPE_IMAGES); | 581 provider.ClearAllContentSettingsRules(CONTENT_SETTINGS_TYPE_IMAGES); |
| 856 provider.ClearAllContentSettingsRules(CONTENT_SETTINGS_TYPE_GEOLOCATION); | 582 provider.ClearAllContentSettingsRules(CONTENT_SETTINGS_TYPE_GEOLOCATION); |
| 857 provider.ClearAllContentSettingsRules(CONTENT_SETTINGS_TYPE_PLUGINS); | 583 provider.ClearAllContentSettingsRules(CONTENT_SETTINGS_TYPE_PLUGINS); |
| 858 | 584 |
| 859 // Test that the new preferences for images, geolocation and plugins | 585 // Test that the preferences for images, geolocation and plugins are empty. |
| 860 // are empty. | |
| 861 const char* empty_prefs[] = { | 586 const char* empty_prefs[] = { |
| 862 prefs::kContentSettingsImagesPatternPairs, | 587 prefs::kContentSettingsImagesPatternPairs, |
| 863 prefs::kContentSettingsGeolocationPatternPairs, | 588 prefs::kContentSettingsGeolocationPatternPairs, |
| 864 prefs::kContentSettingsPluginsPatternPairs | 589 prefs::kContentSettingsPluginsPatternPairs |
| 865 }; | 590 }; |
| 866 | 591 |
| 867 for (const char* pref : empty_prefs) { | 592 for (const char* pref : empty_prefs) { |
| 868 DictionaryPrefUpdate update(&prefs, pref); | 593 DictionaryPrefUpdate update(&prefs, pref); |
| 869 const base::DictionaryValue* dictionary = update.Get(); | 594 const base::DictionaryValue* dictionary = update.Get(); |
| 870 EXPECT_TRUE(dictionary->empty()); | 595 EXPECT_TRUE(dictionary->empty()); |
| 871 } | 596 } |
| 872 | 597 |
| 873 // Test that the preferences for cookies and notifications are not empty. | 598 // Test that the preferences for cookies and notifications are not empty. |
| 874 const char* nonempty_prefs[] = { | 599 const char* nonempty_prefs[] = { |
| 875 prefs::kContentSettingsCookiesPatternPairs, | 600 prefs::kContentSettingsCookiesPatternPairs, |
| 876 prefs::kContentSettingsNotificationsPatternPairs | 601 prefs::kContentSettingsNotificationsPatternPairs |
| 877 }; | 602 }; |
| 878 | 603 |
| 879 for (const char* pref : nonempty_prefs) { | 604 for (const char* pref : nonempty_prefs) { |
| 880 DictionaryPrefUpdate update(&prefs, pref); | 605 DictionaryPrefUpdate update(&prefs, pref); |
| 881 const base::DictionaryValue* dictionary = update.Get(); | 606 const base::DictionaryValue* dictionary = update.Get(); |
| 882 EXPECT_EQ(1u, dictionary->size()); | 607 EXPECT_EQ(1u, dictionary->size()); |
| 883 } | 608 } |
| 884 | 609 |
| 885 // Test that the old preference only contains cookies and notifications. | |
| 886 { | |
| 887 DictionaryPrefUpdate update(&prefs, prefs::kContentSettingsPatternPairs); | |
| 888 const base::DictionaryValue* dictionary = update.Get(); | |
| 889 const base::DictionaryValue* exception; | |
| 890 EXPECT_TRUE(dictionary->GetDictionaryWithoutPathExpansion( | |
| 891 CreatePatternString(pattern, wildcard), &exception)); | |
| 892 EXPECT_EQ(1u, exception->size()); | |
| 893 EXPECT_TRUE(exception->HasKey(GetTypeName(CONTENT_SETTINGS_TYPE_COOKIES))); | |
| 894 | |
| 895 // The notification setting was not cleared, but it was also never written | |
| 896 // to the old preference, as it is unsyncable. | |
| 897 } | |
| 898 | |
| 899 provider.ShutdownOnUIThread(); | 610 provider.ShutdownOnUIThread(); |
| 900 } | 611 } |
| 901 | 612 |
| 902 } // namespace content_settings | 613 } // namespace content_settings |
| OLD | NEW |