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 |
| 24 namespace { | |
| 25 | |
| 21 const char kSystemInfoEventPrefix[] = "experimental.systemInfo"; | 26 const char kSystemInfoEventPrefix[] = "experimental.systemInfo"; |
| 22 const char kStorageEventPrefix[] = "experimental.systemInfo.storage"; | 27 const char kStorageEventPrefix[] = "experimental.systemInfo.storage"; |
| 28 const char kCpuEventPrefix[] = "experimental.systemInfo.cpu"; | |
| 29 | |
| 30 static bool IsStorageEvent(const std::string& event_name) { | |
| 31 return event_name.compare(0, strlen(kStorageEventPrefix), | |
| 32 kStorageEventPrefix) == 0; | |
| 33 } | |
| 34 | |
| 35 static bool IsCpuEvent(const std::string& event_name) { | |
| 36 return event_name.compare(0, strlen(kCpuEventPrefix), kCpuEventPrefix) == 0; | |
| 37 } | |
| 38 | |
| 39 } // namespace | |
| 23 | 40 |
| 24 // static | 41 // static |
| 25 SystemInfoEventRouter* SystemInfoEventRouter::GetInstance() { | 42 SystemInfoEventRouter* SystemInfoEventRouter::GetInstance() { |
| 26 return Singleton<SystemInfoEventRouter>::get(); | 43 return Singleton<SystemInfoEventRouter>::get(); |
| 27 } | 44 } |
| 28 | 45 |
| 29 SystemInfoEventRouter::SystemInfoEventRouter() { | 46 SystemInfoEventRouter::SystemInfoEventRouter() { |
| 30 } | 47 } |
| 31 | 48 |
| 32 SystemInfoEventRouter::~SystemInfoEventRouter() { | 49 SystemInfoEventRouter::~SystemInfoEventRouter() { |
| 33 } | 50 } |
| 34 | 51 |
| 35 void SystemInfoEventRouter::AddEventListener(const std::string& event_name) { | 52 void SystemInfoEventRouter::AddEventListener(const std::string& event_name) { |
| 36 watching_event_set_.insert(event_name); | 53 watching_event_set_.insert(event_name); |
| 37 if (watching_event_set_.count(event_name) > 1) | 54 if (watching_event_set_.count(event_name) > 1) |
| 38 return; | 55 return; |
| 39 // Start watching the |event_name| event if the first event listener arrives. | 56 // Start watching the |event_name| event if the first event listener arrives. |
| 40 std::string prefix(kStorageEventPrefix); | 57 // For systemInfo.storage event. |
| 41 if (event_name.compare(0, prefix.size(), kStorageEventPrefix) == 0) { | 58 if (IsStorageEvent(event_name)) { |
| 42 // TODO (hongbo): Start watching storage device information via calling | 59 // TODO (hongbo): Start watching storage device information via calling |
| 43 // SystemMonitor::StartWatchingStorage. | 60 // SystemMonitor::StartWatchingStorage. |
| 61 return; | |
| 62 } | |
| 63 | |
| 64 // For systemInfo.cpu event. | |
| 65 if (IsCpuEvent(event_name)) { | |
| 66 CpuInfoProvider::Get()->StartSampling( | |
| 67 base::Bind(&SystemInfoEventRouter::OnNextCpuSampling, | |
| 68 base::Unretained(this))); | |
|
benwells
2012/09/21 08:19:50
Will sampling be stopped by the RemoveEventListene
Hongbo Min
2012/09/21 09:00:23
Yes, but mihai mentioned that RemoveEventListener
| |
| 69 return; | |
| 44 } | 70 } |
| 45 } | 71 } |
| 46 | 72 |
| 47 void SystemInfoEventRouter::RemoveEventListener( | 73 void SystemInfoEventRouter::RemoveEventListener( |
| 48 const std::string& event_name) { | 74 const std::string& event_name) { |
| 49 watching_event_set_.erase(event_name); | 75 watching_event_set_.erase(event_name); |
| 50 if (watching_event_set_.count(event_name) > 0) | 76 if (watching_event_set_.count(event_name) > 0) |
| 51 return; | 77 return; |
| 52 | 78 |
| 53 // In case of the last event listener is removed, we need to stop watching | 79 // In case of the last event listener is removed, we need to stop watching |
| 54 // it to avoid unnecessary overhead. | 80 // it to avoid unnecessary overhead. |
| 55 std::string prefix(kStorageEventPrefix); | 81 if (IsStorageEvent(event_name)) { |
| 56 if (event_name.compare( | |
| 57 0, prefix.size(), kStorageEventPrefix) == 0) { | |
| 58 // TODO(hongbo): Stop watching storage device information via calling | 82 // TODO(hongbo): Stop watching storage device information via calling |
| 59 // SystemMonitor::StopWatchingStorage. | 83 // SystemMonitor::StopWatchingStorage. |
| 60 return; | 84 return; |
| 61 } | 85 } |
| 86 if (IsCpuEvent(event_name)) { | |
| 87 CpuInfoProvider::Get()->StopSampling(); | |
| 88 } | |
| 62 } | 89 } |
| 63 | 90 |
| 64 // static | 91 // static |
| 65 bool SystemInfoEventRouter::IsSystemInfoEvent(const std::string& event_name) { | 92 bool SystemInfoEventRouter::IsSystemInfoEvent(const std::string& event_name) { |
| 66 std::string prefix(kSystemInfoEventPrefix); | 93 std::string prefix(kSystemInfoEventPrefix); |
| 67 return event_name.compare(0, prefix.size(), prefix) == 0; | 94 return event_name.compare(0, prefix.size(), prefix) == 0; |
| 68 } | 95 } |
| 69 | 96 |
| 70 void SystemInfoEventRouter::OnStorageAvailableCapacityChanged( | 97 void SystemInfoEventRouter::OnStorageAvailableCapacityChanged( |
| 71 const std::string& id, int64 available_capacity) { | 98 const std::string& id, int64 available_capacity) { |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 95 void SystemInfoEventRouter::OnRemovableStorageDetached(const std::string& id) { | 122 void SystemInfoEventRouter::OnRemovableStorageDetached(const std::string& id) { |
| 96 // TODO(hongbo): Same as above. | 123 // TODO(hongbo): Same as above. |
| 97 } | 124 } |
| 98 | 125 |
| 99 void SystemInfoEventRouter::DispatchEvent(const std::string& event_name, | 126 void SystemInfoEventRouter::DispatchEvent(const std::string& event_name, |
| 100 scoped_ptr<base::ListValue> args) { | 127 scoped_ptr<base::ListValue> args) { |
| 101 g_browser_process->extension_event_router_forwarder()-> | 128 g_browser_process->extension_event_router_forwarder()-> |
| 102 BroadcastEventToRenderers(event_name, args.Pass(), GURL()); | 129 BroadcastEventToRenderers(event_name, args.Pass(), GURL()); |
| 103 } | 130 } |
| 104 | 131 |
| 132 void SystemInfoEventRouter::OnNextCpuSampling(scoped_ptr<CpuUpdateInfo> info) { | |
| 133 scoped_ptr<base::ListValue> args(new base::ListValue()); | |
| 134 args->Append(info->ToValue().release()); | |
| 135 | |
| 136 DispatchEvent(event_names::kOnCpuUpdated, args.Pass()); | |
| 137 } | |
| 138 | |
| 105 } // namespace extensions | 139 } // namespace extensions |
| OLD | NEW |