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 "chrome/browser/content_settings/content_settings_pref_provider.h" | 5 #include "chrome/browser/content_settings/content_settings_pref_provider.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/auto_reset.h" | 11 #include "base/auto_reset.h" |
| 12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/metrics/histogram.h" | 14 #include "base/metrics/histogram.h" |
| 15 #include "base/prefs/pref_service.h" | 15 #include "base/prefs/pref_service.h" |
| 16 #include "base/prefs/scoped_user_pref_update.h" | 16 #include "base/prefs/scoped_user_pref_update.h" |
| 17 #include "base/time/clock.h" | |
| 18 #include "base/time/default_clock.h" | |
| 17 #include "chrome/browser/chrome_notification_types.h" | 19 #include "chrome/browser/chrome_notification_types.h" |
| 18 #include "chrome/browser/content_settings/content_settings_rule.h" | 20 #include "chrome/browser/content_settings/content_settings_rule.h" |
| 19 #include "chrome/browser/content_settings/content_settings_utils.h" | 21 #include "chrome/browser/content_settings/content_settings_utils.h" |
| 20 #include "chrome/browser/content_settings/host_content_settings_map.h" | 22 #include "chrome/browser/content_settings/host_content_settings_map.h" |
| 21 #include "chrome/common/chrome_switches.h" | 23 #include "chrome/common/chrome_switches.h" |
| 22 #include "chrome/common/content_settings.h" | 24 #include "chrome/common/content_settings.h" |
| 23 #include "chrome/common/content_settings_pattern.h" | 25 #include "chrome/common/content_settings_pattern.h" |
| 24 #include "chrome/common/pref_names.h" | 26 #include "chrome/common/pref_names.h" |
| 25 #include "components/pref_registry/pref_registry_syncable.h" | 27 #include "components/pref_registry/pref_registry_syncable.h" |
| 26 #include "content/public/browser/browser_thread.h" | 28 #include "content/public/browser/browser_thread.h" |
| 27 #include "content/public/browser/notification_details.h" | 29 #include "content/public/browser/notification_details.h" |
| 28 #include "content/public/browser/notification_source.h" | 30 #include "content/public/browser/notification_source.h" |
| 29 #include "content/public/browser/user_metrics.h" | 31 #include "content/public/browser/user_metrics.h" |
| 30 #include "url/gurl.h" | 32 #include "url/gurl.h" |
| 31 | 33 |
| 32 using base::UserMetricsAction; | 34 using base::UserMetricsAction; |
| 33 using content::BrowserThread; | 35 using content::BrowserThread; |
| 34 | 36 |
| 35 namespace { | 37 namespace { |
| 36 | 38 |
| 37 typedef std::pair<std::string, std::string> StringPair; | 39 typedef std::pair<std::string, std::string> StringPair; |
| 38 typedef std::map<std::string, std::string> StringMap; | 40 typedef std::map<std::string, std::string> StringMap; |
| 39 | 41 |
| 40 const char kPerPluginPrefName[] = "per_plugin"; | 42 const char kPerPluginPrefName[] = "per_plugin"; |
| 41 const char kAudioKey[] = "audio"; | 43 const char kAudioKey[] = "audio"; |
| 42 const char kVideoKey[] = "video"; | 44 const char kVideoKey[] = "video"; |
| 45 const char kLastUsed[] = "last_used"; | |
| 43 | 46 |
| 44 ContentSetting FixObsoleteCookiePromptMode(ContentSettingsType content_type, | 47 ContentSetting FixObsoleteCookiePromptMode(ContentSettingsType content_type, |
| 45 ContentSetting setting) { | 48 ContentSetting setting) { |
| 46 if (content_type == CONTENT_SETTINGS_TYPE_COOKIES && | 49 if (content_type == CONTENT_SETTINGS_TYPE_COOKIES && |
| 47 setting == CONTENT_SETTING_ASK) { | 50 setting == CONTENT_SETTING_ASK) { |
| 48 return CONTENT_SETTING_BLOCK; | 51 return CONTENT_SETTING_BLOCK; |
| 49 } | 52 } |
| 50 return setting; | 53 return setting; |
| 51 } | 54 } |
| 52 | 55 |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 76 user_prefs::PrefRegistrySyncable* registry) { | 79 user_prefs::PrefRegistrySyncable* registry) { |
| 77 registry->RegisterIntegerPref( | 80 registry->RegisterIntegerPref( |
| 78 prefs::kContentSettingsVersion, | 81 prefs::kContentSettingsVersion, |
| 79 ContentSettingsPattern::kContentSettingsPatternVersion, | 82 ContentSettingsPattern::kContentSettingsPatternVersion, |
| 80 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 83 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 81 registry->RegisterDictionaryPref( | 84 registry->RegisterDictionaryPref( |
| 82 prefs::kContentSettingsPatternPairs, | 85 prefs::kContentSettingsPatternPairs, |
| 83 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | 86 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
| 84 } | 87 } |
| 85 | 88 |
| 86 PrefProvider::PrefProvider(PrefService* prefs, | 89 PrefProvider::PrefProvider(PrefService* prefs, bool incognito) |
| 87 bool incognito) | 90 : prefs_(prefs), |
| 88 : prefs_(prefs), | 91 clock_(scoped_ptr<base::Clock>(new base::DefaultClock())), |
| 89 is_incognito_(incognito), | 92 is_incognito_(incognito), |
| 90 updating_preferences_(false) { | 93 updating_preferences_(false) { |
| 91 DCHECK(prefs_); | 94 DCHECK(prefs_); |
| 92 // Verify preferences version. | 95 // Verify preferences version. |
| 93 if (!prefs_->HasPrefPath(prefs::kContentSettingsVersion)) { | 96 if (!prefs_->HasPrefPath(prefs::kContentSettingsVersion)) { |
| 94 prefs_->SetInteger(prefs::kContentSettingsVersion, | 97 prefs_->SetInteger(prefs::kContentSettingsVersion, |
| 95 ContentSettingsPattern::kContentSettingsPatternVersion); | 98 ContentSettingsPattern::kContentSettingsPatternVersion); |
| 96 } | 99 } |
| 97 if (prefs_->GetInteger(prefs::kContentSettingsVersion) > | 100 if (prefs_->GetInteger(prefs::kContentSettingsVersion) > |
| 98 ContentSettingsPattern::kContentSettingsPatternVersion) { | 101 ContentSettingsPattern::kContentSettingsPatternVersion) { |
| 99 return; | 102 return; |
| 100 } | 103 } |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 283 } else { | 286 } else { |
| 284 resource_dictionary->SetWithoutPathExpansion( | 287 resource_dictionary->SetWithoutPathExpansion( |
| 285 resource_identifier, value->DeepCopy()); | 288 resource_identifier, value->DeepCopy()); |
| 286 } | 289 } |
| 287 } else { | 290 } else { |
| 288 // Update settings dictionary. | 291 // Update settings dictionary. |
| 289 std::string setting_path = GetTypeName(content_type); | 292 std::string setting_path = GetTypeName(content_type); |
| 290 if (value == NULL) { | 293 if (value == NULL) { |
| 291 settings_dictionary->RemoveWithoutPathExpansion(setting_path, | 294 settings_dictionary->RemoveWithoutPathExpansion(setting_path, |
| 292 NULL); | 295 NULL); |
| 296 settings_dictionary->RemoveWithoutPathExpansion(kLastUsed, NULL); | |
| 293 } else { | 297 } else { |
| 294 settings_dictionary->SetWithoutPathExpansion( | 298 settings_dictionary->SetWithoutPathExpansion( |
| 295 setting_path, value->DeepCopy()); | 299 setting_path, value->DeepCopy()); |
| 296 } | 300 } |
| 297 } | 301 } |
| 298 // Remove the settings dictionary if it is empty. | 302 // Remove the settings dictionary if it is empty. |
| 299 if (settings_dictionary->empty()) { | 303 if (settings_dictionary->empty()) { |
| 300 pattern_pairs_settings->RemoveWithoutPathExpansion( | 304 pattern_pairs_settings->RemoveWithoutPathExpansion( |
| 301 pattern_str, NULL); | 305 pattern_str, NULL); |
| 302 } | 306 } |
| (...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 559 } | 563 } |
| 560 | 564 |
| 561 void PrefProvider::ShutdownOnUIThread() { | 565 void PrefProvider::ShutdownOnUIThread() { |
| 562 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 566 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 563 DCHECK(prefs_); | 567 DCHECK(prefs_); |
| 564 RemoveAllObservers(); | 568 RemoveAllObservers(); |
| 565 pref_change_registrar_.RemoveAll(); | 569 pref_change_registrar_.RemoveAll(); |
| 566 prefs_ = NULL; | 570 prefs_ = NULL; |
| 567 } | 571 } |
| 568 | 572 |
| 573 void PrefProvider::UpdateLastUsage( | |
| 574 const ContentSettingsPattern& primary_pattern, | |
| 575 const ContentSettingsPattern& secondary_pattern, | |
| 576 ContentSettingsType content_type) { | |
| 577 // Don't write if in incognito. | |
| 578 if (is_incognito_) { | |
| 579 return; | |
| 580 } | |
| 581 | |
| 582 // Ensure that |lock_| is not held by this thread, since this function will | |
| 583 // send out notifications (by |~DictionaryPrefUpdate|). | |
| 584 AssertLockNotHeld(); | |
| 585 | |
| 586 base::AutoReset<bool> auto_reset(&updating_preferences_, true); | |
| 587 { | |
| 588 DictionaryPrefUpdate update(prefs_, prefs::kContentSettingsPatternPairs); | |
| 589 base::DictionaryValue* pattern_pairs_settings = update.Get(); | |
| 590 | |
| 591 std::string pattern_str( | |
| 592 CreatePatternString(primary_pattern, secondary_pattern)); | |
| 593 base::DictionaryValue* settings_dictionary = NULL; | |
| 594 bool found = pattern_pairs_settings->GetDictionaryWithoutPathExpansion( | |
| 595 pattern_str, &settings_dictionary); | |
| 596 | |
| 597 if (!found) { | |
| 598 settings_dictionary = new base::DictionaryValue; | |
| 599 pattern_pairs_settings->SetWithoutPathExpansion(pattern_str, | |
| 600 settings_dictionary); | |
| 601 } | |
| 602 | |
| 603 if (settings_dictionary) { | |
|
Michael van Ouwerkerk
2014/07/01 11:02:23
Why would this be false? A new one is instantiated
Daniel Nishi
2014/07/01 18:37:48
Good point. The defensive check is unneeded.
Remo
| |
| 604 base::DictionaryValue* last_used_dictionary = NULL; | |
| 605 found = settings_dictionary->GetDictionaryWithoutPathExpansion( | |
| 606 kLastUsed, &last_used_dictionary); | |
| 607 | |
| 608 if (!found) { | |
| 609 last_used_dictionary = new base::DictionaryValue; | |
| 610 settings_dictionary->SetWithoutPathExpansion(kLastUsed, | |
| 611 last_used_dictionary); | |
| 612 } | |
| 613 | |
| 614 std::string settings_path = GetTypeName(content_type); | |
| 615 last_used_dictionary->Set( | |
| 616 settings_path, new base::FundamentalValue(clock_->Now().ToDoubleT())); | |
| 617 } | |
| 618 } | |
| 619 } | |
| 620 | |
| 621 base::Time PrefProvider::GetLastUsage( | |
| 622 const ContentSettingsPattern& primary_pattern, | |
| 623 const ContentSettingsPattern& secondary_pattern, | |
| 624 ContentSettingsType content_type) { | |
| 625 const base::DictionaryValue* pattern_pairs_settings = | |
| 626 prefs_->GetDictionary(prefs::kContentSettingsPatternPairs); | |
| 627 std::string pattern_str( | |
| 628 CreatePatternString(primary_pattern, secondary_pattern)); | |
| 629 | |
| 630 const base::DictionaryValue* settings_dictionary = NULL; | |
| 631 bool found = pattern_pairs_settings->GetDictionaryWithoutPathExpansion( | |
| 632 pattern_str, &settings_dictionary); | |
| 633 | |
| 634 if (!found) | |
| 635 return base::Time(); | |
| 636 | |
| 637 const base::DictionaryValue* last_used_dictionary = NULL; | |
| 638 found = settings_dictionary->GetDictionaryWithoutPathExpansion( | |
| 639 kLastUsed, &last_used_dictionary); | |
| 640 | |
| 641 if (!found) | |
| 642 return base::Time(); | |
| 643 | |
| 644 double last_used_time; | |
| 645 found = last_used_dictionary->GetDoubleWithoutPathExpansion( | |
| 646 GetTypeName(content_type), &last_used_time); | |
| 647 | |
| 648 if (!found) | |
| 649 return base::Time(); | |
| 650 | |
| 651 return base::Time::FromDoubleT(last_used_time); | |
| 652 } | |
| 653 | |
| 569 void PrefProvider::AssertLockNotHeld() const { | 654 void PrefProvider::AssertLockNotHeld() const { |
| 570 #if !defined(NDEBUG) | 655 #if !defined(NDEBUG) |
| 571 // |Lock::Acquire()| will assert if the lock is held by this thread. | 656 // |Lock::Acquire()| will assert if the lock is held by this thread. |
| 572 lock_.Acquire(); | 657 lock_.Acquire(); |
| 573 lock_.Release(); | 658 lock_.Release(); |
| 574 #endif | 659 #endif |
| 575 } | 660 } |
| 576 | 661 |
| 662 void PrefProvider::SetClockForTesting(base::Clock* clock) { | |
| 663 clock_.reset(clock); | |
| 664 } | |
| 665 | |
| 577 } // namespace content_settings | 666 } // namespace content_settings |
| OLD | NEW |