| 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/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 15 #include "base/values.h" | 15 #include "base/values.h" |
| 16 #include "chrome/browser/browser_process.h" | 16 #include "chrome/browser/browser_process.h" |
| 17 #include "chrome/browser/extensions/api/system_info_storage/storage_info_provide
r.h" | 17 #include "chrome/browser/extensions/api/system_info_storage/storage_info_provide
r.h" |
| 18 #include "chrome/browser/extensions/event_names.h" | 18 #include "chrome/browser/extensions/event_names.h" |
| 19 #include "chrome/browser/extensions/event_router_forwarder.h" | 19 #include "chrome/browser/extensions/event_router_forwarder.h" |
| 20 #include "chrome/browser/storage_monitor/removable_storage_observer.h" | 20 #include "chrome/browser/storage_monitor/removable_storage_observer.h" |
| 21 #include "chrome/browser/storage_monitor/storage_info.h" | 21 #include "chrome/browser/storage_monitor/storage_info.h" |
| 22 #include "chrome/browser/storage_monitor/storage_monitor.h" | 22 #include "chrome/browser/storage_monitor/storage_monitor.h" |
| 23 #include "chrome/common/extensions/api/experimental_system_info_storage.h" | 23 #include "chrome/common/extensions/api/experimental_system_info_storage.h" |
| 24 #include "chrome/common/extensions/api/system_info_storage.h" |
| 24 #include "ui/gfx/display_observer.h" | 25 #include "ui/gfx/display_observer.h" |
| 25 | 26 |
| 26 #if defined(USE_ASH) | 27 #if defined(USE_ASH) |
| 27 #include "ash/screen_ash.h" | 28 #include "ash/screen_ash.h" |
| 28 #include "ash/shell.h" | 29 #include "ash/shell.h" |
| 29 #endif | 30 #endif |
| 30 | 31 |
| 31 namespace extensions { | 32 namespace extensions { |
| 32 | 33 |
| 33 using api::experimental_system_info_storage::StorageFreeSpaceChangeInfo; | 34 using api::experimental_system_info_storage::StorageFreeSpaceChangeInfo; |
| 34 using api::experimental_system_info_storage::StorageUnitInfo; | 35 using api::system_info_storage::StorageUnitInfo; |
| 35 using api::experimental_system_info_storage::StorageUnitType; | 36 using api::system_info_storage::StorageUnitType; |
| 36 using content::BrowserThread; | 37 using content::BrowserThread; |
| 37 | 38 |
| 38 namespace { | 39 namespace { |
| 39 | 40 |
| 40 // The display events use the "systemInfo" prefix. | 41 // The display events use the "systemInfo" prefix. |
| 41 const char kSystemInfoEventPrefix[] = "systemInfo"; | 42 const char kSystemInfoEventPrefix[] = "systemInfo"; |
| 42 // The storage events still use the "experimental.systemInfo" prefix. | 43 // The storage events still use the "experimental.systemInfo" prefix. |
| 43 const char kExperimentalSystemInfoEventPrefix[] = "experimental.systemInfo"; | 44 const char kExperimentalSystemInfoEventPrefix[] = "experimental.systemInfo"; |
| 44 | 45 |
| 45 bool IsDisplayChangedEvent(const std::string& event_name) { | 46 bool IsDisplayChangedEvent(const std::string& event_name) { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 virtual void OnDisplayBoundsChanged(const gfx::Display& display) OVERRIDE; | 79 virtual void OnDisplayBoundsChanged(const gfx::Display& display) OVERRIDE; |
| 79 virtual void OnDisplayAdded(const gfx::Display& new_display) OVERRIDE; | 80 virtual void OnDisplayAdded(const gfx::Display& new_display) OVERRIDE; |
| 80 virtual void OnDisplayRemoved(const gfx::Display& old_display) OVERRIDE; | 81 virtual void OnDisplayRemoved(const gfx::Display& old_display) OVERRIDE; |
| 81 | 82 |
| 82 // chrome::RemovableStorageObserver implementation. | 83 // chrome::RemovableStorageObserver implementation. |
| 83 virtual void OnRemovableStorageAttached( | 84 virtual void OnRemovableStorageAttached( |
| 84 const chrome::StorageInfo& info) OVERRIDE; | 85 const chrome::StorageInfo& info) OVERRIDE; |
| 85 virtual void OnRemovableStorageDetached( | 86 virtual void OnRemovableStorageDetached( |
| 86 const chrome::StorageInfo& info) OVERRIDE; | 87 const chrome::StorageInfo& info) OVERRIDE; |
| 87 | 88 |
| 88 void DispatchStorageAttachedEvent(const chrome::StorageInfo& info, | 89 void DispatchStorageAttachedEvent(const chrome::StorageInfo& info); |
| 89 int64 avail_bytes); | |
| 90 | 90 |
| 91 // Called from any thread to dispatch the systemInfo event to all extension | 91 // Called from any thread to dispatch the systemInfo event to all extension |
| 92 // processes cross multiple profiles. | 92 // processes cross multiple profiles. |
| 93 void DispatchEvent(const std::string& event_name, | 93 void DispatchEvent(const std::string& event_name, |
| 94 scoped_ptr<base::ListValue> args); | 94 scoped_ptr<base::ListValue> args); |
| 95 | 95 |
| 96 // The callbacks of querying storage info to start and stop watching the | 96 // The callbacks of querying storage info to start and stop watching the |
| 97 // storages. Called from UI thread. | 97 // storages. Called from UI thread. |
| 98 void StartWatchingStorages(const StorageInfo& info, bool success); | 98 void StartWatchingStorages(const StorageInfo& info, bool success); |
| 99 void StopWatchingStorages(const StorageInfo& info, bool success); | 99 void StopWatchingStorages(const StorageInfo& info, bool success); |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 info.available_capacity = static_cast<double>(new_value); | 216 info.available_capacity = static_cast<double>(new_value); |
| 217 | 217 |
| 218 scoped_ptr<base::ListValue> args(new base::ListValue()); | 218 scoped_ptr<base::ListValue> args(new base::ListValue()); |
| 219 args->Append(info.ToValue().release()); | 219 args->Append(info.ToValue().release()); |
| 220 | 220 |
| 221 DispatchEvent(event_names::kOnStorageAvailableCapacityChanged, args.Pass()); | 221 DispatchEvent(event_names::kOnStorageAvailableCapacityChanged, args.Pass()); |
| 222 } | 222 } |
| 223 | 223 |
| 224 void SystemInfoEventRouter::OnRemovableStorageAttached( | 224 void SystemInfoEventRouter::OnRemovableStorageAttached( |
| 225 const chrome::StorageInfo& info) { | 225 const chrome::StorageInfo& info) { |
| 226 base::PostTaskAndReplyWithResult( | 226 DispatchStorageAttachedEvent(info); |
| 227 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior( | |
| 228 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN), | |
| 229 FROM_HERE, | |
| 230 base::Bind(&StorageInfoProvider::GetStorageFreeSpaceFromTransientId, | |
| 231 StorageInfoProvider::Get(), | |
| 232 StorageInfoProvider::Get()->GetTransientIdForDeviceId( | |
| 233 info.device_id())), | |
| 234 base::Bind(&SystemInfoEventRouter::DispatchStorageAttachedEvent, | |
| 235 // Since SystemInfoEventRouter is a global lazy instance, this | |
| 236 // pointer will be alive when the reply comes back. | |
| 237 base::Unretained(this), | |
| 238 info)); | |
| 239 } | 227 } |
| 240 | 228 |
| 241 void SystemInfoEventRouter::DispatchStorageAttachedEvent( | 229 void SystemInfoEventRouter::DispatchStorageAttachedEvent( |
| 242 const chrome::StorageInfo& info, int64 avail_bytes) { | 230 const chrome::StorageInfo& info) { |
| 243 StorageUnitInfo unit; | 231 StorageUnitInfo unit; |
| 244 systeminfo::BuildStorageUnitInfo(info, &unit); | 232 systeminfo::BuildStorageUnitInfo(info, &unit); |
| 245 | 233 |
| 246 unit.available_capacity = | |
| 247 avail_bytes > 0 ? static_cast<double>(avail_bytes) : 0; | |
| 248 | |
| 249 scoped_ptr<base::ListValue> args(new base::ListValue); | 234 scoped_ptr<base::ListValue> args(new base::ListValue); |
| 250 args->Append(unit.ToValue().release()); | 235 args->Append(unit.ToValue().release()); |
| 251 DispatchEvent(event_names::kOnStorageAttached, args.Pass()); | 236 DispatchEvent(event_names::kOnStorageAttached, args.Pass()); |
| 252 } | 237 } |
| 253 | 238 |
| 254 void SystemInfoEventRouter::OnRemovableStorageDetached( | 239 void SystemInfoEventRouter::OnRemovableStorageDetached( |
| 255 const chrome::StorageInfo& info) { | 240 const chrome::StorageInfo& info) { |
| 256 scoped_ptr<base::ListValue> args(new base::ListValue); | 241 scoped_ptr<base::ListValue> args(new base::ListValue); |
| 257 args->Append(new base::StringValue(StorageInfoProvider::Get()-> | 242 args->Append(new base::StringValue(StorageInfoProvider::Get()-> |
| 258 GetTransientIdForDeviceId(info.device_id()))); | 243 GetTransientIdForDeviceId(info.device_id()))); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 312 | 297 |
| 313 void SystemInfoAPI::OnListenerAdded(const EventListenerInfo& details) { | 298 void SystemInfoAPI::OnListenerAdded(const EventListenerInfo& details) { |
| 314 SystemInfoEventRouter::GetInstance()->AddEventListener(details.event_name); | 299 SystemInfoEventRouter::GetInstance()->AddEventListener(details.event_name); |
| 315 } | 300 } |
| 316 | 301 |
| 317 void SystemInfoAPI::OnListenerRemoved(const EventListenerInfo& details) { | 302 void SystemInfoAPI::OnListenerRemoved(const EventListenerInfo& details) { |
| 318 SystemInfoEventRouter::GetInstance()->RemoveEventListener(details.event_name); | 303 SystemInfoEventRouter::GetInstance()->RemoveEventListener(details.event_name); |
| 319 } | 304 } |
| 320 | 305 |
| 321 } // namespace extensions | 306 } // namespace extensions |
| OLD | NEW |