Upstream version 10.38.222.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / system_storage / system_storage_api.cc
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.
4
5 #include "chrome/browser/extensions/api/system_storage/system_storage_api.h"
6
7 using storage_monitor::StorageMonitor;
8
9 namespace extensions {
10
11 using api::system_storage::StorageUnitInfo;
12 namespace EjectDevice = api::system_storage::EjectDevice;
13 namespace GetAvailableCapacity = api::system_storage::GetAvailableCapacity;
14
15 SystemStorageGetInfoFunction::SystemStorageGetInfoFunction() {
16 }
17
18 SystemStorageGetInfoFunction::~SystemStorageGetInfoFunction() {
19 }
20
21 bool SystemStorageGetInfoFunction::RunAsync() {
22   StorageInfoProvider::Get()->StartQueryInfo(
23       base::Bind(&SystemStorageGetInfoFunction::OnGetStorageInfoCompleted,
24                  this));
25   return true;
26 }
27
28 void SystemStorageGetInfoFunction::OnGetStorageInfoCompleted(bool success) {
29   if (success) {
30     results_ = api::system_storage::GetInfo::Results::Create(
31         StorageInfoProvider::Get()->storage_unit_info_list());
32   } else {
33     SetError("Error occurred when querying storage information.");
34   }
35
36   SendResponse(success);
37 }
38
39 SystemStorageEjectDeviceFunction::~SystemStorageEjectDeviceFunction() {
40 }
41
42 bool SystemStorageEjectDeviceFunction::RunAsync() {
43   DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
44
45   scoped_ptr<EjectDevice::Params> params(EjectDevice::Params::Create(*args_));
46   EXTENSION_FUNCTION_VALIDATE(params.get());
47
48   StorageMonitor::GetInstance()->EnsureInitialized(base::Bind(
49       &SystemStorageEjectDeviceFunction::OnStorageMonitorInit,
50       this,
51       params->id));
52   return true;
53 }
54
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(
61           transient_device_id);
62
63   if (device_id_str == "") {
64     HandleResponse(StorageMonitor::EJECT_NO_SUCH_DEVICE);
65     return;
66   }
67
68   monitor->EjectDevice(
69       device_id_str,
70       base::Bind(&SystemStorageEjectDeviceFunction::HandleResponse,
71                  this));
72 }
73
74 void SystemStorageEjectDeviceFunction::HandleResponse(
75     StorageMonitor::EjectStatus status) {
76   api::system_storage:: EjectDeviceResultCode result =
77       api::system_storage::EJECT_DEVICE_RESULT_CODE_FAILURE;
78   switch (status) {
79     case StorageMonitor::EJECT_OK:
80       result = api::system_storage::EJECT_DEVICE_RESULT_CODE_SUCCESS;
81       break;
82     case StorageMonitor::EJECT_IN_USE:
83       result = api::system_storage::EJECT_DEVICE_RESULT_CODE_IN_USE;
84       break;
85     case StorageMonitor::EJECT_NO_SUCH_DEVICE:
86       result = api::system_storage::
87           EJECT_DEVICE_RESULT_CODE_NO_SUCH_DEVICE;
88       break;
89     case StorageMonitor::EJECT_FAILURE:
90       result = api::system_storage::EJECT_DEVICE_RESULT_CODE_FAILURE;
91   }
92
93   SetResult(new base::StringValue(
94       api::system_storage::ToString(result)));
95   SendResponse(true);
96 }
97
98 SystemStorageGetAvailableCapacityFunction::
99     SystemStorageGetAvailableCapacityFunction() {
100 }
101
102 SystemStorageGetAvailableCapacityFunction::
103     ~SystemStorageGetAvailableCapacityFunction() {
104 }
105
106 bool SystemStorageGetAvailableCapacityFunction::RunAsync() {
107   DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
108
109   scoped_ptr<GetAvailableCapacity::Params> params(
110       GetAvailableCapacity::Params::Create(*args_));
111   EXTENSION_FUNCTION_VALIDATE(params.get());
112
113   StorageMonitor::GetInstance()->EnsureInitialized(base::Bind(
114       &SystemStorageGetAvailableCapacityFunction::OnStorageMonitorInit,
115       this,
116       params->id));
117   return true;
118 }
119
120 void SystemStorageGetAvailableCapacityFunction::OnStorageMonitorInit(
121     const std::string& transient_id) {
122   content::BrowserThread::PostTaskAndReplyWithResult(
123       content::BrowserThread::FILE,
124       FROM_HERE,
125       base::Bind(
126           &StorageInfoProvider::GetStorageFreeSpaceFromTransientIdOnFileThread,
127           StorageInfoProvider::Get(), transient_id),
128       base::Bind(
129           &SystemStorageGetAvailableCapacityFunction::OnQueryCompleted,
130           this, transient_id));
131 }
132
133 void SystemStorageGetAvailableCapacityFunction::OnQueryCompleted(
134     const std::string& transient_id, double available_capacity) {
135   bool success = available_capacity >= 0;
136   if (success) {
137     api::system_storage::StorageAvailableCapacityInfo result;
138     result.id = transient_id;
139     result.available_capacity = available_capacity;
140     SetResult(result.ToValue().release());
141   } else {
142     SetError("Error occurred when querying available capacity.");
143   }
144   SendResponse(success);
145 }
146
147 }  // namespace extensions