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 base::DictionaryValue* exceptions = new base::DictionaryValue(); |
| 477 |
| 478 // Add exceptions for images and app banner. |
| 479 exceptions->SetIntegerWithoutPathExpansion( |
| 480 GetTypeName(CONTENT_SETTINGS_TYPE_IMAGES), CONTENT_SETTING_ALLOW); |
| 481 exceptions->SetIntegerWithoutPathExpansion( |
| 482 GetTypeName(CONTENT_SETTINGS_TYPE_APP_BANNER), CONTENT_SETTING_ALLOW); |
| 483 |
| 484 // Change the old dictionary preference and observe changes |
| 485 // in the new preferences. |
| 486 { |
| 487 DictionaryPrefUpdate update(&prefs, prefs::kContentSettingsPatternPairs); |
| 488 base::DictionaryValue* old_dictionary = update.Get(); |
| 489 old_dictionary->SetWithoutPathExpansion(pattern, exceptions); |
| 490 } |
| 491 |
| 492 // The images exception was synced. |
| 493 { |
| 494 DictionaryPrefUpdate update( |
| 495 &prefs, prefs::kContentSettingsImagesPatternPairs); |
| 496 const base::DictionaryValue* images_dictionary = update.Get(); |
| 497 |
| 498 EXPECT_EQ(1u, images_dictionary->size()); |
| 499 const base::DictionaryValue* images_exception; |
| 500 EXPECT_TRUE(images_dictionary->GetDictionaryWithoutPathExpansion( |
| 501 pattern, &images_exception)); |
| 502 |
| 503 // And it has a correct value. |
| 504 int images_exception_value = CONTENT_SETTING_DEFAULT; |
| 505 EXPECT_TRUE(images_exception->GetIntegerWithoutPathExpansion( |
| 506 "setting", &images_exception_value)); |
| 507 EXPECT_EQ(CONTENT_SETTING_ALLOW, images_exception_value); |
| 508 } |
| 509 |
| 510 // The app banner exception was not synced. |
| 511 { |
| 512 DictionaryPrefUpdate update( |
| 513 &prefs, prefs::kContentSettingsAppBannerPatternPairs); |
| 514 const base::DictionaryValue* app_banner_dictionary = update.Get(); |
| 515 EXPECT_TRUE(app_banner_dictionary->empty()); |
| 516 } |
| 517 |
| 518 provider.ShutdownOnUIThread(); |
| 519 } |
| 520 |
| 521 TEST(PrefProviderTest, SyncingNewToOld) { |
| 522 TestingPrefServiceSyncable prefs; |
| 523 PrefProvider::RegisterProfilePrefs(prefs.registry()); |
| 524 PrefProvider provider(&prefs, false); |
| 525 |
| 526 const std::string pattern = "google.com,*"; |
| 527 base::DictionaryValue block_exception; |
| 528 block_exception.SetIntegerWithoutPathExpansion( |
| 529 "setting", CONTENT_SETTING_BLOCK); |
| 530 |
| 531 // Add a mouselock exception. |
| 532 { |
| 533 DictionaryPrefUpdate update( |
| 534 &prefs, prefs::kContentSettingsMouseLockPatternPairs); |
| 535 base::DictionaryValue* mouselock_dictionary = update.Get(); |
| 536 |
| 537 mouselock_dictionary->SetWithoutPathExpansion( |
| 538 pattern, block_exception.DeepCopy()); |
| 539 } |
| 540 |
| 541 // Add a microphone exception. |
| 542 { |
| 543 DictionaryPrefUpdate update( |
| 544 &prefs, prefs::kContentSettingsMediaStreamMicPatternPairs); |
| 545 base::DictionaryValue* microphone_dictionary = update.Get(); |
| 546 |
| 547 microphone_dictionary->SetWithoutPathExpansion( |
| 548 pattern, block_exception.DeepCopy()); |
| 549 } |
| 550 |
| 551 // Only the notifications exception should appear in the old dictionary. |
| 552 { |
| 553 DictionaryPrefUpdate update( |
| 554 &prefs, prefs::kContentSettingsPatternPairs); |
| 555 const base::DictionaryValue* old_dictionary = update.Get(); |
| 556 |
| 557 const base::DictionaryValue* exception; |
| 558 EXPECT_TRUE(old_dictionary->GetDictionaryWithoutPathExpansion( |
| 559 pattern, &exception)); |
| 560 EXPECT_EQ(1u, exception->size()); |
| 561 EXPECT_FALSE(exception->HasKey( |
| 562 GetTypeName(CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC))); |
| 563 |
| 564 int mouselock_exception_value = CONTENT_SETTING_DEFAULT; |
| 565 exception->GetIntegerWithoutPathExpansion( |
| 566 GetTypeName(CONTENT_SETTINGS_TYPE_MOUSELOCK), |
| 567 &mouselock_exception_value); |
| 568 DCHECK_EQ(CONTENT_SETTING_BLOCK, mouselock_exception_value); |
| 569 } |
| 570 |
| 571 provider.ShutdownOnUIThread(); |
| 572 } |
| 573 |
| 574 #if defined(OS_CHROMEOS) || defined(OS_ANDROID) |
| 575 TEST(PrefProviderTest, PMIMigrateOnlyAllow) { |
| 576 TestingPrefServiceSyncable prefs; |
| 577 PrefProvider::RegisterProfilePrefs(prefs.registry()); |
| 578 |
| 579 const std::string pattern_1 = "google.com,*"; |
| 580 const std::string pattern_2 = "www.google.com,*"; |
| 581 base::DictionaryValue* exception_1 = new base::DictionaryValue(); |
| 582 base::DictionaryValue* exception_2 = new base::DictionaryValue(); |
| 583 |
| 584 // Add both an "allow" and "block" exception for PMI. |
| 585 exception_1->SetIntegerWithoutPathExpansion( |
| 586 GetTypeName(CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER), |
| 587 CONTENT_SETTING_ALLOW); |
| 588 exception_2->SetIntegerWithoutPathExpansion( |
| 589 GetTypeName(CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER), |
| 590 CONTENT_SETTING_BLOCK); |
| 591 |
| 592 // Change the old dictionary preference. |
| 593 { |
| 594 DictionaryPrefUpdate update(&prefs, prefs::kContentSettingsPatternPairs); |
| 595 base::DictionaryValue* old_dictionary = update.Get(); |
| 596 old_dictionary->SetWithoutPathExpansion(pattern_1, exception_1); |
| 597 old_dictionary->SetWithoutPathExpansion(pattern_2, exception_2); |
| 598 } |
| 599 |
| 600 // Create the PrefProvider. It should migrate the settings. |
| 601 PrefProvider provider(&prefs, false); |
| 602 |
| 603 // The "block" exception for PMI was migrated, but "allow" was not. |
| 604 { |
| 605 DictionaryPrefUpdate update( |
| 606 &prefs, prefs::kContentSettingsProtectedMediaIdentifierPatternPairs); |
| 607 const base::DictionaryValue* pmi_dictionary = update.Get(); |
| 608 EXPECT_FALSE(pmi_dictionary->HasKey(pattern_1)); |
| 609 EXPECT_TRUE(pmi_dictionary->HasKey(pattern_2)); |
| 610 } |
| 611 |
| 612 provider.ShutdownOnUIThread(); |
| 613 } |
| 614 #endif |
| 615 |
468 } // namespace content_settings | 616 } // namespace content_settings |
OLD | NEW |