Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(403)

Unified Diff: components/content_settings/core/browser/content_settings_pref.cc

Issue 976243002: Split content settings pref related logic into a separate class (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/content_settings/core/browser/content_settings_pref.cc
diff --git a/components/content_settings/core/browser/content_settings_pref_provider.cc b/components/content_settings/core/browser/content_settings_pref.cc
similarity index 77%
copy from components/content_settings/core/browser/content_settings_pref_provider.cc
copy to components/content_settings/core/browser/content_settings_pref.cc
index c5060511400ba19afa90e83984baf660e7ff136d..a643697cbdee3410ebd42d58d4010f9bda8ecc9f 100644
--- a/components/content_settings/core/browser/content_settings_pref_provider.cc
+++ b/components/content_settings/core/browser/content_settings_pref.cc
@@ -1,40 +1,29 @@
-// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "components/content_settings/core/browser/content_settings_pref_provider.h"
-
-#include <map>
-#include <string>
-#include <utility>
+#include "components/content_settings/core/browser/content_settings_pref.h"
#include "base/auto_reset.h"
#include "base/bind.h"
-#include "base/command_line.h"
#include "base/memory/scoped_ptr.h"
#include "base/metrics/histogram.h"
-#include "base/prefs/pref_service.h"
#include "base/prefs/scoped_user_pref_update.h"
#include "base/strings/string_split.h"
#include "base/time/clock.h"
-#include "base/time/default_clock.h"
#include "components/content_settings/core/browser/content_settings_rule.h"
#include "components/content_settings/core/browser/content_settings_utils.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/content_settings/core/common/content_settings.h"
#include "components/content_settings/core/common/content_settings_pattern.h"
#include "components/content_settings/core/common/pref_names.h"
-#include "components/pref_registry/pref_registry_syncable.h"
#include "url/gurl.h"
namespace {
typedef std::pair<std::string, std::string> StringPair;
-typedef std::map<std::string, std::string> StringMap;
const char kPerPluginPrefName[] = "per_plugin";
-const char kAudioKey[] = "audio";
-const char kVideoKey[] = "video";
const char kLastUsed[] = "last_used";
ContentSetting FixObsoleteCookiePromptMode(ContentSettingsType content_type,
@@ -63,66 +52,52 @@ bool GetResourceTypeName(ContentSettingsType content_type,
namespace content_settings {
-// ////////////////////////////////////////////////////////////////////////////
-// PrefProvider:
-//
-
-// static
-void PrefProvider::RegisterProfilePrefs(
- user_prefs::PrefRegistrySyncable* registry) {
- registry->RegisterIntegerPref(
- prefs::kContentSettingsVersion,
- ContentSettingsPattern::kContentSettingsPatternVersion,
- user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
- registry->RegisterDictionaryPref(
- prefs::kContentSettingsPatternPairs,
- user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
-}
-
-PrefProvider::PrefProvider(PrefService* prefs, bool incognito)
+ContentSettingsPref::ContentSettingsPref(
+ PrefService* prefs,
+ PrefChangeRegistrar* registrar,
+ base::Clock* clock,
+ bool incognito,
+ NotifyObserversCallback notify_callback)
: prefs_(prefs),
- clock_(new base::DefaultClock()),
+ clock_(clock),
+ registrar_(registrar),
is_incognito_(incognito),
- updating_preferences_(false) {
+ updating_preferences_(false),
+ notify_callback_(notify_callback) {
DCHECK(prefs_);
- // Verify preferences version.
- if (!prefs_->HasPrefPath(prefs::kContentSettingsVersion)) {
- prefs_->SetInteger(prefs::kContentSettingsVersion,
- ContentSettingsPattern::kContentSettingsPatternVersion);
- }
- if (prefs_->GetInteger(prefs::kContentSettingsVersion) >
- ContentSettingsPattern::kContentSettingsPatternVersion) {
- return;
- }
// Read content settings exceptions.
ReadContentSettingsFromPref();
- if (!is_incognito_) {
- UMA_HISTOGRAM_COUNTS("ContentSettings.NumberOfExceptions",
- value_map_.size());
- }
-
- // Migrate the obsolete media content setting exceptions to the new settings.
- // This needs to be done after ReadContentSettingsFromPref().
- if (!is_incognito_)
- MigrateObsoleteMediaContentSetting();
-
- pref_change_registrar_.Init(prefs_);
- pref_change_registrar_.Add(
+ registrar->Add(
prefs::kContentSettingsPatternPairs,
- base::Bind(&PrefProvider::OnContentSettingsPatternPairsChanged,
+ base::Bind(&ContentSettingsPref::OnContentSettingsPatternPairsChanged,
base::Unretained(this)));
}
-bool PrefProvider::SetWebsiteSetting(
+ContentSettingsPref::~ContentSettingsPref() {
+}
+
+RuleIterator* ContentSettingsPref::GetRuleIterator(
+ ContentSettingsType content_type,
+ const ResourceIdentifier& resource_identifier,
+ bool incognito) const {
+ if (incognito)
+ return incognito_value_map_.GetRuleIterator(content_type,
+ resource_identifier,
+ &lock_);
+ return value_map_.GetRuleIterator(content_type, resource_identifier, &lock_);
+}
+
+bool ContentSettingsPref::SetWebsiteSetting(
const ContentSettingsPattern& primary_pattern,
const ContentSettingsPattern& secondary_pattern,
ContentSettingsType content_type,
const ResourceIdentifier& resource_identifier,
base::Value* in_value) {
- DCHECK(CalledOnValidThread());
+ DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(prefs_);
+
// Default settings are set using a wildcard pattern for both
// |primary_pattern| and |secondary_pattern|. Don't store default settings in
// the |PrefProvider|. The |PrefProvider| handles settings for specific
@@ -167,15 +142,15 @@ bool PrefProvider::SetWebsiteSetting(
value.get());
}
- NotifyObservers(
+ notify_callback_.Run(
primary_pattern, secondary_pattern, content_type, resource_identifier);
return true;
}
-void PrefProvider::ClearAllContentSettingsRules(
+void ContentSettingsPref::ClearAllContentSettingsRules(
ContentSettingsType content_type) {
- DCHECK(CalledOnValidThread());
+ DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(prefs_);
OriginIdentifierValueMap* map_to_modify = &incognito_value_map_;
@@ -202,157 +177,100 @@ void PrefProvider::ClearAllContentSettingsRules(
std::string(),
NULL);
}
- NotifyObservers(ContentSettingsPattern(),
- ContentSettingsPattern(),
- content_type,
- std::string());
-}
-
-PrefProvider::~PrefProvider() {
- DCHECK(!prefs_);
-}
-
-RuleIterator* PrefProvider::GetRuleIterator(
- ContentSettingsType content_type,
- const ResourceIdentifier& resource_identifier,
- bool incognito) const {
- if (incognito)
- return incognito_value_map_.GetRuleIterator(content_type,
- resource_identifier,
- &lock_);
- return value_map_.GetRuleIterator(content_type, resource_identifier, &lock_);
+ notify_callback_.Run(ContentSettingsPattern(),
+ ContentSettingsPattern(),
+ content_type,
+ std::string());
}
-// ////////////////////////////////////////////////////////////////////////////
-// Private
-
-void PrefProvider::UpdatePref(
+void ContentSettingsPref::UpdateLastUsage(
const ContentSettingsPattern& primary_pattern,
const ContentSettingsPattern& secondary_pattern,
- ContentSettingsType content_type,
- const ResourceIdentifier& resource_identifier,
- const base::Value* value) {
+ ContentSettingsType content_type) {
+ // Don't write if in incognito.
+ if (is_incognito_) {
+ return;
+ }
+
// Ensure that |lock_| is not held by this thread, since this function will
// send out notifications (by |~DictionaryPrefUpdate|).
AssertLockNotHeld();
base::AutoReset<bool> auto_reset(&updating_preferences_, true);
{
- DictionaryPrefUpdate update(prefs_,
- prefs::kContentSettingsPatternPairs);
+ DictionaryPrefUpdate update(prefs_, prefs::kContentSettingsPatternPairs);
base::DictionaryValue* pattern_pairs_settings = update.Get();
- // Get settings dictionary for the given patterns.
- std::string pattern_str(CreatePatternString(primary_pattern,
- secondary_pattern));
+ std::string pattern_str(
+ CreatePatternString(primary_pattern, secondary_pattern));
base::DictionaryValue* settings_dictionary = NULL;
bool found = pattern_pairs_settings->GetDictionaryWithoutPathExpansion(
pattern_str, &settings_dictionary);
- if (!found && value) {
+ if (!found) {
settings_dictionary = new base::DictionaryValue;
- pattern_pairs_settings->SetWithoutPathExpansion(
- pattern_str, settings_dictionary);
+ pattern_pairs_settings->SetWithoutPathExpansion(pattern_str,
+ settings_dictionary);
}
- if (settings_dictionary) {
- std::string res_dictionary_path;
- if (GetResourceTypeName(content_type, &res_dictionary_path) &&
- !resource_identifier.empty()) {
- base::DictionaryValue* resource_dictionary = NULL;
- found = settings_dictionary->GetDictionary(
- res_dictionary_path, &resource_dictionary);
- if (!found) {
- if (value == NULL)
- return; // Nothing to remove. Exit early.
- resource_dictionary = new base::DictionaryValue;
- settings_dictionary->Set(res_dictionary_path, resource_dictionary);
- }
- // Update resource dictionary.
- if (value == NULL) {
- resource_dictionary->RemoveWithoutPathExpansion(resource_identifier,
- NULL);
- if (resource_dictionary->empty()) {
- settings_dictionary->RemoveWithoutPathExpansion(
- res_dictionary_path, NULL);
- }
- } else {
- resource_dictionary->SetWithoutPathExpansion(
- resource_identifier, value->DeepCopy());
- }
- } else {
- // Update settings dictionary.
- std::string setting_path = GetTypeName(content_type);
- if (value == NULL) {
- settings_dictionary->RemoveWithoutPathExpansion(setting_path,
- NULL);
- settings_dictionary->RemoveWithoutPathExpansion(kLastUsed, NULL);
- } else {
- settings_dictionary->SetWithoutPathExpansion(
- setting_path, value->DeepCopy());
- }
- }
- // Remove the settings dictionary if it is empty.
- if (settings_dictionary->empty()) {
- pattern_pairs_settings->RemoveWithoutPathExpansion(
- pattern_str, NULL);
- }
+ base::DictionaryValue* last_used_dictionary = NULL;
+ found = settings_dictionary->GetDictionaryWithoutPathExpansion(
+ kLastUsed, &last_used_dictionary);
+
+ if (!found) {
+ last_used_dictionary = new base::DictionaryValue;
+ settings_dictionary->SetWithoutPathExpansion(kLastUsed,
+ last_used_dictionary);
}
+
+ std::string settings_path = GetTypeName(content_type);
+ last_used_dictionary->Set(
+ settings_path, new base::FundamentalValue(clock_->Now().ToDoubleT()));
}
}
+base::Time ContentSettingsPref::GetLastUsage(
+ const ContentSettingsPattern& primary_pattern,
+ const ContentSettingsPattern& secondary_pattern,
+ ContentSettingsType content_type) {
+ const base::DictionaryValue* pattern_pairs_settings =
+ prefs_->GetDictionary(prefs::kContentSettingsPatternPairs);
+ std::string pattern_str(
+ CreatePatternString(primary_pattern, secondary_pattern));
+
+ const base::DictionaryValue* settings_dictionary = NULL;
+ bool found = pattern_pairs_settings->GetDictionaryWithoutPathExpansion(
+ pattern_str, &settings_dictionary);
-void PrefProvider::MigrateObsoleteMediaContentSetting() {
- std::vector<Rule> rules_to_delete;
- {
- scoped_ptr<RuleIterator> rule_iterator(GetRuleIterator(
- CONTENT_SETTINGS_TYPE_MEDIASTREAM, std::string(), false));
- while (rule_iterator->HasNext()) {
- // Skip default setting and rules without a value.
- const content_settings::Rule& rule = rule_iterator->Next();
- DCHECK(rule.primary_pattern != ContentSettingsPattern::Wildcard());
- if (!rule.value.get())
- continue;
- rules_to_delete.push_back(rule);
- }
- }
+ if (!found)
+ return base::Time();
- for (std::vector<Rule>::const_iterator it = rules_to_delete.begin();
- it != rules_to_delete.end(); ++it) {
- const base::DictionaryValue* value_dict = NULL;
- if (!it->value->GetAsDictionary(&value_dict) || value_dict->empty())
- return;
-
- std::string audio_device, video_device;
- value_dict->GetString(kAudioKey, &audio_device);
- value_dict->GetString(kVideoKey, &video_device);
- // Add the exception to the new microphone content setting.
- if (!audio_device.empty()) {
- SetWebsiteSetting(it->primary_pattern,
- it->secondary_pattern,
- CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC,
- std::string(),
- new base::FundamentalValue(CONTENT_SETTING_ALLOW));
- }
- // Add the exception to the new camera content setting.
- if (!video_device.empty()) {
- SetWebsiteSetting(it->primary_pattern,
- it->secondary_pattern,
- CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA,
- std::string(),
- new base::FundamentalValue(CONTENT_SETTING_ALLOW));
- }
+ const base::DictionaryValue* last_used_dictionary = NULL;
+ found = settings_dictionary->GetDictionaryWithoutPathExpansion(
+ kLastUsed, &last_used_dictionary);
- // Remove the old exception in CONTENT_SETTINGS_TYPE_MEDIASTREAM.
- SetWebsiteSetting(it->primary_pattern,
- it->secondary_pattern,
- CONTENT_SETTINGS_TYPE_MEDIASTREAM,
- std::string(),
- NULL);
- }
+ if (!found)
+ return base::Time();
+
+ double last_used_time;
+ found = last_used_dictionary->GetDoubleWithoutPathExpansion(
+ GetTypeName(content_type), &last_used_time);
+
+ if (!found)
+ return base::Time();
+
+ return base::Time::FromDoubleT(last_used_time);
+}
+
+size_t ContentSettingsPref::GetNumExceptions() {
+ return value_map_.size();
+}
+
+void ContentSettingsPref::SetClockForTesting(base::Clock* clock) {
+ clock_ = clock;
}
-void PrefProvider::ReadContentSettingsFromPref() {
+void ContentSettingsPref::ReadContentSettingsFromPref() {
// |DictionaryPrefUpdate| sends out notifications when destructed. This
// construction order ensures |AutoLock| gets destroyed first and |lock_| is
// not held when the notifications are sent. Also, |auto_reset| must be still
@@ -484,22 +402,97 @@ void PrefProvider::ReadContentSettingsFromPref() {
cookies_session_only_exception_count);
}
-void PrefProvider::OnContentSettingsPatternPairsChanged() {
- DCHECK(CalledOnValidThread());
+void ContentSettingsPref::OnContentSettingsPatternPairsChanged() {
+ DCHECK(thread_checker_.CalledOnValidThread());
if (updating_preferences_)
return;
ReadContentSettingsFromPref();
- NotifyObservers(ContentSettingsPattern(),
- ContentSettingsPattern(),
- CONTENT_SETTINGS_TYPE_DEFAULT,
- std::string());
+ notify_callback_.Run(ContentSettingsPattern(),
+ ContentSettingsPattern(),
+ CONTENT_SETTINGS_TYPE_DEFAULT,
+ std::string());
+}
+
+void ContentSettingsPref::UpdatePref(
+ const ContentSettingsPattern& primary_pattern,
+ const ContentSettingsPattern& secondary_pattern,
+ ContentSettingsType content_type,
+ const ResourceIdentifier& resource_identifier,
+ const base::Value* value) {
+ // Ensure that |lock_| is not held by this thread, since this function will
+ // send out notifications (by |~DictionaryPrefUpdate|).
+ AssertLockNotHeld();
+
+ base::AutoReset<bool> auto_reset(&updating_preferences_, true);
+ {
+ DictionaryPrefUpdate update(prefs_,
+ prefs::kContentSettingsPatternPairs);
+ base::DictionaryValue* pattern_pairs_settings = update.Get();
+
+ // Get settings dictionary for the given patterns.
+ std::string pattern_str(CreatePatternString(primary_pattern,
+ secondary_pattern));
+ base::DictionaryValue* settings_dictionary = NULL;
+ bool found = pattern_pairs_settings->GetDictionaryWithoutPathExpansion(
+ pattern_str, &settings_dictionary);
+
+ if (!found && value) {
+ settings_dictionary = new base::DictionaryValue;
+ pattern_pairs_settings->SetWithoutPathExpansion(
+ pattern_str, settings_dictionary);
+ }
+
+ if (settings_dictionary) {
+ std::string res_dictionary_path;
+ if (GetResourceTypeName(content_type, &res_dictionary_path) &&
+ !resource_identifier.empty()) {
+ base::DictionaryValue* resource_dictionary = NULL;
+ found = settings_dictionary->GetDictionary(
+ res_dictionary_path, &resource_dictionary);
+ if (!found) {
+ if (value == NULL)
+ return; // Nothing to remove. Exit early.
+ resource_dictionary = new base::DictionaryValue;
+ settings_dictionary->Set(res_dictionary_path, resource_dictionary);
+ }
+ // Update resource dictionary.
+ if (value == NULL) {
+ resource_dictionary->RemoveWithoutPathExpansion(resource_identifier,
+ NULL);
+ if (resource_dictionary->empty()) {
+ settings_dictionary->RemoveWithoutPathExpansion(
+ res_dictionary_path, NULL);
+ }
+ } else {
+ resource_dictionary->SetWithoutPathExpansion(
+ resource_identifier, value->DeepCopy());
+ }
+ } else {
+ // Update settings dictionary.
+ std::string setting_path = GetTypeName(content_type);
+ if (value == NULL) {
+ settings_dictionary->RemoveWithoutPathExpansion(setting_path,
+ NULL);
+ settings_dictionary->RemoveWithoutPathExpansion(kLastUsed, NULL);
+ } else {
+ settings_dictionary->SetWithoutPathExpansion(
+ setting_path, value->DeepCopy());
+ }
+ }
+ // Remove the settings dictionary if it is empty.
+ if (settings_dictionary->empty()) {
+ pattern_pairs_settings->RemoveWithoutPathExpansion(
+ pattern_str, NULL);
+ }
+ }
+ }
}
// static
-void PrefProvider::CanonicalizeContentSettingsExceptions(
+void ContentSettingsPref::CanonicalizeContentSettingsExceptions(
base::DictionaryValue* all_settings_dictionary) {
DCHECK(all_settings_dictionary);
@@ -554,94 +547,7 @@ void PrefProvider::CanonicalizeContentSettingsExceptions(
}
}
-void PrefProvider::ShutdownOnUIThread() {
- DCHECK(CalledOnValidThread());
- DCHECK(prefs_);
- RemoveAllObservers();
- pref_change_registrar_.RemoveAll();
- prefs_ = NULL;
-}
-
-void PrefProvider::UpdateLastUsage(
- const ContentSettingsPattern& primary_pattern,
- const ContentSettingsPattern& secondary_pattern,
- ContentSettingsType content_type) {
- // Don't write if in incognito.
- if (is_incognito_) {
- return;
- }
-
- // Ensure that |lock_| is not held by this thread, since this function will
- // send out notifications (by |~DictionaryPrefUpdate|).
- AssertLockNotHeld();
-
- base::AutoReset<bool> auto_reset(&updating_preferences_, true);
- {
- DictionaryPrefUpdate update(prefs_, prefs::kContentSettingsPatternPairs);
- base::DictionaryValue* pattern_pairs_settings = update.Get();
-
- std::string pattern_str(
- CreatePatternString(primary_pattern, secondary_pattern));
- base::DictionaryValue* settings_dictionary = NULL;
- bool found = pattern_pairs_settings->GetDictionaryWithoutPathExpansion(
- pattern_str, &settings_dictionary);
-
- if (!found) {
- settings_dictionary = new base::DictionaryValue;
- pattern_pairs_settings->SetWithoutPathExpansion(pattern_str,
- settings_dictionary);
- }
-
- base::DictionaryValue* last_used_dictionary = NULL;
- found = settings_dictionary->GetDictionaryWithoutPathExpansion(
- kLastUsed, &last_used_dictionary);
-
- if (!found) {
- last_used_dictionary = new base::DictionaryValue;
- settings_dictionary->SetWithoutPathExpansion(kLastUsed,
- last_used_dictionary);
- }
-
- std::string settings_path = GetTypeName(content_type);
- last_used_dictionary->Set(
- settings_path, new base::FundamentalValue(clock_->Now().ToDoubleT()));
- }
-}
-
-base::Time PrefProvider::GetLastUsage(
- const ContentSettingsPattern& primary_pattern,
- const ContentSettingsPattern& secondary_pattern,
- ContentSettingsType content_type) {
- const base::DictionaryValue* pattern_pairs_settings =
- prefs_->GetDictionary(prefs::kContentSettingsPatternPairs);
- std::string pattern_str(
- CreatePatternString(primary_pattern, secondary_pattern));
-
- const base::DictionaryValue* settings_dictionary = NULL;
- bool found = pattern_pairs_settings->GetDictionaryWithoutPathExpansion(
- pattern_str, &settings_dictionary);
-
- if (!found)
- return base::Time();
-
- const base::DictionaryValue* last_used_dictionary = NULL;
- found = settings_dictionary->GetDictionaryWithoutPathExpansion(
- kLastUsed, &last_used_dictionary);
-
- if (!found)
- return base::Time();
-
- double last_used_time;
- found = last_used_dictionary->GetDoubleWithoutPathExpansion(
- GetTypeName(content_type), &last_used_time);
-
- if (!found)
- return base::Time();
-
- return base::Time::FromDoubleT(last_used_time);
-}
-
-void PrefProvider::AssertLockNotHeld() const {
+void ContentSettingsPref::AssertLockNotHeld() const {
#if !defined(NDEBUG)
// |Lock::Acquire()| will assert if the lock is held by this thread.
lock_.Acquire();
@@ -649,8 +555,4 @@ void PrefProvider::AssertLockNotHeld() const {
#endif
}
-void PrefProvider::SetClockForTesting(scoped_ptr<base::Clock> clock) {
- clock_ = clock.Pass();
-}
-
} // namespace content_settings

Powered by Google App Engine
This is Rietveld 408576698