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 api::system_storage::StorageUnitInfo
;
12 namespace EjectDevice
= api::system_storage::EjectDevice
;
13 namespace GetAvailableCapacity
= 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_
= 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 api::system_storage::EjectDeviceResultCode result
=
75 api::system_storage::EJECT_DEVICE_RESULT_CODE_FAILURE
;
77 case StorageMonitor::EJECT_OK
:
78 result
= api::system_storage::EJECT_DEVICE_RESULT_CODE_SUCCESS
;
80 case StorageMonitor::EJECT_IN_USE
:
81 result
= api::system_storage::EJECT_DEVICE_RESULT_CODE_IN_USE
;
83 case StorageMonitor::EJECT_NO_SUCH_DEVICE
:
84 result
= api::system_storage::EJECT_DEVICE_RESULT_CODE_NO_SUCH_DEVICE
;
86 case StorageMonitor::EJECT_FAILURE
:
87 result
= api::system_storage::EJECT_DEVICE_RESULT_CODE_FAILURE
;
90 SetResult(new base::StringValue(api::system_storage::ToString(result
)));
94 SystemStorageGetAvailableCapacityFunction::
95 SystemStorageGetAvailableCapacityFunction() {
98 SystemStorageGetAvailableCapacityFunction::
99 ~SystemStorageGetAvailableCapacityFunction() {
102 bool SystemStorageGetAvailableCapacityFunction::RunAsync() {
103 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
105 scoped_ptr
<GetAvailableCapacity::Params
> params(
106 GetAvailableCapacity::Params::Create(*args_
));
107 EXTENSION_FUNCTION_VALIDATE(params
.get());
109 StorageMonitor::GetInstance()->EnsureInitialized(base::Bind(
110 &SystemStorageGetAvailableCapacityFunction::OnStorageMonitorInit
,
116 void SystemStorageGetAvailableCapacityFunction::OnStorageMonitorInit(
117 const std::string
& transient_id
) {
118 content::BrowserThread::PostTaskAndReplyWithResult(
119 content::BrowserThread::FILE,
122 &StorageInfoProvider::GetStorageFreeSpaceFromTransientIdOnFileThread
,
123 StorageInfoProvider::Get(),
125 base::Bind(&SystemStorageGetAvailableCapacityFunction::OnQueryCompleted
,
130 void SystemStorageGetAvailableCapacityFunction::OnQueryCompleted(
131 const std::string
& transient_id
,
132 double available_capacity
) {
133 bool success
= available_capacity
>= 0;
135 api::system_storage::StorageAvailableCapacityInfo result
;
136 result
.id
= transient_id
;
137 result
.available_capacity
= available_capacity
;
138 SetResult(result
.ToValue().release());
140 SetError("Error occurred when querying available capacity.");
142 SendResponse(success
);
145 } // namespace extensions