- add sources.
[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 namespace extensions {
8
9 using api::system_storage::StorageUnitInfo;
10 namespace EjectDevice = api::system_storage::EjectDevice;
11 namespace GetAvailableCapacity = api::system_storage::GetAvailableCapacity;
12
13 SystemStorageGetInfoFunction::SystemStorageGetInfoFunction() {
14 }
15
16 SystemStorageGetInfoFunction::~SystemStorageGetInfoFunction() {
17 }
18
19 bool SystemStorageGetInfoFunction::RunImpl() {
20   StorageInfoProvider::Get()->StartQueryInfo(
21       base::Bind(&SystemStorageGetInfoFunction::OnGetStorageInfoCompleted,
22                  this));
23   return true;
24 }
25
26 void SystemStorageGetInfoFunction::OnGetStorageInfoCompleted(bool success) {
27   if (success) {
28     results_ = api::system_storage::GetInfo::Results::Create(
29         StorageInfoProvider::Get()->storage_unit_info_list());
30   } else {
31     SetError("Error occurred when querying storage information.");
32   }
33
34   SendResponse(success);
35 }
36
37 SystemStorageEjectDeviceFunction::~SystemStorageEjectDeviceFunction() {
38 }
39
40 bool SystemStorageEjectDeviceFunction::RunImpl() {
41   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
42
43   scoped_ptr<EjectDevice::Params> params(EjectDevice::Params::Create(*args_));
44   EXTENSION_FUNCTION_VALIDATE(params.get());
45
46   StorageMonitor::GetInstance()->EnsureInitialized(base::Bind(
47       &SystemStorageEjectDeviceFunction::OnStorageMonitorInit,
48       this,
49       params->id));
50   return true;
51 }
52
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(
59           transient_device_id);
60
61   if (device_id_str == "") {
62     HandleResponse(StorageMonitor::EJECT_NO_SUCH_DEVICE);
63     return;
64   }
65
66   monitor->EjectDevice(
67       device_id_str,
68       base::Bind(&SystemStorageEjectDeviceFunction::HandleResponse,
69                  this));
70 }
71
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::
85           EJECT_DEVICE_RESULT_CODE_NO_SUCH_DEVICE;
86       break;
87     case StorageMonitor::EJECT_FAILURE:
88       result = api::system_storage::EJECT_DEVICE_RESULT_CODE_FAILURE;
89   }
90
91   SetResult(new base::StringValue(
92       api::system_storage::ToString(result)));
93   SendResponse(true);
94 }
95
96 SystemStorageGetAvailableCapacityFunction::
97     SystemStorageGetAvailableCapacityFunction() {
98 }
99
100 SystemStorageGetAvailableCapacityFunction::
101     ~SystemStorageGetAvailableCapacityFunction() {
102 }
103
104 bool SystemStorageGetAvailableCapacityFunction::RunImpl() {
105   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
106
107   scoped_ptr<GetAvailableCapacity::Params> params(
108       GetAvailableCapacity::Params::Create(*args_));
109   EXTENSION_FUNCTION_VALIDATE(params.get());
110
111   StorageMonitor::GetInstance()->EnsureInitialized(base::Bind(
112       &SystemStorageGetAvailableCapacityFunction::OnStorageMonitorInit,
113       this,
114       params->id));
115   return true;
116 }
117
118 void SystemStorageGetAvailableCapacityFunction::OnStorageMonitorInit(
119     const std::string& transient_id) {
120   content::BrowserThread::PostTaskAndReplyWithResult(
121       content::BrowserThread::FILE,
122       FROM_HERE,
123       base::Bind(
124           &StorageInfoProvider::GetStorageFreeSpaceFromTransientIdOnFileThread,
125           StorageInfoProvider::Get(), transient_id),
126       base::Bind(
127           &SystemStorageGetAvailableCapacityFunction::OnQueryCompleted,
128           this, transient_id));
129 }
130
131 void SystemStorageGetAvailableCapacityFunction::OnQueryCompleted(
132     const std::string& transient_id, 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.");
141   }
142   SendResponse(success);
143 }
144
145 }  // namespace extensions