Update To 11.40.268.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     whitelist_.insert("spdy_proxy.enabled");
29     whitelist_.insert("data_reduction.daily_original_length");
30     whitelist_.insert("data_reduction.daily_received_length");
31     whitelist_.insert("data_reduction.update_daily_lengths");
32     whitelist_.insert("easy_unlock.proximity_required");
33   }
34
35   ~PreferenceWhitelist() {}
36
37   bool IsPreferenceOnWhitelist(const std::string& pref_key){
38     return whitelist_.find(pref_key) != whitelist_.end();
39   }
40
41   void RegisterEventListeners(
42       Profile* profile,
43       EventRouter::Observer* observer) {
44     for (base::hash_set<std::string>::iterator iter = whitelist_.begin();
45          iter != whitelist_.end();
46          iter++) {
47       std::string event_name = base::StringPrintf(
48           kOnPrefChangeFormat,
49           (*iter).c_str());
50       EventRouter::Get(profile)->RegisterObserver(observer, event_name);
51     }
52   }
53
54   void RegisterPropertyListeners(
55       Profile* profile,
56       PrefChangeRegistrar* registrar,
57       const base::Callback<void(const std::string&)>& callback) {
58     for (base::hash_set<std::string>::iterator iter = whitelist_.begin();
59          iter != whitelist_.end();
60          iter++) {
61       const char* pref_key = (*iter).c_str();
62       std::string event_name = base::StringPrintf(
63           kOnPrefChangeFormat,
64           pref_key);
65       registrar->Add(pref_key, callback);
66     }
67   }
68
69  private:
70   base::hash_set<std::string> whitelist_;
71
72   DISALLOW_COPY_AND_ASSIGN(PreferenceWhitelist);
73 };
74
75 base::LazyInstance<PreferenceWhitelist> preference_whitelist =
76     LAZY_INSTANCE_INITIALIZER;
77
78 static base::LazyInstance<
79     BrowserContextKeyedAPIFactory<ChromeDirectSettingAPI> > g_factory =
80     LAZY_INSTANCE_INITIALIZER;
81
82 ChromeDirectSettingAPI::ChromeDirectSettingAPI(content::BrowserContext* context)
83     : profile_(Profile::FromBrowserContext(context)) {
84   preference_whitelist.Get().RegisterEventListeners(profile_, this);
85 }
86
87 ChromeDirectSettingAPI::~ChromeDirectSettingAPI() {}
88
89 // KeyedService implementation.
90 void ChromeDirectSettingAPI::Shutdown() {}
91
92 // BrowserContextKeyedAPI implementation.
93 BrowserContextKeyedAPIFactory<ChromeDirectSettingAPI>*
94 ChromeDirectSettingAPI::GetFactoryInstance() {
95   return g_factory.Pointer();
96 }
97
98 // EventRouter::Observer implementation.
99 void ChromeDirectSettingAPI::OnListenerAdded(const EventListenerInfo& details) {
100   EventRouter::Get(profile_)->UnregisterObserver(this);
101   registrar_.Init(profile_->GetPrefs());
102   preference_whitelist.Get().RegisterPropertyListeners(
103       profile_,
104       &registrar_,
105       base::Bind(&ChromeDirectSettingAPI::OnPrefChanged,
106                  base::Unretained(this),
107                  registrar_.prefs()));
108 }
109
110 bool ChromeDirectSettingAPI::IsPreferenceOnWhitelist(
111     const std::string& pref_key) {
112   return preference_whitelist.Get().IsPreferenceOnWhitelist(pref_key);
113 }
114
115 ChromeDirectSettingAPI* ChromeDirectSettingAPI::Get(
116     content::BrowserContext* context) {
117   return BrowserContextKeyedAPIFactory<ChromeDirectSettingAPI>::Get(context);
118 }
119
120 // BrowserContextKeyedAPI implementation.
121 const char* ChromeDirectSettingAPI::service_name() {
122   return "ChromeDirectSettingAPI";
123 }
124
125 void ChromeDirectSettingAPI::OnPrefChanged(
126     PrefService* pref_service, const std::string& pref_key) {
127   std::string event_name = base::StringPrintf(kOnPrefChangeFormat,
128                                               pref_key.c_str());
129   EventRouter* router = EventRouter::Get(profile_);
130   if (router && router->HasEventListener(event_name)) {
131     const PrefService::Preference* preference =
132         profile_->GetPrefs()->FindPreference(pref_key.c_str());
133     const base::Value* value = preference->GetValue();
134
135     scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue);
136     result->Set(preference_api_constants::kValue, value->DeepCopy());
137     base::ListValue args;
138     args.Append(result.release());
139
140     ExtensionService* extension_service =
141         ExtensionSystem::Get(profile_)->extension_service();
142     const ExtensionSet* extensions = extension_service->extensions();
143     for (ExtensionSet::const_iterator it = extensions->begin();
144          it != extensions->end(); ++it) {
145       const std::string& extension_id = (*it)->id();
146       if (router->ExtensionHasEventListener(extension_id, event_name)) {
147         scoped_ptr<base::ListValue> args_copy(args.DeepCopy());
148         scoped_ptr<Event> event(new Event(event_name, args_copy.Pass()));
149         router->DispatchEventToExtension(extension_id, event.Pass());
150       }
151     }
152   }
153 }
154
155 }  // namespace chromedirectsetting
156 }  // namespace extensions