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

Side by Side Diff: chrome/browser/extensions/api/preference/preference_api.cc

Issue 2781443005: Remove unnecessary calls to std::basic_string<>::c_str() (Closed)
Patch Set: Created 3 years, 8 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/preference/preference_api.h" 5 #include "chrome/browser/extensions/api/preference/preference_api.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <map> 9 #include <map>
10 #include <memory> 10 #include <memory>
(...skipping 363 matching lines...) Expand 10 before | Expand all | Expand 10 after
374 bool incognito = (pref_service != profile_->GetPrefs()); 374 bool incognito = (pref_service != profile_->GetPrefs());
375 375
376 std::string event_name; 376 std::string event_name;
377 APIPermission::ID permission = APIPermission::kInvalid; 377 APIPermission::ID permission = APIPermission::kInvalid;
378 bool rv = PrefMapping::GetInstance()->FindEventForBrowserPref( 378 bool rv = PrefMapping::GetInstance()->FindEventForBrowserPref(
379 browser_pref, &event_name, &permission); 379 browser_pref, &event_name, &permission);
380 DCHECK(rv); 380 DCHECK(rv);
381 381
382 base::ListValue args; 382 base::ListValue args;
383 const PrefService::Preference* pref = 383 const PrefService::Preference* pref =
384 pref_service->FindPreference(browser_pref.c_str()); 384 pref_service->FindPreference(browser_pref);
385 CHECK(pref); 385 CHECK(pref);
386 PrefTransformerInterface* transformer = 386 PrefTransformerInterface* transformer =
387 PrefMapping::GetInstance()->FindTransformerForBrowserPref(browser_pref); 387 PrefMapping::GetInstance()->FindTransformerForBrowserPref(browser_pref);
388 base::Value* transformed_value = 388 base::Value* transformed_value =
389 transformer->BrowserToExtensionPref(pref->GetValue()); 389 transformer->BrowserToExtensionPref(pref->GetValue());
390 if (!transformed_value) { 390 if (!transformed_value) {
391 LOG(ERROR) << ErrorUtils::FormatErrorMessage(kConversionErrorMessage, 391 LOG(ERROR) << ErrorUtils::FormatErrorMessage(kConversionErrorMessage,
392 pref->name()); 392 pref->name());
393 return; 393 return;
394 } 394 }
(...skipping 23 matching lines...) Expand all
418 browser_pref); 418 browser_pref);
419 } 419 }
420 420
421 void PreferenceAPIBase::SetExtensionControlledPref( 421 void PreferenceAPIBase::SetExtensionControlledPref(
422 const std::string& extension_id, 422 const std::string& extension_id,
423 const std::string& pref_key, 423 const std::string& pref_key,
424 ExtensionPrefsScope scope, 424 ExtensionPrefsScope scope,
425 base::Value* value) { 425 base::Value* value) {
426 #ifndef NDEBUG 426 #ifndef NDEBUG
427 const PrefService::Preference* pref = 427 const PrefService::Preference* pref =
428 extension_prefs()->pref_service()->FindPreference(pref_key.c_str()); 428 extension_prefs()->pref_service()->FindPreference(pref_key);
429 DCHECK(pref) << "Extension controlled preference key " << pref_key 429 DCHECK(pref) << "Extension controlled preference key " << pref_key
430 << " not registered."; 430 << " not registered.";
431 DCHECK_EQ(pref->GetType(), value->GetType()) 431 DCHECK_EQ(pref->GetType(), value->GetType())
432 << "Extension controlled preference " << pref_key << " has wrong type."; 432 << "Extension controlled preference " << pref_key << " has wrong type.";
433 #endif 433 #endif
434 434
435 std::string scope_string; 435 std::string scope_string;
436 // ScopeToPrefName() returns false if the scope is not persisted. 436 // ScopeToPrefName() returns false if the scope is not persisted.
437 if (pref_names::ScopeToPrefName(scope, &scope_string)) { 437 if (pref_names::ScopeToPrefName(scope, &scope_string)) {
438 // Also store in persisted Preferences file to recover after a 438 // Also store in persisted Preferences file to recover after a
439 // browser restart. 439 // browser restart.
440 ExtensionPrefs::ScopedDictionaryUpdate update(extension_prefs(), 440 ExtensionPrefs::ScopedDictionaryUpdate update(extension_prefs(),
441 extension_id, 441 extension_id,
442 scope_string); 442 scope_string);
443 base::DictionaryValue* preference = update.Get(); 443 base::DictionaryValue* preference = update.Get();
444 if (!preference) 444 if (!preference)
445 preference = update.Create(); 445 preference = update.Create();
446 preference->SetWithoutPathExpansion(pref_key, value->DeepCopy()); 446 preference->SetWithoutPathExpansion(pref_key, value->DeepCopy());
447 } 447 }
448 extension_pref_value_map()->SetExtensionPref( 448 extension_pref_value_map()->SetExtensionPref(
449 extension_id, pref_key, scope, value); 449 extension_id, pref_key, scope, value);
450 } 450 }
451 451
452 void PreferenceAPIBase::RemoveExtensionControlledPref( 452 void PreferenceAPIBase::RemoveExtensionControlledPref(
453 const std::string& extension_id, 453 const std::string& extension_id,
454 const std::string& pref_key, 454 const std::string& pref_key,
455 ExtensionPrefsScope scope) { 455 ExtensionPrefsScope scope) {
456 DCHECK(extension_prefs()->pref_service()->FindPreference(pref_key.c_str())) 456 DCHECK(extension_prefs()->pref_service()->FindPreference(pref_key))
457 << "Extension controlled preference key " << pref_key 457 << "Extension controlled preference key " << pref_key
458 << " not registered."; 458 << " not registered.";
459 459
460 std::string scope_string; 460 std::string scope_string;
461 if (pref_names::ScopeToPrefName(scope, &scope_string)) { 461 if (pref_names::ScopeToPrefName(scope, &scope_string)) {
462 ExtensionPrefs::ScopedDictionaryUpdate update(extension_prefs(), 462 ExtensionPrefs::ScopedDictionaryUpdate update(extension_prefs(),
463 extension_id, 463 extension_id,
464 scope_string); 464 scope_string);
465 base::DictionaryValue* preference = update.Get(); 465 base::DictionaryValue* preference = update.Get();
466 if (preference) 466 if (preference)
467 preference->RemoveWithoutPathExpansion(pref_key, NULL); 467 preference->RemoveWithoutPathExpansion(pref_key, NULL);
468 } 468 }
469 extension_pref_value_map()->RemoveExtensionPref( 469 extension_pref_value_map()->RemoveExtensionPref(
470 extension_id, pref_key, scope); 470 extension_id, pref_key, scope);
471 } 471 }
472 472
473 bool PreferenceAPIBase::CanExtensionControlPref( 473 bool PreferenceAPIBase::CanExtensionControlPref(
474 const std::string& extension_id, 474 const std::string& extension_id,
475 const std::string& pref_key, 475 const std::string& pref_key,
476 bool incognito) { 476 bool incognito) {
477 DCHECK(extension_prefs()->pref_service()->FindPreference(pref_key.c_str())) 477 DCHECK(extension_prefs()->pref_service()->FindPreference(pref_key))
478 << "Extension controlled preference key " << pref_key 478 << "Extension controlled preference key " << pref_key
479 << " not registered."; 479 << " not registered.";
480 480
481 return extension_pref_value_map()->CanExtensionControlPref( 481 return extension_pref_value_map()->CanExtensionControlPref(
482 extension_id, pref_key, incognito); 482 extension_id, pref_key, incognito);
483 } 483 }
484 484
485 bool PreferenceAPIBase::DoesExtensionControlPref( 485 bool PreferenceAPIBase::DoesExtensionControlPref(
486 const std::string& extension_id, 486 const std::string& extension_id,
487 const std::string& pref_key, 487 const std::string& pref_key,
488 bool* from_incognito) { 488 bool* from_incognito) {
489 DCHECK(extension_prefs()->pref_service()->FindPreference(pref_key.c_str())) 489 DCHECK(extension_prefs()->pref_service()->FindPreference(pref_key))
490 << "Extension controlled preference key " << pref_key 490 << "Extension controlled preference key " << pref_key
491 << " not registered."; 491 << " not registered.";
492 492
493 return extension_pref_value_map()->DoesExtensionControlPref( 493 return extension_pref_value_map()->DoesExtensionControlPref(
494 extension_id, pref_key, from_incognito); 494 extension_id, pref_key, from_incognito);
495 } 495 }
496 496
497 PreferenceAPI::PreferenceAPI(content::BrowserContext* context) 497 PreferenceAPI::PreferenceAPI(content::BrowserContext* context)
498 : profile_(Profile::FromBrowserContext(context)) { 498 : profile_(Profile::FromBrowserContext(context)) {
499 for (size_t i = 0; i < arraysize(kPrefMapping); ++i) { 499 for (size_t i = 0; i < arraysize(kPrefMapping); ++i) {
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
610 APIPermission::ID write_permission = APIPermission::kInvalid; 610 APIPermission::ID write_permission = APIPermission::kInvalid;
611 EXTENSION_FUNCTION_VALIDATE( 611 EXTENSION_FUNCTION_VALIDATE(
612 PrefMapping::GetInstance()->FindBrowserPrefForExtensionPref( 612 PrefMapping::GetInstance()->FindBrowserPrefForExtensionPref(
613 pref_key, &browser_pref, &read_permission, &write_permission)); 613 pref_key, &browser_pref, &read_permission, &write_permission));
614 if (!extension()->permissions_data()->HasAPIPermission(read_permission)) 614 if (!extension()->permissions_data()->HasAPIPermission(read_permission))
615 return RespondNow(Error(keys::kPermissionErrorMessage, pref_key)); 615 return RespondNow(Error(keys::kPermissionErrorMessage, pref_key));
616 616
617 Profile* profile = Profile::FromBrowserContext(browser_context()); 617 Profile* profile = Profile::FromBrowserContext(browser_context());
618 PrefService* prefs = 618 PrefService* prefs =
619 incognito ? profile->GetOffTheRecordPrefs() : profile->GetPrefs(); 619 incognito ? profile->GetOffTheRecordPrefs() : profile->GetPrefs();
620 const PrefService::Preference* pref = 620 const PrefService::Preference* pref = prefs->FindPreference(browser_pref);
621 prefs->FindPreference(browser_pref.c_str());
622 CHECK(pref); 621 CHECK(pref);
623 622
624 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue); 623 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue);
625 624
626 // Retrieve level of control. 625 // Retrieve level of control.
627 std::string level_of_control = helpers::GetLevelOfControl( 626 std::string level_of_control = helpers::GetLevelOfControl(
628 profile, extension_id(), browser_pref, incognito); 627 profile, extension_id(), browser_pref, incognito);
629 result->SetString(keys::kLevelOfControl, level_of_control); 628 result->SetString(keys::kLevelOfControl, level_of_control);
630 629
631 // Retrieve pref value. 630 // Retrieve pref value.
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
699 APIPermission::ID read_permission = APIPermission::kInvalid; 698 APIPermission::ID read_permission = APIPermission::kInvalid;
700 APIPermission::ID write_permission = APIPermission::kInvalid; 699 APIPermission::ID write_permission = APIPermission::kInvalid;
701 EXTENSION_FUNCTION_VALIDATE( 700 EXTENSION_FUNCTION_VALIDATE(
702 PrefMapping::GetInstance()->FindBrowserPrefForExtensionPref( 701 PrefMapping::GetInstance()->FindBrowserPrefForExtensionPref(
703 pref_key, &browser_pref, &read_permission, &write_permission)); 702 pref_key, &browser_pref, &read_permission, &write_permission));
704 if (!extension()->permissions_data()->HasAPIPermission(write_permission)) 703 if (!extension()->permissions_data()->HasAPIPermission(write_permission))
705 return RespondNow(Error(keys::kPermissionErrorMessage, pref_key)); 704 return RespondNow(Error(keys::kPermissionErrorMessage, pref_key));
706 705
707 ExtensionPrefs* prefs = ExtensionPrefs::Get(browser_context()); 706 ExtensionPrefs* prefs = ExtensionPrefs::Get(browser_context());
708 const PrefService::Preference* pref = 707 const PrefService::Preference* pref =
709 prefs->pref_service()->FindPreference(browser_pref.c_str()); 708 prefs->pref_service()->FindPreference(browser_pref);
710 CHECK(pref); 709 CHECK(pref);
711 710
712 // Validate new value. 711 // Validate new value.
713 PrefTransformerInterface* transformer = 712 PrefTransformerInterface* transformer =
714 PrefMapping::GetInstance()->FindTransformerForBrowserPref(browser_pref); 713 PrefMapping::GetInstance()->FindTransformerForBrowserPref(browser_pref);
715 std::string error; 714 std::string error;
716 bool bad_message = false; 715 bool bad_message = false;
717 std::unique_ptr<base::Value> browser_pref_value( 716 std::unique_ptr<base::Value> browser_pref_value(
718 transformer->ExtensionToBrowserPref(value, &error, &bad_message)); 717 transformer->ExtensionToBrowserPref(value, &error, &bad_message));
719 if (!browser_pref_value) { 718 if (!browser_pref_value) {
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
773 pref_key, &browser_pref, &read_permission, &write_permission)); 772 pref_key, &browser_pref, &read_permission, &write_permission));
774 if (!extension()->permissions_data()->HasAPIPermission(write_permission)) 773 if (!extension()->permissions_data()->HasAPIPermission(write_permission))
775 return RespondNow(Error(keys::kPermissionErrorMessage, pref_key)); 774 return RespondNow(Error(keys::kPermissionErrorMessage, pref_key));
776 775
777 PreferenceAPI::Get(browser_context()) 776 PreferenceAPI::Get(browser_context())
778 ->RemoveExtensionControlledPref(extension_id(), browser_pref, scope); 777 ->RemoveExtensionControlledPref(extension_id(), browser_pref, scope);
779 return RespondNow(NoArguments()); 778 return RespondNow(NoArguments());
780 } 779 }
781 780
782 } // namespace extensions 781 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698