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/memory/scoped_ptr.h" 10#include "base/values.h" 11#include "chrome/browser/chromeos/file_system_provider/provided_file_system_interface.h" 12#include "chrome/browser/chromeos/file_system_provider/request_manager.h" 13#include "chrome/browser/chromeos/file_system_provider/request_value.h" 14#include "chrome/browser/chromeos/file_system_provider/service.h" 15#include "chrome/common/extensions/api/file_system_provider.h" 16#include "chrome/common/extensions/api/file_system_provider_internal.h" 17 18using chromeos::file_system_provider::ProvidedFileSystemInterface; 19using chromeos::file_system_provider::RequestValue; 20using chromeos::file_system_provider::Service; 21 22namespace extensions { 23 24bool FileSystemProviderMountFunction::RunSync() { 25 using api::file_system_provider::Mount::Params; 26 const scoped_ptr<Params> params(Params::Create(*args_)); 27 EXTENSION_FUNCTION_VALIDATE(params); 28 29 // It's an error if the file system Id is empty. 30 if (params->options.file_system_id.empty()) { 31 base::ListValue* result = new base::ListValue(); 32 result->Append(CreateError(kSecurityErrorName, kEmptyIdErrorMessage)); 33 SetResult(result); 34 return true; 35 } 36 37 // It's an error if the display name is empty. 38 if (params->options.display_name.empty()) { 39 base::ListValue* result = new base::ListValue(); 40 result->Append(CreateError(kSecurityErrorName, 41 kEmptyNameErrorMessage)); 42 SetResult(result); 43 return true; 44 } 45 46 Service* service = Service::Get(GetProfile()); 47 DCHECK(service); 48 if (!service) 49 return false; 50 51 // TODO(mtomasz): Pass more detailed errors, rather than just a bool. 52 if (!service->MountFileSystem(extension_id(), 53 params->options.file_system_id, 54 params->options.display_name)) { 55 base::ListValue* result = new base::ListValue(); 56 result->Append(CreateError(kSecurityErrorName, kMountFailedErrorMessage)); 57 SetResult(result); 58 return true; 59 } 60 61 base::ListValue* result = new base::ListValue(); 62 SetResult(result); 63 return true; 64} 65 66bool FileSystemProviderUnmountFunction::RunSync() { 67 using api::file_system_provider::Unmount::Params; 68 scoped_ptr<Params> params(Params::Create(*args_)); 69 EXTENSION_FUNCTION_VALIDATE(params); 70 71 Service* service = Service::Get(GetProfile()); 72 DCHECK(service); 73 if (!service) 74 return false; 75 76 if (!service->UnmountFileSystem(extension_id(), 77 params->options.file_system_id)) { 78 // TODO(mtomasz): Pass more detailed errors, rather than just a bool. 79 base::ListValue* result = new base::ListValue(); 80 result->Append(CreateError(kSecurityErrorName, kUnmountFailedErrorMessage)); 81 SetResult(result); 82 return true; 83 } 84 85 base::ListValue* result = new base::ListValue(); 86 SetResult(result); 87 return true; 88} 89 90bool FileSystemProviderInternalUnmountRequestedSuccessFunction::RunWhenValid() { 91 using api::file_system_provider_internal::UnmountRequestedSuccess::Params; 92 scoped_ptr<Params> params(Params::Create(*args_)); 93 EXTENSION_FUNCTION_VALIDATE(params); 94 95 FulfillRequest(RequestValue::CreateForUnmountSuccess(params.Pass()), 96 false /* has_more */); 97 return true; 98} 99 100bool FileSystemProviderInternalUnmountRequestedErrorFunction::RunWhenValid() { 101 using api::file_system_provider_internal::UnmountRequestedError::Params; 102 const scoped_ptr<Params> params(Params::Create(*args_)); 103 EXTENSION_FUNCTION_VALIDATE(params); 104 105 RejectRequest(ProviderErrorToFileError(params->error)); 106 return true; 107} 108 109bool 110FileSystemProviderInternalGetMetadataRequestedSuccessFunction::RunWhenValid() { 111 using api::file_system_provider_internal::GetMetadataRequestedSuccess::Params; 112 scoped_ptr<Params> params(Params::Create(*args_)); 113 EXTENSION_FUNCTION_VALIDATE(params); 114 115 FulfillRequest(RequestValue::CreateForGetMetadataSuccess(params.Pass()), 116 false /* has_more */); 117 return true; 118} 119 120bool 121FileSystemProviderInternalGetMetadataRequestedErrorFunction::RunWhenValid() { 122 using api::file_system_provider_internal::GetMetadataRequestedError::Params; 123 const scoped_ptr<Params> params(Params::Create(*args_)); 124 EXTENSION_FUNCTION_VALIDATE(params); 125 126 RejectRequest(ProviderErrorToFileError(params->error)); 127 return true; 128} 129 130bool FileSystemProviderInternalReadDirectoryRequestedSuccessFunction:: 131 RunWhenValid() { 132 using api::file_system_provider_internal::ReadDirectoryRequestedSuccess:: 133 Params; 134 scoped_ptr<Params> params(Params::Create(*args_)); 135 EXTENSION_FUNCTION_VALIDATE(params); 136 137 const bool has_more = params->has_more; 138 FulfillRequest(RequestValue::CreateForReadDirectorySuccess(params.Pass()), 139 has_more); 140 return true; 141} 142 143bool 144FileSystemProviderInternalReadDirectoryRequestedErrorFunction::RunWhenValid() { 145 using api::file_system_provider_internal::ReadDirectoryRequestedError::Params; 146 const scoped_ptr<Params> params(Params::Create(*args_)); 147 EXTENSION_FUNCTION_VALIDATE(params); 148 149 RejectRequest(ProviderErrorToFileError(params->error)); 150 return true; 151} 152 153bool 154FileSystemProviderInternalOpenFileRequestedSuccessFunction::RunWhenValid() { 155 using api::file_system_provider_internal::OpenFileRequestedSuccess::Params; 156 scoped_ptr<Params> params(Params::Create(*args_)); 157 EXTENSION_FUNCTION_VALIDATE(params); 158 159 FulfillRequest(scoped_ptr<RequestValue>(new RequestValue()), 160 false /* has_more */); 161 return true; 162} 163 164bool FileSystemProviderInternalOpenFileRequestedErrorFunction::RunWhenValid() { 165 using api::file_system_provider_internal::OpenFileRequestedError::Params; 166 const scoped_ptr<Params> params(Params::Create(*args_)); 167 EXTENSION_FUNCTION_VALIDATE(params); 168 169 RejectRequest(ProviderErrorToFileError(params->error)); 170 return true; 171} 172 173bool 174FileSystemProviderInternalCloseFileRequestedSuccessFunction::RunWhenValid() { 175 using api::file_system_provider_internal::CloseFileRequestedSuccess::Params; 176 scoped_ptr<Params> params(Params::Create(*args_)); 177 EXTENSION_FUNCTION_VALIDATE(params); 178 179 FulfillRequest(scoped_ptr<RequestValue>(new RequestValue()), 180 false /* has_more */); 181 return true; 182} 183 184bool FileSystemProviderInternalCloseFileRequestedErrorFunction::RunWhenValid() { 185 using api::file_system_provider_internal::CloseFileRequestedError::Params; 186 const scoped_ptr<Params> params(Params::Create(*args_)); 187 EXTENSION_FUNCTION_VALIDATE(params); 188 189 RejectRequest(ProviderErrorToFileError(params->error)); 190 return true; 191} 192 193bool 194FileSystemProviderInternalReadFileRequestedSuccessFunction::RunWhenValid() { 195 using api::file_system_provider_internal::ReadFileRequestedSuccess::Params; 196 scoped_ptr<Params> params(Params::Create(*args_)); 197 EXTENSION_FUNCTION_VALIDATE(params); 198 199 const bool has_more = params->has_more; 200 FulfillRequest(RequestValue::CreateForReadFileSuccess(params.Pass()), 201 has_more); 202 return true; 203} 204 205bool FileSystemProviderInternalReadFileRequestedErrorFunction::RunWhenValid() { 206 using api::file_system_provider_internal::ReadFileRequestedError::Params; 207 const scoped_ptr<Params> params(Params::Create(*args_)); 208 EXTENSION_FUNCTION_VALIDATE(params); 209 210 RejectRequest(ProviderErrorToFileError(params->error)); 211 return true; 212} 213 214} // namespace extensions 215