Chromium Code Reviews| Index: chrome/browser/extensions/api/system_info/system_info_api.cc |
| diff --git a/chrome/browser/extensions/api/system_info/system_info_api.cc b/chrome/browser/extensions/api/system_info/system_info_api.cc |
| index 0f2eb33a3fe0be1c606d4ad60d0a73d3adaa0cb6..cc996e50ad8f2f1c0fa0ff9b7fbc9dae6faef03b 100644 |
| --- a/chrome/browser/extensions/api/system_info/system_info_api.cc |
| +++ b/chrome/browser/extensions/api/system_info/system_info_api.cc |
| @@ -12,12 +12,17 @@ |
| #include "base/memory/scoped_ptr.h" |
| #include "base/memory/singleton.h" |
| #include "base/string_util.h" |
| +#include "base/sys_info.h" |
| +#include "base/task_runner_util.h" |
| #include "base/values.h" |
| #include "chrome/browser/browser_process.h" |
| #include "chrome/browser/extensions/api/system_info_cpu/cpu_info_provider.h" |
| #include "chrome/browser/extensions/api/system_info_storage/storage_info_provider.h" |
| #include "chrome/browser/extensions/event_names.h" |
| #include "chrome/browser/extensions/event_router_forwarder.h" |
| +#include "chrome/browser/storage_monitor/removable_storage_observer.h" |
| +#include "chrome/browser/storage_monitor/storage_info.h" |
| +#include "chrome/browser/storage_monitor/storage_monitor.h" |
| #include "chrome/common/extensions/api/experimental_system_info_cpu.h" |
| #include "chrome/common/extensions/api/experimental_system_info_storage.h" |
| #include "ui/gfx/display_observer.h" |
| @@ -56,8 +61,9 @@ bool IsCpuUpdatedEvent(const std::string& event_name) { |
| // Event router for systemInfo API. It is a singleton instance shared by |
| // multiple profiles. |
| -class SystemInfoEventRouter |
| - : public gfx::DisplayObserver, public StorageInfoObserver { |
| +class SystemInfoEventRouter : public gfx::DisplayObserver, |
| + public chrome::RemovableStorageObserver, |
| + public StorageFreeSpaceObserver { |
| public: |
| static SystemInfoEventRouter* GetInstance(); |
| @@ -72,22 +78,25 @@ class SystemInfoEventRouter |
| static bool IsSystemInfoEvent(const std::string& event_name); |
| private: |
| - // StorageInfoObserver: |
| - virtual void OnStorageFreeSpaceChanged(const std::string& id, |
| - double new_value, |
| - double old_value) OVERRIDE; |
| - virtual void OnStorageAttached( |
| - const std::string& id, |
| - api::experimental_system_info_storage::StorageUnitType type, |
| - double capacity, |
| - double available_capacity) OVERRIDE; |
| - virtual void OnStorageDetached(const std::string& id) OVERRIDE; |
| - |
| // gfx::DisplayObserver: |
| virtual void OnDisplayBoundsChanged(const gfx::Display& display) OVERRIDE; |
| virtual void OnDisplayAdded(const gfx::Display& new_display) OVERRIDE; |
| virtual void OnDisplayRemoved(const gfx::Display& old_display) OVERRIDE; |
| + // chrome::RemovableStorageObserver implementation. |
| + virtual void OnRemovableStorageAttached( |
| + const chrome::StorageInfo& info) OVERRIDE; |
| + virtual void OnRemovableStorageDetached( |
| + const chrome::StorageInfo& info) OVERRIDE; |
| + |
| + // StorageFreeSpaceObserver implementation. |
| + virtual void OnFreeSpaceChanged(const std::string& id, |
| + double new_value, |
| + double old_value) OVERRIDE; |
| + |
| + void DispatchStorageAttachedEvent(const chrome::StorageInfo& info, |
| + int64 avail_bytes); |
| + |
| // Called from any thread to dispatch the systemInfo event to all extension |
| // processes cross multiple profiles. |
| void DispatchEvent(const std::string& event_name, |
| @@ -95,8 +104,8 @@ class SystemInfoEventRouter |
| // The callbacks of querying storage info to start and stop watching the |
| // storages. Called from UI thread. |
| - void StartWatchingStorages(const StorageInfo& info, bool success); |
| - void StopWatchingStorages(const StorageInfo& info, bool success); |
| + void StartWatchingStorages(const StorageUnitList& info, bool success); |
| + void StopWatchingStorages(const StorageUnitList& info, bool success); |
| // The callback for CPU sampling cycle. Called from FILE thread. |
| void OnNextCpuSampling( |
| @@ -120,31 +129,37 @@ SystemInfoEventRouter* SystemInfoEventRouter::GetInstance() { |
| } |
| SystemInfoEventRouter::SystemInfoEventRouter() { |
| + DCHECK(chrome::StorageMonitor::GetInstance()); |
| + chrome::StorageMonitor::GetInstance()->AddObserver(this); |
| StorageInfoProvider::Get()->AddObserver(this); |
| } |
| SystemInfoEventRouter::~SystemInfoEventRouter() { |
| StorageInfoProvider::Get()->RemoveObserver(this); |
| + if (chrome::StorageMonitor::GetInstance()) |
| + chrome::StorageMonitor::GetInstance()->RemoveObserver(this); |
| } |
| void SystemInfoEventRouter::StartWatchingStorages( |
| - const StorageInfo& info, bool success) { |
| + const StorageUnitList& info, bool success) { |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| if (!success) |
| return; |
| - for (StorageInfo::const_iterator it = info.begin(); it != info.end(); ++it) { |
| + for (StorageUnitList::const_iterator it = info.begin(); |
| + it != info.end(); ++it) { |
| StorageInfoProvider::Get()->StartWatching((*it)->id); |
| } |
| } |
| void SystemInfoEventRouter::StopWatchingStorages( |
| - const StorageInfo& info, bool success) { |
| + const StorageUnitList& info, bool success) { |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| if (!success) |
| return; |
| - for (StorageInfo::const_iterator it = info.begin(); it != info.end(); ++it) { |
| + for (StorageUnitList::const_iterator it = info.begin(); |
| + it != info.end(); ++it) { |
| StorageInfoProvider::Get()->StopWatching((*it)->id); |
| } |
| } |
| @@ -221,11 +236,12 @@ bool SystemInfoEventRouter::IsSystemInfoEvent(const std::string& event_name) { |
| } |
| // 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.
|
| -void SystemInfoEventRouter::OnStorageFreeSpaceChanged( |
| - const std::string& id, double new_value, double old_value) { |
| +void SystemInfoEventRouter::OnFreeSpaceChanged( |
| + const std::string& id, double old_value, double new_value) { |
| StorageChangeInfo info; |
| info.id = id; |
| - info.available_capacity = static_cast<double>(new_value); |
| + info.old_value = old_value; |
| + info.new_value = new_value; |
| scoped_ptr<base::ListValue> args(new base::ListValue()); |
| args->Append(info.ToValue().release()); |
| @@ -233,25 +249,37 @@ void SystemInfoEventRouter::OnStorageFreeSpaceChanged( |
| DispatchEvent(event_names::kOnStorageAvailableCapacityChanged, args.Pass()); |
| } |
| -void SystemInfoEventRouter::OnStorageAttached(const std::string& id, |
| - StorageUnitType type, |
| - double capacity, |
| - double available_capacity) { |
| - StorageUnitInfo info; |
| - info.id = id; |
| - info.type = type; |
| - info.capacity = capacity; |
| - info.available_capacity = available_capacity; |
| +void SystemInfoEventRouter::OnRemovableStorageAttached( |
| + const chrome::StorageInfo& info) { |
| + base::FilePath mount_path(info.location()); |
| + base::PostTaskAndReplyWithResult( |
| + BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior( |
| + base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN), |
| + FROM_HERE, |
| + base::Bind(&StorageInfoProvider::GetStorageFreeSpace, |
| + StorageInfoProvider::Get(), mount_path), |
|
Lei Zhang
2013/05/28 08:08:07
nit: indentation
Hongbo Min
2013/05/28 14:24:01
Done.
|
| + base::Bind(&SystemInfoEventRouter::DispatchStorageAttachedEvent, |
| + base::Unretained(this), info)); |
| +} |
| + |
| +void SystemInfoEventRouter::DispatchStorageAttachedEvent( |
| + const chrome::StorageInfo& info, int64 avail_bytes) { |
| + StorageUnitInfo unit; |
| + systeminfo::BuildStorageUnitInfo(info, &unit); |
| + |
| + unit.available_capacity = |
| + avail_bytes > 0 ? static_cast<double>(avail_bytes) : 0; |
| scoped_ptr<base::ListValue> args(new base::ListValue); |
| - args->Append(info.ToValue().release()); |
| + args->Append(unit.ToValue().release()); |
| DispatchEvent(event_names::kOnStorageAttached, args.Pass()); |
| } |
| -void SystemInfoEventRouter::OnStorageDetached(const std::string& id) { |
| +void SystemInfoEventRouter::OnRemovableStorageDetached( |
| + const chrome::StorageInfo& info) { |
| scoped_ptr<base::ListValue> args(new base::ListValue); |
| - args->Append(new base::StringValue(id)); |
| + args->Append(new base::StringValue(info.device_id())); |
| DispatchEvent(event_names::kOnStorageDetached, args.Pass()); |
| } |