Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / extensions / file_system_provider / file_system_provider_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/chromeos/extensions/file_system_provider/file_system_provider_api.h"
6
7 #include <string>
8
9 #include "base/debug/trace_event.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/values.h"
12 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_interface.h"
13 #include "chrome/browser/chromeos/file_system_provider/request_manager.h"
14 #include "chrome/browser/chromeos/file_system_provider/request_value.h"
15 #include "chrome/browser/chromeos/file_system_provider/service.h"
16 #include "chrome/common/extensions/api/file_system_provider.h"
17 #include "chrome/common/extensions/api/file_system_provider_internal.h"
18
19 using chromeos::file_system_provider::ProvidedFileSystemInterface;
20 using chromeos::file_system_provider::RequestValue;
21 using chromeos::file_system_provider::Service;
22
23 namespace extensions {
24
25 bool FileSystemProviderMountFunction::RunSync() {
26   using api::file_system_provider::Mount::Params;
27   const scoped_ptr<Params> params(Params::Create(*args_));
28   EXTENSION_FUNCTION_VALIDATE(params);
29
30   // It's an error if the file system Id is empty.
31   if (params->options.file_system_id.empty()) {
32     base::ListValue* result = new base::ListValue();
33     result->Append(CreateError(kSecurityErrorName, kEmptyIdErrorMessage));
34     SetResult(result);
35     return true;
36   }
37
38   // It's an error if the display name is empty.
39   if (params->options.display_name.empty()) {
40     base::ListValue* result = new base::ListValue();
41     result->Append(CreateError(kSecurityErrorName,
42                                kEmptyNameErrorMessage));
43     SetResult(result);
44     return true;
45   }
46
47   Service* service = Service::Get(GetProfile());
48   DCHECK(service);
49   if (!service)
50     return false;
51
52   // TODO(mtomasz): Pass more detailed errors, rather than just a bool.
53   if (!service->MountFileSystem(extension_id(),
54                                 params->options.file_system_id,
55                                 params->options.display_name,
56                                 params->options.writable)) {
57     base::ListValue* result = new base::ListValue();
58     result->Append(CreateError(kSecurityErrorName, kMountFailedErrorMessage));
59     SetResult(result);
60     return true;
61   }
62
63   base::ListValue* result = new base::ListValue();
64   SetResult(result);
65   return true;
66 }
67
68 bool FileSystemProviderUnmountFunction::RunSync() {
69   using api::file_system_provider::Unmount::Params;
70   scoped_ptr<Params> params(Params::Create(*args_));
71   EXTENSION_FUNCTION_VALIDATE(params);
72
73   Service* service = Service::Get(GetProfile());
74   DCHECK(service);
75   if (!service)
76     return false;
77
78   if (!service->UnmountFileSystem(extension_id(),
79                                   params->options.file_system_id,
80                                   Service::UNMOUNT_REASON_USER)) {
81     // TODO(mtomasz): Pass more detailed errors, rather than just a bool.
82     base::ListValue* result = new base::ListValue();
83     result->Append(CreateError(kSecurityErrorName, kUnmountFailedErrorMessage));
84     SetResult(result);
85     return true;
86   }
87
88   base::ListValue* result = new base::ListValue();
89   SetResult(result);
90   return true;
91 }
92
93 bool FileSystemProviderInternalUnmountRequestedSuccessFunction::RunWhenValid() {
94   using api::file_system_provider_internal::UnmountRequestedSuccess::Params;
95   scoped_ptr<Params> params(Params::Create(*args_));
96   EXTENSION_FUNCTION_VALIDATE(params);
97
98   FulfillRequest(RequestValue::CreateForUnmountSuccess(params.Pass()),
99                  false /* has_more */);
100   return true;
101 }
102
103 bool
104 FileSystemProviderInternalGetMetadataRequestedSuccessFunction::RunWhenValid() {
105   using api::file_system_provider_internal::GetMetadataRequestedSuccess::Params;
106   scoped_ptr<Params> params(Params::Create(*args_));
107   EXTENSION_FUNCTION_VALIDATE(params);
108
109   FulfillRequest(RequestValue::CreateForGetMetadataSuccess(params.Pass()),
110                  false /* has_more */);
111   return true;
112 }
113
114 bool FileSystemProviderInternalReadDirectoryRequestedSuccessFunction::
115     RunWhenValid() {
116   using api::file_system_provider_internal::ReadDirectoryRequestedSuccess::
117       Params;
118   scoped_ptr<Params> params(Params::Create(*args_));
119   EXTENSION_FUNCTION_VALIDATE(params);
120
121   const bool has_more = params->has_more;
122   FulfillRequest(RequestValue::CreateForReadDirectorySuccess(params.Pass()),
123                  has_more);
124   return true;
125 }
126
127 bool
128 FileSystemProviderInternalReadFileRequestedSuccessFunction::RunWhenValid() {
129   TRACE_EVENT0("file_system_provider", "ReadFileRequestedSuccess");
130   using api::file_system_provider_internal::ReadFileRequestedSuccess::Params;
131
132   scoped_ptr<Params> params(Params::Create(*args_));
133   EXTENSION_FUNCTION_VALIDATE(params);
134
135   const bool has_more = params->has_more;
136   FulfillRequest(RequestValue::CreateForReadFileSuccess(params.Pass()),
137                  has_more);
138   return true;
139 }
140
141 bool
142 FileSystemProviderInternalOperationRequestedSuccessFunction::RunWhenValid() {
143   using api::file_system_provider_internal::OperationRequestedSuccess::Params;
144   scoped_ptr<Params> params(Params::Create(*args_));
145   EXTENSION_FUNCTION_VALIDATE(params);
146
147   FulfillRequest(scoped_ptr<RequestValue>(
148                      RequestValue::CreateForOperationSuccess(params.Pass())),
149                  false /* has_more */);
150   return true;
151 }
152
153 bool FileSystemProviderInternalOperationRequestedErrorFunction::RunWhenValid() {
154   using api::file_system_provider_internal::OperationRequestedError::Params;
155   scoped_ptr<Params> params(Params::Create(*args_));
156   EXTENSION_FUNCTION_VALIDATE(params);
157
158   const base::File::Error error = ProviderErrorToFileError(params->error);
159   RejectRequest(RequestValue::CreateForOperationError(params.Pass()), error);
160   return true;
161 }
162
163 }  // namespace extensions