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 25 matching lines...) Expand all Loading... |
36 using ::testing::_; | 36 using ::testing::_; |
37 | 37 |
38 namespace content_settings { | 38 namespace content_settings { |
39 | 39 |
40 class DeadlockCheckerThread : public base::PlatformThread::Delegate { | 40 class DeadlockCheckerThread : public base::PlatformThread::Delegate { |
41 public: | 41 public: |
42 explicit DeadlockCheckerThread(PrefProvider* provider) | 42 explicit DeadlockCheckerThread(PrefProvider* provider) |
43 : provider_(provider) {} | 43 : provider_(provider) {} |
44 | 44 |
45 void ThreadMain() override { | 45 void ThreadMain() override { |
46 bool got_lock = provider_->content_settings_pref()->lock_.Try(); | 46 EXPECT_TRUE(provider_->TestAllLocks()); |
47 EXPECT_TRUE(got_lock); | |
48 if (got_lock) | |
49 provider_->content_settings_pref()->lock_.Release(); | |
50 } | 47 } |
51 private: | 48 private: |
52 PrefProvider* provider_; | 49 PrefProvider* provider_; |
53 DISALLOW_COPY_AND_ASSIGN(DeadlockCheckerThread); | 50 DISALLOW_COPY_AND_ASSIGN(DeadlockCheckerThread); |
54 }; | 51 }; |
55 | 52 |
56 // A helper for observing an preference changes and testing whether | 53 // A helper for observing an preference changes and testing whether |
57 // |PrefProvider| holds a lock when the preferences change. | 54 // |PrefProvider| holds a lock when the preferences change. |
58 class DeadlockCheckerObserver { | 55 class DeadlockCheckerObserver { |
59 public: | 56 public: |
(...skipping 398 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
458 pattern, pattern, CONTENT_SETTINGS_TYPE_GEOLOCATION); | 455 pattern, pattern, CONTENT_SETTINGS_TYPE_GEOLOCATION); |
459 base::Time second = pref_content_settings_provider.GetLastUsage( | 456 base::Time second = pref_content_settings_provider.GetLastUsage( |
460 pattern, pattern, CONTENT_SETTINGS_TYPE_GEOLOCATION); | 457 pattern, pattern, CONTENT_SETTINGS_TYPE_GEOLOCATION); |
461 | 458 |
462 base::TimeDelta delta = second - first; | 459 base::TimeDelta delta = second - first; |
463 EXPECT_EQ(delta.InSeconds(), 10); | 460 EXPECT_EQ(delta.InSeconds(), 10); |
464 | 461 |
465 pref_content_settings_provider.ShutdownOnUIThread(); | 462 pref_content_settings_provider.ShutdownOnUIThread(); |
466 } | 463 } |
467 | 464 |
| 465 |
| 466 // TODO(msramek): This tests the correct migration behavior between the old |
| 467 // aggregate dictionary preferences for all content settings types and the new |
| 468 // dictionary preferences for individual types. Remove this when the migration |
| 469 // period is over. |
| 470 TEST(PrefProviderTest, SyncingOldToNew) { |
| 471 TestingPrefServiceSyncable prefs; |
| 472 PrefProvider::RegisterProfilePrefs(prefs.registry()); |
| 473 PrefProvider provider(&prefs, false); |
| 474 |
| 475 const std::string pattern = "google.com,*"; |
| 476 const std::string resource_id = "abcde12345"; |
| 477 base::DictionaryValue* exceptions = new base::DictionaryValue(); |
| 478 base::DictionaryValue* plugin_resources = new base::DictionaryValue(); |
| 479 |
| 480 // Add exceptions for images and app banner. |
| 481 exceptions->SetIntegerWithoutPathExpansion( |
| 482 GetTypeName(CONTENT_SETTINGS_TYPE_IMAGES), CONTENT_SETTING_ALLOW); |
| 483 exceptions->SetIntegerWithoutPathExpansion( |
| 484 GetTypeName(CONTENT_SETTINGS_TYPE_APP_BANNER), CONTENT_SETTING_ALLOW); |
| 485 |
| 486 // Add a regular exception for plugins, then one with a resource identifier. |
| 487 exceptions->SetIntegerWithoutPathExpansion( |
| 488 GetTypeName(CONTENT_SETTINGS_TYPE_PLUGINS), CONTENT_SETTING_ALLOW); |
| 489 plugin_resources->SetIntegerWithoutPathExpansion( |
| 490 resource_id, CONTENT_SETTING_BLOCK); |
| 491 exceptions->SetWithoutPathExpansion( |
| 492 "per_plugin", plugin_resources); |
| 493 |
| 494 // Change the old dictionary preference and observe changes |
| 495 // in the new preferences. |
| 496 { |
| 497 DictionaryPrefUpdate update(&prefs, prefs::kContentSettingsPatternPairs); |
| 498 base::DictionaryValue* old_dictionary = update.Get(); |
| 499 old_dictionary->SetWithoutPathExpansion(pattern, exceptions); |
| 500 } |
| 501 |
| 502 // The images exception was synced. |
| 503 { |
| 504 DictionaryPrefUpdate update( |
| 505 &prefs, prefs::kContentSettingsImagesPatternPairs); |
| 506 const base::DictionaryValue* images_dictionary = update.Get(); |
| 507 |
| 508 EXPECT_EQ(1u, images_dictionary->size()); |
| 509 const base::DictionaryValue* images_exception; |
| 510 EXPECT_TRUE(images_dictionary->GetDictionaryWithoutPathExpansion( |
| 511 pattern, &images_exception)); |
| 512 |
| 513 // And it has a correct value. |
| 514 int images_exception_value = CONTENT_SETTING_DEFAULT; |
| 515 EXPECT_TRUE(images_exception->GetIntegerWithoutPathExpansion( |
| 516 "setting", &images_exception_value)); |
| 517 EXPECT_EQ(CONTENT_SETTING_ALLOW, images_exception_value); |
| 518 } |
| 519 |
| 520 // The app banner exception was not synced. |
| 521 { |
| 522 DictionaryPrefUpdate update( |
| 523 &prefs, prefs::kContentSettingsAppBannerPatternPairs); |
| 524 const base::DictionaryValue* app_banner_dictionary = update.Get(); |
| 525 EXPECT_TRUE(app_banner_dictionary->empty()); |
| 526 } |
| 527 |
| 528 // The plugins exception was synced, together with the resource identifiers. |
| 529 { |
| 530 DictionaryPrefUpdate update( |
| 531 &prefs, prefs::kContentSettingsPluginsPatternPairs); |
| 532 const base::DictionaryValue* plugins_dictionary = update.Get(); |
| 533 EXPECT_EQ(1u, plugins_dictionary->size()); |
| 534 |
| 535 const base::DictionaryValue* plugins_exception; |
| 536 EXPECT_TRUE(plugins_dictionary->GetDictionaryWithoutPathExpansion( |
| 537 pattern, &plugins_exception)); |
| 538 |
| 539 int plugins_exception_value = CONTENT_SETTING_DEFAULT; |
| 540 EXPECT_TRUE(plugins_exception->GetIntegerWithoutPathExpansion( |
| 541 "setting", &plugins_exception_value)); |
| 542 EXPECT_EQ(CONTENT_SETTING_ALLOW, plugins_exception_value); |
| 543 |
| 544 int resource_exception_value = CONTENT_SETTING_DEFAULT; |
| 545 const base::DictionaryValue* resource_exception; |
| 546 EXPECT_TRUE(plugins_exception->GetDictionaryWithoutPathExpansion( |
| 547 "per_resource", &resource_exception)); |
| 548 EXPECT_TRUE(resource_exception->GetIntegerWithoutPathExpansion( |
| 549 resource_id, &resource_exception_value)); |
| 550 EXPECT_EQ(CONTENT_SETTING_BLOCK, resource_exception_value); |
| 551 } |
| 552 |
| 553 provider.ShutdownOnUIThread(); |
| 554 } |
| 555 |
| 556 TEST(PrefProviderTest, SyncingNewToOld) { |
| 557 TestingPrefServiceSyncable prefs; |
| 558 PrefProvider::RegisterProfilePrefs(prefs.registry()); |
| 559 PrefProvider provider(&prefs, false); |
| 560 |
| 561 const std::string pattern = "google.com,*"; |
| 562 const std::string resource_id = "abcde12345"; |
| 563 base::DictionaryValue block_exception; |
| 564 block_exception.SetIntegerWithoutPathExpansion( |
| 565 "setting", CONTENT_SETTING_BLOCK); |
| 566 |
| 567 // Add a mouselock exception. |
| 568 { |
| 569 DictionaryPrefUpdate update( |
| 570 &prefs, prefs::kContentSettingsMouseLockPatternPairs); |
| 571 base::DictionaryValue* mouselock_dictionary = update.Get(); |
| 572 |
| 573 mouselock_dictionary->SetWithoutPathExpansion( |
| 574 pattern, block_exception.DeepCopy()); |
| 575 } |
| 576 |
| 577 // Add a microphone exception. |
| 578 { |
| 579 DictionaryPrefUpdate update( |
| 580 &prefs, prefs::kContentSettingsMediaStreamMicPatternPairs); |
| 581 base::DictionaryValue* microphone_dictionary = update.Get(); |
| 582 |
| 583 microphone_dictionary->SetWithoutPathExpansion( |
| 584 pattern, block_exception.DeepCopy()); |
| 585 } |
| 586 |
| 587 // Add a plugin exception with resource identifiers. |
| 588 { |
| 589 DictionaryPrefUpdate update( |
| 590 &prefs, prefs::kContentSettingsPluginsPatternPairs); |
| 591 base::DictionaryValue* plugins_dictionary = update.Get(); |
| 592 |
| 593 base::DictionaryValue* plugin_exception = block_exception.DeepCopy(); |
| 594 plugins_dictionary->SetWithoutPathExpansion( |
| 595 pattern, plugin_exception); |
| 596 |
| 597 base::DictionaryValue* resource_exception = new base::DictionaryValue(); |
| 598 resource_exception->SetIntegerWithoutPathExpansion( |
| 599 resource_id, CONTENT_SETTING_ALLOW); |
| 600 |
| 601 plugin_exception->SetWithoutPathExpansion( |
| 602 "per_resource", resource_exception); |
| 603 } |
| 604 |
| 605 // Only the notifications and plugin exceptions should appear in the |
| 606 // old dictionary. We should also have a resource identifiers section |
| 607 // for plugins. |
| 608 { |
| 609 DictionaryPrefUpdate update( |
| 610 &prefs, prefs::kContentSettingsPatternPairs); |
| 611 const base::DictionaryValue* old_dictionary = update.Get(); |
| 612 |
| 613 const base::DictionaryValue* exception; |
| 614 EXPECT_TRUE(old_dictionary->GetDictionaryWithoutPathExpansion( |
| 615 pattern, &exception)); |
| 616 EXPECT_EQ(3u, exception->size()); |
| 617 EXPECT_FALSE(exception->HasKey( |
| 618 GetTypeName(CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC))); |
| 619 |
| 620 int mouselock_exception_value = CONTENT_SETTING_DEFAULT; |
| 621 exception->GetIntegerWithoutPathExpansion( |
| 622 GetTypeName(CONTENT_SETTINGS_TYPE_MOUSELOCK), |
| 623 &mouselock_exception_value); |
| 624 DCHECK_EQ(CONTENT_SETTING_BLOCK, mouselock_exception_value); |
| 625 |
| 626 int plugins_exception_value = CONTENT_SETTING_DEFAULT; |
| 627 exception->GetIntegerWithoutPathExpansion( |
| 628 GetTypeName(CONTENT_SETTINGS_TYPE_PLUGINS), |
| 629 &plugins_exception_value); |
| 630 DCHECK_EQ(CONTENT_SETTING_BLOCK, plugins_exception_value); |
| 631 |
| 632 int resource_exception_value = CONTENT_SETTING_DEFAULT; |
| 633 const base::DictionaryValue* resource_values; |
| 634 exception->GetDictionaryWithoutPathExpansion( |
| 635 "per_plugin", &resource_values); |
| 636 resource_values->GetIntegerWithoutPathExpansion( |
| 637 resource_id, &resource_exception_value); |
| 638 DCHECK_EQ(CONTENT_SETTING_ALLOW, resource_exception_value); |
| 639 } |
| 640 |
| 641 provider.ShutdownOnUIThread(); |
| 642 } |
| 643 |
| 644 #if defined(OS_CHROMEOS) || defined(OS_ANDROID) |
| 645 TEST(PrefProviderTest, PMIMigrateOnlyAllow) { |
| 646 TestingPrefServiceSyncable prefs; |
| 647 PrefProvider::RegisterProfilePrefs(prefs.registry()); |
| 648 |
| 649 const std::string pattern_1 = "google.com,*"; |
| 650 const std::string pattern_2 = "www.google.com,*"; |
| 651 base::DictionaryValue* exception_1 = new base::DictionaryValue(); |
| 652 base::DictionaryValue* exception_2 = new base::DictionaryValue(); |
| 653 |
| 654 // Add both an "allow" and "block" exception for PMI. |
| 655 exception_1->SetIntegerWithoutPathExpansion( |
| 656 GetTypeName(CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER), |
| 657 CONTENT_SETTING_ALLOW); |
| 658 exception_2->SetIntegerWithoutPathExpansion( |
| 659 GetTypeName(CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER), |
| 660 CONTENT_SETTING_BLOCK); |
| 661 |
| 662 // Change the old dictionary preference. |
| 663 { |
| 664 DictionaryPrefUpdate update(&prefs, prefs::kContentSettingsPatternPairs); |
| 665 base::DictionaryValue* old_dictionary = update.Get(); |
| 666 old_dictionary->SetWithoutPathExpansion(pattern_1, exception_1); |
| 667 old_dictionary->SetWithoutPathExpansion(pattern_2, exception_2); |
| 668 } |
| 669 |
| 670 // Create the PrefProvider. It should migrate the settings. |
| 671 PrefProvider provider(&prefs, false); |
| 672 |
| 673 // The "block" exception for PMI was migrated, but "allow" was not. |
| 674 { |
| 675 DictionaryPrefUpdate update( |
| 676 &prefs, prefs::kContentSettingsProtectedMediaIdentifierPatternPairs); |
| 677 const base::DictionaryValue* pmi_dictionary = update.Get(); |
| 678 EXPECT_FALSE(pmi_dictionary->HasKey(pattern_1)); |
| 679 EXPECT_TRUE(pmi_dictionary->HasKey(pattern_2)); |
| 680 } |
| 681 |
| 682 provider.ShutdownOnUIThread(); |
| 683 } |
| 684 #endif |
| 685 |
| 686 TEST(PrefProviderTest, PrefsMigrateVerbatim) { |
| 687 TestingPrefServiceSyncable prefs; |
| 688 PrefProvider::RegisterProfilePrefs(prefs.registry()); |
| 689 |
| 690 const std::string pattern_1 = "google.com,*"; |
| 691 const std::string pattern_2 = "www.google.com,*"; |
| 692 base::DictionaryValue* exception_1 = new base::DictionaryValue(); |
| 693 base::DictionaryValue* exception_2 = new base::DictionaryValue(); |
| 694 scoped_ptr<base::DictionaryValue> old_dictionary; |
| 695 |
| 696 // Add two exceptions. |
| 697 exception_1->SetIntegerWithoutPathExpansion( |
| 698 GetTypeName(CONTENT_SETTINGS_TYPE_COOKIES), |
| 699 CONTENT_SETTING_ALLOW); |
| 700 exception_2->SetIntegerWithoutPathExpansion( |
| 701 GetTypeName(CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS), |
| 702 CONTENT_SETTING_BLOCK); |
| 703 |
| 704 // Change the old dictionary preference. |
| 705 { |
| 706 DictionaryPrefUpdate update(&prefs, prefs::kContentSettingsPatternPairs); |
| 707 base::DictionaryValue* dictionary = update.Get(); |
| 708 dictionary->SetWithoutPathExpansion(pattern_1, exception_1); |
| 709 dictionary->SetWithoutPathExpansion(pattern_2, exception_2); |
| 710 old_dictionary.reset(dictionary->DeepCopy()); |
| 711 } |
| 712 |
| 713 // Create the PrefProvider. It should copy the settings from the old |
| 714 // preference to the new ones. |
| 715 PrefProvider provider(&prefs, false); |
| 716 |
| 717 // Force copying back from the new preferences to the old one. |
| 718 { |
| 719 DictionaryPrefUpdate update( |
| 720 &prefs, prefs::kContentSettingsCookiesPatternPairs); |
| 721 } |
| 722 { |
| 723 DictionaryPrefUpdate update( |
| 724 &prefs, prefs::kContentSettingsAutomaticDownloadsPatternPairs); |
| 725 } |
| 726 |
| 727 // Test if the value after copying there and back is the same. |
| 728 { |
| 729 DictionaryPrefUpdate update(&prefs, prefs::kContentSettingsPatternPairs); |
| 730 base::DictionaryValue* new_dictionary = update.Get(); |
| 731 EXPECT_TRUE(old_dictionary->Equals(new_dictionary)); |
| 732 } |
| 733 |
| 734 provider.ShutdownOnUIThread(); |
| 735 } |
| 736 |
468 } // namespace content_settings | 737 } // namespace content_settings |
OLD | NEW |