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> |
| 8 |
| 9 #include "base/values.h" |
7 #include "chrome/browser/chromeos/file_system_provider/service.h" | 10 #include "chrome/browser/chromeos/file_system_provider/service.h" |
8 #include "chrome/common/extensions/api/file_system_provider.h" | 11 #include "chrome/common/extensions/api/file_system_provider.h" |
| 12 #include "chrome/common/extensions/api/file_system_provider_internal.h" |
9 | 13 |
10 namespace extensions { | 14 namespace extensions { |
11 namespace { | 15 namespace { |
12 | 16 |
13 // Error names from | 17 // Error names from |
14 // http://www.w3.org/TR/file-system-api/#errors-and-exceptions | 18 // http://www.w3.org/TR/file-system-api/#errors-and-exceptions |
15 const char kSecurityErrorName[] = "SecurityError"; | 19 const char kSecurityErrorName[] = "SecurityError"; |
16 | 20 |
17 // Error messages. | 21 // Error messages. |
18 const char kEmptyNameErrorMessage[] = "Empty display name is not allowed."; | 22 const char kEmptyNameErrorMessage[] = "Empty display name is not allowed."; |
19 const char kRegisteringFailedErrorMessage[] = | 23 const char kMountFailedErrorMessage[] = "Mounting the file system failed."; |
20 "Registering the file system failed."; | 24 const char kUnmountFailedErrorMessage[] = "Unmounting the file system failed."; |
| 25 const char kResponseFailedErrorMessage[] = |
| 26 "Sending a response for the request failed."; |
21 | 27 |
22 // Creates a dictionary, which looks like a DOMError. The returned dictionary | 28 // Creates a dictionary, which looks like a DOMError. The returned dictionary |
23 // will be converted to a real DOMError object in | 29 // will be converted to a real DOMError object in |
24 // file_system_provier_custom_bindings.js. | 30 // file_system_provier_custom_bindings.js. |
25 base::DictionaryValue* CreateError(const std::string& name, | 31 base::DictionaryValue* CreateError(const std::string& name, |
26 const std::string& message) { | 32 const std::string& message) { |
27 base::DictionaryValue* error = new base::DictionaryValue(); | 33 base::DictionaryValue* error = new base::DictionaryValue(); |
28 error->SetString("name", name); | 34 error->SetString("name", name); |
29 error->SetString("message", message); | 35 error->SetString("message", message); |
30 return error; | 36 return error; |
31 } | 37 } |
32 | 38 |
| 39 // Converts ProviderError to base::File::Error. This could be redundant, if it |
| 40 // was possible to create DOMError instances in Javascript easily. |
| 41 base::File::Error ProviderErrorToFileError( |
| 42 api::file_system_provider::ProviderError error) { |
| 43 switch (error) { |
| 44 case api::file_system_provider::PROVIDER_ERROR_OK: |
| 45 return base::File::FILE_OK; |
| 46 case api::file_system_provider::PROVIDER_ERROR_IN_USE: |
| 47 return base::File::FILE_ERROR_IN_USE; |
| 48 case api::file_system_provider::PROVIDER_ERROR_EXISTS: |
| 49 return base::File::FILE_ERROR_EXISTS; |
| 50 case api::file_system_provider::PROVIDER_ERROR_NOT_FOUND: |
| 51 return base::File::FILE_ERROR_NOT_FOUND; |
| 52 case api::file_system_provider::PROVIDER_ERROR_ACCESS_DENIED: |
| 53 return base::File::FILE_ERROR_ACCESS_DENIED; |
| 54 case api::file_system_provider::PROVIDER_ERROR_TOO_MANY_OPENED: |
| 55 return base::File::FILE_ERROR_TOO_MANY_OPENED; |
| 56 case api::file_system_provider::PROVIDER_ERROR_NO_MEMORY: |
| 57 return base::File::FILE_ERROR_NO_MEMORY; |
| 58 case api::file_system_provider::PROVIDER_ERROR_NO_SPACE: |
| 59 return base::File::FILE_ERROR_NO_SPACE; |
| 60 case api::file_system_provider::PROVIDER_ERROR_NOT_A_DIRECTORY: |
| 61 return base::File::FILE_ERROR_NOT_A_DIRECTORY; |
| 62 case api::file_system_provider::PROVIDER_ERROR_INVALID_OPERATION: |
| 63 return base::File::FILE_ERROR_INVALID_OPERATION; |
| 64 case api::file_system_provider::PROVIDER_ERROR_SECURITY: |
| 65 return base::File::FILE_ERROR_SECURITY; |
| 66 case api::file_system_provider::PROVIDER_ERROR_ABORT: |
| 67 return base::File::FILE_ERROR_ABORT; |
| 68 case api::file_system_provider::PROVIDER_ERROR_NOT_A_FILE: |
| 69 return base::File::FILE_ERROR_NOT_A_FILE; |
| 70 case api::file_system_provider::PROVIDER_ERROR_NOT_EMPTY: |
| 71 return base::File::FILE_ERROR_NOT_EMPTY; |
| 72 case api::file_system_provider::PROVIDER_ERROR_INVALID_URL: |
| 73 return base::File::FILE_ERROR_INVALID_URL; |
| 74 case api::file_system_provider::PROVIDER_ERROR_IO: |
| 75 return base::File::FILE_ERROR_IO; |
| 76 default: |
| 77 NOTREACHED(); |
| 78 } |
| 79 return base::File::FILE_ERROR_FAILED; |
| 80 } |
| 81 |
33 } // namespace | 82 } // namespace |
34 | 83 |
35 bool FileSystemProviderMountFunction::RunImpl() { | 84 bool FileSystemProviderMountFunction::RunImpl() { |
36 using extensions::api::file_system_provider::Mount::Params; | 85 using api::file_system_provider::Mount::Params; |
37 const scoped_ptr<Params> params(Params::Create(*args_)); | 86 const scoped_ptr<Params> params(Params::Create(*args_)); |
38 EXTENSION_FUNCTION_VALIDATE(params); | 87 EXTENSION_FUNCTION_VALIDATE(params); |
39 | 88 |
40 // It's an error if the display name is empty. | 89 // It's an error if the display name is empty. |
41 if (params->display_name.empty()) { | 90 if (params->display_name.empty()) { |
42 base::ListValue* result = new base::ListValue(); | 91 base::ListValue* result = new base::ListValue(); |
43 result->Append(new base::StringValue("")); | 92 result->Append(new base::StringValue("")); |
44 result->Append(CreateError(kSecurityErrorName, | 93 result->Append(CreateError(kSecurityErrorName, |
45 kEmptyNameErrorMessage)); | 94 kEmptyNameErrorMessage)); |
46 SetResult(result); | 95 SetResult(result); |
47 return false; | 96 return false; |
48 } | 97 } |
49 | 98 |
50 chromeos::file_system_provider::Service* service = | 99 chromeos::file_system_provider::Service* service = |
51 chromeos::file_system_provider::Service::Get(GetProfile()); | 100 chromeos::file_system_provider::Service::Get(GetProfile()); |
52 DCHECK(service); | 101 DCHECK(service); |
53 | 102 |
54 int file_system_id = | 103 int file_system_id = |
55 service->RegisterFileSystem(extension_id(), params->display_name); | 104 service->MountFileSystem(extension_id(), params->display_name); |
56 | 105 |
57 // If the |file_system_id| is zero, then it means that registering the file | 106 // If the |file_system_id| is zero, then it means that registering the file |
58 // system failed. | 107 // system failed. |
59 // TODO(mtomasz): Pass more detailed errors, rather than just a bool. | 108 // TODO(mtomasz): Pass more detailed errors, rather than just a bool. |
60 if (!file_system_id) { | 109 if (!file_system_id) { |
61 base::ListValue* result = new base::ListValue(); | 110 base::ListValue* result = new base::ListValue(); |
62 result->Append(new base::FundamentalValue(0)); | 111 result->Append(new base::FundamentalValue(0)); |
63 result->Append( | 112 result->Append(CreateError(kSecurityErrorName, kMountFailedErrorMessage)); |
64 CreateError(kSecurityErrorName, kRegisteringFailedErrorMessage)); | |
65 SetResult(result); | 113 SetResult(result); |
66 return false; | 114 return false; |
67 } | 115 } |
68 | 116 |
69 base::ListValue* result = new base::ListValue(); | 117 base::ListValue* result = new base::ListValue(); |
70 result->Append(new base::FundamentalValue(file_system_id)); | 118 result->Append(new base::FundamentalValue(file_system_id)); |
71 // Don't append an error on success. | 119 // Don't append an error on success. |
72 | 120 |
73 SetResult(result); | 121 SetResult(result); |
74 SendResponse(true); | 122 SendResponse(true); |
75 return true; | 123 return true; |
76 } | 124 } |
77 | 125 |
| 126 bool FileSystemProviderUnmountFunction::RunImpl() { |
| 127 using api::file_system_provider::Unmount::Params; |
| 128 const scoped_ptr<Params> params(Params::Create(*args_)); |
| 129 EXTENSION_FUNCTION_VALIDATE(params); |
| 130 |
| 131 chromeos::file_system_provider::Service* service = |
| 132 chromeos::file_system_provider::Service::Get(GetProfile()); |
| 133 DCHECK(service); |
| 134 |
| 135 if (!service->UnmountFileSystem(extension_id(), params->file_system_id)) { |
| 136 // TODO(mtomasz): Pass more detailed errors, rather than just a bool. |
| 137 base::ListValue* result = new base::ListValue(); |
| 138 result->Append(CreateError(kSecurityErrorName, kUnmountFailedErrorMessage)); |
| 139 SetResult(result); |
| 140 return false; |
| 141 } |
| 142 |
| 143 base::ListValue* result = new base::ListValue(); |
| 144 SetResult(result); |
| 145 SendResponse(true); |
| 146 return true; |
| 147 } |
| 148 |
| 149 bool FileSystemProviderInternalUnmountRequestedSuccessFunction::RunImpl() { |
| 150 using api::file_system_provider_internal::UnmountRequestedSuccess::Params; |
| 151 const scoped_ptr<Params> params(Params::Create(*args_)); |
| 152 EXTENSION_FUNCTION_VALIDATE(params); |
| 153 |
| 154 chromeos::file_system_provider::Service* service = |
| 155 chromeos::file_system_provider::Service::Get(GetProfile()); |
| 156 DCHECK(service); |
| 157 |
| 158 if (!service->FulfillRequest(extension_id(), |
| 159 params->file_system_id, |
| 160 params->request_id, |
| 161 scoped_ptr<base::DictionaryValue>(), |
| 162 false /* has_more */)) { |
| 163 // TODO(mtomasz): Pass more detailed errors, rather than just a bool. |
| 164 base::ListValue* result = new base::ListValue(); |
| 165 result->Append( |
| 166 CreateError(kSecurityErrorName, kResponseFailedErrorMessage)); |
| 167 SetResult(result); |
| 168 return false; |
| 169 } |
| 170 |
| 171 base::ListValue* result = new base::ListValue(); |
| 172 SetResult(result); |
| 173 SendResponse(true); |
| 174 return true; |
| 175 } |
| 176 |
| 177 bool FileSystemProviderInternalUnmountRequestedErrorFunction::RunImpl() { |
| 178 using api::file_system_provider_internal::UnmountRequestedError::Params; |
| 179 const scoped_ptr<Params> params(Params::Create(*args_)); |
| 180 EXTENSION_FUNCTION_VALIDATE(params); |
| 181 |
| 182 chromeos::file_system_provider::Service* service = |
| 183 chromeos::file_system_provider::Service::Get(GetProfile()); |
| 184 DCHECK(service); |
| 185 |
| 186 // Currently it is not possible to refer to types/enums defined in a different |
| 187 // IDL file. Therefore we need to convert DOMString to ProviderError, since |
| 188 // UnmountRequestedErrorFunction() is defined in a different namespace than |
| 189 // ProvidedError. |
| 190 // TODO(mtomasz): Remove this trick, once IDL supports namespaces correctly. |
| 191 const api::file_system_provider::ProviderError provider_error = |
| 192 api::file_system_provider::ParseProviderError(params->error); |
| 193 |
| 194 if (!service->RejectRequest(extension_id(), |
| 195 params->file_system_id, |
| 196 params->request_id, |
| 197 ProviderErrorToFileError(provider_error))) { |
| 198 // TODO(mtomasz): Pass more detailed errors, rather than just a bool. |
| 199 base::ListValue* result = new base::ListValue(); |
| 200 result->Append( |
| 201 CreateError(kSecurityErrorName, kResponseFailedErrorMessage)); |
| 202 SetResult(result); |
| 203 return false; |
| 204 } |
| 205 |
| 206 base::ListValue* result = new base::ListValue(); |
| 207 SetResult(result); |
| 208 SendResponse(true); |
| 209 return true; |
| 210 } |
| 211 |
78 } // namespace extensions | 212 } // namespace extensions |
OLD | NEW |