Chromium Code Reviews| 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/sys_info.h" | |
| 16 #include "base/task_runner_util.h" | |
| 15 #include "base/values.h" | 17 #include "base/values.h" |
| 16 #include "chrome/browser/browser_process.h" | 18 #include "chrome/browser/browser_process.h" |
| 17 #include "chrome/browser/extensions/api/system_info_cpu/cpu_info_provider.h" | 19 #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" | 20 #include "chrome/browser/extensions/api/system_info_storage/storage_info_provide r.h" |
| 19 #include "chrome/browser/extensions/event_names.h" | 21 #include "chrome/browser/extensions/event_names.h" |
| 20 #include "chrome/browser/extensions/event_router_forwarder.h" | 22 #include "chrome/browser/extensions/event_router_forwarder.h" |
| 23 #include "chrome/browser/storage_monitor/removable_storage_observer.h" | |
| 24 #include "chrome/browser/storage_monitor/storage_info.h" | |
| 25 #include "chrome/browser/storage_monitor/storage_monitor.h" | |
| 21 #include "chrome/common/extensions/api/experimental_system_info_cpu.h" | 26 #include "chrome/common/extensions/api/experimental_system_info_cpu.h" |
| 22 #include "chrome/common/extensions/api/experimental_system_info_storage.h" | 27 #include "chrome/common/extensions/api/experimental_system_info_storage.h" |
| 23 #include "ui/gfx/display_observer.h" | 28 #include "ui/gfx/display_observer.h" |
| 24 | 29 |
| 25 #if defined(USE_ASH) | 30 #if defined(USE_ASH) |
| 26 #include "ash/screen_ash.h" | 31 #include "ash/screen_ash.h" |
| 27 #include "ash/shell.h" | 32 #include "ash/shell.h" |
| 28 #endif | 33 #endif |
| 29 | 34 |
| 30 namespace extensions { | 35 namespace extensions { |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 49 bool IsAvailableCapacityChangedEvent(const std::string& event_name) { | 54 bool IsAvailableCapacityChangedEvent(const std::string& event_name) { |
| 50 return event_name == event_names::kOnStorageAvailableCapacityChanged; | 55 return event_name == event_names::kOnStorageAvailableCapacityChanged; |
| 51 } | 56 } |
| 52 | 57 |
| 53 bool IsCpuUpdatedEvent(const std::string& event_name) { | 58 bool IsCpuUpdatedEvent(const std::string& event_name) { |
| 54 return event_name == event_names::kOnCpuUpdated; | 59 return event_name == event_names::kOnCpuUpdated; |
| 55 } | 60 } |
| 56 | 61 |
| 57 // Event router for systemInfo API. It is a singleton instance shared by | 62 // Event router for systemInfo API. It is a singleton instance shared by |
| 58 // multiple profiles. | 63 // multiple profiles. |
| 59 class SystemInfoEventRouter | 64 class SystemInfoEventRouter : public gfx::DisplayObserver, |
| 60 : public gfx::DisplayObserver, public StorageInfoObserver { | 65 public chrome::RemovableStorageObserver, |
| 66 public StorageFreeSpaceObserver { | |
| 61 public: | 67 public: |
| 62 static SystemInfoEventRouter* GetInstance(); | 68 static SystemInfoEventRouter* GetInstance(); |
| 63 | 69 |
| 64 SystemInfoEventRouter(); | 70 SystemInfoEventRouter(); |
| 65 virtual ~SystemInfoEventRouter(); | 71 virtual ~SystemInfoEventRouter(); |
| 66 | 72 |
| 67 // Add/remove event listener for the |event_name| event. | 73 // Add/remove event listener for the |event_name| event. |
| 68 void AddEventListener(const std::string& event_name); | 74 void AddEventListener(const std::string& event_name); |
| 69 void RemoveEventListener(const std::string& event_name); | 75 void RemoveEventListener(const std::string& event_name); |
| 70 | 76 |
| 71 // Return true if the |event_name| is an event from systemInfo namespace. | 77 // Return true if the |event_name| is an event from systemInfo namespace. |
| 72 static bool IsSystemInfoEvent(const std::string& event_name); | 78 static bool IsSystemInfoEvent(const std::string& event_name); |
| 73 | 79 |
| 74 private: | 80 private: |
| 75 // StorageInfoObserver: | |
| 76 virtual void OnStorageFreeSpaceChanged(const std::string& id, | |
| 77 double new_value, | |
| 78 double old_value) OVERRIDE; | |
| 79 virtual void OnStorageAttached( | |
| 80 const std::string& id, | |
| 81 api::experimental_system_info_storage::StorageUnitType type, | |
| 82 double capacity, | |
| 83 double available_capacity) OVERRIDE; | |
| 84 virtual void OnStorageDetached(const std::string& id) OVERRIDE; | |
| 85 | |
| 86 // gfx::DisplayObserver: | 81 // gfx::DisplayObserver: |
| 87 virtual void OnDisplayBoundsChanged(const gfx::Display& display) OVERRIDE; | 82 virtual void OnDisplayBoundsChanged(const gfx::Display& display) OVERRIDE; |
| 88 virtual void OnDisplayAdded(const gfx::Display& new_display) OVERRIDE; | 83 virtual void OnDisplayAdded(const gfx::Display& new_display) OVERRIDE; |
| 89 virtual void OnDisplayRemoved(const gfx::Display& old_display) OVERRIDE; | 84 virtual void OnDisplayRemoved(const gfx::Display& old_display) OVERRIDE; |
| 90 | 85 |
| 86 // chrome::RemovableStorageObserver implementation. | |
| 87 virtual void OnRemovableStorageAttached( | |
| 88 const chrome::StorageInfo& info) OVERRIDE; | |
| 89 virtual void OnRemovableStorageDetached( | |
| 90 const chrome::StorageInfo& info) OVERRIDE; | |
| 91 | |
| 92 // StorageFreeSpaceObserver implementation. | |
| 93 virtual void OnFreeSpaceChanged(const std::string& id, | |
| 94 double new_value, | |
| 95 double old_value) OVERRIDE; | |
| 96 | |
| 97 void DispatchStorageAttachedEvent(const chrome::StorageInfo& info, | |
| 98 int64 avail_bytes); | |
| 99 | |
| 91 // Called from any thread to dispatch the systemInfo event to all extension | 100 // Called from any thread to dispatch the systemInfo event to all extension |
| 92 // processes cross multiple profiles. | 101 // processes cross multiple profiles. |
| 93 void DispatchEvent(const std::string& event_name, | 102 void DispatchEvent(const std::string& event_name, |
| 94 scoped_ptr<base::ListValue> args); | 103 scoped_ptr<base::ListValue> args); |
| 95 | 104 |
| 96 // The callbacks of querying storage info to start and stop watching the | 105 // The callbacks of querying storage info to start and stop watching the |
| 97 // storages. Called from UI thread. | 106 // storages. Called from UI thread. |
| 98 void StartWatchingStorages(const StorageInfo& info, bool success); | 107 void StartWatchingStorages(const StorageUnitList& info, bool success); |
| 99 void StopWatchingStorages(const StorageInfo& info, bool success); | 108 void StopWatchingStorages(const StorageUnitList& info, bool success); |
| 100 | 109 |
| 101 // The callback for CPU sampling cycle. Called from FILE thread. | 110 // The callback for CPU sampling cycle. Called from FILE thread. |
| 102 void OnNextCpuSampling( | 111 void OnNextCpuSampling( |
| 103 scoped_ptr<api::experimental_system_info_cpu::CpuUpdateInfo> info); | 112 scoped_ptr<api::experimental_system_info_cpu::CpuUpdateInfo> info); |
| 104 | 113 |
| 105 // Called to dispatch the systemInfo.display.onDisplayChanged event. | 114 // Called to dispatch the systemInfo.display.onDisplayChanged event. |
| 106 void OnDisplayChanged(); | 115 void OnDisplayChanged(); |
| 107 | 116 |
| 108 // Used to record the event names being watched. | 117 // Used to record the event names being watched. |
| 109 std::multiset<std::string> watching_event_set_; | 118 std::multiset<std::string> watching_event_set_; |
| 110 | 119 |
| 111 DISALLOW_COPY_AND_ASSIGN(SystemInfoEventRouter); | 120 DISALLOW_COPY_AND_ASSIGN(SystemInfoEventRouter); |
| 112 }; | 121 }; |
| 113 | 122 |
| 114 static base::LazyInstance<SystemInfoEventRouter>::Leaky | 123 static base::LazyInstance<SystemInfoEventRouter>::Leaky |
| 115 g_system_info_event_router = LAZY_INSTANCE_INITIALIZER; | 124 g_system_info_event_router = LAZY_INSTANCE_INITIALIZER; |
| 116 | 125 |
| 117 // static | 126 // static |
| 118 SystemInfoEventRouter* SystemInfoEventRouter::GetInstance() { | 127 SystemInfoEventRouter* SystemInfoEventRouter::GetInstance() { |
| 119 return g_system_info_event_router.Pointer(); | 128 return g_system_info_event_router.Pointer(); |
| 120 } | 129 } |
| 121 | 130 |
| 122 SystemInfoEventRouter::SystemInfoEventRouter() { | 131 SystemInfoEventRouter::SystemInfoEventRouter() { |
| 132 DCHECK(chrome::StorageMonitor::GetInstance()); | |
| 133 chrome::StorageMonitor::GetInstance()->AddObserver(this); | |
| 123 StorageInfoProvider::Get()->AddObserver(this); | 134 StorageInfoProvider::Get()->AddObserver(this); |
| 124 } | 135 } |
| 125 | 136 |
| 126 SystemInfoEventRouter::~SystemInfoEventRouter() { | 137 SystemInfoEventRouter::~SystemInfoEventRouter() { |
| 127 StorageInfoProvider::Get()->RemoveObserver(this); | 138 StorageInfoProvider::Get()->RemoveObserver(this); |
| 139 if (chrome::StorageMonitor::GetInstance()) | |
| 140 chrome::StorageMonitor::GetInstance()->RemoveObserver(this); | |
| 128 } | 141 } |
| 129 | 142 |
| 130 void SystemInfoEventRouter::StartWatchingStorages( | 143 void SystemInfoEventRouter::StartWatchingStorages( |
| 131 const StorageInfo& info, bool success) { | 144 const StorageUnitList& info, bool success) { |
| 132 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 145 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 133 if (!success) | 146 if (!success) |
| 134 return; | 147 return; |
| 135 | 148 |
| 136 for (StorageInfo::const_iterator it = info.begin(); it != info.end(); ++it) { | 149 for (StorageUnitList::const_iterator it = info.begin(); |
| 150 it != info.end(); ++it) { | |
| 137 StorageInfoProvider::Get()->StartWatching((*it)->id); | 151 StorageInfoProvider::Get()->StartWatching((*it)->id); |
| 138 } | 152 } |
| 139 } | 153 } |
| 140 | 154 |
| 141 void SystemInfoEventRouter::StopWatchingStorages( | 155 void SystemInfoEventRouter::StopWatchingStorages( |
| 142 const StorageInfo& info, bool success) { | 156 const StorageUnitList& info, bool success) { |
| 143 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 157 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 144 if (!success) | 158 if (!success) |
| 145 return; | 159 return; |
| 146 | 160 |
| 147 for (StorageInfo::const_iterator it = info.begin(); it != info.end(); ++it) { | 161 for (StorageUnitList::const_iterator it = info.begin(); |
| 162 it != info.end(); ++it) { | |
| 148 StorageInfoProvider::Get()->StopWatching((*it)->id); | 163 StorageInfoProvider::Get()->StopWatching((*it)->id); |
| 149 } | 164 } |
| 150 } | 165 } |
| 151 | 166 |
| 152 void SystemInfoEventRouter::AddEventListener(const std::string& event_name) { | 167 void SystemInfoEventRouter::AddEventListener(const std::string& event_name) { |
| 153 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 168 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 154 | 169 |
| 155 watching_event_set_.insert(event_name); | 170 watching_event_set_.insert(event_name); |
| 156 if (watching_event_set_.count(event_name) > 1) | 171 if (watching_event_set_.count(event_name) > 1) |
| 157 return; | 172 return; |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 213 } | 228 } |
| 214 | 229 |
| 215 // static | 230 // static |
| 216 bool SystemInfoEventRouter::IsSystemInfoEvent(const std::string& event_name) { | 231 bool SystemInfoEventRouter::IsSystemInfoEvent(const std::string& event_name) { |
| 217 // TODO(hshi): simplify this once all systemInfo APIs are out of experimental. | 232 // TODO(hshi): simplify this once all systemInfo APIs are out of experimental. |
| 218 return (StartsWithASCII(event_name, kSystemInfoEventPrefix, true) || | 233 return (StartsWithASCII(event_name, kSystemInfoEventPrefix, true) || |
| 219 StartsWithASCII(event_name, kExperimentalSystemInfoEventPrefix, | 234 StartsWithASCII(event_name, kExperimentalSystemInfoEventPrefix, |
| 220 true)); | 235 true)); |
| 221 } | 236 } |
| 222 | 237 |
| 223 // Called on UI thread since the observer is added from UI thread. | 238 // Called on UI thread since the observer is added from UI thread. |
|
Lei Zhang
2013/05/28 08:08:07
Turn this comment into a DCHECK.
Hongbo Min
2013/05/28 14:24:01
Done.
| |
| 224 void SystemInfoEventRouter::OnStorageFreeSpaceChanged( | 239 void SystemInfoEventRouter::OnFreeSpaceChanged( |
| 225 const std::string& id, double new_value, double old_value) { | 240 const std::string& id, double old_value, double new_value) { |
| 226 StorageChangeInfo info; | 241 StorageChangeInfo info; |
| 227 info.id = id; | 242 info.id = id; |
| 228 info.available_capacity = static_cast<double>(new_value); | 243 info.old_value = old_value; |
| 244 info.new_value = new_value; | |
| 229 | 245 |
| 230 scoped_ptr<base::ListValue> args(new base::ListValue()); | 246 scoped_ptr<base::ListValue> args(new base::ListValue()); |
| 231 args->Append(info.ToValue().release()); | 247 args->Append(info.ToValue().release()); |
| 232 | 248 |
| 233 DispatchEvent(event_names::kOnStorageAvailableCapacityChanged, args.Pass()); | 249 DispatchEvent(event_names::kOnStorageAvailableCapacityChanged, args.Pass()); |
| 234 } | 250 } |
| 235 | 251 |
| 236 void SystemInfoEventRouter::OnStorageAttached(const std::string& id, | 252 void SystemInfoEventRouter::OnRemovableStorageAttached( |
| 237 StorageUnitType type, | 253 const chrome::StorageInfo& info) { |
| 238 double capacity, | 254 base::FilePath mount_path(info.location()); |
| 239 double available_capacity) { | 255 base::PostTaskAndReplyWithResult( |
| 240 StorageUnitInfo info; | 256 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior( |
| 241 info.id = id; | 257 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN), |
| 242 info.type = type; | 258 FROM_HERE, |
| 243 info.capacity = capacity; | 259 base::Bind(&StorageInfoProvider::GetStorageFreeSpace, |
| 244 info.available_capacity = available_capacity; | 260 StorageInfoProvider::Get(), mount_path), |
|
Lei Zhang
2013/05/28 08:08:07
nit: indentation
Hongbo Min
2013/05/28 14:24:01
Done.
| |
| 261 base::Bind(&SystemInfoEventRouter::DispatchStorageAttachedEvent, | |
| 262 base::Unretained(this), info)); | |
| 263 } | |
| 264 | |
| 265 void SystemInfoEventRouter::DispatchStorageAttachedEvent( | |
| 266 const chrome::StorageInfo& info, int64 avail_bytes) { | |
| 267 StorageUnitInfo unit; | |
| 268 systeminfo::BuildStorageUnitInfo(info, &unit); | |
| 269 | |
| 270 unit.available_capacity = | |
| 271 avail_bytes > 0 ? static_cast<double>(avail_bytes) : 0; | |
| 245 | 272 |
| 246 scoped_ptr<base::ListValue> args(new base::ListValue); | 273 scoped_ptr<base::ListValue> args(new base::ListValue); |
| 247 args->Append(info.ToValue().release()); | 274 args->Append(unit.ToValue().release()); |
| 248 | 275 |
| 249 DispatchEvent(event_names::kOnStorageAttached, args.Pass()); | 276 DispatchEvent(event_names::kOnStorageAttached, args.Pass()); |
| 250 } | 277 } |
| 251 | 278 |
| 252 void SystemInfoEventRouter::OnStorageDetached(const std::string& id) { | 279 void SystemInfoEventRouter::OnRemovableStorageDetached( |
| 280 const chrome::StorageInfo& info) { | |
| 253 scoped_ptr<base::ListValue> args(new base::ListValue); | 281 scoped_ptr<base::ListValue> args(new base::ListValue); |
| 254 args->Append(new base::StringValue(id)); | 282 args->Append(new base::StringValue(info.device_id())); |
| 255 | 283 |
| 256 DispatchEvent(event_names::kOnStorageDetached, args.Pass()); | 284 DispatchEvent(event_names::kOnStorageDetached, args.Pass()); |
| 257 } | 285 } |
| 258 | 286 |
| 259 void SystemInfoEventRouter::OnDisplayBoundsChanged( | 287 void SystemInfoEventRouter::OnDisplayBoundsChanged( |
| 260 const gfx::Display& display) { | 288 const gfx::Display& display) { |
| 261 OnDisplayChanged(); | 289 OnDisplayChanged(); |
| 262 } | 290 } |
| 263 | 291 |
| 264 void SystemInfoEventRouter::OnDisplayAdded(const gfx::Display& new_display) { | 292 void SystemInfoEventRouter::OnDisplayAdded(const gfx::Display& new_display) { |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 317 | 345 |
| 318 void SystemInfoAPI::OnListenerAdded(const EventListenerInfo& details) { | 346 void SystemInfoAPI::OnListenerAdded(const EventListenerInfo& details) { |
| 319 SystemInfoEventRouter::GetInstance()->AddEventListener(details.event_name); | 347 SystemInfoEventRouter::GetInstance()->AddEventListener(details.event_name); |
| 320 } | 348 } |
| 321 | 349 |
| 322 void SystemInfoAPI::OnListenerRemoved(const EventListenerInfo& details) { | 350 void SystemInfoAPI::OnListenerRemoved(const EventListenerInfo& details) { |
| 323 SystemInfoEventRouter::GetInstance()->RemoveEventListener(details.event_name); | 351 SystemInfoEventRouter::GetInstance()->RemoveEventListener(details.event_name); |
| 324 } | 352 } |
| 325 | 353 |
| 326 } // namespace extensions | 354 } // namespace extensions |
| OLD | NEW |