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"
9 using api::system_storage::StorageUnitInfo;
10 namespace EjectDevice = api::system_storage::EjectDevice;
11 namespace GetAvailableCapacity = api::system_storage::GetAvailableCapacity;
13 SystemStorageGetInfoFunction::SystemStorageGetInfoFunction() {
16 SystemStorageGetInfoFunction::~SystemStorageGetInfoFunction() {
19 bool SystemStorageGetInfoFunction::RunImpl() {
20 StorageInfoProvider::Get()->StartQueryInfo(
21 base::Bind(&SystemStorageGetInfoFunction::OnGetStorageInfoCompleted,
26 void SystemStorageGetInfoFunction::OnGetStorageInfoCompleted(bool success) {
28 results_ = api::system_storage::GetInfo::Results::Create(
29 StorageInfoProvider::Get()->storage_unit_info_list());
31 SetError("Error occurred when querying storage information.");
34 SendResponse(success);
37 SystemStorageEjectDeviceFunction::~SystemStorageEjectDeviceFunction() {
40 bool SystemStorageEjectDeviceFunction::RunImpl() {
41 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
43 scoped_ptr<EjectDevice::Params> params(EjectDevice::Params::Create(*args_));
44 EXTENSION_FUNCTION_VALIDATE(params.get());
46 StorageMonitor::GetInstance()->EnsureInitialized(base::Bind(
47 &SystemStorageEjectDeviceFunction::OnStorageMonitorInit,
53 void SystemStorageEjectDeviceFunction::OnStorageMonitorInit(
54 const std::string& transient_device_id) {
55 DCHECK(StorageMonitor::GetInstance()->IsInitialized());
56 StorageMonitor* monitor = StorageMonitor::GetInstance();
57 std::string device_id_str =
58 StorageMonitor::GetInstance()->GetDeviceIdForTransientId(
61 if (device_id_str == "") {
62 HandleResponse(StorageMonitor::EJECT_NO_SUCH_DEVICE);
68 base::Bind(&SystemStorageEjectDeviceFunction::HandleResponse,
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::
85 EJECT_DEVICE_RESULT_CODE_NO_SUCH_DEVICE;
87 case StorageMonitor::EJECT_FAILURE:
88 result = api::system_storage::EJECT_DEVICE_RESULT_CODE_FAILURE;
91 SetResult(new base::StringValue(
92 api::system_storage::ToString(result)));
96 SystemStorageGetAvailableCapacityFunction::
97 SystemStorageGetAvailableCapacityFunction() {
100 SystemStorageGetAvailableCapacityFunction::
101 ~SystemStorageGetAvailableCapacityFunction() {
104 bool SystemStorageGetAvailableCapacityFunction::RunImpl() {
105 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
107 scoped_ptr<GetAvailableCapacity::Params> params(
108 GetAvailableCapacity::Params::Create(*args_));
109 EXTENSION_FUNCTION_VALIDATE(params.get());
111 StorageMonitor::GetInstance()->EnsureInitialized(base::Bind(
112 &SystemStorageGetAvailableCapacityFunction::OnStorageMonitorInit,
118 void SystemStorageGetAvailableCapacityFunction::OnStorageMonitorInit(
119 const std::string& transient_id) {
120 content::BrowserThread::PostTaskAndReplyWithResult(
121 content::BrowserThread::FILE,
124 &StorageInfoProvider::GetStorageFreeSpaceFromTransientIdOnFileThread,
125 StorageInfoProvider::Get(), transient_id),
127 &SystemStorageGetAvailableCapacityFunction::OnQueryCompleted,
128 this, transient_id));
131 void SystemStorageGetAvailableCapacityFunction::OnQueryCompleted(
132 const std::string& transient_id, 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