Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / extensions / browser / api / system_storage / system_storage_api.cc
blobde460183ea3af7b2a2c459e593c9e5b54c55b873
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;
9 namespace extensions {
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));
24 return true;
27 void SystemStorageGetInfoFunction::OnGetStorageInfoCompleted(bool success) {
28 if (success) {
29 results_ = api::system_storage::GetInfo::Results::Create(
30 StorageInfoProvider::Get()->storage_unit_info_list());
31 } else {
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,
49 this,
50 params->id));
51 return true;
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(
60 transient_device_id);
62 if (device_id_str.empty()) {
63 HandleResponse(StorageMonitor::EJECT_NO_SUCH_DEVICE);
64 return;
67 monitor->EjectDevice(
68 device_id_str,
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;
76 switch (status) {
77 case StorageMonitor::EJECT_OK:
78 result = api::system_storage::EJECT_DEVICE_RESULT_CODE_SUCCESS;
79 break;
80 case StorageMonitor::EJECT_IN_USE:
81 result = api::system_storage::EJECT_DEVICE_RESULT_CODE_IN_USE;
82 break;
83 case StorageMonitor::EJECT_NO_SUCH_DEVICE:
84 result = api::system_storage::EJECT_DEVICE_RESULT_CODE_NO_SUCH_DEVICE;
85 break;
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)));
91 SendResponse(true);
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,
111 this,
112 params->id));
113 return true;
116 void SystemStorageGetAvailableCapacityFunction::OnStorageMonitorInit(
117 const std::string& transient_id) {
118 content::BrowserThread::PostTaskAndReplyWithResult(
119 content::BrowserThread::FILE,
120 FROM_HERE,
121 base::Bind(
122 &StorageInfoProvider::GetStorageFreeSpaceFromTransientIdOnFileThread,
123 StorageInfoProvider::Get(),
124 transient_id),
125 base::Bind(&SystemStorageGetAvailableCapacityFunction::OnQueryCompleted,
126 this,
127 transient_id));
130 void SystemStorageGetAvailableCapacityFunction::OnQueryCompleted(
131 const std::string& transient_id,
132 double available_capacity) {
133 bool success = available_capacity >= 0;
134 if (success) {
135 api::system_storage::StorageAvailableCapacityInfo result;
136 result.id = transient_id;
137 result.available_capacity = available_capacity;
138 SetResult(result.ToValue().release());
139 } else {
140 SetError("Error occurred when querying available capacity.");
142 SendResponse(success);
145 } // namespace extensions