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"
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"
19 using chromeos::file_system_provider::ProvidedFileSystemInterface;
20 using chromeos::file_system_provider::RequestValue;
21 using chromeos::file_system_provider::Service;
23 namespace extensions {
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);
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));
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));
47 Service* service = Service::Get(GetProfile());
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));
63 base::ListValue* result = new base::ListValue();
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);
73 Service* service = Service::Get(GetProfile());
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));
88 base::ListValue* result = new base::ListValue();
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);
98 FulfillRequest(RequestValue::CreateForUnmountSuccess(params.Pass()),
99 false /* has_more */);
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);
109 FulfillRequest(RequestValue::CreateForGetMetadataSuccess(params.Pass()),
110 false /* has_more */);
114 bool FileSystemProviderInternalReadDirectoryRequestedSuccessFunction::
116 using api::file_system_provider_internal::ReadDirectoryRequestedSuccess::
118 scoped_ptr<Params> params(Params::Create(*args_));
119 EXTENSION_FUNCTION_VALIDATE(params);
121 const bool has_more = params->has_more;
122 FulfillRequest(RequestValue::CreateForReadDirectorySuccess(params.Pass()),
128 FileSystemProviderInternalReadFileRequestedSuccessFunction::RunWhenValid() {
129 TRACE_EVENT0("file_system_provider", "ReadFileRequestedSuccess");
130 using api::file_system_provider_internal::ReadFileRequestedSuccess::Params;
132 scoped_ptr<Params> params(Params::Create(*args_));
133 EXTENSION_FUNCTION_VALIDATE(params);
135 const bool has_more = params->has_more;
136 FulfillRequest(RequestValue::CreateForReadFileSuccess(params.Pass()),
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);
147 FulfillRequest(scoped_ptr<RequestValue>(
148 RequestValue::CreateForOperationSuccess(params.Pass())),
149 false /* has_more */);
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);
158 const base::File::Error error = ProviderErrorToFileError(params->error);
159 RejectRequest(RequestValue::CreateForOperationError(params.Pass()), error);
163 } // namespace extensions