Home | History | Annotate | Download | only in preference
      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 
     15 namespace extensions {
     16 namespace chromedirectsetting {
     17 
     18 DirectSettingFunctionBase::DirectSettingFunctionBase() {}
     19 
     20 DirectSettingFunctionBase::~DirectSettingFunctionBase() {}
     21 
     22 PrefService* DirectSettingFunctionBase::GetPrefService() {
     23   return profile()->GetPrefs();
     24 }
     25 
     26 bool DirectSettingFunctionBase::IsCalledFromComponentExtension() {
     27   return GetExtension()->location() == Manifest::COMPONENT;
     28 }
     29 
     30 GetDirectSettingFunction::GetDirectSettingFunction() {}
     31 
     32 bool GetDirectSettingFunction::RunImpl() {
     33   EXTENSION_FUNCTION_VALIDATE(IsCalledFromComponentExtension());
     34 
     35   std::string pref_key;
     36   EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key));
     37   EXTENSION_FUNCTION_VALIDATE(
     38     ChromeDirectSettingAPI::Get(profile())->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<DictionaryValue> result(new DictionaryValue);
     46   result->Set(preference_api_constants::kValue, value->DeepCopy());
     47   SetResult(result.release());
     48 
     49   return true;
     50 }
     51 
     52 GetDirectSettingFunction::~GetDirectSettingFunction() {}
     53 
     54 SetDirectSettingFunction::SetDirectSettingFunction() {}
     55 
     56 bool SetDirectSettingFunction::RunImpl() {
     57   EXTENSION_FUNCTION_VALIDATE(IsCalledFromComponentExtension());
     58 
     59   std::string pref_key;
     60   EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key));
     61   EXTENSION_FUNCTION_VALIDATE(
     62     ChromeDirectSettingAPI::Get(profile())->IsPreferenceOnWhitelist(pref_key));
     63 
     64   DictionaryValue* details = NULL;
     65   EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &details));
     66 
     67   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 
     83 SetDirectSettingFunction::~SetDirectSettingFunction() {}
     84 
     85 ClearDirectSettingFunction::ClearDirectSettingFunction() {}
     86 
     87 bool ClearDirectSettingFunction::RunImpl() {
     88   EXTENSION_FUNCTION_VALIDATE(IsCalledFromComponentExtension());
     89 
     90   std::string pref_key;
     91   EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &pref_key));
     92   EXTENSION_FUNCTION_VALIDATE(
     93     ChromeDirectSettingAPI::Get(profile())->IsPreferenceOnWhitelist(pref_key));
     94   GetPrefService()->ClearPref(pref_key.c_str());
     95 
     96   return true;
     97 }
     98 
     99 ClearDirectSettingFunction::~ClearDirectSettingFunction() {}
    100 
    101 }  // namespace chromedirectsetting
    102 }  // namespace extensions
    103 
    104