OLD | NEW |
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/files/file_path.h" | 10 #include "base/files/file_path.h" |
11 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/memory/singleton.h" | 13 #include "base/memory/singleton.h" |
14 #include "base/string_util.h" | 14 #include "base/string_util.h" |
15 #include "base/values.h" | 15 #include "base/values.h" |
16 #include "chrome/browser/browser_process.h" | 16 #include "chrome/browser/browser_process.h" |
17 #include "chrome/browser/extensions/api/system_info_cpu/cpu_info_provider.h" | |
18 #include "chrome/browser/extensions/api/system_info_storage/storage_info_provide
r.h" | 17 #include "chrome/browser/extensions/api/system_info_storage/storage_info_provide
r.h" |
19 #include "chrome/browser/extensions/event_names.h" | 18 #include "chrome/browser/extensions/event_names.h" |
20 #include "chrome/browser/extensions/event_router_forwarder.h" | 19 #include "chrome/browser/extensions/event_router_forwarder.h" |
21 #include "chrome/common/extensions/api/experimental_system_info_storage.h" | 20 #include "chrome/common/extensions/api/experimental_system_info_storage.h" |
22 #include "chrome/common/extensions/api/system_info_cpu.h" | |
23 #include "ui/gfx/display_observer.h" | 21 #include "ui/gfx/display_observer.h" |
24 | 22 |
25 #if defined(USE_ASH) | 23 #if defined(USE_ASH) |
26 #include "ash/screen_ash.h" | 24 #include "ash/screen_ash.h" |
27 #include "ash/shell.h" | 25 #include "ash/shell.h" |
28 #endif | 26 #endif |
29 | 27 |
30 namespace extensions { | 28 namespace extensions { |
31 | 29 |
32 using api::system_info_cpu::CpuUpdateInfo; | |
33 using api::experimental_system_info_storage::StorageUnitInfo; | 30 using api::experimental_system_info_storage::StorageUnitInfo; |
34 using api::experimental_system_info_storage::StorageUnitType; | 31 using api::experimental_system_info_storage::StorageUnitType; |
35 using api::experimental_system_info_storage::StorageChangeInfo; | 32 using api::experimental_system_info_storage::StorageChangeInfo; |
36 using content::BrowserThread; | 33 using content::BrowserThread; |
37 | 34 |
38 namespace { | 35 namespace { |
39 | 36 |
40 // The display events use the "systemInfo" prefix. | 37 // The display events use the "systemInfo" prefix. |
41 const char kSystemInfoEventPrefix[] = "systemInfo"; | 38 const char kSystemInfoEventPrefix[] = "systemInfo"; |
42 // The storage and cpu events still use the "experimental.systemInfo" prefix. | 39 // The storage events still use the "experimental.systemInfo" prefix. |
43 const char kExperimentalSystemInfoEventPrefix[] = "experimental.systemInfo"; | 40 const char kExperimentalSystemInfoEventPrefix[] = "experimental.systemInfo"; |
44 | 41 |
45 bool IsDisplayChangedEvent(const std::string& event_name) { | 42 bool IsDisplayChangedEvent(const std::string& event_name) { |
46 return event_name == event_names::kOnDisplayChanged; | 43 return event_name == event_names::kOnDisplayChanged; |
47 } | 44 } |
48 | 45 |
49 bool IsAvailableCapacityChangedEvent(const std::string& event_name) { | 46 bool IsAvailableCapacityChangedEvent(const std::string& event_name) { |
50 return event_name == event_names::kOnStorageAvailableCapacityChanged; | 47 return event_name == event_names::kOnStorageAvailableCapacityChanged; |
51 } | 48 } |
52 | 49 |
53 bool IsCpuUpdatedEvent(const std::string& event_name) { | |
54 return event_name == event_names::kOnCpuUpdated; | |
55 } | |
56 | |
57 // Event router for systemInfo API. It is a singleton instance shared by | 50 // Event router for systemInfo API. It is a singleton instance shared by |
58 // multiple profiles. | 51 // multiple profiles. |
59 class SystemInfoEventRouter | 52 class SystemInfoEventRouter |
60 : public gfx::DisplayObserver, public StorageInfoObserver { | 53 : public gfx::DisplayObserver, public StorageInfoObserver { |
61 public: | 54 public: |
62 static SystemInfoEventRouter* GetInstance(); | 55 static SystemInfoEventRouter* GetInstance(); |
63 | 56 |
64 SystemInfoEventRouter(); | 57 SystemInfoEventRouter(); |
65 virtual ~SystemInfoEventRouter(); | 58 virtual ~SystemInfoEventRouter(); |
66 | 59 |
(...skipping 24 matching lines...) Expand all Loading... |
91 // Called from any thread to dispatch the systemInfo event to all extension | 84 // Called from any thread to dispatch the systemInfo event to all extension |
92 // processes cross multiple profiles. | 85 // processes cross multiple profiles. |
93 void DispatchEvent(const std::string& event_name, | 86 void DispatchEvent(const std::string& event_name, |
94 scoped_ptr<base::ListValue> args); | 87 scoped_ptr<base::ListValue> args); |
95 | 88 |
96 // The callbacks of querying storage info to start and stop watching the | 89 // The callbacks of querying storage info to start and stop watching the |
97 // storages. Called from UI thread. | 90 // storages. Called from UI thread. |
98 void StartWatchingStorages(const StorageInfo& info, bool success); | 91 void StartWatchingStorages(const StorageInfo& info, bool success); |
99 void StopWatchingStorages(const StorageInfo& info, bool success); | 92 void StopWatchingStorages(const StorageInfo& info, bool success); |
100 | 93 |
101 // The callback for CPU sampling cycle. Called from FILE thread. | |
102 void OnNextCpuSampling( | |
103 scoped_ptr<api::system_info_cpu::CpuUpdateInfo> info); | |
104 | |
105 // Called to dispatch the systemInfo.display.onDisplayChanged event. | 94 // Called to dispatch the systemInfo.display.onDisplayChanged event. |
106 void OnDisplayChanged(); | 95 void OnDisplayChanged(); |
107 | 96 |
108 // Used to record the event names being watched. | 97 // Used to record the event names being watched. |
109 std::multiset<std::string> watching_event_set_; | 98 std::multiset<std::string> watching_event_set_; |
110 | 99 |
111 DISALLOW_COPY_AND_ASSIGN(SystemInfoEventRouter); | 100 DISALLOW_COPY_AND_ASSIGN(SystemInfoEventRouter); |
112 }; | 101 }; |
113 | 102 |
114 static base::LazyInstance<SystemInfoEventRouter>::Leaky | 103 static base::LazyInstance<SystemInfoEventRouter>::Leaky |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 | 147 |
159 // Start watching the |event_name| event if the first event listener arrives. | 148 // Start watching the |event_name| event if the first event listener arrives. |
160 // For systemInfo.storage event. | 149 // For systemInfo.storage event. |
161 if (IsAvailableCapacityChangedEvent(event_name)) { | 150 if (IsAvailableCapacityChangedEvent(event_name)) { |
162 StorageInfoProvider::Get()->StartQueryInfo( | 151 StorageInfoProvider::Get()->StartQueryInfo( |
163 base::Bind(&SystemInfoEventRouter::StartWatchingStorages, | 152 base::Bind(&SystemInfoEventRouter::StartWatchingStorages, |
164 base::Unretained(this))); | 153 base::Unretained(this))); |
165 return; | 154 return; |
166 } | 155 } |
167 | 156 |
168 // For systemInfo.cpu event. | |
169 if (IsCpuUpdatedEvent(event_name)) { | |
170 CpuInfoProvider::Get()->StartSampling( | |
171 base::Bind(&SystemInfoEventRouter::OnNextCpuSampling, | |
172 base::Unretained(this))); | |
173 return; | |
174 } | |
175 | |
176 // For systemInfo.display event. | 157 // For systemInfo.display event. |
177 if (IsDisplayChangedEvent(event_name)) { | 158 if (IsDisplayChangedEvent(event_name)) { |
178 #if defined(USE_ASH) | 159 #if defined(USE_ASH) |
179 ash::Shell::GetScreen()->AddObserver(this); | 160 ash::Shell::GetScreen()->AddObserver(this); |
180 #endif | 161 #endif |
181 } | 162 } |
182 } | 163 } |
183 | 164 |
184 void SystemInfoEventRouter::RemoveEventListener( | 165 void SystemInfoEventRouter::RemoveEventListener( |
185 const std::string& event_name) { | 166 const std::string& event_name) { |
186 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 167 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
187 | 168 |
188 std::multiset<std::string>::iterator it = | 169 std::multiset<std::string>::iterator it = |
189 watching_event_set_.find(event_name); | 170 watching_event_set_.find(event_name); |
190 if (it != watching_event_set_.end()) | 171 if (it != watching_event_set_.end()) |
191 watching_event_set_.erase(it); | 172 watching_event_set_.erase(it); |
192 | 173 |
193 if (watching_event_set_.count(event_name) > 0) | 174 if (watching_event_set_.count(event_name) > 0) |
194 return; | 175 return; |
195 | 176 |
196 // In case of the last event listener is removed, we need to stop watching | 177 // In case of the last event listener is removed, we need to stop watching |
197 // it to avoid unnecessary overhead. | 178 // it to avoid unnecessary overhead. |
198 if (IsAvailableCapacityChangedEvent(event_name)) { | 179 if (IsAvailableCapacityChangedEvent(event_name)) { |
199 StorageInfoProvider::Get()->StartQueryInfo( | 180 StorageInfoProvider::Get()->StartQueryInfo( |
200 base::Bind(&SystemInfoEventRouter::StopWatchingStorages, | 181 base::Bind(&SystemInfoEventRouter::StopWatchingStorages, |
201 base::Unretained(this))); | 182 base::Unretained(this))); |
202 } | 183 } |
203 | 184 |
204 if (IsCpuUpdatedEvent(event_name)) { | |
205 CpuInfoProvider::Get()->StopSampling(); | |
206 } | |
207 | |
208 if (IsDisplayChangedEvent(event_name)) { | 185 if (IsDisplayChangedEvent(event_name)) { |
209 #if defined(USE_ASH) | 186 #if defined(USE_ASH) |
210 ash::Shell::GetScreen()->RemoveObserver(this); | 187 ash::Shell::GetScreen()->RemoveObserver(this); |
211 #endif | 188 #endif |
212 } | 189 } |
213 } | 190 } |
214 | 191 |
215 // static | 192 // static |
216 bool SystemInfoEventRouter::IsSystemInfoEvent(const std::string& event_name) { | 193 bool SystemInfoEventRouter::IsSystemInfoEvent(const std::string& event_name) { |
217 // TODO(hshi): simplify this once all systemInfo APIs are out of experimental. | 194 // TODO(hshi): simplify this once all systemInfo APIs are out of experimental. |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
273 scoped_ptr<base::ListValue> args(new base::ListValue()); | 250 scoped_ptr<base::ListValue> args(new base::ListValue()); |
274 DispatchEvent(event_names::kOnDisplayChanged, args.Pass()); | 251 DispatchEvent(event_names::kOnDisplayChanged, args.Pass()); |
275 } | 252 } |
276 | 253 |
277 void SystemInfoEventRouter::DispatchEvent(const std::string& event_name, | 254 void SystemInfoEventRouter::DispatchEvent(const std::string& event_name, |
278 scoped_ptr<base::ListValue> args) { | 255 scoped_ptr<base::ListValue> args) { |
279 g_browser_process->extension_event_router_forwarder()-> | 256 g_browser_process->extension_event_router_forwarder()-> |
280 BroadcastEventToRenderers(event_name, args.Pass(), GURL()); | 257 BroadcastEventToRenderers(event_name, args.Pass(), GURL()); |
281 } | 258 } |
282 | 259 |
283 void SystemInfoEventRouter::OnNextCpuSampling(scoped_ptr<CpuUpdateInfo> info) { | |
284 scoped_ptr<base::ListValue> args(new base::ListValue()); | |
285 args->Append(info->ToValue().release()); | |
286 | |
287 DispatchEvent(event_names::kOnCpuUpdated, args.Pass()); | |
288 } | |
289 | |
290 } // namespace | 260 } // namespace |
291 | 261 |
292 static base::LazyInstance<ProfileKeyedAPIFactory<SystemInfoAPI> > | 262 static base::LazyInstance<ProfileKeyedAPIFactory<SystemInfoAPI> > |
293 g_factory = LAZY_INSTANCE_INITIALIZER; | 263 g_factory = LAZY_INSTANCE_INITIALIZER; |
294 | 264 |
295 // static | 265 // static |
296 ProfileKeyedAPIFactory<SystemInfoAPI>* SystemInfoAPI::GetFactoryInstance() { | 266 ProfileKeyedAPIFactory<SystemInfoAPI>* SystemInfoAPI::GetFactoryInstance() { |
297 return &g_factory.Get(); | 267 return &g_factory.Get(); |
298 } | 268 } |
299 | 269 |
300 SystemInfoAPI::SystemInfoAPI(Profile* profile) : profile_(profile) { | 270 SystemInfoAPI::SystemInfoAPI(Profile* profile) : profile_(profile) { |
301 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( | 271 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( |
302 this, event_names::kOnCpuUpdated); | |
303 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( | |
304 this, event_names::kOnStorageAvailableCapacityChanged); | 272 this, event_names::kOnStorageAvailableCapacityChanged); |
305 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( | 273 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( |
306 this, event_names::kOnStorageAttached); | 274 this, event_names::kOnStorageAttached); |
307 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( | 275 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( |
308 this, event_names::kOnStorageDetached); | 276 this, event_names::kOnStorageDetached); |
309 } | 277 } |
310 | 278 |
311 SystemInfoAPI::~SystemInfoAPI() { | 279 SystemInfoAPI::~SystemInfoAPI() { |
312 } | 280 } |
313 | 281 |
314 void SystemInfoAPI::Shutdown() { | 282 void SystemInfoAPI::Shutdown() { |
315 ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this); | 283 ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this); |
316 } | 284 } |
317 | 285 |
318 void SystemInfoAPI::OnListenerAdded(const EventListenerInfo& details) { | 286 void SystemInfoAPI::OnListenerAdded(const EventListenerInfo& details) { |
319 SystemInfoEventRouter::GetInstance()->AddEventListener(details.event_name); | 287 SystemInfoEventRouter::GetInstance()->AddEventListener(details.event_name); |
320 } | 288 } |
321 | 289 |
322 void SystemInfoAPI::OnListenerRemoved(const EventListenerInfo& details) { | 290 void SystemInfoAPI::OnListenerRemoved(const EventListenerInfo& details) { |
323 SystemInfoEventRouter::GetInstance()->RemoveEventListener(details.event_name); | 291 SystemInfoEventRouter::GetInstance()->RemoveEventListener(details.event_name); |
324 } | 292 } |
325 | 293 |
326 } // namespace extensions | 294 } // namespace extensions |
OLD | NEW |