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 "chrome/browser/extensions/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(
23 base::Bind(&SystemStorageGetInfoFunction::OnGetStorageInfoCompleted,
28 void SystemStorageGetInfoFunction::OnGetStorageInfoCompleted(bool success) {
30 results_ = api::system_storage::GetInfo::Results::Create(
31 StorageInfoProvider::Get()->storage_unit_info_list());
33 SetError("Error occurred when querying storage information.");
36 SendResponse(success);
39 SystemStorageEjectDeviceFunction::~SystemStorageEjectDeviceFunction() {
42 bool SystemStorageEjectDeviceFunction::RunAsync() {
43 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
45 scoped_ptr<EjectDevice::Params> params(EjectDevice::Params::Create(*args_));
46 EXTENSION_FUNCTION_VALIDATE(params.get());
48 StorageMonitor::GetInstance()->EnsureInitialized(base::Bind(
49 &SystemStorageEjectDeviceFunction::OnStorageMonitorInit,
55 void SystemStorageEjectDeviceFunction::OnStorageMonitorInit(
56 const std::string& transient_device_id) {
57 DCHECK(StorageMonitor::GetInstance()->IsInitialized());
58 StorageMonitor* monitor = StorageMonitor::GetInstance();
59 std::string device_id_str =
60 StorageMonitor::GetInstance()->GetDeviceIdForTransientId(
63 if (device_id_str == "") {
64 HandleResponse(StorageMonitor::EJECT_NO_SUCH_DEVICE);
70 base::Bind(&SystemStorageEjectDeviceFunction::HandleResponse,
74 void SystemStorageEjectDeviceFunction::HandleResponse(
75 StorageMonitor::EjectStatus status) {
76 api::system_storage:: EjectDeviceResultCode result =
77 api::system_storage::EJECT_DEVICE_RESULT_CODE_FAILURE;
79 case StorageMonitor::EJECT_OK:
80 result = api::system_storage::EJECT_DEVICE_RESULT_CODE_SUCCESS;
82 case StorageMonitor::EJECT_IN_USE:
83 result = api::system_storage::EJECT_DEVICE_RESULT_CODE_IN_USE;
85 case StorageMonitor::EJECT_NO_SUCH_DEVICE:
86 result = api::system_storage::
87 EJECT_DEVICE_RESULT_CODE_NO_SUCH_DEVICE;
89 case StorageMonitor::EJECT_FAILURE:
90 result = api::system_storage::EJECT_DEVICE_RESULT_CODE_FAILURE;
93 SetResult(new base::StringValue(
94 api::system_storage::ToString(result)));
98 SystemStorageGetAvailableCapacityFunction::
99 SystemStorageGetAvailableCapacityFunction() {
102 SystemStorageGetAvailableCapacityFunction::
103 ~SystemStorageGetAvailableCapacityFunction() {
106 bool SystemStorageGetAvailableCapacityFunction::RunAsync() {
107 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
109 scoped_ptr<GetAvailableCapacity::Params> params(
110 GetAvailableCapacity::Params::Create(*args_));
111 EXTENSION_FUNCTION_VALIDATE(params.get());
113 StorageMonitor::GetInstance()->EnsureInitialized(base::Bind(
114 &SystemStorageGetAvailableCapacityFunction::OnStorageMonitorInit,
120 void SystemStorageGetAvailableCapacityFunction::OnStorageMonitorInit(
121 const std::string& transient_id) {
122 content::BrowserThread::PostTaskAndReplyWithResult(
123 content::BrowserThread::FILE,
126 &StorageInfoProvider::GetStorageFreeSpaceFromTransientIdOnFileThread,
127 StorageInfoProvider::Get(), transient_id),
129 &SystemStorageGetAvailableCapacityFunction::OnQueryCompleted,
130 this, transient_id));
133 void SystemStorageGetAvailableCapacityFunction::OnQueryCompleted(
134 const std::string& transient_id, double available_capacity) {
135 bool success = available_capacity >= 0;
137 api::system_storage::StorageAvailableCapacityInfo result;
138 result.id = transient_id;
139 result.available_capacity = available_capacity;
140 SetResult(result.ToValue().release());
142 SetError("Error occurred when querying available capacity.");
144 SendResponse(success);
147 } // namespace extensions