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..d19c41c815cbb9dde6038dd5157d8428a31a0efb |
| --- /dev/null |
| +++ b/chrome/browser/content_settings/content_settings_override_provider.cc |
| @@ -0,0 +1,163 @@ |
| +// 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_rule.h" |
| +#include "chrome/browser/content_settings/content_settings_utils.h" |
| +#include "chrome/common/content_settings.h" |
| +#include "chrome/common/pref_names.h" |
| +#include "components/content_settings/core/common/content_settings_pattern.h" |
| +#include "components/pref_registry/pref_registry_syncable.h" |
| +#include "content/public/browser/browser_thread.h" |
| + |
| + |
| +using content::BrowserThread; |
| + |
| +namespace content_settings { |
| + |
| +namespace { |
| + |
| +class OverrideRuleIterator : public RuleIterator { |
| + public: |
| + explicit OverrideRuleIterator(const base::Value* value) { |
| + if (value) |
| + value_.reset(value->DeepCopy()); |
| + } |
| + |
| + virtual bool HasNext() const OVERRIDE { return value_.get() != NULL; } |
| + |
| + virtual Rule Next() OVERRIDE { |
| + DCHECK(value_.get()); |
| + return Rule(ContentSettingsPattern::Wildcard(), |
| + ContentSettingsPattern::Wildcard(), |
| + value_.release()); |
| + } |
| + |
| + private: |
| + scoped_ptr<base::Value> value_; |
|
Bernhard Bauer
2014/09/11 16:49:56
If we remove the ALLOW setting (instead falling th
Daniel Nishi
2014/09/11 20:58:12
Done.
|
| +}; |
| + |
| +} // namespace |
| + |
| +// 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() { |
| +} |
| + |
| +RuleIterator* OverrideProvider::GetRuleIterator( |
| + ContentSettingsType content_type, |
| + const ResourceIdentifier& resource_identifier, |
| + bool incognito) const { |
| + base::AutoLock lock(lock_); |
| + if (resource_identifier.empty()) { |
| + ValueMap::const_iterator it(override_settings_.find(content_type)); |
| + if (it != override_settings_.end()) |
| + return new OverrideRuleIterator(it->second.get()); |
| + } |
| + return new EmptyRuleIterator(); |
| +} |
| + |
| +void OverrideProvider::ClearAllContentSettingsRules( |
| + ContentSettingsType content_type) { |
| +} |
| + |
| +bool OverrideProvider::SetWebsiteSetting( |
| + const ContentSettingsPattern& primary_pattern, |
| + const ContentSettingsPattern& secondary_pattern, |
| + ContentSettingsType content_type, |
| + const ResourceIdentifier& resource_identifier, |
| + base::Value* in_value) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| + DCHECK(prefs_); |
| + |
| + // Disallow incognito to change the state. |
| + DCHECK(!is_incognito_); |
| + |
| + // Ignore non-global settings |
| + if (primary_pattern != ContentSettingsPattern::Wildcard() || |
| + secondary_pattern != ContentSettingsPattern::Wildcard()) { |
| + return false; |
| + } |
| + |
| + scoped_ptr<base::Value> value(in_value); |
| + base::AutoLock lock(lock_); |
| + DictionaryPrefUpdate update(prefs_, prefs::kOverrideContentSettings); |
| + base::DictionaryValue* default_settings_dictionary = update.Get(); |
| + if (value.get() == NULL) { |
| + override_settings_[content_type].reset( |
| + new base::FundamentalValue(CONTENT_SETTING_ALLOW)); |
| + default_settings_dictionary->SetWithoutPathExpansion( |
| + GetTypeName(content_type), |
| + new base::FundamentalValue(CONTENT_SETTING_ALLOW)); |
| + } else { |
| + ContentSetting setting = ValueToContentSetting(value.get()); |
| + if (setting != CONTENT_SETTING_ALLOW && setting != CONTENT_SETTING_BLOCK) |
|
Bernhard Bauer
2014/09/11 16:49:56
This should be a DCHECK.
Daniel Nishi
2014/09/11 20:58:12
Function now just returns false.
|
| + NOTREACHED(); |
| + |
| + override_settings_[content_type].reset(value->DeepCopy()); |
| + default_settings_dictionary->SetWithoutPathExpansion( |
| + GetTypeName(content_type), value.release()); |
| + } |
| + |
| + return true; |
| +} |
| + |
| +void OverrideProvider::ShutdownOnUIThread() { |
| + DCHECK(prefs_); |
| + prefs_ = NULL; |
| +} |
| + |
| +bool OverrideProvider::IsEnabled(ContentSettingsType content_type) const { |
| + base::AutoLock lock(lock_); |
| + ValueMap::const_iterator it = override_settings_.find(content_type); |
| + if (it == override_settings_.end()) |
| + return true; |
| + |
| + ContentSetting ret = ValueToContentSetting(it->second.get()); |
| + return ret == CONTENT_SETTING_ALLOW; |
| +} |
| + |
| +void OverrideProvider::ReadOverrideSettings() { |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| + const base::DictionaryValue* override_settings_dictionary = |
| + prefs_->GetDictionary(prefs::kOverrideContentSettings); |
| + |
| + 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))) { |
| + ContentSetting setting = ValueToContentSetting(&(i.value())); |
| + override_settings_[ContentSettingsType(type)].reset( |
| + new base::FundamentalValue(setting)); |
| + break; |
| + } |
| + } |
| + } |
| + } |
| +} |
| +} // namespace content_settings |