| Index: chrome/browser/extensions/extension_content_settings_store.cc
|
| diff --git a/chrome/browser/extensions/extension_content_settings_store.cc b/chrome/browser/extensions/extension_content_settings_store.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..117ce2f8de7b4dbd9d2cb0408b2d255460f8dc1e
|
| --- /dev/null
|
| +++ b/chrome/browser/extensions/extension_content_settings_store.cc
|
| @@ -0,0 +1,258 @@
|
| +// Copyright (c) 2011 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/extensions/extension_content_settings_store.h"
|
| +
|
| +#include <set>
|
| +
|
| +#include "base/logging.h"
|
| +
|
| +struct ExtensionContentSettingsStore::ExtensionEntry {
|
| + // Installation time of the extension.
|
| + base::Time install_time;
|
| + // Whether extension is enabled in the profile.
|
| + bool enabled;
|
| + // Content settings.
|
| + ContentSettingSpecList settings;
|
| + // Incognito content settings
|
| + ContentSettingSpecList incognito_settings;
|
| +};
|
| +
|
| +ExtensionContentSettingsStore::ExtensionContentSettingsStore() {
|
| +}
|
| +
|
| +ExtensionContentSettingsStore::~ExtensionContentSettingsStore() {
|
| + NotifyOfDestruction();
|
| +}
|
| +
|
| +void ExtensionContentSettingsStore::SetExtensionDefaultContentSetting(
|
| + ContentSettingsType content_type,
|
| + ContentSetting setting) {
|
| + // TODO(markusheintz): Implement.
|
| +}
|
| +
|
| +ContentSetting
|
| + ExtensionContentSettingsStore::GetEffectiveDefaultContentSetting(
|
| + ContentSettingsType content_type) {
|
| + // TODO(markusheintz): Implement.
|
| + return CONTENT_SETTING_DEFAULT;
|
| +}
|
| +
|
| +void ExtensionContentSettingsStore::SetExtensionContentSetting(
|
| + const std::string& ext_id,
|
| + const ContentSettingsPattern& pattern,
|
| + const ContentSettingsPattern& embedder_pattern,
|
| + const ContentSettingsType& type,
|
| + const content_settings::ResourceIdentifier& identifier,
|
| + ContentSetting setting,
|
| + bool incognito) {
|
| + ContentSettingSpecList* setting_spec_list =
|
| + GetContentSettingSpecList(ext_id, incognito);
|
| +
|
| + // Find |ContentSettingSpec|
|
| + ContentSettingSpecList::iterator setting_spec = setting_spec_list->begin();
|
| + while (setting_spec != setting_spec_list->end()) {
|
| + if (setting_spec->a == pattern &&
|
| + setting_spec->b == embedder_pattern &&
|
| + setting_spec->c == type &&
|
| + setting_spec->d == identifier) {
|
| + break;
|
| + }
|
| + ++setting_spec;
|
| + }
|
| + if (setting_spec == setting_spec_list->end()) {
|
| + setting_spec = setting_spec_list->insert(
|
| + setting_spec_list->end(),
|
| + MakeTuple(pattern, embedder_pattern, type, identifier, setting));
|
| + }
|
| +
|
| + // Update setting
|
| + if (setting != CONTENT_SETTING_DEFAULT) {
|
| + setting_spec->e = setting;
|
| + } else {
|
| + setting_spec_list->erase(setting_spec);
|
| + }
|
| +
|
| + // Send notification that content settings changed
|
| + // TODO(markusheintz): Notifications should only be sent if the set content
|
| + // setting is effective and not hidden by an other setting of an other
|
| + // extensions installed more recently.
|
| + NotifyOfContentSettingChanged(
|
| + pattern,
|
| + embedder_pattern,
|
| + type,
|
| + identifier,
|
| + setting,
|
| + incognito);
|
| +}
|
| +
|
| +void ExtensionContentSettingsStore::RegisterExtension(
|
| + const std::string& ext_id,
|
| + const base::Time& install_time,
|
| + bool is_enabled) {
|
| + if (entries_.find(ext_id) != entries_.end())
|
| + UnregisterExtension(ext_id);
|
| + entries_[ext_id] = new ExtensionEntry;
|
| + entries_[ext_id]->install_time = install_time;
|
| + entries_[ext_id]->enabled = is_enabled;
|
| +}
|
| +void ExtensionContentSettingsStore::UnregisterExtension(
|
| + const std::string& ext_id) {
|
| + ExtensionEntryMap::iterator i = entries_.find(ext_id);
|
| + if (i == entries_.end())
|
| + return;
|
| +
|
| + delete i->second;
|
| + entries_.erase(i);
|
| +}
|
| +
|
| +void ExtensionContentSettingsStore::SetExtensionState(
|
| + const std::string& ext_id, bool is_enabled) {
|
| + ExtensionEntryMap::const_iterator i = entries_.find(ext_id);
|
| + CHECK(i != entries_.end());
|
| + if (i->second->enabled == is_enabled)
|
| + return;
|
| + i->second->enabled = is_enabled;
|
| +}
|
| +
|
| +ExtensionContentSettingsStore::ContentSettingSpecList*
|
| + ExtensionContentSettingsStore::GetContentSettingSpecList(
|
| + const std::string& ext_id,
|
| + bool incognito) {
|
| + ExtensionEntryMap::const_iterator i = entries_.find(ext_id);
|
| + CHECK(i != entries_.end());
|
| + return incognito ? &(i->second->incognito_settings)
|
| + : &(i->second->settings);
|
| +}
|
| +
|
| +const ExtensionContentSettingsStore::ContentSettingSpecList*
|
| + ExtensionContentSettingsStore::GetContentSettingSpecList(
|
| + const std::string& ext_id,
|
| + bool incognito) const {
|
| + ExtensionEntryMap::const_iterator i = entries_.find(ext_id);
|
| + CHECK(i != entries_.end());
|
| + return incognito ? &(i->second->incognito_settings)
|
| + : &(i->second->settings);
|
| +}
|
| +
|
| +ContentSetting ExtensionContentSettingsStore::GetContentSettingFrom(
|
| + const GURL& url,
|
| + const GURL& embedder_url,
|
| + const ContentSettingsType& type,
|
| + const content_settings::ResourceIdentifier& identifier,
|
| + const ContentSettingSpecList* setting_spec_list) const {
|
| + ContentSettingSpecList::const_iterator winner_spec =
|
| + setting_spec_list->end();
|
| +
|
| + for (ContentSettingSpecList::const_iterator spec =
|
| + setting_spec_list->begin();
|
| + spec!= setting_spec_list->end();
|
| + ++spec) {
|
| + if (!spec->a.Matches(url) ||
|
| + !spec->b.Matches(embedder_url) ||
|
| + spec->c != type ||
|
| + spec->d != identifier) {
|
| + continue;
|
| + }
|
| +
|
| + if (winner_spec == setting_spec_list->end() ||
|
| + spec->a.Compare(winner_spec->a) == ContentSettingsPattern::SUCCESSOR) {
|
| + winner_spec = spec;
|
| + }
|
| + }
|
| +
|
| + return (winner_spec != setting_spec_list->end()) ? winner_spec->e
|
| + : CONTENT_SETTING_DEFAULT;
|
| +}
|
| +
|
| +ContentSetting ExtensionContentSettingsStore::GetEffectiveContentSetting(
|
| + const GURL& url,
|
| + const GURL& embedder_url,
|
| + const ContentSettingsType& type,
|
| + const content_settings::ResourceIdentifier& identifier,
|
| + bool incognito) const {
|
| + ExtensionEntryMap::const_iterator winner = entries_.end();
|
| + base::Time winners_install_time;
|
| + ContentSetting winner_setting = CONTENT_SETTING_DEFAULT;
|
| +
|
| + ExtensionEntryMap::const_iterator i;
|
| + for (i = entries_.begin(); i != entries_.end(); ++i) {
|
| + const std::string& ext_id = i->first;
|
| + const base::Time& install_time = i->second->install_time;
|
| + const bool enabled = i->second->enabled;
|
| +
|
| + if (!enabled)
|
| + continue;
|
| + if (install_time < winners_install_time)
|
| + continue;
|
| +
|
| + const ContentSettingSpecList* setting_spec_list =
|
| + GetContentSettingSpecList(ext_id, false);
|
| + // Get non incognito setting
|
| + ContentSetting setting = GetContentSettingFrom(
|
| + url,
|
| + embedder_url,
|
| + type,
|
| + identifier,
|
| + setting_spec_list);
|
| +
|
| + if (setting != CONTENT_SETTING_DEFAULT) {
|
| + winner = i;
|
| + winners_install_time = install_time;
|
| + winner_setting = setting;
|
| + }
|
| +
|
| + if (!incognito)
|
| + continue;
|
| + // Get incognito setting
|
| + setting_spec_list =
|
| + GetContentSettingSpecList(ext_id, true);
|
| + setting = GetContentSettingFrom(
|
| + url,
|
| + embedder_url,
|
| + type,
|
| + identifier,
|
| + setting_spec_list);
|
| +
|
| + if (setting != CONTENT_SETTING_DEFAULT) {
|
| + winner = i;
|
| + winners_install_time = install_time;
|
| + winner_setting = setting;
|
| + }
|
| + }
|
| +
|
| + return winner_setting;
|
| +}
|
| +
|
| +void ExtensionContentSettingsStore::AddObserver(Observer* observer) {
|
| + observers_.AddObserver(observer);
|
| +}
|
| +
|
| +void ExtensionContentSettingsStore::RemoveObserver(Observer* observer) {
|
| + observers_.RemoveObserver(observer);
|
| +}
|
| +
|
| +void ExtensionContentSettingsStore::NotifyOfDestruction() {
|
| + FOR_EACH_OBSERVER(ExtensionContentSettingsStore::Observer,
|
| + observers_,
|
| + OnDestruction());
|
| +}
|
| +
|
| +void ExtensionContentSettingsStore::NotifyOfContentSettingChanged(
|
| + const ContentSettingsPattern& pattern,
|
| + const ContentSettingsPattern& embedder_pattern,
|
| + const ContentSettingsType& type,
|
| + const content_settings::ResourceIdentifier& identifier,
|
| + ContentSetting setting,
|
| + bool incognito) {
|
| + FOR_EACH_OBSERVER(ExtensionContentSettingsStore::Observer,
|
| + observers_,
|
| + OnContentSettingChanged(
|
| + pattern,
|
| + embedder_pattern,
|
| + type,
|
| + identifier,
|
| + setting,
|
| + incognito));
|
| +}
|
|
|