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_api.h"
      6 
      7 #include "base/bind.h"
      8 #include "base/containers/hash_tables.h"
      9 #include "base/lazy_instance.h"
     10 #include "base/prefs/pref_change_registrar.h"
     11 #include "base/prefs/pref_service.h"
     12 #include "base/strings/stringprintf.h"
     13 #include "chrome/browser/extensions/api/preference/preference_api_constants.h"
     14 #include "chrome/browser/extensions/extension_service.h"
     15 #include "chrome/browser/profiles/profile.h"
     16 
     17 namespace extensions {
     18 namespace chromedirectsetting {
     19 
     20 const char kOnPrefChangeFormat[] =
     21     "types.private.ChromeDirectSetting.%s.onChange";
     22 
     23 class PreferenceWhitelist {
     24  public:
     25   PreferenceWhitelist() {
     26     whitelist_.insert("googlegeolocationaccess.enabled");
     27   }
     28 
     29   ~PreferenceWhitelist() {}
     30 
     31   bool IsPreferenceOnWhitelist(const std::string& pref_key){
     32     return whitelist_.find(pref_key) != whitelist_.end();
     33   }
     34 
     35   void RegisterEventListeners(
     36       Profile* profile,
     37       EventRouter::Observer* observer) {
     38     for (base::hash_set<std::string>::iterator iter = whitelist_.begin();
     39          iter != whitelist_.end();
     40          iter++) {
     41       std::string event_name = base::StringPrintf(
     42           kOnPrefChangeFormat,
     43           (*iter).c_str());
     44       ExtensionSystem::Get(profile)->event_router()->RegisterObserver(
     45           observer,
     46           event_name);
     47     }
     48   }
     49 
     50   void RegisterPropertyListeners(
     51       Profile* profile,
     52       PrefChangeRegistrar* registrar,
     53       const base::Callback<void(const std::string&)>& callback) {
     54     for (base::hash_set<std::string>::iterator iter = whitelist_.begin();
     55          iter != whitelist_.end();
     56          iter++) {
     57       const char* pref_key = (*iter).c_str();
     58       std::string event_name = base::StringPrintf(
     59           kOnPrefChangeFormat,
     60           pref_key);
     61       registrar->Add(pref_key, callback);
     62     }
     63   }
     64 
     65  private:
     66   base::hash_set<std::string> whitelist_;
     67 
     68   DISALLOW_COPY_AND_ASSIGN(PreferenceWhitelist);
     69 };
     70 
     71 base::LazyInstance<PreferenceWhitelist> preference_whitelist =
     72     LAZY_INSTANCE_INITIALIZER;
     73 
     74 static base::LazyInstance<ProfileKeyedAPIFactory<ChromeDirectSettingAPI> >
     75     g_factory = LAZY_INSTANCE_INITIALIZER;
     76 
     77 ChromeDirectSettingAPI::ChromeDirectSettingAPI(Profile* profile)
     78     : profile_(profile) {
     79   preference_whitelist.Get().RegisterEventListeners(profile, this);
     80 }
     81 
     82 ChromeDirectSettingAPI::~ChromeDirectSettingAPI() {}
     83 
     84 // BrowserContextKeyedService implementation.
     85 void ChromeDirectSettingAPI::Shutdown() {}
     86 
     87 // ProfileKeyedAPI implementation.
     88 ProfileKeyedAPIFactory<ChromeDirectSettingAPI>*
     89     ChromeDirectSettingAPI::GetFactoryInstance() {
     90   return &g_factory.Get();
     91 }
     92 
     93 // EventRouter::Observer implementation.
     94 void ChromeDirectSettingAPI::OnListenerAdded(const EventListenerInfo& details) {
     95   ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this);
     96   registrar_.Init(profile_->GetPrefs());
     97   preference_whitelist.Get().RegisterPropertyListeners(
     98       profile_,
     99       &registrar_,
    100       base::Bind(&ChromeDirectSettingAPI::OnPrefChanged,
    101                  base::Unretained(this),
    102                  registrar_.prefs()));
    103 }
    104 
    105 bool ChromeDirectSettingAPI::IsPreferenceOnWhitelist(
    106     const std::string& pref_key) {
    107   return preference_whitelist.Get().IsPreferenceOnWhitelist(pref_key);
    108 }
    109 
    110 ChromeDirectSettingAPI* ChromeDirectSettingAPI::Get(Profile* profile) {
    111   return
    112       ProfileKeyedAPIFactory<ChromeDirectSettingAPI>::GetForProfile(profile);
    113 }
    114 
    115 // ProfileKeyedAPI implementation.
    116 const char* ChromeDirectSettingAPI::service_name() {
    117   return "ChromeDirectSettingAPI";
    118 }
    119 
    120 void ChromeDirectSettingAPI::OnPrefChanged(
    121     PrefService* pref_service, const std::string& pref_key) {
    122   std::string event_name = base::StringPrintf(kOnPrefChangeFormat,
    123                                               pref_key.c_str());
    124   EventRouter* router = ExtensionSystem::Get(profile_)->event_router();
    125   if (router && router->HasEventListener(event_name)) {
    126     const PrefService::Preference* preference =
    127         profile_->GetPrefs()->FindPreference(pref_key.c_str());
    128     const base::Value* value = preference->GetValue();
    129 
    130     scoped_ptr<DictionaryValue> result(new DictionaryValue);
    131     result->Set(preference_api_constants::kValue, value->DeepCopy());
    132     base::ListValue args;
    133     args.Append(result.release());
    134 
    135     ExtensionService* extension_service =
    136         ExtensionSystem::Get(profile_)->extension_service();
    137     const ExtensionSet* extensions = extension_service->extensions();
    138     for (ExtensionSet::const_iterator it = extensions->begin();
    139          it != extensions->end(); ++it) {
    140       if ((*it)->location() == Manifest::COMPONENT) {
    141         std::string extension_id = (*it)->id();
    142         if (router->ExtensionHasEventListener(extension_id, event_name)) {
    143           scoped_ptr<base::ListValue> args_copy(args.DeepCopy());
    144           scoped_ptr<Event> event(new Event(event_name, args_copy.Pass()));
    145           router->DispatchEventToExtension(extension_id, event.Pass());
    146         }
    147       }
    148     }
    149   }
    150 }
    151 
    152 }  // namespace chromedirectsetting
    153 }  // namespace extensions
    154 
    155