Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(400)

Side by Side Diff: chrome/browser/extensions/api/preference/chrome_direct_setting_api.cc

Issue 238633009: cleanup: Use EventRouter instead of ExtensionSystem::Get->event_router() (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix compile error for chromeos build. Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/extensions/api/preference/chrome_direct_setting_api.h" 5 #include "chrome/browser/extensions/api/preference/chrome_direct_setting_api.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/containers/hash_tables.h" 8 #include "base/containers/hash_tables.h"
9 #include "base/lazy_instance.h" 9 #include "base/lazy_instance.h"
10 #include "base/prefs/pref_change_registrar.h" 10 #include "base/prefs/pref_change_registrar.h"
(...skipping 24 matching lines...) Expand all
35 35
36 void RegisterEventListeners( 36 void RegisterEventListeners(
37 Profile* profile, 37 Profile* profile,
38 EventRouter::Observer* observer) { 38 EventRouter::Observer* observer) {
39 for (base::hash_set<std::string>::iterator iter = whitelist_.begin(); 39 for (base::hash_set<std::string>::iterator iter = whitelist_.begin();
40 iter != whitelist_.end(); 40 iter != whitelist_.end();
41 iter++) { 41 iter++) {
42 std::string event_name = base::StringPrintf( 42 std::string event_name = base::StringPrintf(
43 kOnPrefChangeFormat, 43 kOnPrefChangeFormat,
44 (*iter).c_str()); 44 (*iter).c_str());
45 ExtensionSystem::Get(profile)->event_router()->RegisterObserver( 45 EventRouter::Get(profile)->RegisterObserver(observer, event_name);
46 observer,
47 event_name);
48 } 46 }
49 } 47 }
50 48
51 void RegisterPropertyListeners( 49 void RegisterPropertyListeners(
52 Profile* profile, 50 Profile* profile,
53 PrefChangeRegistrar* registrar, 51 PrefChangeRegistrar* registrar,
54 const base::Callback<void(const std::string&)>& callback) { 52 const base::Callback<void(const std::string&)>& callback) {
55 for (base::hash_set<std::string>::iterator iter = whitelist_.begin(); 53 for (base::hash_set<std::string>::iterator iter = whitelist_.begin();
56 iter != whitelist_.end(); 54 iter != whitelist_.end();
57 iter++) { 55 iter++) {
(...skipping 29 matching lines...) Expand all
87 void ChromeDirectSettingAPI::Shutdown() {} 85 void ChromeDirectSettingAPI::Shutdown() {}
88 86
89 // BrowserContextKeyedAPI implementation. 87 // BrowserContextKeyedAPI implementation.
90 BrowserContextKeyedAPIFactory<ChromeDirectSettingAPI>* 88 BrowserContextKeyedAPIFactory<ChromeDirectSettingAPI>*
91 ChromeDirectSettingAPI::GetFactoryInstance() { 89 ChromeDirectSettingAPI::GetFactoryInstance() {
92 return g_factory.Pointer(); 90 return g_factory.Pointer();
93 } 91 }
94 92
95 // EventRouter::Observer implementation. 93 // EventRouter::Observer implementation.
96 void ChromeDirectSettingAPI::OnListenerAdded(const EventListenerInfo& details) { 94 void ChromeDirectSettingAPI::OnListenerAdded(const EventListenerInfo& details) {
97 ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this); 95 EventRouter::Get(profile_)->UnregisterObserver(this);
98 registrar_.Init(profile_->GetPrefs()); 96 registrar_.Init(profile_->GetPrefs());
99 preference_whitelist.Get().RegisterPropertyListeners( 97 preference_whitelist.Get().RegisterPropertyListeners(
100 profile_, 98 profile_,
101 &registrar_, 99 &registrar_,
102 base::Bind(&ChromeDirectSettingAPI::OnPrefChanged, 100 base::Bind(&ChromeDirectSettingAPI::OnPrefChanged,
103 base::Unretained(this), 101 base::Unretained(this),
104 registrar_.prefs())); 102 registrar_.prefs()));
105 } 103 }
106 104
107 bool ChromeDirectSettingAPI::IsPreferenceOnWhitelist( 105 bool ChromeDirectSettingAPI::IsPreferenceOnWhitelist(
108 const std::string& pref_key) { 106 const std::string& pref_key) {
109 return preference_whitelist.Get().IsPreferenceOnWhitelist(pref_key); 107 return preference_whitelist.Get().IsPreferenceOnWhitelist(pref_key);
110 } 108 }
111 109
112 ChromeDirectSettingAPI* ChromeDirectSettingAPI::Get( 110 ChromeDirectSettingAPI* ChromeDirectSettingAPI::Get(
113 content::BrowserContext* context) { 111 content::BrowserContext* context) {
114 return BrowserContextKeyedAPIFactory<ChromeDirectSettingAPI>::Get(context); 112 return BrowserContextKeyedAPIFactory<ChromeDirectSettingAPI>::Get(context);
115 } 113 }
116 114
117 // BrowserContextKeyedAPI implementation. 115 // BrowserContextKeyedAPI implementation.
118 const char* ChromeDirectSettingAPI::service_name() { 116 const char* ChromeDirectSettingAPI::service_name() {
119 return "ChromeDirectSettingAPI"; 117 return "ChromeDirectSettingAPI";
120 } 118 }
121 119
122 void ChromeDirectSettingAPI::OnPrefChanged( 120 void ChromeDirectSettingAPI::OnPrefChanged(
123 PrefService* pref_service, const std::string& pref_key) { 121 PrefService* pref_service, const std::string& pref_key) {
124 std::string event_name = base::StringPrintf(kOnPrefChangeFormat, 122 std::string event_name = base::StringPrintf(kOnPrefChangeFormat,
125 pref_key.c_str()); 123 pref_key.c_str());
126 EventRouter* router = ExtensionSystem::Get(profile_)->event_router(); 124 EventRouter* router = EventRouter::Get(profile_);
127 if (router && router->HasEventListener(event_name)) { 125 if (router && router->HasEventListener(event_name)) {
128 const PrefService::Preference* preference = 126 const PrefService::Preference* preference =
129 profile_->GetPrefs()->FindPreference(pref_key.c_str()); 127 profile_->GetPrefs()->FindPreference(pref_key.c_str());
130 const base::Value* value = preference->GetValue(); 128 const base::Value* value = preference->GetValue();
131 129
132 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue); 130 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue);
133 result->Set(preference_api_constants::kValue, value->DeepCopy()); 131 result->Set(preference_api_constants::kValue, value->DeepCopy());
134 base::ListValue args; 132 base::ListValue args;
135 args.Append(result.release()); 133 args.Append(result.release());
136 134
137 ExtensionService* extension_service = 135 ExtensionService* extension_service =
138 ExtensionSystem::Get(profile_)->extension_service(); 136 ExtensionSystem::Get(profile_)->extension_service();
139 const ExtensionSet* extensions = extension_service->extensions(); 137 const ExtensionSet* extensions = extension_service->extensions();
140 for (ExtensionSet::const_iterator it = extensions->begin(); 138 for (ExtensionSet::const_iterator it = extensions->begin();
141 it != extensions->end(); ++it) { 139 it != extensions->end(); ++it) {
142 if ((*it)->location() == Manifest::COMPONENT) { 140 if ((*it)->location() == Manifest::COMPONENT) {
143 std::string extension_id = (*it)->id(); 141 std::string extension_id = (*it)->id();
144 if (router->ExtensionHasEventListener(extension_id, event_name)) { 142 if (router->ExtensionHasEventListener(extension_id, event_name)) {
145 scoped_ptr<base::ListValue> args_copy(args.DeepCopy()); 143 scoped_ptr<base::ListValue> args_copy(args.DeepCopy());
146 scoped_ptr<Event> event(new Event(event_name, args_copy.Pass())); 144 scoped_ptr<Event> event(new Event(event_name, args_copy.Pass()));
147 router->DispatchEventToExtension(extension_id, event.Pass()); 145 router->DispatchEventToExtension(extension_id, event.Pass());
148 } 146 }
149 } 147 }
150 } 148 }
151 } 149 }
152 } 150 }
153 151
154 } // namespace chromedirectsetting 152 } // namespace chromedirectsetting
155 } // namespace extensions 153 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/omnibox/omnibox_api.cc ('k') | chrome/browser/extensions/api/preference/preference_api.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698