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

Side by Side Diff: chrome/browser/extensions/api/system_info/system_info_api.cc

Issue 132873008: SystemInfo API: Fix SystemInfoEventRouter initialization code. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 6 years, 11 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/system_info/system_info_api.h" 5 #include "chrome/browser/extensions/api/system_info/system_info_api.h"
6 6
7 #include <set> 7 #include <set>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/lazy_instance.h" 10 #include "base/lazy_instance.h"
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 virtual void OnDisplayAdded(const gfx::Display& new_display) OVERRIDE; 61 virtual void OnDisplayAdded(const gfx::Display& new_display) OVERRIDE;
62 virtual void OnDisplayRemoved(const gfx::Display& old_display) OVERRIDE; 62 virtual void OnDisplayRemoved(const gfx::Display& old_display) OVERRIDE;
63 63
64 // RemovableStorageObserver implementation. 64 // RemovableStorageObserver implementation.
65 virtual void OnRemovableStorageAttached(const StorageInfo& info) OVERRIDE; 65 virtual void OnRemovableStorageAttached(const StorageInfo& info) OVERRIDE;
66 virtual void OnRemovableStorageDetached(const StorageInfo& info) OVERRIDE; 66 virtual void OnRemovableStorageDetached(const StorageInfo& info) OVERRIDE;
67 67
68 // Called from any thread to dispatch the systemInfo event to all extension 68 // Called from any thread to dispatch the systemInfo event to all extension
69 // processes cross multiple profiles. 69 // processes cross multiple profiles.
70 void DispatchEvent(const std::string& event_name, 70 void DispatchEvent(const std::string& event_name,
71 scoped_ptr<base::ListValue> args); 71 scoped_ptr<base::ListValue> args);
72 72
73 // Called to dispatch the systemInfo.display.onDisplayChanged event. 73 // Called to dispatch the systemInfo.display.onDisplayChanged event.
74 void OnDisplayChanged(); 74 void OnDisplayChanged();
75 75
76 // Used to record the event names being watched. 76 // Used to record the event names being watched.
77 std::multiset<std::string> watching_event_set_; 77 std::multiset<std::string> watching_event_set_;
78 78
79 DISALLOW_COPY_AND_ASSIGN(SystemInfoEventRouter); 79 DISALLOW_COPY_AND_ASSIGN(SystemInfoEventRouter);
80 }; 80 };
81 81
82 static base::LazyInstance<SystemInfoEventRouter>::Leaky 82 static base::LazyInstance<SystemInfoEventRouter>::Leaky
83 g_system_info_event_router = LAZY_INSTANCE_INITIALIZER; 83 g_system_info_event_router = LAZY_INSTANCE_INITIALIZER;
84 84
85 // static 85 // static
86 SystemInfoEventRouter* SystemInfoEventRouter::GetInstance() { 86 SystemInfoEventRouter* SystemInfoEventRouter::GetInstance() {
87 return g_system_info_event_router.Pointer(); 87 return g_system_info_event_router.Pointer();
88 } 88 }
89 89
90 SystemInfoEventRouter::SystemInfoEventRouter() { 90 SystemInfoEventRouter::SystemInfoEventRouter() {
91 StorageMonitor::GetInstance()->AddObserver(this); 91 StorageMonitor::GetInstance()->AddObserver(this);
92 StorageMonitor::GetInstance()->EnsureInitialized(base::Closure());
93 } 92 }
94 93
95 SystemInfoEventRouter::~SystemInfoEventRouter() { 94 SystemInfoEventRouter::~SystemInfoEventRouter() {
96 if (StorageMonitor* storage_monitor = StorageMonitor::GetInstance()) 95 if (StorageMonitor* storage_monitor = StorageMonitor::GetInstance())
97 storage_monitor->RemoveObserver(this); 96 storage_monitor->RemoveObserver(this);
98 } 97 }
99 98
100 void SystemInfoEventRouter::AddEventListener(const std::string& event_name) { 99 void SystemInfoEventRouter::AddEventListener(const std::string& event_name) {
101 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 100 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
102 101
103 watching_event_set_.insert(event_name); 102 watching_event_set_.insert(event_name);
104 if (watching_event_set_.count(event_name) > 1) 103 if (watching_event_set_.count(event_name) > 1)
105 return; 104 return;
106 105
107 // For systemInfo.display event. 106 // For systemInfo.display event.
108 if (IsDisplayChangedEvent(event_name)) { 107 if (IsDisplayChangedEvent(event_name)) {
109 #if defined(USE_ASH) 108 #if defined(USE_ASH)
110 ash::Shell::GetScreen()->AddObserver(this); 109 ash::Shell::GetScreen()->AddObserver(this);
111 #endif 110 #endif
112 } 111 }
113 } 112 }
114 113
115 void SystemInfoEventRouter::RemoveEventListener( 114 void SystemInfoEventRouter::RemoveEventListener(const std::string& event_name) {
116 const std::string& event_name) {
117 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 115 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
118 116
119 std::multiset<std::string>::iterator it = 117 std::multiset<std::string>::iterator it =
120 watching_event_set_.find(event_name); 118 watching_event_set_.find(event_name);
121 if (it != watching_event_set_.end()) 119 if (it != watching_event_set_.end())
122 watching_event_set_.erase(it); 120 watching_event_set_.erase(it);
123 121
124 if (watching_event_set_.count(event_name) > 0) 122 if (watching_event_set_.count(event_name) > 0)
125 return; 123 return;
126 124
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
165 void SystemInfoEventRouter::OnDisplayChanged() { 163 void SystemInfoEventRouter::OnDisplayChanged() {
166 scoped_ptr<base::ListValue> args(new base::ListValue()); 164 scoped_ptr<base::ListValue> args(new base::ListValue());
167 DispatchEvent(system_display::OnDisplayChanged::kEventName, args.Pass()); 165 DispatchEvent(system_display::OnDisplayChanged::kEventName, args.Pass());
168 } 166 }
169 167
170 void SystemInfoEventRouter::DispatchEvent(const std::string& event_name, 168 void SystemInfoEventRouter::DispatchEvent(const std::string& event_name,
171 scoped_ptr<base::ListValue> args) { 169 scoped_ptr<base::ListValue> args) {
172 g_browser_process->extension_event_router_forwarder()-> 170 g_browser_process->extension_event_router_forwarder()->
173 BroadcastEventToRenderers(event_name, args.Pass(), GURL()); 171 BroadcastEventToRenderers(event_name, args.Pass(), GURL());
174 } 172 }
175 173
Haojian Wu 2014/01/16 06:53:27 Some performance concerns: everytime we add an sys
Lei Zhang 2014/01/16 23:24:11 See patch set 2.
174 void AddEventListener(const std::string& event_name) {
175 DCHECK(StorageMonitor::GetInstance()->IsInitialized());
176 SystemInfoEventRouter::GetInstance()->AddEventListener(event_name);
177 }
178
179 void RemoveEventListener(const std::string& event_name) {
180 DCHECK(StorageMonitor::GetInstance()->IsInitialized());
181 SystemInfoEventRouter::GetInstance()->RemoveEventListener(event_name);
182 }
183
176 } // namespace 184 } // namespace
177 185
178 static base::LazyInstance<ProfileKeyedAPIFactory<SystemInfoAPI> > 186 static base::LazyInstance<ProfileKeyedAPIFactory<SystemInfoAPI> >
179 g_factory = LAZY_INSTANCE_INITIALIZER; 187 g_factory = LAZY_INSTANCE_INITIALIZER;
180 188
181 // static 189 // static
182 ProfileKeyedAPIFactory<SystemInfoAPI>* SystemInfoAPI::GetFactoryInstance() { 190 ProfileKeyedAPIFactory<SystemInfoAPI>* SystemInfoAPI::GetFactoryInstance() {
183 return &g_factory.Get(); 191 return &g_factory.Get();
184 } 192 }
185 193
186 SystemInfoAPI::SystemInfoAPI(Profile* profile) : profile_(profile) { 194 SystemInfoAPI::SystemInfoAPI(Profile* profile) : profile_(profile) {
187 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( 195 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver(
188 this, system_storage::OnAttached::kEventName); 196 this, system_storage::OnAttached::kEventName);
189 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( 197 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver(
190 this, system_storage::OnDetached::kEventName); 198 this, system_storage::OnDetached::kEventName);
191 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( 199 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver(
192 this, system_display::OnDisplayChanged::kEventName); 200 this, system_display::OnDisplayChanged::kEventName);
193 } 201 }
194 202
195 SystemInfoAPI::~SystemInfoAPI() { 203 SystemInfoAPI::~SystemInfoAPI() {
196 } 204 }
197 205
198 void SystemInfoAPI::Shutdown() { 206 void SystemInfoAPI::Shutdown() {
199 ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this); 207 ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this);
200 } 208 }
201 209
202 void SystemInfoAPI::OnListenerAdded(const EventListenerInfo& details) { 210 void SystemInfoAPI::OnListenerAdded(const EventListenerInfo& details) {
203 SystemInfoEventRouter::GetInstance()->AddEventListener(details.event_name); 211 StorageMonitor::GetInstance()->EnsureInitialized(
212 base::Bind(&AddEventListener, details.event_name));
204 } 213 }
205 214
206 void SystemInfoAPI::OnListenerRemoved(const EventListenerInfo& details) { 215 void SystemInfoAPI::OnListenerRemoved(const EventListenerInfo& details) {
207 SystemInfoEventRouter::GetInstance()->RemoveEventListener(details.event_name); 216 StorageMonitor::GetInstance()->EnsureInitialized(
217 base::Bind(&RemoveEventListener, details.event_name));
208 } 218 }
209 219
210 } // namespace extensions 220 } // namespace extensions
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698