Chromium Code Reviews| Index: chrome/browser/content_settings/content_settings_override_provider.cc |
| diff --git a/chrome/browser/content_settings/content_settings_override_provider.cc b/chrome/browser/content_settings/content_settings_override_provider.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..8e34c8f3a9dbf31e8fde51564da2b86baedf8da9 |
| --- /dev/null |
| +++ b/chrome/browser/content_settings/content_settings_override_provider.cc |
| @@ -0,0 +1,91 @@ |
| +// Copyright 2014 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 "chrome/browser/content_settings/content_settings_override_provider.h" |
| + |
| +#include <string> |
| + |
| +#include "base/auto_reset.h" |
| +#include "base/prefs/pref_service.h" |
| +#include "base/prefs/scoped_user_pref_update.h" |
| +#include "base/values.h" |
| +#include "chrome/browser/content_settings/content_settings_utils.h" |
| +#include "chrome/common/content_settings.h" |
| +#include "chrome/common/pref_names.h" |
| +#include "components/pref_registry/pref_registry_syncable.h" |
| +#include "content/public/browser/browser_thread.h" |
| + |
| +using content::BrowserThread; |
| + |
| +namespace content_settings { |
| + |
| +// static |
| +void OverrideProvider::RegisterProfilePrefs( |
| + user_prefs::PrefRegistrySyncable* registry) { |
| + registry->RegisterDictionaryPref( |
| + prefs::kOverrideContentSettings, |
| + user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
| +} |
| + |
| +OverrideProvider::OverrideProvider(PrefService* prefs, bool incognito) |
| + : prefs_(prefs), is_incognito_(incognito) { |
| + DCHECK(prefs_); |
| + |
| + // Read global overrides. |
| + ReadOverrideSettings(); |
| +} |
| + |
| +OverrideProvider::~OverrideProvider() { |
| +} |
| + |
| +void OverrideProvider::SetContentSetting(ContentSettingsType content_type, |
| + bool is_enabled) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
|
Bernhard Bauer
2014/09/10 17:44:53
There is a DCHECK_CURRENTLY_ON macro that will giv
Daniel Nishi
2014/09/10 23:30:20
Done.
|
| + DCHECK(prefs_); |
| + |
| + if (is_incognito_) |
| + return; |
| + |
| + DictionaryPrefUpdate update(prefs_, prefs::kOverrideContentSettings); |
| + base::DictionaryValue* default_settings_dictionary = update.Get(); |
| + base::AutoLock lock(lock_); |
| + override_settings_[content_type] = is_enabled; |
| + default_settings_dictionary->SetWithoutPathExpansion( |
| + GetTypeName(content_type), new base::FundamentalValue(is_enabled)); |
| +} |
| + |
| +bool OverrideProvider::IsEnabled(ContentSettingsType content_type) const { |
| + base::AutoLock lock(lock_); |
| + std::map<ContentSettingsType, bool>::const_iterator it = |
| + override_settings_.find(content_type); |
| + if (it == override_settings_.end()) |
| + return true; |
| + |
| + return it->second; |
| +} |
| + |
| +void OverrideProvider::ReadOverrideSettings() { |
| + const base::DictionaryValue* override_settings_dictionary = |
|
Bernhard Bauer
2014/09/10 17:44:54
I would DCHECK that you're on the UI thread here.
Daniel Nishi
2014/09/10 23:30:21
Done.
|
| + prefs_->GetDictionary(prefs::kOverrideContentSettings); |
| + |
| + override_settings_.clear(); |
| + |
| + if (override_settings_dictionary) { |
| + for (base::DictionaryValue::Iterator i(*override_settings_dictionary); |
| + !i.IsAtEnd(); |
| + i.Advance()) { |
| + const std::string& content_type(i.key()); |
| + for (size_t type = 0; type < CONTENT_SETTINGS_NUM_TYPES; ++type) { |
| + if (content_type == GetTypeName(ContentSettingsType(type))) { |
| + bool bool_value = true; |
| + bool rv = i.value().GetAsBoolean(&bool_value); |
| + DCHECK(rv); |
| + override_settings_[ContentSettingsType(type)] = bool_value; |
| + break; |
| + } |
| + } |
| + } |
| + } |
| +} |
| +} // namespace content_settings |