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

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

Issue 2328643003: [Extensions] Convert some ChromeSyncExtensionFunctions (Closed)
Patch Set: Created 4 years, 3 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 <memory> 7 #include <memory>
8 #include <set> 8 #include <set>
9 #include <utility> 9 #include <utility>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/bind.h" 12 #include "base/bind.h"
13 #include "base/command_line.h" 13 #include "base/command_line.h"
14 #include "base/strings/stringprintf.h" 14 #include "base/strings/stringprintf.h"
15 #include "base/values.h" 15 #include "base/values.h"
16 #include "chrome/browser/content_settings/cookie_settings_factory.h" 16 #include "chrome/browser/content_settings/cookie_settings_factory.h"
17 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" 17 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
18 #include "chrome/browser/extensions/api/content_settings/content_settings_api_co nstants.h" 18 #include "chrome/browser/extensions/api/content_settings/content_settings_api_co nstants.h"
19 #include "chrome/browser/extensions/api/content_settings/content_settings_helper s.h" 19 #include "chrome/browser/extensions/api/content_settings/content_settings_helper s.h"
20 #include "chrome/browser/extensions/api/content_settings/content_settings_servic e.h" 20 #include "chrome/browser/extensions/api/content_settings/content_settings_servic e.h"
21 #include "chrome/browser/extensions/api/content_settings/content_settings_store. h" 21 #include "chrome/browser/extensions/api/content_settings/content_settings_store. h"
22 #include "chrome/browser/extensions/api/preference/preference_api_constants.h" 22 #include "chrome/browser/extensions/api/preference/preference_api_constants.h"
23 #include "chrome/browser/extensions/api/preference/preference_helpers.h" 23 #include "chrome/browser/extensions/api/preference/preference_helpers.h"
24 #include "chrome/browser/extensions/chrome_extension_function_details.h"
24 #include "chrome/browser/plugins/plugin_finder.h" 25 #include "chrome/browser/plugins/plugin_finder.h"
25 #include "chrome/browser/plugins/plugin_installer.h" 26 #include "chrome/browser/plugins/plugin_installer.h"
26 #include "chrome/browser/profiles/profile.h" 27 #include "chrome/browser/profiles/profile.h"
27 #include "chrome/common/chrome_switches.h" 28 #include "chrome/common/chrome_switches.h"
28 #include "chrome/common/extensions/api/content_settings.h" 29 #include "chrome/common/extensions/api/content_settings.h"
29 #include "components/content_settings/core/browser/content_settings_info.h" 30 #include "components/content_settings/core/browser/content_settings_info.h"
30 #include "components/content_settings/core/browser/content_settings_registry.h" 31 #include "components/content_settings/core/browser/content_settings_registry.h"
31 #include "components/content_settings/core/browser/content_settings_utils.h" 32 #include "components/content_settings/core/browser/content_settings_utils.h"
32 #include "components/content_settings/core/browser/cookie_settings.h" 33 #include "components/content_settings/core/browser/cookie_settings.h"
33 #include "components/content_settings/core/browser/host_content_settings_map.h" 34 #include "components/content_settings/core/browser/host_content_settings_map.h"
(...skipping 26 matching lines...) Expand all
60 return *content_type != CONTENT_SETTINGS_TYPE_DEFAULT; 61 return *content_type != CONTENT_SETTINGS_TYPE_DEFAULT;
61 } 62 }
62 63
63 } // namespace 64 } // namespace
64 65
65 namespace extensions { 66 namespace extensions {
66 67
67 namespace helpers = content_settings_helpers; 68 namespace helpers = content_settings_helpers;
68 namespace keys = content_settings_api_constants; 69 namespace keys = content_settings_api_constants;
69 70
70 bool ContentSettingsContentSettingClearFunction::RunSync() { 71 ExtensionFunction::ResponseAction
72 ContentSettingsContentSettingClearFunction::Run() {
71 ContentSettingsType content_type; 73 ContentSettingsType content_type;
72 EXTENSION_FUNCTION_VALIDATE(RemoveContentType(args_.get(), &content_type)); 74 EXTENSION_FUNCTION_VALIDATE(RemoveContentType(args_.get(), &content_type));
73 75
74 std::unique_ptr<Clear::Params> params(Clear::Params::Create(*args_)); 76 std::unique_ptr<Clear::Params> params(Clear::Params::Create(*args_));
75 EXTENSION_FUNCTION_VALIDATE(params.get()); 77 EXTENSION_FUNCTION_VALIDATE(params.get());
76 78
77 ExtensionPrefsScope scope = kExtensionPrefsScopeRegular; 79 ExtensionPrefsScope scope = kExtensionPrefsScopeRegular;
78 bool incognito = false; 80 bool incognito = false;
79 if (params->details.scope == 81 if (params->details.scope ==
80 api::content_settings::SCOPE_INCOGNITO_SESSION_ONLY) { 82 api::content_settings::SCOPE_INCOGNITO_SESSION_ONLY) {
81 scope = kExtensionPrefsScopeIncognitoSessionOnly; 83 scope = kExtensionPrefsScopeIncognitoSessionOnly;
82 incognito = true; 84 incognito = true;
83 } 85 }
84 86
85 if (incognito) { 87 if (incognito) {
86 // We don't check incognito permissions here, as an extension should be 88 // We don't check incognito permissions here, as an extension should be
87 // always allowed to clear its own settings. 89 // always allowed to clear its own settings.
88 } else { 90 } else if (browser_context()->IsOffTheRecord()) {
89 // Incognito profiles can't access regular mode ever, they only exist in 91 // Incognito profiles can't access regular mode ever, they only exist in
90 // split mode. 92 // split mode.
91 if (GetProfile()->IsOffTheRecord()) { 93 return RespondNow(Error(keys::kIncognitoContextError));
92 error_ = keys::kIncognitoContextError;
93 return false;
94 }
95 } 94 }
96 95
97 scoped_refptr<ContentSettingsStore> store = 96 scoped_refptr<ContentSettingsStore> store =
98 ContentSettingsService::Get(GetProfile())->content_settings_store(); 97 ContentSettingsService::Get(browser_context())->content_settings_store();
99 store->ClearContentSettingsForExtension(extension_id(), scope); 98 store->ClearContentSettingsForExtension(extension_id(), scope);
100 99
101 return true; 100 return RespondNow(NoArguments());
102 } 101 }
103 102
104 bool ContentSettingsContentSettingGetFunction::RunSync() { 103 ExtensionFunction::ResponseAction
104 ContentSettingsContentSettingGetFunction::Run() {
105 ContentSettingsType content_type; 105 ContentSettingsType content_type;
106 EXTENSION_FUNCTION_VALIDATE(RemoveContentType(args_.get(), &content_type)); 106 EXTENSION_FUNCTION_VALIDATE(RemoveContentType(args_.get(), &content_type));
107 107
108 std::unique_ptr<Get::Params> params(Get::Params::Create(*args_)); 108 std::unique_ptr<Get::Params> params(Get::Params::Create(*args_));
109 EXTENSION_FUNCTION_VALIDATE(params.get()); 109 EXTENSION_FUNCTION_VALIDATE(params.get());
110 110
111 GURL primary_url(params->details.primary_url); 111 GURL primary_url(params->details.primary_url);
112 if (!primary_url.is_valid()) { 112 if (!primary_url.is_valid()) {
113 error_ = ErrorUtils::FormatErrorMessage(keys::kInvalidUrlError, 113 return RespondNow(
114 params->details.primary_url); 114 Error(keys::kInvalidUrlError, params->details.primary_url));
115 return false;
116 } 115 }
117 116
118 GURL secondary_url(primary_url); 117 GURL secondary_url(primary_url);
119 if (params->details.secondary_url.get()) { 118 if (params->details.secondary_url.get()) {
120 secondary_url = GURL(*params->details.secondary_url); 119 secondary_url = GURL(*params->details.secondary_url);
121 if (!secondary_url.is_valid()) { 120 if (!secondary_url.is_valid()) {
122 error_ = ErrorUtils::FormatErrorMessage(keys::kInvalidUrlError, 121 return RespondNow(
123 *params->details.secondary_url); 122 Error(keys::kInvalidUrlError, *params->details.secondary_url));
124 return false;
125 } 123 }
126 } 124 }
127 125
128 std::string resource_identifier; 126 std::string resource_identifier;
129 if (params->details.resource_identifier.get()) 127 if (params->details.resource_identifier.get())
130 resource_identifier = params->details.resource_identifier->id; 128 resource_identifier = params->details.resource_identifier->id;
131 129
132 bool incognito = false; 130 bool incognito = false;
133 if (params->details.incognito.get()) 131 if (params->details.incognito.get())
134 incognito = *params->details.incognito; 132 incognito = *params->details.incognito;
135 if (incognito && !include_incognito()) { 133 if (incognito && !include_incognito())
136 error_ = pref_keys::kIncognitoErrorMessage; 134 return RespondNow(Error(pref_keys::kIncognitoErrorMessage));
137 return false;
138 }
139 135
140 HostContentSettingsMap* map; 136 HostContentSettingsMap* map;
141 content_settings::CookieSettings* cookie_settings; 137 content_settings::CookieSettings* cookie_settings;
138 Profile* profile = ChromeExtensionFunctionDetails(this).GetProfile();
asargent_no_longer_on_chrome 2016/09/09 05:09:34 Out of curiousity, why do we have this ChromeExten
Devlin 2016/09/09 18:29:04 Excellent question! For some reason, I thought we
142 if (incognito) { 139 if (incognito) {
143 if (!GetProfile()->HasOffTheRecordProfile()) { 140 if (!profile->HasOffTheRecordProfile()) {
144 // TODO(bauerb): Allow reading incognito content settings 141 // TODO(bauerb): Allow reading incognito content settings
145 // outside of an incognito session. 142 // outside of an incognito session.
146 error_ = keys::kIncognitoSessionOnlyError; 143 return RespondNow(Error(keys::kIncognitoSessionOnlyError));
147 return false;
148 } 144 }
149 map = HostContentSettingsMapFactory::GetForProfile( 145 map = HostContentSettingsMapFactory::GetForProfile(
150 GetProfile()->GetOffTheRecordProfile()); 146 profile->GetOffTheRecordProfile());
151 cookie_settings = CookieSettingsFactory::GetForProfile( 147 cookie_settings =
152 GetProfile()->GetOffTheRecordProfile()).get(); 148 CookieSettingsFactory::GetForProfile(profile->GetOffTheRecordProfile())
149 .get();
153 } else { 150 } else {
154 map = HostContentSettingsMapFactory::GetForProfile(GetProfile()); 151 map = HostContentSettingsMapFactory::GetForProfile(profile);
155 cookie_settings = CookieSettingsFactory::GetForProfile(GetProfile()).get(); 152 cookie_settings = CookieSettingsFactory::GetForProfile(profile).get();
156 } 153 }
157 154
158 ContentSetting setting; 155 ContentSetting setting;
159 if (content_type == CONTENT_SETTINGS_TYPE_COOKIES) { 156 if (content_type == CONTENT_SETTINGS_TYPE_COOKIES) {
160 // TODO(jochen): Do we return the value for setting or for reading cookies? 157 // TODO(jochen): Do we return the value for setting or for reading cookies?
161 bool setting_cookie = false; 158 bool setting_cookie = false;
162 setting = cookie_settings->GetCookieSetting(primary_url, secondary_url, 159 setting = cookie_settings->GetCookieSetting(primary_url, secondary_url,
163 setting_cookie, NULL); 160 setting_cookie, NULL);
164 } else { 161 } else {
165 setting = map->GetContentSetting(primary_url, secondary_url, content_type, 162 setting = map->GetContentSetting(primary_url, secondary_url, content_type,
166 resource_identifier); 163 resource_identifier);
167 } 164 }
168 165
169 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue()); 166 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue());
170 std::string setting_string = 167 std::string setting_string =
171 content_settings::ContentSettingToString(setting); 168 content_settings::ContentSettingToString(setting);
172 DCHECK(!setting_string.empty()); 169 DCHECK(!setting_string.empty());
173 result->SetString(keys::kContentSettingKey, setting_string); 170 result->SetString(keys::kContentSettingKey, setting_string);
174 171
175 SetResult(std::move(result)); 172 return RespondNow(OneArgument(std::move(result)));
176
177 return true;
178 } 173 }
179 174
180 bool ContentSettingsContentSettingSetFunction::RunSync() { 175 ExtensionFunction::ResponseAction
176 ContentSettingsContentSettingSetFunction::Run() {
181 ContentSettingsType content_type; 177 ContentSettingsType content_type;
182 EXTENSION_FUNCTION_VALIDATE(RemoveContentType(args_.get(), &content_type)); 178 EXTENSION_FUNCTION_VALIDATE(RemoveContentType(args_.get(), &content_type));
183 179
184 std::unique_ptr<Set::Params> params(Set::Params::Create(*args_)); 180 std::unique_ptr<Set::Params> params(Set::Params::Create(*args_));
185 EXTENSION_FUNCTION_VALIDATE(params.get()); 181 EXTENSION_FUNCTION_VALIDATE(params.get());
186 182
187 std::string primary_error; 183 std::string primary_error;
188 ContentSettingsPattern primary_pattern = 184 ContentSettingsPattern primary_pattern =
189 helpers::ParseExtensionPattern(params->details.primary_pattern, 185 helpers::ParseExtensionPattern(params->details.primary_pattern,
190 &primary_error); 186 &primary_error);
191 if (!primary_pattern.IsValid()) { 187 if (!primary_pattern.IsValid())
192 error_ = primary_error; 188 return RespondNow(Error(primary_error));
193 return false;
194 }
195 189
196 ContentSettingsPattern secondary_pattern = ContentSettingsPattern::Wildcard(); 190 ContentSettingsPattern secondary_pattern = ContentSettingsPattern::Wildcard();
197 if (params->details.secondary_pattern.get()) { 191 if (params->details.secondary_pattern.get()) {
198 std::string secondary_error; 192 std::string secondary_error;
199 secondary_pattern = 193 secondary_pattern =
200 helpers::ParseExtensionPattern(*params->details.secondary_pattern, 194 helpers::ParseExtensionPattern(*params->details.secondary_pattern,
201 &secondary_error); 195 &secondary_error);
202 if (!secondary_pattern.IsValid()) { 196 if (!secondary_pattern.IsValid())
203 error_ = secondary_error; 197 return RespondNow(Error(secondary_error));
204 return false;
205 }
206 } 198 }
207 199
208 std::string resource_identifier; 200 std::string resource_identifier;
209 if (params->details.resource_identifier.get()) 201 if (params->details.resource_identifier.get())
210 resource_identifier = params->details.resource_identifier->id; 202 resource_identifier = params->details.resource_identifier->id;
211 203
212 std::string setting_str; 204 std::string setting_str;
213 EXTENSION_FUNCTION_VALIDATE( 205 EXTENSION_FUNCTION_VALIDATE(
214 params->details.setting->GetAsString(&setting_str)); 206 params->details.setting->GetAsString(&setting_str));
215 ContentSetting setting; 207 ContentSetting setting;
(...skipping 20 matching lines...) Expand all
236 // externally in the API, i.e. chrome.contentSettings.<name>.set(). 228 // externally in the API, i.e. chrome.contentSettings.<name>.set().
237 std::string readable_type_name; 229 std::string readable_type_name;
238 if (content_type == CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC) { 230 if (content_type == CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC) {
239 readable_type_name = "microphone"; 231 readable_type_name = "microphone";
240 } else if (content_type == CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA) { 232 } else if (content_type == CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA) {
241 readable_type_name = "camera"; 233 readable_type_name = "camera";
242 } else { 234 } else {
243 NOTREACHED() << "No human-readable type name defined for this type."; 235 NOTREACHED() << "No human-readable type name defined for this type.";
244 } 236 }
245 237
246 error_ = base::StringPrintf( 238 return RespondNow(Error(base::StringPrintf(kUnsupportedDefaultSettingError,
247 kUnsupportedDefaultSettingError, 239 setting_str.c_str(),
248 setting_str.c_str(), 240 readable_type_name.c_str())));
249 readable_type_name.c_str());
250 return false;
251 } 241 }
252 242
253 ExtensionPrefsScope scope = kExtensionPrefsScopeRegular; 243 ExtensionPrefsScope scope = kExtensionPrefsScopeRegular;
254 bool incognito = false; 244 bool incognito = false;
255 if (params->details.scope == 245 if (params->details.scope ==
256 api::content_settings::SCOPE_INCOGNITO_SESSION_ONLY) { 246 api::content_settings::SCOPE_INCOGNITO_SESSION_ONLY) {
257 scope = kExtensionPrefsScopeIncognitoSessionOnly; 247 scope = kExtensionPrefsScopeIncognitoSessionOnly;
258 incognito = true; 248 incognito = true;
259 } 249 }
260 250
261 if (incognito) { 251 if (incognito) {
262 // Regular profiles can't access incognito unless include_incognito is true. 252 // Regular profiles can't access incognito unless include_incognito is true.
263 if (!GetProfile()->IsOffTheRecord() && !include_incognito()) { 253 if (!browser_context()->IsOffTheRecord() && !include_incognito())
264 error_ = pref_keys::kIncognitoErrorMessage; 254 return RespondNow(Error(pref_keys::kIncognitoErrorMessage));
265 return false;
266 }
267 } else { 255 } else {
268 // Incognito profiles can't access regular mode ever, they only exist in 256 // Incognito profiles can't access regular mode ever, they only exist in
269 // split mode. 257 // split mode.
270 if (GetProfile()->IsOffTheRecord()) { 258 if (browser_context()->IsOffTheRecord())
271 error_ = keys::kIncognitoContextError; 259 return RespondNow(Error(keys::kIncognitoContextError));
272 return false;
273 }
274 } 260 }
275 261
276 if (scope == kExtensionPrefsScopeIncognitoSessionOnly && 262 if (scope == kExtensionPrefsScopeIncognitoSessionOnly &&
277 !GetProfile()->HasOffTheRecordProfile()) { 263 !ChromeExtensionFunctionDetails(this)
278 error_ = pref_keys::kIncognitoSessionOnlyErrorMessage; 264 .GetProfile()
279 return false; 265 ->HasOffTheRecordProfile()) {
266 return RespondNow(Error(pref_keys::kIncognitoSessionOnlyErrorMessage));
280 } 267 }
281 268
282 scoped_refptr<ContentSettingsStore> store = 269 scoped_refptr<ContentSettingsStore> store =
283 ContentSettingsService::Get(GetProfile())->content_settings_store(); 270 ContentSettingsService::Get(browser_context())->content_settings_store();
284 store->SetExtensionContentSetting(extension_id(), primary_pattern, 271 store->SetExtensionContentSetting(extension_id(), primary_pattern,
285 secondary_pattern, content_type, 272 secondary_pattern, content_type,
286 resource_identifier, setting, scope); 273 resource_identifier, setting, scope);
287 return true; 274 return RespondNow(NoArguments());
288 } 275 }
289 276
290 bool ContentSettingsContentSettingGetResourceIdentifiersFunction::RunAsync() { 277 bool ContentSettingsContentSettingGetResourceIdentifiersFunction::RunAsync() {
291 ContentSettingsType content_type; 278 ContentSettingsType content_type;
292 EXTENSION_FUNCTION_VALIDATE(RemoveContentType(args_.get(), &content_type)); 279 EXTENSION_FUNCTION_VALIDATE(RemoveContentType(args_.get(), &content_type));
293 280
294 if (content_type != CONTENT_SETTINGS_TYPE_PLUGINS) { 281 if (content_type != CONTENT_SETTINGS_TYPE_PLUGINS) {
295 SendResponse(true); 282 SendResponse(true);
296 return true; 283 return true;
297 } 284 }
(...skipping 27 matching lines...) Expand all
325 SetResult(std::move(list)); 312 SetResult(std::move(list));
326 BrowserThread::PostTask( 313 BrowserThread::PostTask(
327 BrowserThread::UI, FROM_HERE, base::Bind( 314 BrowserThread::UI, FROM_HERE, base::Bind(
328 &ContentSettingsContentSettingGetResourceIdentifiersFunction:: 315 &ContentSettingsContentSettingGetResourceIdentifiersFunction::
329 SendResponse, 316 SendResponse,
330 this, 317 this,
331 true)); 318 true));
332 } 319 }
333 320
334 } // namespace extensions 321 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698