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 fdca73e3ef226e220d5d175e08c22fc45860e643..a4a8c68e8ec239c210a9507923fc1ea99c3c5c59 100644 |
| --- a/chrome/browser/extensions/api/system_info/system_info_api.cc |
| +++ b/chrome/browser/extensions/api/system_info/system_info_api.cc |
| @@ -17,6 +17,9 @@ |
| #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_storage.h" |
| #include "ui/gfx/display_observer.h" |
| @@ -27,9 +30,9 @@ |
| namespace extensions { |
| +using api::experimental_system_info_storage::StorageCapacityChangeInfo; |
| using api::experimental_system_info_storage::StorageUnitInfo; |
| using api::experimental_system_info_storage::StorageUnitType; |
| -using api::experimental_system_info_storage::StorageChangeInfo; |
| using content::BrowserThread; |
| namespace { |
| @@ -49,8 +52,9 @@ bool IsAvailableCapacityChangedEvent(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 StorageFreeSpaceObserver, |
| + public chrome::RemovableStorageObserver { |
| public: |
| static SystemInfoEventRouter* GetInstance(); |
| @@ -65,22 +69,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; |
| + // StorageFreeSpaceObserver: |
| + virtual void OnFreeSpaceChanged(const std::string& id, |
| + double new_value, |
| + double old_value) 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; |
| + |
| + 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, |
| @@ -110,10 +117,13 @@ SystemInfoEventRouter* SystemInfoEventRouter::GetInstance() { |
| SystemInfoEventRouter::SystemInfoEventRouter() { |
| StorageInfoProvider::Get()->AddObserver(this); |
| + chrome::StorageMonitor::GetInstance()->AddObserver(this); |
| } |
| SystemInfoEventRouter::~SystemInfoEventRouter() { |
| StorageInfoProvider::Get()->RemoveObserver(this); |
| + if (chrome::StorageMonitor::GetInstance()) |
|
Jeffrey Yasskin
2013/07/02 23:00:50
You can avoid calling GetInstance twice by writing
Haojian Wu
2013/07/03 16:23:49
Done.
|
| + chrome::StorageMonitor::GetInstance()->RemoveObserver(this); |
| } |
| void SystemInfoEventRouter::StartWatchingStorages( |
| @@ -198,10 +208,10 @@ bool SystemInfoEventRouter::IsSystemInfoEvent(const std::string& event_name) { |
| } |
| // Called on UI thread since the observer is added from UI thread. |
| -void SystemInfoEventRouter::OnStorageFreeSpaceChanged( |
| - const std::string& id, double new_value, double old_value) { |
| - StorageChangeInfo info; |
| - info.id = id; |
| +void SystemInfoEventRouter::OnFreeSpaceChanged( |
| + const std::string& transient_id, double new_value, double old_value) { |
| + StorageCapacityChangeInfo info; |
| + info.id = transient_id; |
| info.available_capacity = static_cast<double>(new_value); |
| scoped_ptr<base::ListValue> args(new base::ListValue()); |
| @@ -210,25 +220,38 @@ 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::PostTaskAndReplyWithResult( |
| + BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior( |
| + base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN), |
| + FROM_HERE, |
| + base::Bind(&StorageInfoProvider::GetStorageFreeSpaceFromTransientId, |
| + StorageInfoProvider::Get(), |
| + StorageInfoProvider::Get()->GetTransientIdForDeviceId( |
| + info.device_id())), |
| + base::Bind(&SystemInfoEventRouter::DispatchStorageAttachedEvent, |
| + base::Unretained(this), info)); |
|
Jeffrey Yasskin
2013/07/02 23:00:50
What makes sure that *this is still alive when the
Hongbo Min
2013/07/03 04:59:40
OK. It is because SystemInfoProvider instance is a
Jeffrey Yasskin
2013/07/03 21:44:48
Please comment that.
Haojian Wu
2013/07/04 00:41:17
Done.
|
| +} |
| - scoped_ptr<base::ListValue> args(new base::ListValue); |
| - args->Append(info.ToValue().release()); |
| +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(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(StorageInfoProvider::Get()-> |
| + GetTransientIdForDeviceId(info.device_id()))); |
| DispatchEvent(event_names::kOnStorageDetached, args.Pass()); |
| } |