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

Side by Side Diff: chrome/browser/extensions/api/font_settings/font_settings_api.cc

Issue 2298493003: [Extensions] Convert some ChromeSyncExtensionFunctions (Closed)
Patch Set: fix 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 // Font Settings Extension API implementation. 5 // Font Settings Extension API implementation.
6 6
7 #include "chrome/browser/extensions/api/font_settings/font_settings_api.h" 7 #include "chrome/browser/extensions/api/font_settings/font_settings_api.h"
8 8
9 #include <stddef.h> 9 #include <stddef.h>
10 10
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after
220 220
221 static base::LazyInstance<BrowserContextKeyedAPIFactory<FontSettingsAPI> > 221 static base::LazyInstance<BrowserContextKeyedAPIFactory<FontSettingsAPI> >
222 g_factory = LAZY_INSTANCE_INITIALIZER; 222 g_factory = LAZY_INSTANCE_INITIALIZER;
223 223
224 // static 224 // static
225 BrowserContextKeyedAPIFactory<FontSettingsAPI>* 225 BrowserContextKeyedAPIFactory<FontSettingsAPI>*
226 FontSettingsAPI::GetFactoryInstance() { 226 FontSettingsAPI::GetFactoryInstance() {
227 return g_factory.Pointer(); 227 return g_factory.Pointer();
228 } 228 }
229 229
230 bool FontSettingsClearFontFunction::RunSync() { 230 ExtensionFunction::ResponseAction FontSettingsClearFontFunction::Run() {
231 if (GetProfile()->IsOffTheRecord()) { 231 Profile* profile = Profile::FromBrowserContext(browser_context());
232 error_ = kSetFromIncognitoError; 232 if (profile->IsOffTheRecord())
233 return false; 233 return RespondNow(Error(kSetFromIncognitoError));
234 }
235 234
236 std::unique_ptr<fonts::ClearFont::Params> params( 235 std::unique_ptr<fonts::ClearFont::Params> params(
237 fonts::ClearFont::Params::Create(*args_)); 236 fonts::ClearFont::Params::Create(*args_));
238 EXTENSION_FUNCTION_VALIDATE(params.get()); 237 EXTENSION_FUNCTION_VALIDATE(params.get());
239 238
240 std::string pref_path = GetFontNamePrefPath(params->details.generic_family, 239 std::string pref_path = GetFontNamePrefPath(params->details.generic_family,
241 params->details.script); 240 params->details.script);
242 241
243 // Ensure |pref_path| really is for a registered per-script font pref. 242 // Ensure |pref_path| really is for a registered per-script font pref.
244 EXTENSION_FUNCTION_VALIDATE( 243 EXTENSION_FUNCTION_VALIDATE(profile->GetPrefs()->FindPreference(pref_path));
245 GetProfile()->GetPrefs()->FindPreference(pref_path));
246 244
247 PreferenceAPI::Get(GetProfile())->RemoveExtensionControlledPref( 245 PreferenceAPI::Get(profile)->RemoveExtensionControlledPref(
248 extension_id(), pref_path, kExtensionPrefsScopeRegular); 246 extension_id(), pref_path, kExtensionPrefsScopeRegular);
249 return true; 247 return RespondNow(NoArguments());
250 } 248 }
251 249
252 bool FontSettingsGetFontFunction::RunSync() { 250 ExtensionFunction::ResponseAction FontSettingsGetFontFunction::Run() {
253 std::unique_ptr<fonts::GetFont::Params> params( 251 std::unique_ptr<fonts::GetFont::Params> params(
254 fonts::GetFont::Params::Create(*args_)); 252 fonts::GetFont::Params::Create(*args_));
255 EXTENSION_FUNCTION_VALIDATE(params.get()); 253 EXTENSION_FUNCTION_VALIDATE(params.get());
256 254
257 std::string pref_path = GetFontNamePrefPath(params->details.generic_family, 255 std::string pref_path = GetFontNamePrefPath(params->details.generic_family,
258 params->details.script); 256 params->details.script);
259 257
260 PrefService* prefs = GetProfile()->GetPrefs(); 258 Profile* profile = Profile::FromBrowserContext(browser_context());
259 PrefService* prefs = profile->GetPrefs();
261 const PrefService::Preference* pref = 260 const PrefService::Preference* pref =
262 prefs->FindPreference(pref_path); 261 prefs->FindPreference(pref_path);
263 262
264 std::string font_name; 263 std::string font_name;
265 EXTENSION_FUNCTION_VALIDATE( 264 EXTENSION_FUNCTION_VALIDATE(
266 pref && pref->GetValue()->GetAsString(&font_name)); 265 pref && pref->GetValue()->GetAsString(&font_name));
267 font_name = MaybeGetLocalizedFontName(font_name); 266 font_name = MaybeGetLocalizedFontName(font_name);
268 267
269 // We don't support incognito-specific font prefs, so don't consider them when 268 // We don't support incognito-specific font prefs, so don't consider them when
270 // getting level of control. 269 // getting level of control.
271 const bool kIncognito = false; 270 const bool kIncognito = false;
272 std::string level_of_control = 271 std::string level_of_control =
273 extensions::preference_helpers::GetLevelOfControl( 272 extensions::preference_helpers::GetLevelOfControl(profile, extension_id(),
274 GetProfile(), extension_id(), pref_path, kIncognito); 273 pref_path, kIncognito);
275 274
276 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue()); 275 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue());
277 result->SetString(kFontIdKey, font_name); 276 result->SetString(kFontIdKey, font_name);
278 result->SetString(kLevelOfControlKey, level_of_control); 277 result->SetString(kLevelOfControlKey, level_of_control);
279 SetResult(std::move(result)); 278 return RespondNow(OneArgument(std::move(result)));
280 return true;
281 } 279 }
282 280
283 bool FontSettingsSetFontFunction::RunSync() { 281 ExtensionFunction::ResponseAction FontSettingsSetFontFunction::Run() {
284 if (GetProfile()->IsOffTheRecord()) { 282 Profile* profile = Profile::FromBrowserContext(browser_context());
285 error_ = kSetFromIncognitoError; 283 if (profile->IsOffTheRecord())
286 return false; 284 return RespondNow(Error(kSetFromIncognitoError));
287 }
288 285
289 std::unique_ptr<fonts::SetFont::Params> params( 286 std::unique_ptr<fonts::SetFont::Params> params(
290 fonts::SetFont::Params::Create(*args_)); 287 fonts::SetFont::Params::Create(*args_));
291 EXTENSION_FUNCTION_VALIDATE(params.get()); 288 EXTENSION_FUNCTION_VALIDATE(params.get());
292 289
293 std::string pref_path = GetFontNamePrefPath(params->details.generic_family, 290 std::string pref_path = GetFontNamePrefPath(params->details.generic_family,
294 params->details.script); 291 params->details.script);
295 292
296 // Ensure |pref_path| really is for a registered font pref. 293 // Ensure |pref_path| really is for a registered font pref.
297 EXTENSION_FUNCTION_VALIDATE( 294 EXTENSION_FUNCTION_VALIDATE(profile->GetPrefs()->FindPreference(pref_path));
298 GetProfile()->GetPrefs()->FindPreference(pref_path));
299 295
300 PreferenceAPI::Get(GetProfile())->SetExtensionControlledPref( 296 PreferenceAPI::Get(profile)->SetExtensionControlledPref(
301 extension_id(), 297 extension_id(), pref_path, kExtensionPrefsScopeRegular,
302 pref_path,
303 kExtensionPrefsScopeRegular,
304 new base::StringValue(params->details.font_id)); 298 new base::StringValue(params->details.font_id));
305 return true; 299 return RespondNow(NoArguments());
306 } 300 }
307 301
308 bool FontSettingsGetFontListFunction::RunAsync() { 302 bool FontSettingsGetFontListFunction::RunAsync() {
309 content::GetFontListAsync( 303 content::GetFontListAsync(
310 Bind(&FontSettingsGetFontListFunction::FontListHasLoaded, this)); 304 Bind(&FontSettingsGetFontListFunction::FontListHasLoaded, this));
311 return true; 305 return true;
312 } 306 }
313 307
314 void FontSettingsGetFontListFunction::FontListHasLoaded( 308 void FontSettingsGetFontListFunction::FontListHasLoaded(
315 std::unique_ptr<base::ListValue> list) { 309 std::unique_ptr<base::ListValue> list) {
(...skipping 28 matching lines...) Expand all
344 new base::DictionaryValue()); 338 new base::DictionaryValue());
345 font_name->Set(kFontIdKey, new base::StringValue(name)); 339 font_name->Set(kFontIdKey, new base::StringValue(name));
346 font_name->Set(kDisplayNameKey, new base::StringValue(localized_name)); 340 font_name->Set(kDisplayNameKey, new base::StringValue(localized_name));
347 result->Append(std::move(font_name)); 341 result->Append(std::move(font_name));
348 } 342 }
349 343
350 SetResult(std::move(result)); 344 SetResult(std::move(result));
351 return true; 345 return true;
352 } 346 }
353 347
354 bool ClearFontPrefExtensionFunction::RunSync() { 348 ExtensionFunction::ResponseAction ClearFontPrefExtensionFunction::Run() {
355 if (GetProfile()->IsOffTheRecord()) { 349 Profile* profile = Profile::FromBrowserContext(browser_context());
356 error_ = kSetFromIncognitoError; 350 if (profile->IsOffTheRecord())
357 return false; 351 return RespondNow(Error(kSetFromIncognitoError));
358 }
359 352
360 PreferenceAPI::Get(GetProfile())->RemoveExtensionControlledPref( 353 PreferenceAPI::Get(profile)->RemoveExtensionControlledPref(
361 extension_id(), GetPrefName(), kExtensionPrefsScopeRegular); 354 extension_id(), GetPrefName(), kExtensionPrefsScopeRegular);
362 return true; 355 return RespondNow(NoArguments());
363 } 356 }
364 357
365 bool GetFontPrefExtensionFunction::RunSync() { 358 ExtensionFunction::ResponseAction GetFontPrefExtensionFunction::Run() {
366 PrefService* prefs = GetProfile()->GetPrefs(); 359 Profile* profile = Profile::FromBrowserContext(browser_context());
360 PrefService* prefs = profile->GetPrefs();
367 const PrefService::Preference* pref = prefs->FindPreference(GetPrefName()); 361 const PrefService::Preference* pref = prefs->FindPreference(GetPrefName());
368 EXTENSION_FUNCTION_VALIDATE(pref); 362 EXTENSION_FUNCTION_VALIDATE(pref);
369 363
370 // We don't support incognito-specific font prefs, so don't consider them when 364 // We don't support incognito-specific font prefs, so don't consider them when
371 // getting level of control. 365 // getting level of control.
372 const bool kIncognito = false; 366 const bool kIncognito = false;
373 367
374 std::string level_of_control = 368 std::string level_of_control =
375 extensions::preference_helpers::GetLevelOfControl( 369 extensions::preference_helpers::GetLevelOfControl(
376 GetProfile(), extension_id(), GetPrefName(), kIncognito); 370 profile, extension_id(), GetPrefName(), kIncognito);
377 371
378 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue()); 372 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue());
379 result->Set(GetKey(), pref->GetValue()->DeepCopy()); 373 result->Set(GetKey(), pref->GetValue()->DeepCopy());
380 result->SetString(kLevelOfControlKey, level_of_control); 374 result->SetString(kLevelOfControlKey, level_of_control);
381 SetResult(std::move(result)); 375 return RespondNow(OneArgument(std::move(result)));
382 return true;
383 } 376 }
384 377
385 bool SetFontPrefExtensionFunction::RunSync() { 378 ExtensionFunction::ResponseAction SetFontPrefExtensionFunction::Run() {
386 if (GetProfile()->IsOffTheRecord()) { 379 Profile* profile = Profile::FromBrowserContext(browser_context());
387 error_ = kSetFromIncognitoError; 380 if (profile->IsOffTheRecord())
388 return false; 381 return RespondNow(Error(kSetFromIncognitoError));
389 }
390 382
391 base::DictionaryValue* details = NULL; 383 base::DictionaryValue* details = NULL;
392 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details)); 384 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details));
393 385
394 base::Value* value; 386 base::Value* value;
395 EXTENSION_FUNCTION_VALIDATE(details->Get(GetKey(), &value)); 387 EXTENSION_FUNCTION_VALIDATE(details->Get(GetKey(), &value));
396 388
397 PreferenceAPI::Get(GetProfile()) 389 PreferenceAPI::Get(profile)->SetExtensionControlledPref(
398 ->SetExtensionControlledPref(extension_id(), 390 extension_id(), GetPrefName(), kExtensionPrefsScopeRegular,
399 GetPrefName(), 391 value->DeepCopy());
400 kExtensionPrefsScopeRegular, 392 return RespondNow(NoArguments());
401 value->DeepCopy());
402 return true;
403 } 393 }
404 394
405 const char* FontSettingsClearDefaultFontSizeFunction::GetPrefName() { 395 const char* FontSettingsClearDefaultFontSizeFunction::GetPrefName() {
406 return prefs::kWebKitDefaultFontSize; 396 return prefs::kWebKitDefaultFontSize;
407 } 397 }
408 398
409 const char* FontSettingsGetDefaultFontSizeFunction::GetPrefName() { 399 const char* FontSettingsGetDefaultFontSizeFunction::GetPrefName() {
410 return prefs::kWebKitDefaultFontSize; 400 return prefs::kWebKitDefaultFontSize;
411 } 401 }
412 402
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
456 446
457 const char* FontSettingsSetMinimumFontSizeFunction::GetPrefName() { 447 const char* FontSettingsSetMinimumFontSizeFunction::GetPrefName() {
458 return prefs::kWebKitMinimumFontSize; 448 return prefs::kWebKitMinimumFontSize;
459 } 449 }
460 450
461 const char* FontSettingsSetMinimumFontSizeFunction::GetKey() { 451 const char* FontSettingsSetMinimumFontSizeFunction::GetKey() {
462 return kPixelSizeKey; 452 return kPixelSizeKey;
463 } 453 }
464 454
465 } // namespace extensions 455 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698