Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/extensions/extension_content_settings_api.h" | |
| 6 | |
| 7 #include "base/values.h" | |
| 8 #include "chrome/browser/content_settings/host_content_settings_map.h" | |
| 9 #include "chrome/browser/extensions/extension_service.h" | |
| 10 #include "chrome/browser/extensions/extension_content_settings_api_constants.h" | |
| 11 #include "chrome/browser/extensions/extension_content_settings_helpers.h" | |
| 12 #include "chrome/browser/extensions/extension_content_settings_store.h" | |
| 13 #include "chrome/browser/extensions/extension_preference_api_constants.h" | |
| 14 #include "chrome/browser/extensions/extension_preference_helpers.h" | |
| 15 #include "chrome/browser/profiles/profile.h" | |
| 16 #include "chrome/common/extensions/extension_error_utils.h" | |
| 17 | |
| 18 namespace helpers = extension_content_settings_helpers; | |
| 19 namespace keys = extension_content_settings_api_constants; | |
| 20 namespace pref_helpers = extension_preference_helpers; | |
| 21 namespace pref_keys = extension_preference_api_constants; | |
| 22 | |
| 23 bool ClearContentSettingsFunction::RunImpl() { | |
| 24 std::string content_type_str; | |
| 25 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &content_type_str)); | |
| 26 ContentSettingsType content_type = | |
| 27 helpers::StringToContentSettingsType(content_type_str); | |
| 28 EXTENSION_FUNCTION_VALIDATE(content_type != CONTENT_SETTINGS_TYPE_DEFAULT); | |
| 29 | |
| 30 DictionaryValue* details = NULL; | |
| 31 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); | |
| 32 | |
| 33 extension_prefs_scope::Scope scope = extension_prefs_scope::kRegular; | |
| 34 if (details->HasKey(pref_keys::kScopeKey)) { | |
| 35 std::string scope_str; | |
| 36 EXTENSION_FUNCTION_VALIDATE(details->GetString(pref_keys::kScopeKey, | |
| 37 &scope_str)); | |
| 38 | |
| 39 EXTENSION_FUNCTION_VALIDATE(pref_helpers::StringToScope(scope_str, &scope)); | |
| 40 EXTENSION_FUNCTION_VALIDATE( | |
| 41 scope != extension_prefs_scope::kIncognitoPersistent); | |
| 42 } | |
| 43 | |
| 44 bool incognito = (scope == extension_prefs_scope::kIncognitoPersistent || | |
| 45 scope == extension_prefs_scope::kIncognitoSessionOnly); | |
| 46 if (incognito) { | |
| 47 // We don't check incognito permissions here, as an extension should be | |
| 48 // always allowed to clear its own settings. | |
| 49 } else { | |
| 50 // Incognito profiles can't access regular mode ever, they only exist in | |
| 51 // split mode. | |
| 52 if (profile()->IsOffTheRecord()) { | |
| 53 error_ = keys::kIncognitoContextError; | |
| 54 return false; | |
| 55 } | |
| 56 } | |
| 57 | |
| 58 ExtensionContentSettingsStore* store = | |
| 59 profile_->GetExtensionService()->GetExtensionContentSettingsStore(); | |
| 60 store->ClearContentSettingsForExtension(extension_id(), scope); | |
| 61 | |
| 62 return true; | |
| 63 } | |
| 64 | |
| 65 bool GetContentSettingFunction::RunImpl() { | |
| 66 std::string content_type_str; | |
| 67 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &content_type_str)); | |
| 68 ContentSettingsType content_type = | |
| 69 helpers::StringToContentSettingsType(content_type_str); | |
| 70 EXTENSION_FUNCTION_VALIDATE(content_type != CONTENT_SETTINGS_TYPE_DEFAULT); | |
| 71 | |
| 72 DictionaryValue* details = NULL; | |
| 73 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); | |
| 74 | |
| 75 std::string embedded_url_spec; | |
| 76 EXTENSION_FUNCTION_VALIDATE( | |
| 77 details->GetString(keys::kEmbeddedUrlKey, &embedded_url_spec)); | |
| 78 GURL embedded_url(embedded_url_spec); | |
| 79 if (!embedded_url.is_valid()) { | |
| 80 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kInvalidUrlError, | |
| 81 embedded_url_spec); | |
| 82 return false; | |
| 83 } | |
| 84 | |
| 85 std::string top_level_url_spec; | |
| 86 EXTENSION_FUNCTION_VALIDATE( | |
| 87 details->GetString(keys::kTopLevelUrlKey, &top_level_url_spec)); | |
| 88 GURL top_level_url(top_level_url_spec); | |
| 89 if (!top_level_url.is_valid()) { | |
| 90 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kInvalidUrlError, | |
| 91 top_level_url_spec); | |
| 92 return false; | |
| 93 } | |
| 94 | |
| 95 std::string resource_identifier; | |
| 96 if (details->HasKey(keys::kResourceIdentifierKey)) { | |
| 97 DictionaryValue* resource_identifier_dict = NULL; | |
| 98 EXTENSION_FUNCTION_VALIDATE( | |
| 99 details->GetDictionary(keys::kResourceIdentifierKey, | |
| 100 &resource_identifier_dict)); | |
| 101 EXTENSION_FUNCTION_VALIDATE( | |
| 102 resource_identifier_dict->GetString(keys::kIdKey, | |
| 103 &resource_identifier)); | |
| 104 } | |
| 105 | |
| 106 bool incognito = false; | |
| 107 if (details->HasKey(pref_keys::kIncognitoKey)) { | |
| 108 EXTENSION_FUNCTION_VALIDATE( | |
| 109 details->GetBoolean(pref_keys::kIncognitoKey, &incognito)); | |
| 110 } | |
| 111 if (incognito && !include_incognito()) { | |
| 112 error_ = pref_keys::kIncognitoErrorMessage; | |
| 113 return false; | |
| 114 } | |
| 115 | |
| 116 HostContentSettingsMap* map; | |
| 117 if (incognito) { | |
| 118 if (!profile()->HasOffTheRecordProfile()) { | |
| 119 // TODO(bauerb): Allow reading incognito content settings | |
| 120 // outside of an incognito session. | |
| 121 error_ = keys::kIncognitoSessionOnlyError; | |
| 122 return false; | |
| 123 } | |
| 124 map = profile()->GetOffTheRecordProfile()->GetHostContentSettingsMap(); | |
| 125 } else { | |
| 126 map = profile()->GetHostContentSettingsMap(); | |
| 127 } | |
| 128 | |
| 129 ContentSetting setting; | |
| 130 if (content_type == CONTENT_SETTINGS_TYPE_COOKIES) { | |
| 131 // TODO(jochen): Do we return the value for setting or for reading cookies? | |
| 132 bool setting_cookie = false; | |
| 133 setting = map->GetCookieContentSetting(embedded_url, top_level_url, | |
| 134 setting_cookie); | |
| 135 } else { | |
| 136 setting = map->GetContentSetting(top_level_url, content_type, | |
| 137 resource_identifier); | |
| 138 } | |
| 139 | |
| 140 DictionaryValue* result = new DictionaryValue(); | |
| 141 result->SetString(keys::kContentSettingKey, | |
| 142 helpers::ContentSettingToString(setting)); | |
| 143 | |
| 144 result_.reset(result); | |
| 145 | |
| 146 return true; | |
| 147 } | |
| 148 | |
| 149 bool SetContentSettingFunction::RunImpl() { | |
| 150 std::string content_type_str; | |
| 151 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &content_type_str)); | |
| 152 ContentSettingsType content_type = | |
| 153 helpers::StringToContentSettingsType(content_type_str); | |
| 154 EXTENSION_FUNCTION_VALIDATE(content_type != CONTENT_SETTINGS_TYPE_DEFAULT); | |
| 155 | |
| 156 DictionaryValue* details = NULL; | |
| 157 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); | |
| 158 | |
| 159 DictionaryValue* top_level_pattern_dict = NULL; | |
| 160 EXTENSION_FUNCTION_VALIDATE( | |
| 161 details->GetDictionary(keys::kTopLevelPatternKey, | |
| 162 &top_level_pattern_dict)); | |
| 163 std::string top_level_pattern_str; | |
| 164 EXTENSION_FUNCTION_VALIDATE( | |
| 165 top_level_pattern_dict->GetString(keys::kPatternKey, | |
| 166 &top_level_pattern_str)); | |
| 167 ContentSettingsPattern top_level_pattern = | |
| 168 ContentSettingsPattern::FromString(top_level_pattern_str); | |
| 169 EXTENSION_FUNCTION_VALIDATE(top_level_pattern.IsValid()); | |
| 170 | |
| 171 DictionaryValue* embedded_pattern_dict = NULL; | |
| 172 EXTENSION_FUNCTION_VALIDATE( | |
| 173 details->GetDictionary(keys::kEmbeddedPatternKey, | |
| 174 &embedded_pattern_dict)); | |
| 175 std::string embedded_pattern_str; | |
| 176 EXTENSION_FUNCTION_VALIDATE( | |
| 177 embedded_pattern_dict->GetString(keys::kPatternKey, | |
| 178 &embedded_pattern_str)); | |
| 179 ContentSettingsPattern embedded_pattern = | |
| 180 ContentSettingsPattern::FromString(embedded_pattern_str); | |
| 181 EXTENSION_FUNCTION_VALIDATE(embedded_pattern.IsValid()); | |
| 182 | |
| 183 std::string resource_identifier; | |
| 184 if (details->HasKey(keys::kResourceIdentifierKey)) { | |
| 185 DictionaryValue* resource_identifier_dict = NULL; | |
| 186 EXTENSION_FUNCTION_VALIDATE( | |
| 187 details->GetDictionary(keys::kResourceIdentifierKey, | |
| 188 &resource_identifier_dict)); | |
| 189 EXTENSION_FUNCTION_VALIDATE( | |
| 190 resource_identifier_dict->GetString(keys::kIdKey, | |
| 191 &resource_identifier)); | |
| 192 } | |
|
battre
2011/06/07 18:16:14
nit: newline
Bernhard Bauer
2011/06/08 14:20:16
Done.
| |
| 193 std::string setting_str; | |
| 194 EXTENSION_FUNCTION_VALIDATE( | |
| 195 details->GetString(keys::kContentSettingKey, &setting_str)); | |
| 196 ContentSetting setting = CONTENT_SETTING_DEFAULT; | |
| 197 EXTENSION_FUNCTION_VALIDATE( | |
| 198 helpers::StringToContentSetting(setting_str, &setting)); | |
| 199 EXTENSION_FUNCTION_VALIDATE( | |
| 200 HostContentSettingsMap::IsSettingAllowedForType(setting, content_type)); | |
| 201 | |
| 202 extension_prefs_scope::Scope scope = extension_prefs_scope::kRegular; | |
|
battre
2011/06/07 18:16:14
Please note that the extension_prefs_scope is gone
Bernhard Bauer
2011/06/08 14:20:16
Yup, rebasing.
| |
| 203 if (details->HasKey(pref_keys::kScopeKey)) { | |
| 204 std::string scope_str; | |
| 205 EXTENSION_FUNCTION_VALIDATE(details->GetString(pref_keys::kScopeKey, | |
| 206 &scope_str)); | |
| 207 | |
| 208 EXTENSION_FUNCTION_VALIDATE(pref_helpers::StringToScope(scope_str, &scope)); | |
| 209 EXTENSION_FUNCTION_VALIDATE( | |
| 210 scope != extension_prefs_scope::kIncognitoPersistent); | |
| 211 } | |
| 212 | |
| 213 bool incognito = (scope == extension_prefs_scope::kIncognitoPersistent || | |
| 214 scope == extension_prefs_scope::kIncognitoSessionOnly); | |
| 215 if (incognito) { | |
| 216 // Regular profiles can't access incognito unless include_incognito is true. | |
| 217 if (!profile()->IsOffTheRecord() && !include_incognito()) { | |
| 218 error_ = pref_keys::kIncognitoErrorMessage; | |
| 219 return false; | |
| 220 } | |
| 221 } else { | |
| 222 // Incognito profiles can't access regular mode ever, they only exist in | |
| 223 // split mode. | |
| 224 if (profile()->IsOffTheRecord()) { | |
| 225 error_ = keys::kIncognitoContextError; | |
| 226 return false; | |
| 227 } | |
| 228 } | |
| 229 | |
| 230 if (scope == extension_prefs_scope::kIncognitoSessionOnly && | |
| 231 !profile_->HasOffTheRecordProfile()) { | |
| 232 error_ = pref_keys::kIncognitoSessionOnlyErrorMessage; | |
| 233 return false; | |
| 234 } | |
| 235 | |
| 236 ExtensionContentSettingsStore* store = | |
| 237 profile_->GetExtensionService()->GetExtensionContentSettingsStore(); | |
| 238 store->SetExtensionContentSetting(extension_id(), top_level_pattern, | |
| 239 embedded_pattern, content_type, | |
| 240 resource_identifier, setting, scope); | |
| 241 return true; | |
| 242 } | |
| OLD | NEW |