summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/extension_content_settings_api.cc
diff options
context:
space:
mode:
authorbauerb@chromium.org <bauerb@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-06-09 14:17:38 +0000
committerbauerb@chromium.org <bauerb@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-06-09 14:17:38 +0000
commitfa062426cde3d551d07a82a9e6a9ee2dfc15e6a4 (patch)
tree4baf4ceac1ddfffa7234c86e20776173c43fc6a7 /chrome/browser/extensions/extension_content_settings_api.cc
parent9f39293c0507c538c944de86ca6ce002c6feb2b2 (diff)
downloadchromium_src-fa062426cde3d551d07a82a9e6a9ee2dfc15e6a4.zip
chromium_src-fa062426cde3d551d07a82a9e6a9ee2dfc15e6a4.tar.gz
chromium_src-fa062426cde3d551d07a82a9e6a9ee2dfc15e6a4.tar.bz2
Content settings extension API
BUG=71067 TEST= Review URL: http://codereview.chromium.org/7029031 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@88526 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/extensions/extension_content_settings_api.cc')
-rw-r--r--chrome/browser/extensions/extension_content_settings_api.cc243
1 files changed, 243 insertions, 0 deletions
diff --git a/chrome/browser/extensions/extension_content_settings_api.cc b/chrome/browser/extensions/extension_content_settings_api.cc
new file mode 100644
index 0000000..0013b20
--- /dev/null
+++ b/chrome/browser/extensions/extension_content_settings_api.cc
@@ -0,0 +1,243 @@
+// 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_api.h"
+
+#include "base/values.h"
+#include "chrome/browser/content_settings/host_content_settings_map.h"
+#include "chrome/browser/extensions/extension_content_settings_api_constants.h"
+#include "chrome/browser/extensions/extension_content_settings_helpers.h"
+#include "chrome/browser/extensions/extension_content_settings_store.h"
+#include "chrome/browser/extensions/extension_preference_api_constants.h"
+#include "chrome/browser/extensions/extension_preference_helpers.h"
+#include "chrome/browser/extensions/extension_service.h"
+#include "chrome/browser/profiles/profile.h"
+#include "chrome/common/extensions/extension_error_utils.h"
+
+namespace helpers = extension_content_settings_helpers;
+namespace keys = extension_content_settings_api_constants;
+namespace pref_helpers = extension_preference_helpers;
+namespace pref_keys = extension_preference_api_constants;
+
+bool ClearContentSettingsFunction::RunImpl() {
+ std::string content_type_str;
+ EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &content_type_str));
+ ContentSettingsType content_type =
+ helpers::StringToContentSettingsType(content_type_str);
+ EXTENSION_FUNCTION_VALIDATE(content_type != CONTENT_SETTINGS_TYPE_DEFAULT);
+
+ DictionaryValue* details = NULL;
+ EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details));
+
+ ExtensionPrefsScope scope = kExtensionPrefsScopeRegular;
+ if (details->HasKey(pref_keys::kScopeKey)) {
+ std::string scope_str;
+ EXTENSION_FUNCTION_VALIDATE(details->GetString(pref_keys::kScopeKey,
+ &scope_str));
+
+ EXTENSION_FUNCTION_VALIDATE(pref_helpers::StringToScope(scope_str, &scope));
+ EXTENSION_FUNCTION_VALIDATE(
+ scope != kExtensionPrefsScopeIncognitoPersistent);
+ }
+
+ bool incognito = (scope == kExtensionPrefsScopeIncognitoPersistent ||
+ scope == kExtensionPrefsScopeIncognitoSessionOnly);
+ if (incognito) {
+ // We don't check incognito permissions here, as an extension should be
+ // always allowed to clear its own settings.
+ } else {
+ // Incognito profiles can't access regular mode ever, they only exist in
+ // split mode.
+ if (profile()->IsOffTheRecord()) {
+ error_ = keys::kIncognitoContextError;
+ return false;
+ }
+ }
+
+ ExtensionContentSettingsStore* store =
+ profile_->GetExtensionService()->GetExtensionContentSettingsStore();
+ store->ClearContentSettingsForExtension(extension_id(), scope);
+
+ return true;
+}
+
+bool GetContentSettingFunction::RunImpl() {
+ std::string content_type_str;
+ EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &content_type_str));
+ ContentSettingsType content_type =
+ helpers::StringToContentSettingsType(content_type_str);
+ EXTENSION_FUNCTION_VALIDATE(content_type != CONTENT_SETTINGS_TYPE_DEFAULT);
+
+ DictionaryValue* details = NULL;
+ EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details));
+
+ std::string embedded_url_spec;
+ EXTENSION_FUNCTION_VALIDATE(
+ details->GetString(keys::kEmbeddedUrlKey, &embedded_url_spec));
+ GURL embedded_url(embedded_url_spec);
+ if (!embedded_url.is_valid()) {
+ error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kInvalidUrlError,
+ embedded_url_spec);
+ return false;
+ }
+
+ std::string top_level_url_spec;
+ EXTENSION_FUNCTION_VALIDATE(
+ details->GetString(keys::kTopLevelUrlKey, &top_level_url_spec));
+ GURL top_level_url(top_level_url_spec);
+ if (!top_level_url.is_valid()) {
+ error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kInvalidUrlError,
+ top_level_url_spec);
+ return false;
+ }
+
+ std::string resource_identifier;
+ if (details->HasKey(keys::kResourceIdentifierKey)) {
+ DictionaryValue* resource_identifier_dict = NULL;
+ EXTENSION_FUNCTION_VALIDATE(
+ details->GetDictionary(keys::kResourceIdentifierKey,
+ &resource_identifier_dict));
+ EXTENSION_FUNCTION_VALIDATE(
+ resource_identifier_dict->GetString(keys::kIdKey,
+ &resource_identifier));
+ }
+
+ bool incognito = false;
+ if (details->HasKey(pref_keys::kIncognitoKey)) {
+ EXTENSION_FUNCTION_VALIDATE(
+ details->GetBoolean(pref_keys::kIncognitoKey, &incognito));
+ }
+ if (incognito && !include_incognito()) {
+ error_ = pref_keys::kIncognitoErrorMessage;
+ return false;
+ }
+
+ HostContentSettingsMap* map;
+ if (incognito) {
+ if (!profile()->HasOffTheRecordProfile()) {
+ // TODO(bauerb): Allow reading incognito content settings
+ // outside of an incognito session.
+ error_ = keys::kIncognitoSessionOnlyError;
+ return false;
+ }
+ map = profile()->GetOffTheRecordProfile()->GetHostContentSettingsMap();
+ } else {
+ map = profile()->GetHostContentSettingsMap();
+ }
+
+ ContentSetting setting;
+ if (content_type == CONTENT_SETTINGS_TYPE_COOKIES) {
+ // TODO(jochen): Do we return the value for setting or for reading cookies?
+ bool setting_cookie = false;
+ setting = map->GetCookieContentSetting(embedded_url, top_level_url,
+ setting_cookie);
+ } else {
+ setting = map->GetContentSetting(top_level_url, content_type,
+ resource_identifier);
+ }
+
+ DictionaryValue* result = new DictionaryValue();
+ result->SetString(keys::kContentSettingKey,
+ helpers::ContentSettingToString(setting));
+
+ result_.reset(result);
+
+ return true;
+}
+
+bool SetContentSettingFunction::RunImpl() {
+ std::string content_type_str;
+ EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &content_type_str));
+ ContentSettingsType content_type =
+ helpers::StringToContentSettingsType(content_type_str);
+ EXTENSION_FUNCTION_VALIDATE(content_type != CONTENT_SETTINGS_TYPE_DEFAULT);
+
+ DictionaryValue* details = NULL;
+ EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details));
+
+ DictionaryValue* top_level_pattern_dict = NULL;
+ EXTENSION_FUNCTION_VALIDATE(
+ details->GetDictionary(keys::kTopLevelPatternKey,
+ &top_level_pattern_dict));
+ std::string top_level_pattern_str;
+ EXTENSION_FUNCTION_VALIDATE(
+ top_level_pattern_dict->GetString(keys::kPatternKey,
+ &top_level_pattern_str));
+ ContentSettingsPattern top_level_pattern =
+ ContentSettingsPattern::FromString(top_level_pattern_str);
+ EXTENSION_FUNCTION_VALIDATE(top_level_pattern.IsValid());
+
+ DictionaryValue* embedded_pattern_dict = NULL;
+ EXTENSION_FUNCTION_VALIDATE(
+ details->GetDictionary(keys::kEmbeddedPatternKey,
+ &embedded_pattern_dict));
+ std::string embedded_pattern_str;
+ EXTENSION_FUNCTION_VALIDATE(
+ embedded_pattern_dict->GetString(keys::kPatternKey,
+ &embedded_pattern_str));
+ ContentSettingsPattern embedded_pattern =
+ ContentSettingsPattern::FromString(embedded_pattern_str);
+ EXTENSION_FUNCTION_VALIDATE(embedded_pattern.IsValid());
+
+ std::string resource_identifier;
+ if (details->HasKey(keys::kResourceIdentifierKey)) {
+ DictionaryValue* resource_identifier_dict = NULL;
+ EXTENSION_FUNCTION_VALIDATE(
+ details->GetDictionary(keys::kResourceIdentifierKey,
+ &resource_identifier_dict));
+ EXTENSION_FUNCTION_VALIDATE(
+ resource_identifier_dict->GetString(keys::kIdKey,
+ &resource_identifier));
+ }
+
+ std::string setting_str;
+ EXTENSION_FUNCTION_VALIDATE(
+ details->GetString(keys::kContentSettingKey, &setting_str));
+ ContentSetting setting = CONTENT_SETTING_DEFAULT;
+ EXTENSION_FUNCTION_VALIDATE(
+ helpers::StringToContentSetting(setting_str, &setting));
+ EXTENSION_FUNCTION_VALIDATE(
+ HostContentSettingsMap::IsSettingAllowedForType(setting, content_type));
+
+ ExtensionPrefsScope scope = kExtensionPrefsScopeRegular;
+ if (details->HasKey(pref_keys::kScopeKey)) {
+ std::string scope_str;
+ EXTENSION_FUNCTION_VALIDATE(details->GetString(pref_keys::kScopeKey,
+ &scope_str));
+
+ EXTENSION_FUNCTION_VALIDATE(pref_helpers::StringToScope(scope_str, &scope));
+ EXTENSION_FUNCTION_VALIDATE(
+ scope != kExtensionPrefsScopeIncognitoPersistent);
+ }
+
+ bool incognito = (scope == kExtensionPrefsScopeIncognitoPersistent ||
+ scope == kExtensionPrefsScopeIncognitoSessionOnly);
+ if (incognito) {
+ // Regular profiles can't access incognito unless include_incognito is true.
+ if (!profile()->IsOffTheRecord() && !include_incognito()) {
+ error_ = pref_keys::kIncognitoErrorMessage;
+ return false;
+ }
+ } else {
+ // Incognito profiles can't access regular mode ever, they only exist in
+ // split mode.
+ if (profile()->IsOffTheRecord()) {
+ error_ = keys::kIncognitoContextError;
+ return false;
+ }
+ }
+
+ if (scope == kExtensionPrefsScopeIncognitoSessionOnly &&
+ !profile_->HasOffTheRecordProfile()) {
+ error_ = pref_keys::kIncognitoSessionOnlyErrorMessage;
+ return false;
+ }
+
+ ExtensionContentSettingsStore* store =
+ profile_->GetExtensionService()->GetExtensionContentSettingsStore();
+ store->SetExtensionContentSetting(extension_id(), top_level_pattern,
+ embedded_pattern, content_type,
+ resource_identifier, setting, scope);
+ return true;
+}