Chromium Code Reviews| Index: chrome/browser/extensions/api/preference/chrome_direct_setting.cc | 
| diff --git a/chrome/browser/extensions/api/preference/chrome_direct_setting.cc b/chrome/browser/extensions/api/preference/chrome_direct_setting.cc | 
| new file mode 100644 | 
| index 0000000000000000000000000000000000000000..2444dcf84b60de49b5a2efc27ce2647133460cba | 
| --- /dev/null | 
| +++ b/chrome/browser/extensions/api/preference/chrome_direct_setting.cc | 
| @@ -0,0 +1,82 @@ | 
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. | 
| +// Use of this source code is governed by a BSD-style license that can be | 
| +// found in the LICENSE file. | 
| + | 
| +#include "chrome/browser/extensions/api/preference/chrome_direct_setting.h" | 
| + | 
| +#include "base/prefs/pref_service.h" | 
| +#include "base/values.h" | 
| +#include "chrome/browser/extensions/api/preference/preference_api_constants.h" | 
| +#include "chrome/browser/profiles/profile.h" | 
| + | 
| +namespace extensions { | 
| +namespace chromedirectsetting { | 
| + | 
| +void DirectSettingFunctionBase::CheckCalledFromComponentExtension() { | 
| + bool isComponentExtension = | 
| + (GetExtension()->location() == Manifest::COMPONENT); | 
| + | 
| + // If we've made it this far and the caller isn't a component extension, | 
| + // this means the API permissions system has failed. | 
| 
 
Bernhard Bauer
2013/07/09 07:34:32
Is the API permissions system enforced on the brow
 
robliao
2013/07/09 17:27:45
The renderer performs the API check, so compromise
 
 | 
| + // Fatal check to prevent any damage from being done. | 
| + CHECK(isComponentExtension); | 
| +} | 
| + | 
| +PrefService* DirectSettingFunctionBase::GetPrefService() { | 
| + return profile()->GetPrefs(); | 
| +} | 
| + | 
| +bool GetDirectSettingFunction::RunImpl() { | 
| + CheckCalledFromComponentExtension(); | 
| + | 
| + std::string pref_key; | 
| + EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); | 
| + | 
| + const PrefService::Preference* preference = | 
| + GetPrefService()->FindPreference(pref_key.c_str()); | 
| 
 
Bernhard Bauer
2013/07/09 17:58:27
I'm also worried about directly using the passed i
 
robliao
2013/07/09 18:15:53
Isn't the JSON API declaration sufficient for limi
 
Bernhard Bauer
2013/07/09 18:45:41
Sorry, no :-(
The JSON API here is really just us
 
robliao
2013/07/09 18:53:38
I'm going to think about the best way for getting
 
 | 
| + const base::Value* value = preference->GetValue(); | 
| 
 
Bernhard Bauer
2013/07/09 07:34:32
This will crash if the pref_key isn't found. Can y
 
robliao
2013/07/09 17:27:45
This was a deliberate decision for the following r
 
Bernhard Bauer
2013/07/09 17:58:27
The preference API checks with a EXTENSION_FUNCTIO
 
robliao
2013/07/09 18:15:53
Take a look at
https://code.google.com/p/chromium/
 
Bernhard Bauer
2013/07/09 18:45:41
Thanks!
This API function won't touch ExtensionPr
 
robliao
2013/07/09 18:53:38
That's correct. Part of the impetus of this change
 
 | 
| + | 
| + scoped_ptr<DictionaryValue> result(new DictionaryValue); | 
| + result->Set(preference_api_constants::kValue, value->DeepCopy()); | 
| + SetResult(result.release()); | 
| + | 
| + return true; | 
| +} | 
| + | 
| +bool SetDirectSettingFunction::RunImpl() { | 
| + CheckCalledFromComponentExtension(); | 
| + | 
| + std::string pref_key; | 
| + EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); | 
| + | 
| + DictionaryValue* details = NULL; | 
| + EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); | 
| + | 
| + Value* value = NULL; | 
| + EXTENSION_FUNCTION_VALIDATE( | 
| + details->Get(preference_api_constants::kValue, &value)); | 
| + | 
| + PrefService* prefService = GetPrefService(); | 
| 
 
Bernhard Bauer
2013/07/09 07:34:32
Local variables are named unix_hacker_style.
 
robliao
2013/07/09 17:27:45
Habits from a previous life. Fixed.
On 2013/07/09
 
 | 
| + const PrefService::Preference* preference = | 
| + prefService->FindPreference(pref_key.c_str()); | 
| + | 
| + EXTENSION_FUNCTION_VALIDATE(value->GetType() == preference->GetType()); | 
| + | 
| + prefService->Set(pref_key.c_str(), *value); | 
| + | 
| + return true; | 
| +} | 
| + | 
| +bool ClearDirectSettingFunction::RunImpl() { | 
| + CheckCalledFromComponentExtension(); | 
| + | 
| + std::string pref_key; | 
| + EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); | 
| + GetPrefService()->ClearPref(pref_key.c_str()); | 
| + | 
| + return true; | 
| +} | 
| + | 
| +} // namespace chromedirectsetting | 
| +} // namespace extensions | 
| + |