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_content_settings_api_constants.h" |
| 10 #include "chrome/browser/extensions/extension_content_settings_helpers.h" |
| 11 #include "chrome/browser/extensions/extension_content_settings_store.h" |
| 12 #include "chrome/browser/extensions/extension_preference_api_constants.h" |
| 13 #include "chrome/browser/extensions/extension_preference_helpers.h" |
| 14 #include "chrome/browser/extensions/extension_service.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 ExtensionPrefsScope scope = kExtensionPrefsScopeRegular; |
| 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 != kExtensionPrefsScopeIncognitoPersistent); |
| 42 } |
| 43 |
| 44 bool incognito = (scope == kExtensionPrefsScopeIncognitoPersistent || |
| 45 scope == kExtensionPrefsScopeIncognitoSessionOnly); |
| 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 } |
| 193 |
| 194 std::string setting_str; |
| 195 EXTENSION_FUNCTION_VALIDATE( |
| 196 details->GetString(keys::kContentSettingKey, &setting_str)); |
| 197 ContentSetting setting = CONTENT_SETTING_DEFAULT; |
| 198 EXTENSION_FUNCTION_VALIDATE( |
| 199 helpers::StringToContentSetting(setting_str, &setting)); |
| 200 EXTENSION_FUNCTION_VALIDATE( |
| 201 HostContentSettingsMap::IsSettingAllowedForType(setting, content_type)); |
| 202 |
| 203 ExtensionPrefsScope scope = kExtensionPrefsScopeRegular; |
| 204 if (details->HasKey(pref_keys::kScopeKey)) { |
| 205 std::string scope_str; |
| 206 EXTENSION_FUNCTION_VALIDATE(details->GetString(pref_keys::kScopeKey, |
| 207 &scope_str)); |
| 208 |
| 209 EXTENSION_FUNCTION_VALIDATE(pref_helpers::StringToScope(scope_str, &scope)); |
| 210 EXTENSION_FUNCTION_VALIDATE( |
| 211 scope != kExtensionPrefsScopeIncognitoPersistent); |
| 212 } |
| 213 |
| 214 bool incognito = (scope == kExtensionPrefsScopeIncognitoPersistent || |
| 215 scope == kExtensionPrefsScopeIncognitoSessionOnly); |
| 216 if (incognito) { |
| 217 // Regular profiles can't access incognito unless include_incognito is true. |
| 218 if (!profile()->IsOffTheRecord() && !include_incognito()) { |
| 219 error_ = pref_keys::kIncognitoErrorMessage; |
| 220 return false; |
| 221 } |
| 222 } else { |
| 223 // Incognito profiles can't access regular mode ever, they only exist in |
| 224 // split mode. |
| 225 if (profile()->IsOffTheRecord()) { |
| 226 error_ = keys::kIncognitoContextError; |
| 227 return false; |
| 228 } |
| 229 } |
| 230 |
| 231 if (scope == kExtensionPrefsScopeIncognitoSessionOnly && |
| 232 !profile_->HasOffTheRecordProfile()) { |
| 233 error_ = pref_keys::kIncognitoSessionOnlyErrorMessage; |
| 234 return false; |
| 235 } |
| 236 |
| 237 ExtensionContentSettingsStore* store = |
| 238 profile_->GetExtensionService()->GetExtensionContentSettingsStore(); |
| 239 store->SetExtensionContentSetting(extension_id(), top_level_pattern, |
| 240 embedded_pattern, content_type, |
| 241 resource_identifier, setting, scope); |
| 242 return true; |
| 243 } |
OLD | NEW |