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/chromeos/extensions/file_system_provider/file_system_provider_api.h"
10 #include "base/debug/trace_event.h"
11 #include "base/memory/linked_ptr.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/values.h"
14 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_interface.h"
15 #include "chrome/browser/chromeos/file_system_provider/request_manager.h"
16 #include "chrome/browser/chromeos/file_system_provider/request_value.h"
17 #include "chrome/browser/chromeos/file_system_provider/service.h"
18 #include "chrome/common/extensions/api/file_system_provider.h"
19 #include "chrome/common/extensions/api/file_system_provider_internal.h"
21 using chromeos::file_system_provider::ProvidedFileSystemInfo;
22 using chromeos::file_system_provider::ProvidedFileSystemInterface;
23 using chromeos::file_system_provider::RequestValue;
24 using chromeos::file_system_provider::Service;
26 namespace extensions {
28 bool FileSystemProviderMountFunction::RunSync() {
29 using api::file_system_provider::Mount::Params;
30 const scoped_ptr<Params> params(Params::Create(*args_));
31 EXTENSION_FUNCTION_VALIDATE(params);
33 // It's an error if the file system Id is empty.
34 if (params->options.file_system_id.empty()) {
35 base::ListValue* const result = new base::ListValue();
36 result->Append(CreateError(kSecurityErrorName, kEmptyIdErrorMessage));
41 // It's an error if the display name is empty.
42 if (params->options.display_name.empty()) {
43 base::ListValue* const result = new base::ListValue();
44 result->Append(CreateError(kSecurityErrorName,
45 kEmptyNameErrorMessage));
50 Service* const service = Service::Get(GetProfile());
55 // TODO(mtomasz): Pass more detailed errors, rather than just a bool.
56 if (!service->MountFileSystem(extension_id(),
57 params->options.file_system_id,
58 params->options.display_name,
59 params->options.writable)) {
60 base::ListValue* const result = new base::ListValue();
61 result->Append(CreateError(kSecurityErrorName, kMountFailedErrorMessage));
66 base::ListValue* result = new base::ListValue();
71 bool FileSystemProviderUnmountFunction::RunSync() {
72 using api::file_system_provider::Unmount::Params;
73 scoped_ptr<Params> params(Params::Create(*args_));
74 EXTENSION_FUNCTION_VALIDATE(params);
76 Service* const service = Service::Get(GetProfile());
81 if (!service->UnmountFileSystem(extension_id(),
82 params->options.file_system_id,
83 Service::UNMOUNT_REASON_USER)) {
84 // TODO(mtomasz): Pass more detailed errors, rather than just a bool.
85 base::ListValue* result = new base::ListValue();
86 result->Append(CreateError(kSecurityErrorName, kUnmountFailedErrorMessage));
91 base::ListValue* const result = new base::ListValue();
96 bool FileSystemProviderGetAllFunction::RunSync() {
97 using api::file_system_provider::FileSystemInfo;
98 Service* const service = Service::Get(GetProfile());
103 const std::vector<ProvidedFileSystemInfo> file_systems =
104 service->GetProvidedFileSystemInfoList();
105 std::vector<linked_ptr<FileSystemInfo> > items;
107 for (size_t i = 0; i < file_systems.size(); ++i) {
108 linked_ptr<FileSystemInfo> item(new FileSystemInfo);
109 item->file_system_id = file_systems[i].file_system_id();
110 item->display_name = file_systems[i].display_name();
111 item->writable = file_systems[i].writable();
112 items.push_back(item);
115 SetResultList(api::file_system_provider::GetAll::Results::Create(items));
119 bool FileSystemProviderInternalUnmountRequestedSuccessFunction::RunWhenValid() {
120 using api::file_system_provider_internal::UnmountRequestedSuccess::Params;
121 scoped_ptr<Params> params(Params::Create(*args_));
122 EXTENSION_FUNCTION_VALIDATE(params);
124 FulfillRequest(RequestValue::CreateForUnmountSuccess(params.Pass()),
125 false /* has_more */);
130 FileSystemProviderInternalGetMetadataRequestedSuccessFunction::RunWhenValid() {
131 using api::file_system_provider_internal::GetMetadataRequestedSuccess::Params;
132 scoped_ptr<Params> params(Params::Create(*args_));
133 EXTENSION_FUNCTION_VALIDATE(params);
135 FulfillRequest(RequestValue::CreateForGetMetadataSuccess(params.Pass()),
136 false /* has_more */);
140 bool FileSystemProviderInternalReadDirectoryRequestedSuccessFunction::
142 using api::file_system_provider_internal::ReadDirectoryRequestedSuccess::
144 scoped_ptr<Params> params(Params::Create(*args_));
145 EXTENSION_FUNCTION_VALIDATE(params);
147 const bool has_more = params->has_more;
148 FulfillRequest(RequestValue::CreateForReadDirectorySuccess(params.Pass()),
154 FileSystemProviderInternalReadFileRequestedSuccessFunction::RunWhenValid() {
155 TRACE_EVENT0("file_system_provider", "ReadFileRequestedSuccess");
156 using api::file_system_provider_internal::ReadFileRequestedSuccess::Params;
158 scoped_ptr<Params> params(Params::Create(*args_));
159 EXTENSION_FUNCTION_VALIDATE(params);
161 const bool has_more = params->has_more;
162 FulfillRequest(RequestValue::CreateForReadFileSuccess(params.Pass()),
168 FileSystemProviderInternalOperationRequestedSuccessFunction::RunWhenValid() {
169 using api::file_system_provider_internal::OperationRequestedSuccess::Params;
170 scoped_ptr<Params> params(Params::Create(*args_));
171 EXTENSION_FUNCTION_VALIDATE(params);
173 FulfillRequest(scoped_ptr<RequestValue>(
174 RequestValue::CreateForOperationSuccess(params.Pass())),
175 false /* has_more */);
179 bool FileSystemProviderInternalOperationRequestedErrorFunction::RunWhenValid() {
180 using api::file_system_provider_internal::OperationRequestedError::Params;
181 scoped_ptr<Params> params(Params::Create(*args_));
182 EXTENSION_FUNCTION_VALIDATE(params);
184 const base::File::Error error = ProviderErrorToFileError(params->error);
185 RejectRequest(RequestValue::CreateForOperationError(params.Pass()), error);
189 } // namespace extensions