| 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 "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 SystemInfoEventRouter* SystemInfoEventRouter::GetInstance() { | 56 SystemInfoEventRouter* SystemInfoEventRouter::GetInstance() { |
| 57 return Singleton<SystemInfoEventRouter>::get(); | 57 return Singleton<SystemInfoEventRouter>::get(); |
| 58 } | 58 } |
| 59 | 59 |
| 60 SystemInfoEventRouter::SystemInfoEventRouter() { | 60 SystemInfoEventRouter::SystemInfoEventRouter() { |
| 61 } | 61 } |
| 62 | 62 |
| 63 SystemInfoEventRouter::~SystemInfoEventRouter() { | 63 SystemInfoEventRouter::~SystemInfoEventRouter() { |
| 64 } | 64 } |
| 65 | 65 |
| 66 void SystemInfoEventRouter::StartWatchingStorages( |
| 67 const StorageInfo& info, bool success) { |
| 68 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 69 if (!success) |
| 70 return; |
| 71 |
| 72 for (StorageInfo::const_iterator it = info.begin(); it != info.end(); ++it) { |
| 73 StorageInfoProvider::Get()->StartWatching((*it)->id); |
| 74 } |
| 75 } |
| 76 |
| 77 void SystemInfoEventRouter::StopWatchingStorages( |
| 78 const StorageInfo& info, bool success) { |
| 79 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 80 if (!success) |
| 81 return; |
| 82 |
| 83 for (StorageInfo::const_iterator it = info.begin(); it != info.end(); ++it) { |
| 84 StorageInfoProvider::Get()->StopWatching((*it)->id); |
| 85 } |
| 86 } |
| 87 |
| 66 void SystemInfoEventRouter::AddEventListener(const std::string& event_name) { | 88 void SystemInfoEventRouter::AddEventListener(const std::string& event_name) { |
| 89 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 90 |
| 67 watching_event_set_.insert(event_name); | 91 watching_event_set_.insert(event_name); |
| 68 if (watching_event_set_.count(event_name) > 1) | 92 if (watching_event_set_.count(event_name) > 1) |
| 69 return; | 93 return; |
| 70 // Start watching the |event_name| event if the first event listener arrives. | 94 // Start watching the |event_name| event if the first event listener arrives. |
| 71 // For systemInfo.storage event. | 95 // For systemInfo.storage event. |
| 72 if (IsStorageEvent(event_name)) { | 96 if (IsStorageEvent(event_name)) { |
| 73 // TODO (hongbo): Start watching storage device information via calling | 97 StorageInfoProvider::Get()->AddObserver(this); |
| 74 // SystemMonitor::StartWatchingStorage. | 98 StorageInfoProvider::Get()->StartQueryInfo( |
| 99 base::Bind(&SystemInfoEventRouter::StartWatchingStorages, |
| 100 base::Unretained(this))); |
| 75 return; | 101 return; |
| 76 } | 102 } |
| 77 | 103 |
| 78 // For systemInfo.cpu event. | 104 // For systemInfo.cpu event. |
| 79 if (IsCpuEvent(event_name)) { | 105 if (IsCpuEvent(event_name)) { |
| 80 CpuInfoProvider::Get()->StartSampling( | 106 CpuInfoProvider::Get()->StartSampling( |
| 81 base::Bind(&SystemInfoEventRouter::OnNextCpuSampling, | 107 base::Bind(&SystemInfoEventRouter::OnNextCpuSampling, |
| 82 base::Unretained(this))); | 108 base::Unretained(this))); |
| 83 return; | 109 return; |
| 84 } | 110 } |
| 85 | 111 |
| 86 // For systemInfo.display event. | 112 // For systemInfo.display event. |
| 87 if (IsDisplayEvent(event_name)) { | 113 if (IsDisplayEvent(event_name)) { |
| 88 #if defined(USE_ASH) | 114 #if defined(USE_ASH) |
| 89 ash::Shell::GetScreen()->AddObserver(this); | 115 ash::Shell::GetScreen()->AddObserver(this); |
| 90 #endif | 116 #endif |
| 91 } | 117 } |
| 92 } | 118 } |
| 93 | 119 |
| 94 void SystemInfoEventRouter::RemoveEventListener( | 120 void SystemInfoEventRouter::RemoveEventListener( |
| 95 const std::string& event_name) { | 121 const std::string& event_name) { |
| 122 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 123 |
| 96 watching_event_set_.erase(event_name); | 124 watching_event_set_.erase(event_name); |
| 97 if (watching_event_set_.count(event_name) > 0) | 125 if (watching_event_set_.count(event_name) > 0) |
| 98 return; | 126 return; |
| 99 | 127 |
| 100 // In case of the last event listener is removed, we need to stop watching | 128 // In case of the last event listener is removed, we need to stop watching |
| 101 // it to avoid unnecessary overhead. | 129 // it to avoid unnecessary overhead. |
| 102 if (IsStorageEvent(event_name)) { | 130 if (IsStorageEvent(event_name)) { |
| 103 // TODO(hongbo): Stop watching storage device information via calling | 131 StorageInfoProvider::Get()->StartQueryInfo( |
| 104 // SystemMonitor::StopWatchingStorage. | 132 base::Bind(&SystemInfoEventRouter::StopWatchingStorages, |
| 105 return; | 133 base::Unretained(this))); |
| 134 StorageInfoProvider::Get()->RemoveObserver(this); |
| 106 } | 135 } |
| 136 |
| 107 if (IsCpuEvent(event_name)) { | 137 if (IsCpuEvent(event_name)) { |
| 108 CpuInfoProvider::Get()->StopSampling(); | 138 CpuInfoProvider::Get()->StopSampling(); |
| 109 } | 139 } |
| 110 | 140 |
| 111 if (IsDisplayEvent(event_name)) { | 141 if (IsDisplayEvent(event_name)) { |
| 112 #if defined(USE_ASH) | 142 #if defined(USE_ASH) |
| 113 ash::Shell::GetScreen()->RemoveObserver(this); | 143 ash::Shell::GetScreen()->RemoveObserver(this); |
| 114 #endif | 144 #endif |
| 115 } | 145 } |
| 116 } | 146 } |
| 117 | 147 |
| 118 // static | 148 // static |
| 119 bool SystemInfoEventRouter::IsSystemInfoEvent(const std::string& event_name) { | 149 bool SystemInfoEventRouter::IsSystemInfoEvent(const std::string& event_name) { |
| 120 // TODO(hshi): simplify this once all systemInfo APIs are out of experimental. | 150 // TODO(hshi): simplify this once all systemInfo APIs are out of experimental. |
| 121 return (StartsWithASCII(event_name, kSystemInfoEventPrefix, true) || | 151 return (StartsWithASCII(event_name, kSystemInfoEventPrefix, true) || |
| 122 StartsWithASCII(event_name, kExperimentalSystemInfoEventPrefix, | 152 StartsWithASCII(event_name, kExperimentalSystemInfoEventPrefix, |
| 123 true)); | 153 true)); |
| 124 } | 154 } |
| 125 | 155 |
| 126 void SystemInfoEventRouter::OnStorageAvailableCapacityChanged( | 156 // Called on UI thread since the observer is added from UI thread. |
| 127 const std::string& id, int64 available_capacity) { | 157 void SystemInfoEventRouter::OnStorageFreeSpaceChanged( |
| 128 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 158 const std::string& id, double new_value, double old_value) { |
| 129 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 130 base::Bind(&SystemInfoEventRouter::OnStorageAvailableCapacityChanged, | |
| 131 base::Unretained(this), id, available_capacity)); | |
| 132 return; | |
| 133 } | |
| 134 | |
| 135 // We are on the UI thread now. | |
| 136 StorageChangeInfo info; | 159 StorageChangeInfo info; |
| 137 info.id = id; | 160 info.id = id; |
| 138 info.available_capacity = static_cast<double>(available_capacity); | 161 info.available_capacity = static_cast<double>(new_value); |
| 139 | 162 |
| 140 scoped_ptr<base::ListValue> args(new base::ListValue()); | 163 scoped_ptr<base::ListValue> args(new base::ListValue()); |
| 141 args->Append(info.ToValue().release()); | 164 args->Append(info.ToValue().release()); |
| 142 | 165 |
| 143 DispatchEvent(event_names::kOnStorageAvailableCapacityChanged, args.Pass()); | 166 DispatchEvent(event_names::kOnStorageAvailableCapacityChanged, args.Pass()); |
| 144 } | 167 } |
| 145 | 168 |
| 146 void SystemInfoEventRouter::OnRemovableStorageAttached(const std::string& id, | 169 void SystemInfoEventRouter::OnRemovableStorageAttached(const std::string& id, |
| 147 const string16& name, const FilePath::StringType& location) { | 170 const string16& name, const FilePath::StringType& location) { |
| 148 // TODO(hongbo): Handle storage device arrival/removal event. | 171 // TODO(hongbo): Handle storage device arrival/removal event. |
| (...skipping 28 matching lines...) Expand all Loading... |
| 177 } | 200 } |
| 178 | 201 |
| 179 void SystemInfoEventRouter::OnNextCpuSampling(scoped_ptr<CpuUpdateInfo> info) { | 202 void SystemInfoEventRouter::OnNextCpuSampling(scoped_ptr<CpuUpdateInfo> info) { |
| 180 scoped_ptr<base::ListValue> args(new base::ListValue()); | 203 scoped_ptr<base::ListValue> args(new base::ListValue()); |
| 181 args->Append(info->ToValue().release()); | 204 args->Append(info->ToValue().release()); |
| 182 | 205 |
| 183 DispatchEvent(event_names::kOnCpuUpdated, args.Pass()); | 206 DispatchEvent(event_names::kOnCpuUpdated, args.Pass()); |
| 184 } | 207 } |
| 185 | 208 |
| 186 } // namespace extensions | 209 } // namespace extensions |
| OLD | NEW |