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

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

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

Powered by Google App Engine
This is Rietveld 408576698