Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(213)

Side by Side Diff: chrome/browser/extensions/api/content_settings/content_settings_api.cc

Issue 10553018: ContentSettings now uses the JSON Schema Compiler. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: patched Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/extensions/api/content_settings/content_settings_api.h" 5 #include "chrome/browser/extensions/api/content_settings/content_settings_api.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
11 #include "base/values.h" 11 #include "base/values.h"
12 #include "chrome/browser/extensions/api/content_settings/content_settings_api_co nstants.h" 12 #include "chrome/browser/extensions/api/content_settings/content_settings_api_co nstants.h"
13 #include "chrome/browser/extensions/api/content_settings/content_settings_helper s.h" 13 #include "chrome/browser/extensions/api/content_settings/content_settings_helper s.h"
14 #include "chrome/browser/extensions/api/content_settings/content_settings_store. h" 14 #include "chrome/browser/extensions/api/content_settings/content_settings_store. h"
15 #include "chrome/browser/content_settings/cookie_settings.h" 15 #include "chrome/browser/content_settings/cookie_settings.h"
16 #include "chrome/browser/content_settings/host_content_settings_map.h" 16 #include "chrome/browser/content_settings/host_content_settings_map.h"
17 #include "chrome/browser/extensions/extension_preference_api_constants.h" 17 #include "chrome/browser/extensions/extension_preference_api_constants.h"
18 #include "chrome/browser/extensions/extension_preference_helpers.h" 18 #include "chrome/browser/extensions/extension_preference_helpers.h"
19 #include "chrome/browser/extensions/extension_service.h" 19 #include "chrome/browser/extensions/extension_service.h"
20 #include "chrome/browser/prefs/pref_service.h" 20 #include "chrome/browser/prefs/pref_service.h"
21 #include "chrome/browser/profiles/profile.h" 21 #include "chrome/browser/profiles/profile.h"
22 #include "chrome/common/chrome_switches.h" 22 #include "chrome/common/chrome_switches.h"
23 #include "chrome/common/extensions/api/content_settings.h"
23 #include "chrome/common/extensions/extension_error_utils.h" 24 #include "chrome/common/extensions/extension_error_utils.h"
24 #include "content/public/browser/plugin_service.h" 25 #include "content/public/browser/plugin_service.h"
25 #include "webkit/plugins/npapi/plugin_group.h" 26 #include "webkit/plugins/npapi/plugin_group.h"
26 27
27 using content::BrowserThread; 28 using content::BrowserThread;
28 using content::PluginService; 29 using content::PluginService;
29 30
31 namespace Clear = extensions::api::content_settings::ContentSetting::Clear;
32 namespace Get = extensions::api::content_settings::ContentSetting::Get;
33 namespace Set = extensions::api::content_settings::ContentSetting::Set;
30 namespace pref_helpers = extension_preference_helpers; 34 namespace pref_helpers = extension_preference_helpers;
31 namespace pref_keys = extension_preference_api_constants; 35 namespace pref_keys = extension_preference_api_constants;
32 36
33 namespace { 37 namespace {
34 38
35 const std::vector<webkit::npapi::PluginGroup>* g_testing_plugin_groups_; 39 const std::vector<webkit::npapi::PluginGroup>* g_testing_plugin_groups_;
36 40
41 bool ExtractContentType(ListValue* args, ContentSettingsType* content_type) {
42 std::string content_type_str;
43 args->GetString(0, &content_type_str);
chebert 2012/06/15 23:28:02 EXTENSION_FUNCTION_VALIDATE relies on a bad_messag
battre 2012/06/18 15:19:07 you could still return false here, right?
44 args->Remove(0, NULL);
chebert 2012/06/15 23:28:02 We need to remove the ContentSettingsType param si
battre 2012/06/18 15:19:07 can you please make this a comment?
45 *content_type =
46 extensions::content_settings_helpers::StringToContentSettingsType(
47 content_type_str);
48 return *content_type != CONTENT_SETTINGS_TYPE_DEFAULT;
49 }
50
37 } // namespace 51 } // namespace
38 52
39 namespace extensions { 53 namespace extensions {
40 54
41 namespace helpers = content_settings_helpers; 55 namespace helpers = content_settings_helpers;
42 namespace keys = content_settings_api_constants; 56 namespace keys = content_settings_api_constants;
43 57
44 bool ClearContentSettingsFunction::RunImpl() { 58 bool ClearContentSettingsFunction::RunImpl() {
45 std::string content_type_str; 59 ContentSettingsType content_type;
46 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &content_type_str)); 60 EXTENSION_FUNCTION_VALIDATE(ExtractContentType(args_.get(), &content_type));
47 ContentSettingsType content_type = 61
48 helpers::StringToContentSettingsType(content_type_str); 62 scoped_ptr<Clear::Params> params(Clear::Params::Create(*args_));
49 EXTENSION_FUNCTION_VALIDATE(content_type != CONTENT_SETTINGS_TYPE_DEFAULT); 63 EXTENSION_FUNCTION_VALIDATE(params.get());
50 64
51 DictionaryValue* details = NULL; 65 DictionaryValue* details = NULL;
52 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); 66 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details));
battre 2012/06/18 15:19:07 This is not used any more.
53 67
54 ExtensionPrefsScope scope = kExtensionPrefsScopeRegular; 68 ExtensionPrefsScope scope = kExtensionPrefsScopeRegular;
55 if (details->HasKey(pref_keys::kScopeKey)) { 69 if (params->details.scope ==
56 std::string scope_str; 70 Clear::Params::Details::SCOPE_INCOGNITO_SESSION_ONLY) {
57 EXTENSION_FUNCTION_VALIDATE(details->GetString(pref_keys::kScopeKey, 71 scope = kExtensionPrefsScopeIncognitoSessionOnly;
58 &scope_str));
59
60 EXTENSION_FUNCTION_VALIDATE(pref_helpers::StringToScope(scope_str, &scope));
61 EXTENSION_FUNCTION_VALIDATE(
62 scope != kExtensionPrefsScopeIncognitoPersistent);
63 } 72 }
64 73
65 bool incognito = (scope == kExtensionPrefsScopeIncognitoPersistent || 74 bool incognito = (scope == kExtensionPrefsScopeIncognitoPersistent ||
66 scope == kExtensionPrefsScopeIncognitoSessionOnly); 75 scope == kExtensionPrefsScopeIncognitoSessionOnly);
67 if (incognito) { 76 if (incognito) {
68 // We don't check incognito permissions here, as an extension should be 77 // We don't check incognito permissions here, as an extension should be
69 // always allowed to clear its own settings. 78 // always allowed to clear its own settings.
70 } else { 79 } else {
71 // Incognito profiles can't access regular mode ever, they only exist in 80 // Incognito profiles can't access regular mode ever, they only exist in
72 // split mode. 81 // split mode.
73 if (profile()->IsOffTheRecord()) { 82 if (profile()->IsOffTheRecord()) {
74 error_ = keys::kIncognitoContextError; 83 error_ = keys::kIncognitoContextError;
75 return false; 84 return false;
76 } 85 }
77 } 86 }
78 87
79 ContentSettingsStore* store = 88 ContentSettingsStore* store =
80 profile_->GetExtensionService()->GetContentSettingsStore(); 89 profile_->GetExtensionService()->GetContentSettingsStore();
81 store->ClearContentSettingsForExtension(extension_id(), scope); 90 store->ClearContentSettingsForExtension(extension_id(), scope);
82 91
83 return true; 92 return true;
84 } 93 }
85 94
86 bool GetContentSettingFunction::RunImpl() { 95 bool GetContentSettingFunction::RunImpl() {
87 std::string content_type_str; 96 ContentSettingsType content_type;
88 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &content_type_str)); 97 EXTENSION_FUNCTION_VALIDATE(ExtractContentType(args_.get(), &content_type));
89 ContentSettingsType content_type =
90 helpers::StringToContentSettingsType(content_type_str);
91 EXTENSION_FUNCTION_VALIDATE(content_type != CONTENT_SETTINGS_TYPE_DEFAULT);
92 98
93 DictionaryValue* details = NULL; 99 scoped_ptr<Get::Params> params(Get::Params::Create(*args_));
94 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); 100 EXTENSION_FUNCTION_VALIDATE(params.get());
95 101
96 std::string primary_url_spec; 102 GURL primary_url(params->details.primary_url);
97 EXTENSION_FUNCTION_VALIDATE(
98 details->GetString(keys::kPrimaryUrlKey, &primary_url_spec));
99 GURL primary_url(primary_url_spec);
100 if (!primary_url.is_valid()) { 103 if (!primary_url.is_valid()) {
101 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kInvalidUrlError, 104 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kInvalidUrlError,
102 primary_url_spec); 105 params->details.primary_url);
103 return false; 106 return false;
104 } 107 }
105 108
106 GURL secondary_url(primary_url); 109 GURL secondary_url(primary_url);
107 std::string secondary_url_spec; 110 if (params->details.secondary_url.get()) {
108 if (details->GetString(keys::kSecondaryUrlKey, &secondary_url_spec)) { 111 secondary_url = GURL(*params->details.secondary_url);
109 secondary_url = GURL(secondary_url_spec);
110 if (!secondary_url.is_valid()) { 112 if (!secondary_url.is_valid()) {
111 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kInvalidUrlError, 113 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kInvalidUrlError,
112 secondary_url_spec); 114 *params->details.secondary_url);
113 return false; 115 return false;
114 } 116 }
115 } 117 }
116 118
117 std::string resource_identifier; 119 std::string resource_identifier;
118 if (details->HasKey(keys::kResourceIdentifierKey)) { 120 if (params->details.resource_identifier.get()) {
119 DictionaryValue* resource_identifier_dict = NULL;
120 EXTENSION_FUNCTION_VALIDATE( 121 EXTENSION_FUNCTION_VALIDATE(
121 details->GetDictionary(keys::kResourceIdentifierKey, 122 params->details.resource_identifier->ToValue()->GetString(keys::kIdKey,
122 &resource_identifier_dict)); 123 &resource_identifier));
123 EXTENSION_FUNCTION_VALIDATE(
124 resource_identifier_dict->GetString(keys::kIdKey,
125 &resource_identifier));
126 } 124 }
127 125
128 bool incognito = false; 126 bool incognito = false;
129 if (details->HasKey(pref_keys::kIncognitoKey)) { 127 if (params->details.incognito.get())
130 EXTENSION_FUNCTION_VALIDATE( 128 incognito = *params->details.incognito;
131 details->GetBoolean(pref_keys::kIncognitoKey, &incognito));
132 }
133 if (incognito && !include_incognito()) { 129 if (incognito && !include_incognito()) {
134 error_ = pref_keys::kIncognitoErrorMessage; 130 error_ = pref_keys::kIncognitoErrorMessage;
135 return false; 131 return false;
136 } 132 }
137 133
138 HostContentSettingsMap* map; 134 HostContentSettingsMap* map;
139 CookieSettings* cookie_settings; 135 CookieSettings* cookie_settings;
140 if (incognito) { 136 if (incognito) {
141 if (!profile()->HasOffTheRecordProfile()) { 137 if (!profile()->HasOffTheRecordProfile()) {
142 // TODO(bauerb): Allow reading incognito content settings 138 // TODO(bauerb): Allow reading incognito content settings
(...skipping 23 matching lines...) Expand all
166 DictionaryValue* result = new DictionaryValue(); 162 DictionaryValue* result = new DictionaryValue();
167 result->SetString(keys::kContentSettingKey, 163 result->SetString(keys::kContentSettingKey,
168 helpers::ContentSettingToString(setting)); 164 helpers::ContentSettingToString(setting));
169 165
170 result_.reset(result); 166 result_.reset(result);
171 167
172 return true; 168 return true;
173 } 169 }
174 170
175 bool SetContentSettingFunction::RunImpl() { 171 bool SetContentSettingFunction::RunImpl() {
176 std::string content_type_str; 172 ContentSettingsType content_type;
177 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &content_type_str)); 173 EXTENSION_FUNCTION_VALIDATE(ExtractContentType(args_.get(), &content_type));
178 ContentSettingsType content_type =
179 helpers::StringToContentSettingsType(content_type_str);
180 EXTENSION_FUNCTION_VALIDATE(content_type != CONTENT_SETTINGS_TYPE_DEFAULT);
181 174
182 DictionaryValue* details = NULL; 175 scoped_ptr<Set::Params> params(Set::Params::Create(*args_));
183 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); 176 EXTENSION_FUNCTION_VALIDATE(params.get());
184 177
185 std::string primary_pattern_str;
186 EXTENSION_FUNCTION_VALIDATE(
187 details->GetString(keys::kPrimaryPatternKey, &primary_pattern_str));
188 std::string primary_error; 178 std::string primary_error;
189 ContentSettingsPattern primary_pattern = 179 ContentSettingsPattern primary_pattern =
190 helpers::ParseExtensionPattern(primary_pattern_str, &primary_error); 180 helpers::ParseExtensionPattern(params->details.primary_pattern,
181 &primary_error);
191 if (!primary_pattern.IsValid()) { 182 if (!primary_pattern.IsValid()) {
192 error_ = primary_error; 183 error_ = primary_error;
193 return false; 184 return false;
194 } 185 }
195 186
196 ContentSettingsPattern secondary_pattern = ContentSettingsPattern::Wildcard(); 187 ContentSettingsPattern secondary_pattern = ContentSettingsPattern::Wildcard();
197 std::string secondary_pattern_str; 188 std::string secondary_pattern_str;
198 if (details->GetString(keys::kSecondaryPatternKey, &secondary_pattern_str)) { 189 if (params->details.secondary_pattern.get()) {
199 std::string secondary_error; 190 std::string secondary_error;
200 secondary_pattern = 191 secondary_pattern =
201 helpers::ParseExtensionPattern(secondary_pattern_str, &secondary_error); 192 helpers::ParseExtensionPattern(*params->details.secondary_pattern,
193 &secondary_error);
202 if (!secondary_pattern.IsValid()) { 194 if (!secondary_pattern.IsValid()) {
203 error_ = secondary_error; 195 error_ = secondary_error;
204 return false; 196 return false;
205 } 197 }
206 } 198 }
207 199
208 std::string resource_identifier; 200 std::string resource_identifier;
209 if (details->HasKey(keys::kResourceIdentifierKey)) { 201 if (params->details.resource_identifier.get()) {
210 DictionaryValue* resource_identifier_dict = NULL; 202 DictionaryValue* resource_identifier_dict =
211 EXTENSION_FUNCTION_VALIDATE( 203 params->details.resource_identifier->ToValue().get();
212 details->GetDictionary(keys::kResourceIdentifierKey,
213 &resource_identifier_dict));
214 EXTENSION_FUNCTION_VALIDATE( 204 EXTENSION_FUNCTION_VALIDATE(
215 resource_identifier_dict->GetString(keys::kIdKey, 205 resource_identifier_dict->GetString(keys::kIdKey,
216 &resource_identifier)); 206 &resource_identifier));
217 } 207 }
218 208
219 std::string setting_str; 209 std::string setting_str;
220 EXTENSION_FUNCTION_VALIDATE( 210 EXTENSION_FUNCTION_VALIDATE(
221 details->GetString(keys::kContentSettingKey, &setting_str)); 211 params->details.setting.value().GetAsString(&setting_str));
222 ContentSetting setting = CONTENT_SETTING_DEFAULT; 212 ContentSetting setting;
223 EXTENSION_FUNCTION_VALIDATE( 213 EXTENSION_FUNCTION_VALIDATE(
224 helpers::StringToContentSetting(setting_str, &setting)); 214 helpers::StringToContentSetting(setting_str, &setting));
225 EXTENSION_FUNCTION_VALIDATE( 215 EXTENSION_FUNCTION_VALIDATE(
226 HostContentSettingsMap::IsSettingAllowedForType(profile()->GetPrefs(), 216 HostContentSettingsMap::IsSettingAllowedForType(profile()->GetPrefs(),
227 setting, 217 setting,
228 content_type)); 218 content_type));
229 219
230 ExtensionPrefsScope scope = kExtensionPrefsScopeRegular; 220 ExtensionPrefsScope scope = kExtensionPrefsScopeRegular;
231 if (details->HasKey(pref_keys::kScopeKey)) { 221 if (params->details.scope ==
232 std::string scope_str; 222 Set::Params::Details::SCOPE_INCOGNITO_SESSION_ONLY) {
233 EXTENSION_FUNCTION_VALIDATE(details->GetString(pref_keys::kScopeKey, 223 scope = kExtensionPrefsScopeIncognitoSessionOnly;
234 &scope_str));
235
236 EXTENSION_FUNCTION_VALIDATE(pref_helpers::StringToScope(scope_str, &scope));
237 EXTENSION_FUNCTION_VALIDATE(
238 scope != kExtensionPrefsScopeIncognitoPersistent);
239 } 224 }
240 225
241 bool incognito = (scope == kExtensionPrefsScopeIncognitoPersistent || 226 bool incognito = (scope == kExtensionPrefsScopeIncognitoPersistent ||
242 scope == kExtensionPrefsScopeIncognitoSessionOnly); 227 scope == kExtensionPrefsScopeIncognitoSessionOnly);
243 if (incognito) { 228 if (incognito) {
244 // Regular profiles can't access incognito unless include_incognito is true. 229 // Regular profiles can't access incognito unless include_incognito is true.
245 if (!profile()->IsOffTheRecord() && !include_incognito()) { 230 if (!profile()->IsOffTheRecord() && !include_incognito()) {
246 error_ = pref_keys::kIncognitoErrorMessage; 231 error_ = pref_keys::kIncognitoErrorMessage;
247 return false; 232 return false;
248 } 233 }
(...skipping 14 matching lines...) Expand all
263 248
264 ContentSettingsStore* store = 249 ContentSettingsStore* store =
265 profile_->GetExtensionService()->GetContentSettingsStore(); 250 profile_->GetExtensionService()->GetContentSettingsStore();
266 store->SetExtensionContentSetting(extension_id(), primary_pattern, 251 store->SetExtensionContentSetting(extension_id(), primary_pattern,
267 secondary_pattern, content_type, 252 secondary_pattern, content_type,
268 resource_identifier, setting, scope); 253 resource_identifier, setting, scope);
269 return true; 254 return true;
270 } 255 }
271 256
272 bool GetResourceIdentifiersFunction::RunImpl() { 257 bool GetResourceIdentifiersFunction::RunImpl() {
273 std::string content_type_str; 258 ContentSettingsType content_type;
274 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &content_type_str)); 259 EXTENSION_FUNCTION_VALIDATE(ExtractContentType(args_.get(), &content_type));
275 ContentSettingsType content_type =
276 helpers::StringToContentSettingsType(content_type_str);
277 EXTENSION_FUNCTION_VALIDATE(content_type != CONTENT_SETTINGS_TYPE_DEFAULT);
278 260
279 if (content_type == CONTENT_SETTINGS_TYPE_PLUGINS) { 261 if (content_type == CONTENT_SETTINGS_TYPE_PLUGINS) {
280 if (g_testing_plugin_groups_) { 262 if (g_testing_plugin_groups_) {
281 OnGotPluginGroups(*g_testing_plugin_groups_); 263 OnGotPluginGroups(*g_testing_plugin_groups_);
282 } else { 264 } else {
283 PluginService::GetInstance()->GetPluginGroups( 265 PluginService::GetInstance()->GetPluginGroups(
284 base::Bind(&GetResourceIdentifiersFunction::OnGotPluginGroups, this)); 266 base::Bind(&GetResourceIdentifiersFunction::OnGotPluginGroups, this));
285 } 267 }
286 } else { 268 } else {
287 SendResponse(true); 269 SendResponse(true);
(...skipping 19 matching lines...) Expand all
307 &GetResourceIdentifiersFunction::SendResponse, this, true)); 289 &GetResourceIdentifiersFunction::SendResponse, this, true));
308 } 290 }
309 291
310 // static 292 // static
311 void GetResourceIdentifiersFunction::SetPluginGroupsForTesting( 293 void GetResourceIdentifiersFunction::SetPluginGroupsForTesting(
312 const std::vector<webkit::npapi::PluginGroup>* plugin_groups) { 294 const std::vector<webkit::npapi::PluginGroup>* plugin_groups) {
313 g_testing_plugin_groups_ = plugin_groups; 295 g_testing_plugin_groups_ = plugin_groups;
314 } 296 }
315 297
316 } // namespace extensions 298 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/alarms/alarms_api.cc ('k') | chrome/common/extensions/api/api.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698