1// Copyright 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/containers/hash_tables.h" 8#include "base/lazy_instance.h" 9#include "base/prefs/pref_service.h" 10#include "base/values.h" 11#include "chrome/browser/extensions/api/preference/chrome_direct_setting_api.h" 12#include "chrome/browser/extensions/api/preference/preference_api_constants.h" 13#include "chrome/browser/profiles/profile.h" 14 15namespace extensions { 16namespace chromedirectsetting { 17 18DirectSettingFunctionBase::DirectSettingFunctionBase() {} 19 20DirectSettingFunctionBase::~DirectSettingFunctionBase() {} 21 22PrefService* DirectSettingFunctionBase::GetPrefService() { 23 return GetProfile()->GetPrefs(); 24} 25 26bool DirectSettingFunctionBase::IsCalledFromComponentExtension() { 27 return extension()->location() == Manifest::COMPONENT; 28} 29 30GetDirectSettingFunction::GetDirectSettingFunction() {} 31 32bool GetDirectSettingFunction::RunSync() { 33 EXTENSION_FUNCTION_VALIDATE(IsCalledFromComponentExtension()); 34 35 std::string pref_key; 36 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); 37 EXTENSION_FUNCTION_VALIDATE(ChromeDirectSettingAPI::Get(GetProfile()) 38 ->IsPreferenceOnWhitelist(pref_key)); 39 40 const PrefService::Preference* preference = 41 GetPrefService()->FindPreference(pref_key.c_str()); 42 EXTENSION_FUNCTION_VALIDATE(preference); 43 const base::Value* value = preference->GetValue(); 44 45 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue); 46 result->Set(preference_api_constants::kValue, value->DeepCopy()); 47 SetResult(result.release()); 48 49 return true; 50} 51 52GetDirectSettingFunction::~GetDirectSettingFunction() {} 53 54SetDirectSettingFunction::SetDirectSettingFunction() {} 55 56bool SetDirectSettingFunction::RunSync() { 57 EXTENSION_FUNCTION_VALIDATE(IsCalledFromComponentExtension()); 58 59 std::string pref_key; 60 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); 61 EXTENSION_FUNCTION_VALIDATE(ChromeDirectSettingAPI::Get(GetProfile()) 62 ->IsPreferenceOnWhitelist(pref_key)); 63 64 base::DictionaryValue* details = NULL; 65 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details)); 66 67 base::Value* value = NULL; 68 EXTENSION_FUNCTION_VALIDATE( 69 details->Get(preference_api_constants::kValue, &value)); 70 71 PrefService* pref_service = GetPrefService(); 72 const PrefService::Preference* preference = 73 pref_service->FindPreference(pref_key.c_str()); 74 EXTENSION_FUNCTION_VALIDATE(preference); 75 76 EXTENSION_FUNCTION_VALIDATE(value->GetType() == preference->GetType()); 77 78 pref_service->Set(pref_key.c_str(), *value); 79 80 return true; 81} 82 83SetDirectSettingFunction::~SetDirectSettingFunction() {} 84 85ClearDirectSettingFunction::ClearDirectSettingFunction() {} 86 87bool ClearDirectSettingFunction::RunSync() { 88 EXTENSION_FUNCTION_VALIDATE(IsCalledFromComponentExtension()); 89 90 std::string pref_key; 91 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key)); 92 EXTENSION_FUNCTION_VALIDATE(ChromeDirectSettingAPI::Get(GetProfile()) 93 ->IsPreferenceOnWhitelist(pref_key)); 94 GetPrefService()->ClearPref(pref_key.c_str()); 95 96 return true; 97} 98 99ClearDirectSettingFunction::~ClearDirectSettingFunction() {} 100 101} // namespace chromedirectsetting 102} // namespace extensions 103 104