Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/system_info_event_router.h" | 5 #include "chrome/browser/extensions/system_info_event_router.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
| 10 #include "chrome/browser/extensions/event_router_forwarder.h" | 10 #include "chrome/browser/extensions/event_router_forwarder.h" |
| 11 #include "chrome/browser/extensions/event_names.h" | 11 #include "chrome/browser/extensions/event_names.h" |
| 12 #include "chrome/browser/extensions/api/system_info_cpu/cpu_info_provider.h" | |
| 12 #include "chrome/browser/extensions/api/system_info_storage/storage_info_provide r.h" | 13 #include "chrome/browser/extensions/api/system_info_storage/storage_info_provide r.h" |
| 14 #include "chrome/common/extensions/api/experimental_system_info_cpu.h" | |
| 13 #include "chrome/common/extensions/api/experimental_system_info_storage.h" | 15 #include "chrome/common/extensions/api/experimental_system_info_storage.h" |
| 14 | 16 |
| 15 namespace extensions { | 17 namespace extensions { |
| 16 | 18 |
| 19 using api::experimental_system_info_cpu::CpuUpdateInfo; | |
| 17 using api::experimental_system_info_storage::StorageUnitInfo; | 20 using api::experimental_system_info_storage::StorageUnitInfo; |
| 18 using api::experimental_system_info_storage::StorageChangeInfo; | 21 using api::experimental_system_info_storage::StorageChangeInfo; |
| 19 using content::BrowserThread; | 22 using content::BrowserThread; |
| 20 | 23 |
| 21 const char kSystemInfoEventPrefix[] = "experimental.systemInfo"; | 24 const char kSystemInfoEventPrefix[] = "experimental.systemInfo"; |
| 22 const char kStorageEventPrefix[] = "experimental.systemInfo.storage"; | 25 const char kStorageEventPrefix[] = "experimental.systemInfo.storage"; |
| 26 const char kCpuEventPrefix[] = "experimental.systemInfo.cpu"; | |
| 23 | 27 |
| 24 // static | 28 // static |
| 25 SystemInfoEventRouter* SystemInfoEventRouter::GetInstance() { | 29 SystemInfoEventRouter* SystemInfoEventRouter::GetInstance() { |
| 26 return Singleton<SystemInfoEventRouter>::get(); | 30 return Singleton<SystemInfoEventRouter>::get(); |
| 27 } | 31 } |
| 28 | 32 |
| 29 SystemInfoEventRouter::SystemInfoEventRouter() { | 33 SystemInfoEventRouter::SystemInfoEventRouter() { |
| 30 } | 34 } |
| 31 | 35 |
| 32 SystemInfoEventRouter::~SystemInfoEventRouter() { | 36 SystemInfoEventRouter::~SystemInfoEventRouter() { |
| 33 } | 37 } |
| 34 | 38 |
| 35 void SystemInfoEventRouter::AddEventListener(const std::string& event_name) { | 39 void SystemInfoEventRouter::AddEventListener(const std::string& event_name) { |
| 36 watching_event_set_.insert(event_name); | 40 watching_event_set_.insert(event_name); |
| 37 if (watching_event_set_.count(event_name) > 1) | 41 if (watching_event_set_.count(event_name) > 1) |
| 38 return; | 42 return; |
| 39 // Start watching the |event_name| event if the first event listener arrives. | 43 // Start watching the |event_name| event if the first event listener arrives. |
| 40 std::string prefix(kStorageEventPrefix); | 44 // For systemInfo.storage event. |
| 41 if (event_name.compare(0, prefix.size(), kStorageEventPrefix) == 0) { | 45 if (event_name.compare( |
|
Mihai Parparita -not on Chrome
2012/09/16 06:23:05
To make this code easier to read (and less repetit
Hongbo Min
2012/09/16 14:07:49
Done.
| |
| 46 0, strlen(kStorageEventPrefix), kStorageEventPrefix) == 0) { | |
| 42 // TODO (hongbo): Start watching storage device information via calling | 47 // TODO (hongbo): Start watching storage device information via calling |
| 43 // SystemMonitor::StartWatchingStorage. | 48 // SystemMonitor::StartWatchingStorage. |
| 49 return; | |
| 50 } | |
| 51 | |
| 52 // For systemInfo.cpu event. | |
| 53 if (event_name.compare( | |
| 54 0, strlen(kCpuEventPrefix), kCpuEventPrefix) == 0) { | |
| 55 CpuInfoProvider::Get()->StartSampling( | |
| 56 base::Bind(&SystemInfoEventRouter::OnNextCpuSampling, | |
| 57 base::Unretained(this))); | |
| 58 return; | |
| 44 } | 59 } |
| 45 } | 60 } |
| 46 | 61 |
| 47 void SystemInfoEventRouter::RemoveEventListener( | 62 void SystemInfoEventRouter::RemoveEventListener( |
| 48 const std::string& event_name) { | 63 const std::string& event_name) { |
| 49 watching_event_set_.erase(event_name); | 64 watching_event_set_.erase(event_name); |
| 50 if (watching_event_set_.count(event_name) > 0) | 65 if (watching_event_set_.count(event_name) > 0) |
| 51 return; | 66 return; |
| 52 | 67 |
| 53 // In case of the last event listener is removed, we need to stop watching | 68 // In case of the last event listener is removed, we need to stop watching |
| 54 // it to avoid unnecessary overhead. | 69 // it to avoid unnecessary overhead. |
| 55 std::string prefix(kStorageEventPrefix); | |
| 56 if (event_name.compare( | 70 if (event_name.compare( |
| 57 0, prefix.size(), kStorageEventPrefix) == 0) { | 71 0, strlen(kStorageEventPrefix), kStorageEventPrefix) == 0) { |
| 58 // TODO(hongbo): Stop watching storage device information via calling | 72 // TODO(hongbo): Stop watching storage device information via calling |
| 59 // SystemMonitor::StopWatchingStorage. | 73 // SystemMonitor::StopWatchingStorage. |
| 60 return; | 74 return; |
| 61 } | 75 } |
| 76 if (event_name.compare( | |
| 77 0, strlen(kCpuEventPrefix), kCpuEventPrefix) == 0) { | |
| 78 CpuInfoProvider::Get()->StopSampling(); | |
|
Mihai Parparita -not on Chrome
2012/09/16 06:23:05
When quitting Chrome (or unloading an extension),
Hongbo Min
2012/09/16 14:07:49
Done.
| |
| 79 } | |
| 62 } | 80 } |
| 63 | 81 |
| 64 // static | 82 // static |
| 65 bool SystemInfoEventRouter::IsSystemInfoEvent(const std::string& event_name) { | 83 bool SystemInfoEventRouter::IsSystemInfoEvent(const std::string& event_name) { |
| 66 std::string prefix(kSystemInfoEventPrefix); | 84 std::string prefix(kSystemInfoEventPrefix); |
| 67 return event_name.compare(0, prefix.size(), prefix) == 0; | 85 return event_name.compare(0, prefix.size(), prefix) == 0; |
| 68 } | 86 } |
| 69 | 87 |
| 70 void SystemInfoEventRouter::OnStorageAvailableCapacityChanged( | 88 void SystemInfoEventRouter::OnStorageAvailableCapacityChanged( |
| 71 const std::string& id, int64 available_capacity) { | 89 const std::string& id, int64 available_capacity) { |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 95 void SystemInfoEventRouter::OnRemovableStorageDetached(const std::string& id) { | 113 void SystemInfoEventRouter::OnRemovableStorageDetached(const std::string& id) { |
| 96 // TODO(hongbo): Same as above. | 114 // TODO(hongbo): Same as above. |
| 97 } | 115 } |
| 98 | 116 |
| 99 void SystemInfoEventRouter::DispatchEvent(const std::string& event_name, | 117 void SystemInfoEventRouter::DispatchEvent(const std::string& event_name, |
| 100 scoped_ptr<base::ListValue> args) { | 118 scoped_ptr<base::ListValue> args) { |
| 101 g_browser_process->extension_event_router_forwarder()-> | 119 g_browser_process->extension_event_router_forwarder()-> |
| 102 BroadcastEventToRenderers(event_name, args.Pass(), GURL()); | 120 BroadcastEventToRenderers(event_name, args.Pass(), GURL()); |
| 103 } | 121 } |
| 104 | 122 |
| 123 void SystemInfoEventRouter::OnNextCpuSampling(scoped_ptr<CpuUpdateInfo> info) { | |
| 124 scoped_ptr<base::ListValue> args(new base::ListValue()); | |
| 125 args->Append(info->ToValue().release()); | |
| 126 | |
| 127 DispatchEvent(event_names::kOnCpuUpdated, args.Pass()); | |
| 128 } | |
| 129 | |
| 105 } // namespace extensions | 130 } // namespace extensions |
| OLD | NEW |