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"
16 #include "extensions/browser/extension_system.h"
18 namespace extensions {
19 namespace chromedirectsetting {
21 const char kOnPrefChangeFormat[] =
22 "types.private.ChromeDirectSetting.%s.onChange";
24 class PreferenceWhitelist {
26 PreferenceWhitelist() {
27 whitelist_.insert("googlegeolocationaccess.enabled");
30 ~PreferenceWhitelist() {}
32 bool IsPreferenceOnWhitelist(const std::string& pref_key){
33 return whitelist_.find(pref_key) != whitelist_.end();
36 void RegisterEventListeners(
38 EventRouter::Observer* observer) {
39 for (base::hash_set<std::string>::iterator iter = whitelist_.begin();
40 iter != whitelist_.end();
42 std::string event_name = base::StringPrintf(
45 ExtensionSystem::Get(profile)->event_router()->RegisterObserver(
51 void RegisterPropertyListeners(
53 PrefChangeRegistrar* registrar,
54 const base::Callback<void(const std::string&)>& callback) {
55 for (base::hash_set<std::string>::iterator iter = whitelist_.begin();
56 iter != whitelist_.end();
58 const char* pref_key = (*iter).c_str();
59 std::string event_name = base::StringPrintf(
62 registrar->Add(pref_key, callback);
67 base::hash_set<std::string> whitelist_;
69 DISALLOW_COPY_AND_ASSIGN(PreferenceWhitelist);
72 base::LazyInstance<PreferenceWhitelist> preference_whitelist =
73 LAZY_INSTANCE_INITIALIZER;
75 static base::LazyInstance<ProfileKeyedAPIFactory<ChromeDirectSettingAPI> >
76 g_factory = LAZY_INSTANCE_INITIALIZER;
78 ChromeDirectSettingAPI::ChromeDirectSettingAPI(Profile* profile)
80 preference_whitelist.Get().RegisterEventListeners(profile, this);
83 ChromeDirectSettingAPI::~ChromeDirectSettingAPI() {}
85 // BrowserContextKeyedService implementation.
86 void ChromeDirectSettingAPI::Shutdown() {}
88 // ProfileKeyedAPI implementation.
89 ProfileKeyedAPIFactory<ChromeDirectSettingAPI>*
90 ChromeDirectSettingAPI::GetFactoryInstance() {
91 return g_factory.Pointer();
94 // EventRouter::Observer implementation.
95 void ChromeDirectSettingAPI::OnListenerAdded(const EventListenerInfo& details) {
96 ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this);
97 registrar_.Init(profile_->GetPrefs());
98 preference_whitelist.Get().RegisterPropertyListeners(
101 base::Bind(&ChromeDirectSettingAPI::OnPrefChanged,
102 base::Unretained(this),
103 registrar_.prefs()));
106 bool ChromeDirectSettingAPI::IsPreferenceOnWhitelist(
107 const std::string& pref_key) {
108 return preference_whitelist.Get().IsPreferenceOnWhitelist(pref_key);
111 ChromeDirectSettingAPI* ChromeDirectSettingAPI::Get(Profile* profile) {
113 ProfileKeyedAPIFactory<ChromeDirectSettingAPI>::GetForProfile(profile);
116 // ProfileKeyedAPI implementation.
117 const char* ChromeDirectSettingAPI::service_name() {
118 return "ChromeDirectSettingAPI";
121 void ChromeDirectSettingAPI::OnPrefChanged(
122 PrefService* pref_service, const std::string& pref_key) {
123 std::string event_name = base::StringPrintf(kOnPrefChangeFormat,
125 EventRouter* router = ExtensionSystem::Get(profile_)->event_router();
126 if (router && router->HasEventListener(event_name)) {
127 const PrefService::Preference* preference =
128 profile_->GetPrefs()->FindPreference(pref_key.c_str());
129 const base::Value* value = preference->GetValue();
131 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue);
132 result->Set(preference_api_constants::kValue, value->DeepCopy());
133 base::ListValue args;
134 args.Append(result.release());
136 ExtensionService* extension_service =
137 ExtensionSystem::Get(profile_)->extension_service();
138 const ExtensionSet* extensions = extension_service->extensions();
139 for (ExtensionSet::const_iterator it = extensions->begin();
140 it != extensions->end(); ++it) {
141 if ((*it)->location() == Manifest::COMPONENT) {
142 std::string extension_id = (*it)->id();
143 if (router->ExtensionHasEventListener(extension_id, event_name)) {
144 scoped_ptr<base::ListValue> args_copy(args.DeepCopy());
145 scoped_ptr<Event> event(new Event(event_name, args_copy.Pass()));
146 router->DispatchEventToExtension(extension_id, event.Pass());
153 } // namespace chromedirectsetting
154 } // namespace extensions