Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / preference / chrome_direct_setting_api.cc
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 #include "extensions/browser/extension_system.h"
17
18 namespace extensions {
19 namespace chromedirectsetting {
20
21 const char kOnPrefChangeFormat[] =
22     "types.private.ChromeDirectSetting.%s.onChange";
23
24 class PreferenceWhitelist {
25  public:
26   PreferenceWhitelist() {
27     whitelist_.insert("googlegeolocationaccess.enabled");
28   }
29
30   ~PreferenceWhitelist() {}
31
32   bool IsPreferenceOnWhitelist(const std::string& pref_key){
33     return whitelist_.find(pref_key) != whitelist_.end();
34   }
35
36   void RegisterEventListeners(
37       Profile* profile,
38       EventRouter::Observer* observer) {
39     for (base::hash_set<std::string>::iterator iter = whitelist_.begin();
40          iter != whitelist_.end();
41          iter++) {
42       std::string event_name = base::StringPrintf(
43           kOnPrefChangeFormat,
44           (*iter).c_str());
45       ExtensionSystem::Get(profile)->event_router()->RegisterObserver(
46           observer,
47           event_name);
48     }
49   }
50
51   void RegisterPropertyListeners(
52       Profile* profile,
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();
57          iter++) {
58       const char* pref_key = (*iter).c_str();
59       std::string event_name = base::StringPrintf(
60           kOnPrefChangeFormat,
61           pref_key);
62       registrar->Add(pref_key, callback);
63     }
64   }
65
66  private:
67   base::hash_set<std::string> whitelist_;
68
69   DISALLOW_COPY_AND_ASSIGN(PreferenceWhitelist);
70 };
71
72 base::LazyInstance<PreferenceWhitelist> preference_whitelist =
73     LAZY_INSTANCE_INITIALIZER;
74
75 static base::LazyInstance<ProfileKeyedAPIFactory<ChromeDirectSettingAPI> >
76     g_factory = LAZY_INSTANCE_INITIALIZER;
77
78 ChromeDirectSettingAPI::ChromeDirectSettingAPI(Profile* profile)
79     : profile_(profile) {
80   preference_whitelist.Get().RegisterEventListeners(profile, this);
81 }
82
83 ChromeDirectSettingAPI::~ChromeDirectSettingAPI() {}
84
85 // BrowserContextKeyedService implementation.
86 void ChromeDirectSettingAPI::Shutdown() {}
87
88 // ProfileKeyedAPI implementation.
89 ProfileKeyedAPIFactory<ChromeDirectSettingAPI>*
90     ChromeDirectSettingAPI::GetFactoryInstance() {
91   return g_factory.Pointer();
92 }
93
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(
99       profile_,
100       &registrar_,
101       base::Bind(&ChromeDirectSettingAPI::OnPrefChanged,
102                  base::Unretained(this),
103                  registrar_.prefs()));
104 }
105
106 bool ChromeDirectSettingAPI::IsPreferenceOnWhitelist(
107     const std::string& pref_key) {
108   return preference_whitelist.Get().IsPreferenceOnWhitelist(pref_key);
109 }
110
111 ChromeDirectSettingAPI* ChromeDirectSettingAPI::Get(Profile* profile) {
112   return
113       ProfileKeyedAPIFactory<ChromeDirectSettingAPI>::GetForProfile(profile);
114 }
115
116 // ProfileKeyedAPI implementation.
117 const char* ChromeDirectSettingAPI::service_name() {
118   return "ChromeDirectSettingAPI";
119 }
120
121 void ChromeDirectSettingAPI::OnPrefChanged(
122     PrefService* pref_service, const std::string& pref_key) {
123   std::string event_name = base::StringPrintf(kOnPrefChangeFormat,
124                                               pref_key.c_str());
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();
130
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());
135
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());
147         }
148       }
149     }
150   }
151 }
152
153 }  // namespace chromedirectsetting
154 }  // namespace extensions