| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/extensions/file_system_provider/file_system_pr
ovider_api.h" | 5 #include "chrome/browser/chromeos/extensions/file_system_provider/file_system_pr
ovider_api.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_inte
rface.h" | 10 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_inte
rface.h" |
| 11 #include "chrome/browser/chromeos/file_system_provider/request_manager.h" | 11 #include "chrome/browser/chromeos/file_system_provider/request_manager.h" |
| 12 #include "chrome/browser/chromeos/file_system_provider/request_value.h" | 12 #include "chrome/browser/chromeos/file_system_provider/request_value.h" |
| 13 #include "chrome/browser/chromeos/file_system_provider/service.h" | 13 #include "chrome/browser/chromeos/file_system_provider/service.h" |
| 14 #include "chrome/common/extensions/api/file_system_provider.h" | 14 #include "chrome/common/extensions/api/file_system_provider.h" |
| 15 #include "chrome/common/extensions/api/file_system_provider_internal.h" | 15 #include "chrome/common/extensions/api/file_system_provider_internal.h" |
| 16 | 16 |
| 17 using chromeos::file_system_provider::ProvidedFileSystemInterface; | 17 using chromeos::file_system_provider::ProvidedFileSystemInterface; |
| 18 using chromeos::file_system_provider::RequestManager; | |
| 19 using chromeos::file_system_provider::RequestValue; | 18 using chromeos::file_system_provider::RequestValue; |
| 20 using chromeos::file_system_provider::Service; | 19 using chromeos::file_system_provider::Service; |
| 21 | 20 |
| 22 namespace extensions { | 21 namespace extensions { |
| 23 namespace { | |
| 24 | |
| 25 // Error names from | |
| 26 // http://www.w3.org/TR/file-system-api/#errors-and-exceptions | |
| 27 const char kNotFoundErrorName[] = "NotFoundError"; | |
| 28 const char kSecurityErrorName[] = "SecurityError"; | |
| 29 | |
| 30 // Error messages. | |
| 31 const char kEmptyNameErrorMessage[] = "Empty display name is not allowed."; | |
| 32 const char kMountFailedErrorMessage[] = "Mounting the file system failed."; | |
| 33 const char kUnmountFailedErrorMessage[] = "Unmounting the file system failed."; | |
| 34 const char kResponseFailedErrorMessage[] = | |
| 35 "Sending a response for the request failed."; | |
| 36 | |
| 37 // Creates a dictionary, which looks like a DOMError. The returned dictionary | |
| 38 // will be converted to a real DOMError object in | |
| 39 // file_system_provier_custom_bindings.js. | |
| 40 base::DictionaryValue* CreateError(const std::string& name, | |
| 41 const std::string& message) { | |
| 42 base::DictionaryValue* error = new base::DictionaryValue(); | |
| 43 error->SetString("name", name); | |
| 44 error->SetString("message", message); | |
| 45 return error; | |
| 46 } | |
| 47 | |
| 48 // Converts ProviderError to base::File::Error. This could be redundant, if it | |
| 49 // was possible to create DOMError instances in Javascript easily. | |
| 50 base::File::Error ProviderErrorToFileError( | |
| 51 api::file_system_provider::ProviderError error) { | |
| 52 switch (error) { | |
| 53 case api::file_system_provider::PROVIDER_ERROR_OK: | |
| 54 return base::File::FILE_OK; | |
| 55 case api::file_system_provider::PROVIDER_ERROR_IN_USE: | |
| 56 return base::File::FILE_ERROR_IN_USE; | |
| 57 case api::file_system_provider::PROVIDER_ERROR_EXISTS: | |
| 58 return base::File::FILE_ERROR_EXISTS; | |
| 59 case api::file_system_provider::PROVIDER_ERROR_NOT_FOUND: | |
| 60 return base::File::FILE_ERROR_NOT_FOUND; | |
| 61 case api::file_system_provider::PROVIDER_ERROR_ACCESS_DENIED: | |
| 62 return base::File::FILE_ERROR_ACCESS_DENIED; | |
| 63 case api::file_system_provider::PROVIDER_ERROR_TOO_MANY_OPENED: | |
| 64 return base::File::FILE_ERROR_TOO_MANY_OPENED; | |
| 65 case api::file_system_provider::PROVIDER_ERROR_NO_MEMORY: | |
| 66 return base::File::FILE_ERROR_NO_MEMORY; | |
| 67 case api::file_system_provider::PROVIDER_ERROR_NO_SPACE: | |
| 68 return base::File::FILE_ERROR_NO_SPACE; | |
| 69 case api::file_system_provider::PROVIDER_ERROR_NOT_A_DIRECTORY: | |
| 70 return base::File::FILE_ERROR_NOT_A_DIRECTORY; | |
| 71 case api::file_system_provider::PROVIDER_ERROR_INVALID_OPERATION: | |
| 72 return base::File::FILE_ERROR_INVALID_OPERATION; | |
| 73 case api::file_system_provider::PROVIDER_ERROR_SECURITY: | |
| 74 return base::File::FILE_ERROR_SECURITY; | |
| 75 case api::file_system_provider::PROVIDER_ERROR_ABORT: | |
| 76 return base::File::FILE_ERROR_ABORT; | |
| 77 case api::file_system_provider::PROVIDER_ERROR_NOT_A_FILE: | |
| 78 return base::File::FILE_ERROR_NOT_A_FILE; | |
| 79 case api::file_system_provider::PROVIDER_ERROR_NOT_EMPTY: | |
| 80 return base::File::FILE_ERROR_NOT_EMPTY; | |
| 81 case api::file_system_provider::PROVIDER_ERROR_INVALID_URL: | |
| 82 return base::File::FILE_ERROR_INVALID_URL; | |
| 83 case api::file_system_provider::PROVIDER_ERROR_IO: | |
| 84 return base::File::FILE_ERROR_IO; | |
| 85 default: | |
| 86 NOTREACHED(); | |
| 87 } | |
| 88 return base::File::FILE_ERROR_FAILED; | |
| 89 } | |
| 90 | |
| 91 } // namespace | |
| 92 | 22 |
| 93 bool FileSystemProviderMountFunction::RunSync() { | 23 bool FileSystemProviderMountFunction::RunSync() { |
| 94 using api::file_system_provider::Mount::Params; | 24 using api::file_system_provider::Mount::Params; |
| 95 const scoped_ptr<Params> params(Params::Create(*args_)); | 25 const scoped_ptr<Params> params(Params::Create(*args_)); |
| 96 EXTENSION_FUNCTION_VALIDATE(params); | 26 EXTENSION_FUNCTION_VALIDATE(params); |
| 97 | 27 |
| 98 // It's an error if the display name is empty. | 28 // It's an error if the display name is empty. |
| 99 if (params->display_name.empty()) { | 29 if (params->display_name.empty()) { |
| 100 base::ListValue* result = new base::ListValue(); | 30 base::ListValue* result = new base::ListValue(); |
| 101 result->Append(new base::StringValue("")); | 31 result->Append(new base::StringValue("")); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 base::ListValue* result = new base::ListValue(); | 83 base::ListValue* result = new base::ListValue(); |
| 154 SetResult(result); | 84 SetResult(result); |
| 155 return true; | 85 return true; |
| 156 } | 86 } |
| 157 | 87 |
| 158 bool FileSystemProviderInternalUnmountRequestedSuccessFunction::RunSync() { | 88 bool FileSystemProviderInternalUnmountRequestedSuccessFunction::RunSync() { |
| 159 using api::file_system_provider_internal::UnmountRequestedSuccess::Params; | 89 using api::file_system_provider_internal::UnmountRequestedSuccess::Params; |
| 160 scoped_ptr<Params> params(Params::Create(*args_)); | 90 scoped_ptr<Params> params(Params::Create(*args_)); |
| 161 EXTENSION_FUNCTION_VALIDATE(params); | 91 EXTENSION_FUNCTION_VALIDATE(params); |
| 162 | 92 |
| 163 Service* service = Service::Get(GetProfile()); | 93 FulfillRequest(RequestValue::CreateForUnmountSuccess(params.Pass()), |
| 164 DCHECK(service); | 94 false /* has_more */); |
| 165 if (!service) | |
| 166 return false; | |
| 167 | |
| 168 ProvidedFileSystemInterface* file_system = | |
| 169 service->GetProvidedFileSystem(extension_id(), params->file_system_id); | |
| 170 if (!file_system) { | |
| 171 base::ListValue* result = new base::ListValue(); | |
| 172 result->Append( | |
| 173 CreateError(kNotFoundErrorName, kResponseFailedErrorMessage)); | |
| 174 SetResult(result); | |
| 175 return true; | |
| 176 } | |
| 177 | |
| 178 RequestManager* request_manager = file_system->GetRequestManager(); | |
| 179 DCHECK(request_manager); | |
| 180 | |
| 181 const int request_id = params->request_id; | |
| 182 if (!request_manager->FulfillRequest( | |
| 183 request_id, | |
| 184 RequestValue::CreateForUnmountSuccess(params.Pass()), | |
| 185 false /* has_more */)) { | |
| 186 // TODO(mtomasz): Pass more detailed errors, rather than just a bool. | |
| 187 base::ListValue* result = new base::ListValue(); | |
| 188 result->Append( | |
| 189 CreateError(kSecurityErrorName, kResponseFailedErrorMessage)); | |
| 190 SetResult(result); | |
| 191 return true; | |
| 192 } | |
| 193 | |
| 194 base::ListValue* result = new base::ListValue(); | |
| 195 SetResult(result); | |
| 196 return true; | 95 return true; |
| 197 } | 96 } |
| 198 | 97 |
| 199 bool FileSystemProviderInternalUnmountRequestedErrorFunction::RunSync() { | 98 bool FileSystemProviderInternalUnmountRequestedErrorFunction::RunSync() { |
| 200 using api::file_system_provider_internal::UnmountRequestedError::Params; | 99 using api::file_system_provider_internal::UnmountRequestedError::Params; |
| 201 const scoped_ptr<Params> params(Params::Create(*args_)); | 100 const scoped_ptr<Params> params(Params::Create(*args_)); |
| 202 EXTENSION_FUNCTION_VALIDATE(params); | 101 EXTENSION_FUNCTION_VALIDATE(params); |
| 203 | 102 |
| 204 Service* service = Service::Get(GetProfile()); | 103 RejectRequest(ProviderErrorToFileError(params->error)); |
| 205 DCHECK(service); | |
| 206 if (!service) | |
| 207 return false; | |
| 208 | |
| 209 ProvidedFileSystemInterface* file_system = | |
| 210 service->GetProvidedFileSystem(extension_id(), params->file_system_id); | |
| 211 if (!file_system) { | |
| 212 base::ListValue* result = new base::ListValue(); | |
| 213 result->Append( | |
| 214 CreateError(kNotFoundErrorName, kResponseFailedErrorMessage)); | |
| 215 SetResult(result); | |
| 216 return false; | |
| 217 } | |
| 218 | |
| 219 RequestManager* request_manager = file_system->GetRequestManager(); | |
| 220 DCHECK(request_manager); | |
| 221 | |
| 222 if (!request_manager->RejectRequest( | |
| 223 params->request_id, ProviderErrorToFileError(params->error))) { | |
| 224 // TODO(mtomasz): Pass more detailed errors, rather than just a bool. | |
| 225 base::ListValue* result = new base::ListValue(); | |
| 226 result->Append( | |
| 227 CreateError(kSecurityErrorName, kResponseFailedErrorMessage)); | |
| 228 SetResult(result); | |
| 229 return false; | |
| 230 } | |
| 231 | |
| 232 base::ListValue* result = new base::ListValue(); | |
| 233 SetResult(result); | |
| 234 return true; | 104 return true; |
| 235 } | 105 } |
| 236 | 106 |
| 237 bool FileSystemProviderInternalGetMetadataRequestedSuccessFunction::RunSync() { | 107 bool FileSystemProviderInternalGetMetadataRequestedSuccessFunction::RunSync() { |
| 238 // TODO(mtomasz): Create a common class for these internal functions so | |
| 239 // most of the code could be easily reused. | |
| 240 using api::file_system_provider_internal::GetMetadataRequestedSuccess::Params; | 108 using api::file_system_provider_internal::GetMetadataRequestedSuccess::Params; |
| 241 scoped_ptr<Params> params(Params::Create(*args_)); | 109 scoped_ptr<Params> params(Params::Create(*args_)); |
| 242 EXTENSION_FUNCTION_VALIDATE(params); | 110 EXTENSION_FUNCTION_VALIDATE(params); |
| 243 | 111 |
| 244 Service* service = Service::Get(GetProfile()); | 112 FulfillRequest(RequestValue::CreateForGetMetadataSuccess(params.Pass()), |
| 245 DCHECK(service); | 113 false /* has_more */); |
| 246 if (!service) | |
| 247 return false; | |
| 248 | |
| 249 ProvidedFileSystemInterface* file_system = | |
| 250 service->GetProvidedFileSystem(extension_id(), params->file_system_id); | |
| 251 if (!file_system) { | |
| 252 base::ListValue* result = new base::ListValue(); | |
| 253 result->Append( | |
| 254 CreateError(kNotFoundErrorName, kResponseFailedErrorMessage)); | |
| 255 SetResult(result); | |
| 256 return false; | |
| 257 } | |
| 258 | |
| 259 RequestManager* request_manager = file_system->GetRequestManager(); | |
| 260 DCHECK(request_manager); | |
| 261 | |
| 262 const int request_id = params->request_id; | |
| 263 if (!request_manager->FulfillRequest( | |
| 264 request_id, | |
| 265 RequestValue::CreateForGetMetadataSuccess(params.Pass()), | |
| 266 false /* has_more */)) { | |
| 267 // TODO(mtomasz): Pass more detailed errors, rather than just a bool. | |
| 268 base::ListValue* result = new base::ListValue(); | |
| 269 result->Append( | |
| 270 CreateError(kSecurityErrorName, kResponseFailedErrorMessage)); | |
| 271 SetResult(result); | |
| 272 return false; | |
| 273 } | |
| 274 | |
| 275 base::ListValue* result = new base::ListValue(); | |
| 276 SetResult(result); | |
| 277 return true; | 114 return true; |
| 278 } | 115 } |
| 279 | 116 |
| 280 bool FileSystemProviderInternalGetMetadataRequestedErrorFunction::RunSync() { | 117 bool FileSystemProviderInternalGetMetadataRequestedErrorFunction::RunSync() { |
| 281 using api::file_system_provider_internal::UnmountRequestedError::Params; | 118 using api::file_system_provider_internal::GetMetadataRequestedError::Params; |
| 282 const scoped_ptr<Params> params(Params::Create(*args_)); | 119 const scoped_ptr<Params> params(Params::Create(*args_)); |
| 283 EXTENSION_FUNCTION_VALIDATE(params); | 120 EXTENSION_FUNCTION_VALIDATE(params); |
| 284 | 121 |
| 285 Service* service = Service::Get(GetProfile()); | 122 RejectRequest(ProviderErrorToFileError(params->error)); |
| 286 DCHECK(service); | |
| 287 if (!service) | |
| 288 return false; | |
| 289 | |
| 290 ProvidedFileSystemInterface* file_system = | |
| 291 service->GetProvidedFileSystem(extension_id(), params->file_system_id); | |
| 292 if (!file_system) { | |
| 293 base::ListValue* result = new base::ListValue(); | |
| 294 result->Append( | |
| 295 CreateError(kNotFoundErrorName, kResponseFailedErrorMessage)); | |
| 296 SetResult(result); | |
| 297 return true; | |
| 298 } | |
| 299 | |
| 300 RequestManager* request_manager = file_system->GetRequestManager(); | |
| 301 DCHECK(request_manager); | |
| 302 | |
| 303 if (!request_manager->RejectRequest( | |
| 304 params->request_id, ProviderErrorToFileError(params->error))) { | |
| 305 // TODO(mtomasz): Pass more detailed errors, rather than just a bool. | |
| 306 base::ListValue* result = new base::ListValue(); | |
| 307 result->Append( | |
| 308 CreateError(kSecurityErrorName, kResponseFailedErrorMessage)); | |
| 309 SetResult(result); | |
| 310 return true; | |
| 311 } | |
| 312 | |
| 313 base::ListValue* result = new base::ListValue(); | |
| 314 SetResult(result); | |
| 315 return true; | 123 return true; |
| 316 } | 124 } |
| 317 | 125 |
| 318 } // namespace extensions | 126 } // namespace extensions |
| OLD | NEW |