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
3 // found in the LICENSE file.
5 #include "extensions/browser/api/system_storage/system_storage_api.h"
7 using storage_monitor::StorageMonitor
;
11 using core_api::system_storage::StorageUnitInfo
;
12 namespace EjectDevice
= core_api::system_storage::EjectDevice
;
13 namespace GetAvailableCapacity
= core_api::system_storage::GetAvailableCapacity
;
15 SystemStorageGetInfoFunction::SystemStorageGetInfoFunction() {
18 SystemStorageGetInfoFunction::~SystemStorageGetInfoFunction() {
21 bool SystemStorageGetInfoFunction::RunAsync() {
22 StorageInfoProvider::Get()->StartQueryInfo(base::Bind(
23 &SystemStorageGetInfoFunction::OnGetStorageInfoCompleted
, this));
27 void SystemStorageGetInfoFunction::OnGetStorageInfoCompleted(bool success
) {
29 results_
= core_api::system_storage::GetInfo::Results::Create(
30 StorageInfoProvider::Get()->storage_unit_info_list());
32 SetError("Error occurred when querying storage information.");
35 SendResponse(success
);
38 SystemStorageEjectDeviceFunction::~SystemStorageEjectDeviceFunction() {
41 bool SystemStorageEjectDeviceFunction::RunAsync() {
42 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
44 scoped_ptr
<EjectDevice::Params
> params(EjectDevice::Params::Create(*args_
));
45 EXTENSION_FUNCTION_VALIDATE(params
.get());
47 StorageMonitor::GetInstance()->EnsureInitialized(
48 base::Bind(&SystemStorageEjectDeviceFunction::OnStorageMonitorInit
,
54 void SystemStorageEjectDeviceFunction::OnStorageMonitorInit(
55 const std::string
& transient_device_id
) {
56 DCHECK(StorageMonitor::GetInstance()->IsInitialized());
57 StorageMonitor
* monitor
= StorageMonitor::GetInstance();
58 std::string device_id_str
=
59 StorageMonitor::GetInstance()->GetDeviceIdForTransientId(
62 if (device_id_str
.empty()) {
63 HandleResponse(StorageMonitor::EJECT_NO_SUCH_DEVICE
);
69 base::Bind(&SystemStorageEjectDeviceFunction::HandleResponse
, this));
72 void SystemStorageEjectDeviceFunction::HandleResponse(
73 StorageMonitor::EjectStatus status
) {
74 core_api::system_storage::EjectDeviceResultCode result
=
75 core_api::system_storage::EJECT_DEVICE_RESULT_CODE_FAILURE
;
77 case StorageMonitor::EJECT_OK
:
78 result
= core_api::system_storage::EJECT_DEVICE_RESULT_CODE_SUCCESS
;
80 case StorageMonitor::EJECT_IN_USE
:
81 result
= core_api::system_storage::EJECT_DEVICE_RESULT_CODE_IN_USE
;
83 case StorageMonitor::EJECT_NO_SUCH_DEVICE
:
85 core_api::system_storage::EJECT_DEVICE_RESULT_CODE_NO_SUCH_DEVICE
;
87 case StorageMonitor::EJECT_FAILURE
:
88 result
= core_api::system_storage::EJECT_DEVICE_RESULT_CODE_FAILURE
;
91 SetResult(new base::StringValue(core_api::system_storage::ToString(result
)));
95 SystemStorageGetAvailableCapacityFunction::
96 SystemStorageGetAvailableCapacityFunction() {
99 SystemStorageGetAvailableCapacityFunction::
100 ~SystemStorageGetAvailableCapacityFunction() {
103 bool SystemStorageGetAvailableCapacityFunction::RunAsync() {
104 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
106 scoped_ptr
<GetAvailableCapacity::Params
> params(
107 GetAvailableCapacity::Params::Create(*args_
));
108 EXTENSION_FUNCTION_VALIDATE(params
.get());
110 StorageMonitor::GetInstance()->EnsureInitialized(base::Bind(
111 &SystemStorageGetAvailableCapacityFunction::OnStorageMonitorInit
,
117 void SystemStorageGetAvailableCapacityFunction::OnStorageMonitorInit(
118 const std::string
& transient_id
) {
119 content::BrowserThread::PostTaskAndReplyWithResult(
120 content::BrowserThread::FILE,
123 &StorageInfoProvider::GetStorageFreeSpaceFromTransientIdOnFileThread
,
124 StorageInfoProvider::Get(),
126 base::Bind(&SystemStorageGetAvailableCapacityFunction::OnQueryCompleted
,
131 void SystemStorageGetAvailableCapacityFunction::OnQueryCompleted(
132 const std::string
& transient_id
,
133 double available_capacity
) {
134 bool success
= available_capacity
>= 0;
136 core_api::system_storage::StorageAvailableCapacityInfo result
;
137 result
.id
= transient_id
;
138 result
.available_capacity
= available_capacity
;
139 SetResult(result
.ToValue().release());
141 SetError("Error occurred when querying available capacity.");
143 SendResponse(success
);
146 } // namespace extensions