OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/extensions/api/file_system/file_system_api.h" | 5 #include "chrome/browser/extensions/api/file_system/file_system_api.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/path_service.h" | 11 #include "base/path_service.h" |
12 #include "base/string_split.h" | |
13 #include "base/string_util.h" | |
12 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
13 #include "chrome/browser/extensions/shell_window_registry.h" | 15 #include "chrome/browser/extensions/shell_window_registry.h" |
14 #include "chrome/browser/platform_util.h" | 16 #include "chrome/browser/platform_util.h" |
15 #include "chrome/browser/ui/chrome_select_file_policy.h" | 17 #include "chrome/browser/ui/chrome_select_file_policy.h" |
16 #include "chrome/browser/ui/extensions/shell_window.h" | 18 #include "chrome/browser/ui/extensions/shell_window.h" |
17 #include "chrome/common/extensions/api/file_system.h" | 19 #include "chrome/common/extensions/api/file_system.h" |
18 #include "chrome/common/extensions/permissions/api_permission.h" | 20 #include "chrome/common/extensions/permissions/api_permission.h" |
21 #include "grit/generated_resources.h" | |
22 #include "net/base/mime_util.h" | |
19 #include "content/public/browser/child_process_security_policy.h" | 23 #include "content/public/browser/child_process_security_policy.h" |
20 #include "content/public/browser/render_view_host.h" | 24 #include "content/public/browser/render_view_host.h" |
21 #include "content/public/browser/render_process_host.h" | 25 #include "content/public/browser/render_process_host.h" |
22 #include "content/public/browser/web_contents.h" | 26 #include "content/public/browser/web_contents.h" |
23 #include "webkit/fileapi/file_system_util.h" | 27 #include "webkit/fileapi/file_system_util.h" |
24 #include "webkit/fileapi/isolated_context.h" | 28 #include "webkit/fileapi/isolated_context.h" |
29 #include "ui/base/l10n/l10n_util.h" | |
25 | 30 |
26 using fileapi::IsolatedContext; | 31 using fileapi::IsolatedContext; |
27 | 32 |
28 const char kInvalidParameters[] = "Invalid parameters"; | 33 const char kInvalidParameters[] = "Invalid parameters"; |
29 const char kSecurityError[] = "Security error"; | 34 const char kSecurityError[] = "Security error"; |
30 const char kInvalidCallingPage[] = "Invalid calling page"; | 35 const char kInvalidCallingPage[] = "Invalid calling page"; |
31 const char kUserCancelled[] = "User cancelled"; | 36 const char kUserCancelled[] = "User cancelled"; |
32 const char kWritableFileError[] = "Invalid file for writing"; | 37 const char kWritableFileError[] = "Invalid file for writing"; |
33 const char kRequiresFileSystemWriteError[] = | 38 const char kRequiresFileSystemWriteError[] = |
34 "Operation requires fileSystemWrite permission"; | 39 "Operation requires fileSystemWrite permission"; |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
124 // Create the file if it doesn't already exist. | 129 // Create the file if it doesn't already exist. |
125 base::PlatformFileError error = base::PLATFORM_FILE_OK; | 130 base::PlatformFileError error = base::PLATFORM_FILE_OK; |
126 int creation_flags = base::PLATFORM_FILE_CREATE | | 131 int creation_flags = base::PLATFORM_FILE_CREATE | |
127 base::PLATFORM_FILE_READ | | 132 base::PLATFORM_FILE_READ | |
128 base::PLATFORM_FILE_WRITE; | 133 base::PLATFORM_FILE_WRITE; |
129 base::CreatePlatformFile(path, creation_flags, NULL, &error); | 134 base::CreatePlatformFile(path, creation_flags, NULL, &error); |
130 return error == base::PLATFORM_FILE_OK || | 135 return error == base::PLATFORM_FILE_OK || |
131 error == base::PLATFORM_FILE_ERROR_EXISTS; | 136 error == base::PLATFORM_FILE_ERROR_EXISTS; |
132 } | 137 } |
133 | 138 |
139 std::vector<std::string> ParseAcceptValue(const std::string& accept_types_) { | |
benwells
2012/07/27 03:43:56
Put a comment explaining how it's being parsed.
thorogood
2012/07/27 04:49:55
Done.
| |
140 std::vector<std::string> type_list; | |
141 std::string accept_types(accept_types_); | |
142 | |
143 RemoveChars(accept_types, kWhitespaceASCII, &accept_types); | |
144 StringToLowerASCII(&accept_types); | |
145 | |
146 // Note that type_list might contain blank entries, e.g., if the source data | |
147 // was "foo,,bar", the output will be "foo", "", "bar". | |
148 base::SplitString(accept_types, ',', &type_list); | |
149 return type_list; | |
150 } | |
151 | |
152 std::vector<FilePath::StringType> GetDescriptionIdAndExtensions( | |
benwells
2012/07/27 03:43:56
This is all convoluted, can be simpler by stopping
thorogood
2012/07/27 04:49:55
I certainly agree that it's all convoluted. :)
| |
153 const std::string& type, | |
154 int *description_id) { | |
benwells
2012/07/27 03:43:56
int *des -> int* des
thorogood
2012/07/27 04:49:55
Done.
| |
155 std::vector<FilePath::StringType> extensions; | |
156 | |
157 if (type.empty()) | |
158 return extensions; | |
159 | |
160 if (type[0] == '.') { | |
161 // Assume this is a file extension, and return a list with just a single | |
162 // extension type. | |
163 FilePath::StringType ext(type.begin(), type.end()); | |
164 extensions.push_back(ext.substr(1)); | |
165 } else if (type == "image/*") { | |
166 *description_id = IDS_IMAGE_FILES; | |
167 net::GetImageExtensions(&extensions); | |
168 } else if (type == "audio/*") { | |
169 *description_id = IDS_AUDIO_FILES; | |
170 net::GetAudioExtensions(&extensions); | |
171 } else if (type == "video/*") { | |
172 *description_id = IDS_VIDEO_FILES; | |
173 net::GetVideoExtensions(&extensions); | |
174 } else { | |
175 net::GetExtensionsForMimeType(type, &extensions); | |
176 } | |
177 | |
178 return extensions; | |
179 } | |
180 | |
181 bool GetFileTypesFromAcceptType(const std::string& accept_type, | |
182 std::vector<FilePath::StringType>* extensions, | |
183 string16 *description) { | |
benwells
2012/07/27 03:43:56
string16 *des -> string16* des
thorogood
2012/07/27 04:49:55
Done.
| |
184 std::vector<std::string> type_list = ParseAcceptValue(accept_type); | |
185 std::set<FilePath::StringType> extension_set; | |
186 | |
187 int description_id = 0; | |
188 int valid_type_count = 0; | |
189 | |
190 for (std::vector<std::string>::const_iterator iter = type_list.begin(); | |
191 iter != type_list.end(); ++iter) { | |
192 size_t old_extension_size = extensions->size(); | |
193 | |
194 std::vector<FilePath::StringType> local = GetDescriptionIdAndExtensions( | |
195 *iter, &description_id); | |
196 extension_set.insert(local.begin(), local.end()); | |
197 | |
198 if (extension_set.size() > old_extension_size) | |
199 valid_type_count++; | |
200 } | |
201 | |
202 if (valid_type_count == 0) | |
benwells
2012/07/27 03:43:56
This can change to if (extension_set.size() > 0)
thorogood
2012/07/27 04:49:55
Done, well, to if (extension_set.empty())...
| |
203 return false; | |
204 | |
205 if (!description_id || valid_type_count > 1) { | |
benwells
2012/07/27 03:43:56
This can change to if (has_valid_description) { ..
thorogood
2012/07/27 04:49:55
I've changed it just to "!description_id", since t
| |
206 // We don't have a description ID, or we have one but need to include | |
207 // other valid types (e.g. "image/*,text/html"); build a string like | |
208 // "*.ext1, *.ext2, *.other-ext". | |
209 *description = string16(); | |
210 for (std::set<FilePath::StringType>::const_iterator iter | |
211 = extension_set.begin(); iter != extension_set.end(); ++iter) { | |
212 if (!description->empty()) | |
213 description->append(UTF8ToUTF16(", ")); | |
214 description->append(UTF8ToUTF16("*.")); | |
215 #if defined(OS_WIN) // FilePath::StringType is already string16. | |
216 description->append(*iter); | |
217 #else | |
218 description->append(UTF8ToUTF16(*iter)); | |
219 #endif | |
220 } | |
221 } else { | |
222 *description = l10n_util::GetStringUTF16(description_id); | |
223 } | |
224 extensions->assign(extension_set.begin(), extension_set.end()); | |
225 | |
226 return true; | |
227 } | |
228 | |
134 } // namespace | 229 } // namespace |
135 | 230 |
136 namespace extensions { | 231 namespace extensions { |
137 | 232 |
138 bool FileSystemGetDisplayPathFunction::RunImpl() { | 233 bool FileSystemGetDisplayPathFunction::RunImpl() { |
139 std::string filesystem_name; | 234 std::string filesystem_name; |
140 std::string filesystem_path; | 235 std::string filesystem_path; |
141 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &filesystem_name)); | 236 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &filesystem_name)); |
142 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &filesystem_path)); | 237 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &filesystem_path)); |
143 | 238 |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
256 } | 351 } |
257 | 352 |
258 // Handles showing a dialog to the user to ask for the filename for a file to | 353 // Handles showing a dialog to the user to ask for the filename for a file to |
259 // save or open. | 354 // save or open. |
260 class FileSystemChooseFileFunction::FilePicker | 355 class FileSystemChooseFileFunction::FilePicker |
261 : public SelectFileDialog::Listener { | 356 : public SelectFileDialog::Listener { |
262 public: | 357 public: |
263 FilePicker(FileSystemChooseFileFunction* function, | 358 FilePicker(FileSystemChooseFileFunction* function, |
264 content::WebContents* web_contents, | 359 content::WebContents* web_contents, |
265 const FilePath& suggested_name, | 360 const FilePath& suggested_name, |
361 const SelectFileDialog::FileTypeInfo& file_type_info, | |
266 SelectFileDialog::Type picker_type, | 362 SelectFileDialog::Type picker_type, |
267 EntryType entry_type) | 363 EntryType entry_type) |
268 : suggested_name_(suggested_name), | 364 : suggested_name_(suggested_name), |
269 entry_type_(entry_type), | 365 entry_type_(entry_type), |
270 function_(function) { | 366 function_(function) { |
271 select_file_dialog_ = SelectFileDialog::Create( | 367 select_file_dialog_ = SelectFileDialog::Create( |
272 this, new ChromeSelectFilePolicy(web_contents)); | 368 this, new ChromeSelectFilePolicy(web_contents)); |
273 SelectFileDialog::FileTypeInfo file_type_info; | |
274 FilePath::StringType extension = suggested_name.Extension(); | |
275 if (!extension.empty()) { | |
276 extension.erase(extension.begin()); // drop the . | |
277 file_type_info.extensions.resize(1); | |
278 file_type_info.extensions[0].push_back(extension); | |
279 } | |
280 file_type_info.include_all_files = true; | |
281 gfx::NativeWindow owning_window = web_contents ? | 369 gfx::NativeWindow owning_window = web_contents ? |
282 platform_util::GetTopLevel(web_contents->GetNativeView()) : NULL; | 370 platform_util::GetTopLevel(web_contents->GetNativeView()) : NULL; |
283 | 371 |
284 if (g_skip_picker_for_test) { | 372 if (g_skip_picker_for_test) { |
285 if (g_path_to_be_picked_for_test) { | 373 if (g_path_to_be_picked_for_test) { |
286 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | 374 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
287 base::Bind( | 375 base::Bind( |
288 &FileSystemChooseFileFunction::FilePicker::FileSelected, | 376 &FileSystemChooseFileFunction::FilePicker::FileSelected, |
289 base::Unretained(this), *g_path_to_be_picked_for_test, 1, | 377 base::Unretained(this), *g_path_to_be_picked_for_test, 1, |
290 static_cast<void*>(NULL))); | 378 static_cast<void*>(NULL))); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
326 EntryType entry_type_; | 414 EntryType entry_type_; |
327 | 415 |
328 scoped_refptr<SelectFileDialog> select_file_dialog_; | 416 scoped_refptr<SelectFileDialog> select_file_dialog_; |
329 scoped_refptr<FileSystemChooseFileFunction> function_; | 417 scoped_refptr<FileSystemChooseFileFunction> function_; |
330 | 418 |
331 DISALLOW_COPY_AND_ASSIGN(FilePicker); | 419 DISALLOW_COPY_AND_ASSIGN(FilePicker); |
332 }; | 420 }; |
333 | 421 |
334 bool FileSystemChooseFileFunction::ShowPicker( | 422 bool FileSystemChooseFileFunction::ShowPicker( |
335 const FilePath& suggested_name, | 423 const FilePath& suggested_name, |
424 const SelectFileDialog::FileTypeInfo& file_type_info, | |
336 SelectFileDialog::Type picker_type, | 425 SelectFileDialog::Type picker_type, |
337 EntryType entry_type) { | 426 EntryType entry_type) { |
338 ShellWindowRegistry* registry = ShellWindowRegistry::Get(profile()); | 427 ShellWindowRegistry* registry = ShellWindowRegistry::Get(profile()); |
339 DCHECK(registry); | 428 DCHECK(registry); |
340 ShellWindow* shell_window = registry->GetShellWindowForRenderViewHost( | 429 ShellWindow* shell_window = registry->GetShellWindowForRenderViewHost( |
341 render_view_host()); | 430 render_view_host()); |
342 if (!shell_window) { | 431 if (!shell_window) { |
343 error_ = kInvalidCallingPage; | 432 error_ = kInvalidCallingPage; |
344 return false; | 433 return false; |
345 } | 434 } |
346 | 435 |
347 // The file picker will hold a reference to this function instance, preventing | 436 // The file picker will hold a reference to this function instance, preventing |
348 // its destruction (and subsequent sending of the function response) until the | 437 // its destruction (and subsequent sending of the function response) until the |
349 // user has selected a file or cancelled the picker. At that point, the picker | 438 // user has selected a file or cancelled the picker. At that point, the picker |
350 // will delete itself, which will also free the function instance. | 439 // will delete itself, which will also free the function instance. |
351 new FilePicker(this, shell_window->web_contents(), suggested_name, | 440 new FilePicker(this, shell_window->web_contents(), suggested_name, |
352 picker_type, entry_type); | 441 file_type_info, picker_type, entry_type); |
353 return true; | 442 return true; |
354 } | 443 } |
355 | 444 |
356 // static | 445 // static |
357 void FileSystemChooseFileFunction::SkipPickerAndAlwaysSelectPathForTest( | 446 void FileSystemChooseFileFunction::SkipPickerAndAlwaysSelectPathForTest( |
358 FilePath* path) { | 447 FilePath* path) { |
359 g_skip_picker_for_test = true; | 448 g_skip_picker_for_test = true; |
360 g_path_to_be_picked_for_test = path; | 449 g_path_to_be_picked_for_test = path; |
361 } | 450 } |
362 | 451 |
(...skipping 19 matching lines...) Expand all Loading... | |
382 | 471 |
383 // Don't need to check the file, it's for reading. | 472 // Don't need to check the file, it's for reading. |
384 RegisterFileSystemAndSendResponse(path, READ_ONLY); | 473 RegisterFileSystemAndSendResponse(path, READ_ONLY); |
385 } | 474 } |
386 | 475 |
387 void FileSystemChooseFileFunction::FileSelectionCanceled() { | 476 void FileSystemChooseFileFunction::FileSelectionCanceled() { |
388 error_ = kUserCancelled; | 477 error_ = kUserCancelled; |
389 SendResponse(false); | 478 SendResponse(false); |
390 } | 479 } |
391 | 480 |
481 void FileSystemChooseFileFunction::BuildFileTypeInfo( | |
482 SelectFileDialog::FileTypeInfo* file_type_info, | |
483 const std::vector<std::string>* accepts, | |
484 const bool* acceptsAllTypes) { | |
485 if (accepts) { | |
486 for (std::vector<std::string>::const_iterator iter = accepts->begin(); | |
487 iter != accepts->end(); ++iter) { | |
488 string16 description; | |
489 std::vector<FilePath::StringType> extensions; | |
490 if (GetFileTypesFromAcceptType(*iter, &extensions, &description)) { | |
491 file_type_info->extensions.push_back(extensions); | |
492 file_type_info->extension_description_overrides.push_back(description); | |
493 } | |
494 // TODO(thorogood): If suggested_extension is non-empty, ensure that | |
benwells
2012/07/27 03:43:56
Implement this now or remove the suggested_extensi
thorogood
2012/07/27 04:49:55
Done, taken the implemented approach.
| |
495 // at least once of our extensions list includes it. | |
496 } | |
497 } | |
498 | |
499 if (acceptsAllTypes && !file_type_info->extensions.empty()) { | |
500 file_type_info->include_all_files = *acceptsAllTypes; | |
501 } else { | |
502 // There's nothing in our accepted extension list; default to accepting | |
503 // all types. | |
504 file_type_info->include_all_files = true; | |
505 } | |
506 } | |
507 | |
508 void FileSystemChooseFileFunction::BuildSuggestion( | |
509 const std::string *opt_name, | |
510 FilePath* suggested_name, | |
511 FilePath::StringType* suggested_extension) { | |
512 if (opt_name) { | |
513 *suggested_name = FilePath::FromUTF8Unsafe(*opt_name); | |
514 | |
515 // Don't allow any path components; shorten to the base name. This should | |
516 // result in a relative path, but in some cases may not. Clear the | |
517 // suggestion for safety if this is the case. | |
518 *suggested_name = suggested_name->BaseName(); | |
519 if (suggested_name->IsAbsolute()) { | |
520 *suggested_name = FilePath(); | |
521 } | |
522 *suggested_extension = suggested_name->Extension(); | |
523 if (!suggested_extension->empty()) { | |
524 suggested_extension->erase(suggested_extension->begin()); // drop the . | |
525 } | |
526 } | |
527 } | |
528 | |
392 bool FileSystemChooseFileFunction::RunImpl() { | 529 bool FileSystemChooseFileFunction::RunImpl() { |
393 scoped_ptr<ChooseFile::Params> params(ChooseFile::Params::Create(*args_)); | 530 scoped_ptr<ChooseFile::Params> params(ChooseFile::Params::Create(*args_)); |
394 EXTENSION_FUNCTION_VALIDATE(params.get()); | 531 EXTENSION_FUNCTION_VALIDATE(params.get()); |
395 | 532 |
396 FilePath suggested_name; | 533 FilePath suggested_name; |
534 scoped_ptr<SelectFileDialog::FileTypeInfo> file_type_info( | |
535 new SelectFileDialog::FileTypeInfo()); | |
397 EntryType entry_type = READ_ONLY; | 536 EntryType entry_type = READ_ONLY; |
398 SelectFileDialog::Type picker_type = SelectFileDialog::SELECT_OPEN_FILE; | 537 SelectFileDialog::Type picker_type = SelectFileDialog::SELECT_OPEN_FILE; |
399 | 538 |
400 file_system::ChooseFileOptions* options = params->options.get(); | 539 file_system::ChooseFileOptions* options = params->options.get(); |
401 if (options) { | 540 if (options) { |
402 if (options->type.get()) { | 541 if (options->type.get()) { |
403 if (*options->type == kOpenWritableFileOption) { | 542 if (*options->type == kOpenWritableFileOption) { |
404 entry_type = WRITABLE; | 543 entry_type = WRITABLE; |
405 } else if (*options->type == kSaveFileOption) { | 544 } else if (*options->type == kSaveFileOption) { |
406 entry_type = WRITABLE; | 545 entry_type = WRITABLE; |
407 picker_type = SelectFileDialog::SELECT_SAVEAS_FILE; | 546 picker_type = SelectFileDialog::SELECT_SAVEAS_FILE; |
408 } else if (*options->type != kOpenFileOption) { | 547 } else if (*options->type != kOpenFileOption) { |
409 error_ = kUnknownChooseFileType; | 548 error_ = kUnknownChooseFileType; |
410 return false; | 549 return false; |
411 } | 550 } |
412 } | 551 } |
413 | 552 |
414 if (options->suggested_name.get()) { | 553 FilePath::StringType suggested_extension; |
415 suggested_name = FilePath::FromUTF8Unsafe( | 554 BuildSuggestion(options->suggested_name.get(), &suggested_name, |
416 *options->suggested_name.get()); | 555 &suggested_extension); |
417 | 556 |
418 // Don't allow any path components; shorten to the base name. This should | 557 BuildFileTypeInfo(file_type_info.get(), options->accepts.get(), |
419 // result in a relative path, but in some cases may not. Clear the | 558 options->accepts_all_types.get()); |
420 // suggestion for safety if this is the case. | |
421 suggested_name = suggested_name.BaseName(); | |
422 if (suggested_name.IsAbsolute()) { | |
423 suggested_name = FilePath(); | |
424 } | |
425 } | |
426 } | 559 } |
427 | 560 |
428 if (entry_type == WRITABLE && !HasFileSystemWritePermission()) { | 561 if (entry_type == WRITABLE && !HasFileSystemWritePermission()) { |
429 error_ = kRequiresFileSystemWriteError; | 562 error_ = kRequiresFileSystemWriteError; |
430 return false; | 563 return false; |
431 } | 564 } |
432 | 565 |
433 return ShowPicker(suggested_name, picker_type, entry_type); | 566 return ShowPicker(suggested_name, *file_type_info, picker_type, entry_type); |
434 } | 567 } |
435 | 568 |
436 } // namespace extensions | 569 } // namespace extensions |
OLD | NEW |