Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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_pr ovider_api.h" | |
| 6 | |
| 7 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_inte rface.h" | |
| 8 #include "chrome/browser/chromeos/file_system_provider/request_manager.h" | |
| 9 #include "chrome/browser/chromeos/file_system_provider/request_value.h" | |
| 10 #include "chrome/browser/chromeos/file_system_provider/service.h" | |
| 11 #include "chrome/common/extensions/api/file_system_provider_internal.h" | |
| 12 | |
| 13 using chromeos::file_system_provider::ProvidedFileSystemInterface; | |
| 14 using chromeos::file_system_provider::RequestManager; | |
| 15 using chromeos::file_system_provider::RequestValue; | |
| 16 using chromeos::file_system_provider::Service; | |
| 17 | |
| 18 namespace extensions { | |
| 19 | |
| 20 const char kNotFoundErrorName[] = "NotFoundError"; | |
| 21 const char kSecurityErrorName[] = "SecurityError"; | |
| 22 | |
| 23 const char kEmptyNameErrorMessage[] = "Empty display name is not allowed."; | |
| 24 const char kMountFailedErrorMessage[] = "Mounting the file system failed."; | |
| 25 const char kUnmountFailedErrorMessage[] = "Unmounting the file system failed."; | |
| 26 const char kResponseFailedErrorMessage[] = | |
| 27 "Sending a response for the request failed."; | |
| 28 | |
| 29 base::DictionaryValue* CreateError(const std::string& name, | |
| 30 const std::string& message) { | |
| 31 base::DictionaryValue* error = new base::DictionaryValue(); | |
| 32 error->SetString("name", name); | |
| 33 error->SetString("message", message); | |
| 34 return error; | |
| 35 } | |
| 36 | |
| 37 base::File::Error ProviderErrorToFileError( | |
| 38 api::file_system_provider::ProviderError error) { | |
| 39 switch (error) { | |
| 40 case api::file_system_provider::PROVIDER_ERROR_OK: | |
| 41 return base::File::FILE_OK; | |
| 42 case api::file_system_provider::PROVIDER_ERROR_IN_USE: | |
| 43 return base::File::FILE_ERROR_IN_USE; | |
| 44 case api::file_system_provider::PROVIDER_ERROR_EXISTS: | |
| 45 return base::File::FILE_ERROR_EXISTS; | |
| 46 case api::file_system_provider::PROVIDER_ERROR_NOT_FOUND: | |
| 47 return base::File::FILE_ERROR_NOT_FOUND; | |
| 48 case api::file_system_provider::PROVIDER_ERROR_ACCESS_DENIED: | |
| 49 return base::File::FILE_ERROR_ACCESS_DENIED; | |
| 50 case api::file_system_provider::PROVIDER_ERROR_TOO_MANY_OPENED: | |
| 51 return base::File::FILE_ERROR_TOO_MANY_OPENED; | |
| 52 case api::file_system_provider::PROVIDER_ERROR_NO_MEMORY: | |
| 53 return base::File::FILE_ERROR_NO_MEMORY; | |
| 54 case api::file_system_provider::PROVIDER_ERROR_NO_SPACE: | |
| 55 return base::File::FILE_ERROR_NO_SPACE; | |
| 56 case api::file_system_provider::PROVIDER_ERROR_NOT_A_DIRECTORY: | |
| 57 return base::File::FILE_ERROR_NOT_A_DIRECTORY; | |
| 58 case api::file_system_provider::PROVIDER_ERROR_INVALID_OPERATION: | |
| 59 return base::File::FILE_ERROR_INVALID_OPERATION; | |
| 60 case api::file_system_provider::PROVIDER_ERROR_SECURITY: | |
| 61 return base::File::FILE_ERROR_SECURITY; | |
| 62 case api::file_system_provider::PROVIDER_ERROR_ABORT: | |
| 63 return base::File::FILE_ERROR_ABORT; | |
| 64 case api::file_system_provider::PROVIDER_ERROR_NOT_A_FILE: | |
| 65 return base::File::FILE_ERROR_NOT_A_FILE; | |
| 66 case api::file_system_provider::PROVIDER_ERROR_NOT_EMPTY: | |
| 67 return base::File::FILE_ERROR_NOT_EMPTY; | |
| 68 case api::file_system_provider::PROVIDER_ERROR_INVALID_URL: | |
| 69 return base::File::FILE_ERROR_INVALID_URL; | |
| 70 case api::file_system_provider::PROVIDER_ERROR_IO: | |
| 71 return base::File::FILE_ERROR_IO; | |
| 72 default: | |
| 73 NOTREACHED(); | |
|
hashimoto
2014/05/01 09:20:12
nit: How about removing "defualt" here and moving
mtomasz
2014/05/02 01:26:22
Done.
| |
| 74 } | |
| 75 return base::File::FILE_ERROR_FAILED; | |
| 76 } | |
| 77 | |
| 78 FileSystemProviderInternalFunction::FileSystemProviderInternalFunction() | |
| 79 : file_system_id_(0), request_id_(0), is_valid_(false) { | |
| 80 } | |
| 81 | |
| 82 bool FileSystemProviderInternalFunction::Parse() { | |
|
hashimoto
2014/05/01 09:20:12
nit: Our style guide says "Function declaration or
mtomasz
2014/05/02 01:26:22
Done.
| |
| 83 if (!args_->GetInteger(0, &file_system_id_)) | |
|
hashimoto
2014/05/01 09:20:12
nit: !args_->GetInteger(0, &file_system_id_) && !a
mtomasz
2014/05/02 01:26:22
Done.
| |
| 84 return false; | |
| 85 | |
| 86 if (!args_->GetInteger(1, &request_id_)) | |
| 87 return false; | |
| 88 | |
| 89 Service* service = Service::Get(GetProfile()); | |
| 90 DCHECK(service); | |
| 91 if (!service) | |
| 92 return false; | |
| 93 | |
| 94 // Check if the file system exists. Note, that it may be gone if the api | |
| 95 // function involves any asynchronous operations. | |
| 96 ProvidedFileSystemInterface* file_system = | |
| 97 service->GetProvidedFileSystem(extension_id(), file_system_id_); | |
| 98 if (!file_system) { | |
| 99 SetErrorMessage(kNotFoundErrorName, kResponseFailedErrorMessage); | |
| 100 return false; | |
| 101 } | |
| 102 | |
| 103 return true; | |
| 104 } | |
| 105 | |
| 106 bool FileSystemProviderInternalFunction::RejectRequest( | |
| 107 base::File::Error error) { | |
| 108 DCHECK(is_valid_); | |
| 109 if (!is_valid_) | |
| 110 return false; | |
| 111 | |
| 112 Service* service = Service::Get(GetProfile()); | |
| 113 if (!service) | |
| 114 return false; | |
| 115 | |
| 116 ProvidedFileSystemInterface* file_system = | |
| 117 service->GetProvidedFileSystem(extension_id(), file_system_id_); | |
| 118 if (!file_system) { | |
| 119 SetErrorMessage(kNotFoundErrorName, kResponseFailedErrorMessage); | |
| 120 return false; | |
| 121 } | |
| 122 | |
| 123 RequestManager* request_manager = file_system->GetRequestManager(); | |
| 124 DCHECK(request_manager); | |
| 125 if (!request_manager) | |
| 126 return false; | |
| 127 | |
| 128 if (!request_manager->RejectRequest(request_id_, error)) { | |
| 129 SetErrorMessage(kSecurityErrorName, kResponseFailedErrorMessage); | |
| 130 return false; | |
| 131 } | |
| 132 | |
| 133 return true; | |
| 134 } | |
| 135 | |
| 136 bool FileSystemProviderInternalFunction::FulfillRequest( | |
| 137 scoped_ptr<RequestValue> value, | |
| 138 bool has_next) { | |
| 139 DCHECK(is_valid_); | |
| 140 if (!is_valid_) | |
| 141 return false; | |
| 142 | |
| 143 Service* service = Service::Get(GetProfile()); | |
| 144 if (!service) | |
| 145 return false; | |
| 146 | |
| 147 ProvidedFileSystemInterface* file_system = | |
| 148 service->GetProvidedFileSystem(extension_id(), file_system_id_); | |
| 149 if (!file_system) { | |
| 150 SetErrorMessage(kNotFoundErrorName, kResponseFailedErrorMessage); | |
| 151 return false; | |
| 152 } | |
| 153 | |
| 154 RequestManager* request_manager = file_system->GetRequestManager(); | |
| 155 DCHECK(request_manager); | |
| 156 if (!request_manager) | |
| 157 return false; | |
|
hashimoto
2014/05/01 09:20:12
You are repeating the same code to get RequestMana
mtomasz
2014/05/02 01:26:22
I simplified the code. Done.
| |
| 158 | |
| 159 if (!request_manager->FulfillRequest(request_id_, value.Pass(), has_next)) { | |
| 160 SetErrorMessage(kSecurityErrorName, kResponseFailedErrorMessage); | |
| 161 return false; | |
| 162 } | |
| 163 | |
| 164 return true; | |
| 165 } | |
| 166 | |
| 167 void FileSystemProviderInternalFunction::SetErrorMessage( | |
| 168 const std::string& name, | |
| 169 const std::string& message) { | |
| 170 base::ListValue* result = new base::ListValue(); | |
| 171 result->Append(CreateError(kNotFoundErrorName, kResponseFailedErrorMessage)); | |
| 172 SetResult(result); | |
| 173 } | |
| 174 | |
| 175 bool FileSystemProviderInternalFunction::RunImpl() { | |
| 176 DCHECK(args_); | |
| 177 is_valid_ = Parse(); | |
|
hashimoto
2014/05/01 09:20:12
Why saving the result to is_valid_ here?
Do we sti
mtomasz
2014/05/02 01:26:22
I wanted the macro EXTENSION_FUNCTION_VALIDATE to
| |
| 178 SendResponse(RunSync()); | |
| 179 return true; | |
| 180 } | |
| 181 | |
| 182 } // namespace extensions | |
| OLD | NEW |