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.
5 #include "chrome/browser/extensions/api/preference/chrome_direct_setting_api.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"
17 namespace extensions {
18 namespace chromedirectsetting {
20 const char kOnPrefChangeFormat[] =
21 "types.private.ChromeDirectSetting.%s.onChange";
23 class PreferenceWhitelist {
25 PreferenceWhitelist() {
26 whitelist_.insert("googlegeolocationaccess.enabled");
29 ~PreferenceWhitelist() {}
31 bool IsPreferenceOnWhitelist(const std::string& pref_key){
32 return whitelist_.find(pref_key) != whitelist_.end();
35 void RegisterEventListeners(
37 EventRouter::Observer* observer) {
38 for (base::hash_set<std::string>::iterator iter = whitelist_.begin();
39 iter != whitelist_.end();
41 std::string event_name = base::StringPrintf(
44 ExtensionSystem::Get(profile)->event_router()->RegisterObserver(
50 void RegisterPropertyListeners(
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();
57 const char* pref_key = (*iter).c_str();
58 std::string event_name = base::StringPrintf(
61 registrar->Add(pref_key, callback);
66 base::hash_set<std::string> whitelist_;
68 DISALLOW_COPY_AND_ASSIGN(PreferenceWhitelist);
71 base::LazyInstance<PreferenceWhitelist> preference_whitelist =
72 LAZY_INSTANCE_INITIALIZER;
74 static base::LazyInstance<ProfileKeyedAPIFactory<ChromeDirectSettingAPI> >
75 g_factory = LAZY_INSTANCE_INITIALIZER;
77 ChromeDirectSettingAPI::ChromeDirectSettingAPI(Profile* profile)
79 preference_whitelist.Get().RegisterEventListeners(profile, this);
82 ChromeDirectSettingAPI::~ChromeDirectSettingAPI() {}
84 // BrowserContextKeyedService implementation.
85 void ChromeDirectSettingAPI::Shutdown() {}
87 // ProfileKeyedAPI implementation.
88 ProfileKeyedAPIFactory<ChromeDirectSettingAPI>*
89 ChromeDirectSettingAPI::GetFactoryInstance() {
90 return &g_factory.Get();
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(
100 base::Bind(&ChromeDirectSettingAPI::OnPrefChanged,
101 base::Unretained(this),
102 registrar_.prefs()));
105 bool ChromeDirectSettingAPI::IsPreferenceOnWhitelist(
106 const std::string& pref_key) {
107 return preference_whitelist.Get().IsPreferenceOnWhitelist(pref_key);
110 ChromeDirectSettingAPI* ChromeDirectSettingAPI::Get(Profile* profile) {
112 ProfileKeyedAPIFactory<ChromeDirectSettingAPI>::GetForProfile(profile);
115 // ProfileKeyedAPI implementation.
116 const char* ChromeDirectSettingAPI::service_name() {
117 return "ChromeDirectSettingAPI";
120 void ChromeDirectSettingAPI::OnPrefChanged(
121 PrefService* pref_service, const std::string& pref_key) {
122 std::string event_name = base::StringPrintf(kOnPrefChangeFormat,
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();
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());
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());
152 } // namespace chromedirectsetting
153 } // namespace extensions