OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/extensions/api/preference/chrome_direct_setting.h" |
| 6 |
| 7 #include "base/prefs/pref_service.h" |
| 8 #include "base/values.h" |
| 9 #include "chrome/browser/extensions/api/preference/preference_api_constants.h" |
| 10 #include "chrome/browser/profiles/profile.h" |
| 11 |
| 12 namespace extensions { |
| 13 namespace chromedirectsetting { |
| 14 |
| 15 void DirectSettingFunctionBase::CheckCalledFromComponentExtension() { |
| 16 bool isComponentExtension = |
| 17 (GetExtension()->location() == Manifest::COMPONENT); |
| 18 |
| 19 // If we've made it this far and the caller isn't a component extension, |
| 20 // this means the API permissions system has failed. |
| 21 // Fatal check to prevent any damage from being done. |
| 22 CHECK(isComponentExtension); |
| 23 } |
| 24 |
| 25 PrefService* DirectSettingFunctionBase::GetPrefService() { |
| 26 return profile()->GetPrefs(); |
| 27 } |
| 28 |
| 29 bool GetDirectSettingFunction::RunImpl() { |
| 30 CheckCalledFromComponentExtension(); |
| 31 |
| 32 std::string pref_key; |
| 33 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); |
| 34 |
| 35 const PrefService::Preference* preference = |
| 36 GetPrefService()->FindPreference(pref_key.c_str()); |
| 37 const base::Value* value = preference->GetValue(); |
| 38 |
| 39 scoped_ptr<DictionaryValue> result(new DictionaryValue); |
| 40 result->Set(preference_api_constants::kValue, value->DeepCopy()); |
| 41 SetResult(result.release()); |
| 42 |
| 43 return true; |
| 44 } |
| 45 |
| 46 bool SetDirectSettingFunction::RunImpl() { |
| 47 CheckCalledFromComponentExtension(); |
| 48 |
| 49 std::string pref_key; |
| 50 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); |
| 51 |
| 52 DictionaryValue* details = NULL; |
| 53 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); |
| 54 |
| 55 Value* value = NULL; |
| 56 EXTENSION_FUNCTION_VALIDATE( |
| 57 details->Get(preference_api_constants::kValue, &value)); |
| 58 |
| 59 PrefService* prefService = GetPrefService(); |
| 60 const PrefService::Preference* preference = |
| 61 prefService->FindPreference(pref_key.c_str()); |
| 62 |
| 63 EXTENSION_FUNCTION_VALIDATE(value->GetType() == preference->GetType()); |
| 64 |
| 65 prefService->Set(pref_key.c_str(), *value); |
| 66 |
| 67 return true; |
| 68 } |
| 69 |
| 70 bool ClearDirectSettingFunction::RunImpl() { |
| 71 CheckCalledFromComponentExtension(); |
| 72 |
| 73 std::string pref_key; |
| 74 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); |
| 75 GetPrefService()->ClearPref(pref_key.c_str()); |
| 76 |
| 77 return true; |
| 78 } |
| 79 |
| 80 } // namespace chromedirectsetting |
| 81 } // namespace extensions |
| 82 |
OLD | NEW |