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_util.h" |
12 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
13 #include "chrome/browser/extensions/shell_window_registry.h" | 14 #include "chrome/browser/extensions/shell_window_registry.h" |
14 #include "chrome/browser/platform_util.h" | 15 #include "chrome/browser/platform_util.h" |
15 #include "chrome/browser/ui/chrome_select_file_policy.h" | 16 #include "chrome/browser/ui/chrome_select_file_policy.h" |
16 #include "chrome/browser/ui/extensions/shell_window.h" | 17 #include "chrome/browser/ui/extensions/shell_window.h" |
17 #include "chrome/common/extensions/api/file_system.h" | 18 #include "chrome/common/extensions/api/file_system.h" |
18 #include "chrome/common/extensions/permissions/api_permission.h" | 19 #include "chrome/common/extensions/permissions/api_permission.h" |
| 20 #include "grit/generated_resources.h" |
| 21 #include "net/base/mime_util.h" |
19 #include "content/public/browser/child_process_security_policy.h" | 22 #include "content/public/browser/child_process_security_policy.h" |
20 #include "content/public/browser/render_view_host.h" | 23 #include "content/public/browser/render_view_host.h" |
21 #include "content/public/browser/render_process_host.h" | 24 #include "content/public/browser/render_process_host.h" |
22 #include "content/public/browser/web_contents.h" | 25 #include "content/public/browser/web_contents.h" |
23 #include "webkit/fileapi/file_system_types.h" | 26 #include "webkit/fileapi/file_system_types.h" |
24 #include "webkit/fileapi/file_system_util.h" | 27 #include "webkit/fileapi/file_system_util.h" |
25 #include "webkit/fileapi/isolated_context.h" | 28 #include "webkit/fileapi/isolated_context.h" |
| 29 #include "ui/base/l10n/l10n_util.h" |
| 30 #include "ui/base/dialogs/select_file_dialog.h" |
26 | 31 |
27 using fileapi::IsolatedContext; | 32 using fileapi::IsolatedContext; |
28 | 33 |
29 const char kInvalidParameters[] = "Invalid parameters"; | 34 const char kInvalidParameters[] = "Invalid parameters"; |
30 const char kSecurityError[] = "Security error"; | 35 const char kSecurityError[] = "Security error"; |
31 const char kInvalidCallingPage[] = "Invalid calling page"; | 36 const char kInvalidCallingPage[] = "Invalid calling page"; |
32 const char kUserCancelled[] = "User cancelled"; | 37 const char kUserCancelled[] = "User cancelled"; |
33 const char kWritableFileError[] = "Invalid file for writing"; | 38 const char kWritableFileError[] = "Invalid file for writing"; |
34 const char kRequiresFileSystemWriteError[] = | 39 const char kRequiresFileSystemWriteError[] = |
35 "Operation requires fileSystemWrite permission"; | 40 "Operation requires fileSystemWrite permission"; |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 // Create the file if it doesn't already exist. | 130 // Create the file if it doesn't already exist. |
126 base::PlatformFileError error = base::PLATFORM_FILE_OK; | 131 base::PlatformFileError error = base::PLATFORM_FILE_OK; |
127 int creation_flags = base::PLATFORM_FILE_CREATE | | 132 int creation_flags = base::PLATFORM_FILE_CREATE | |
128 base::PLATFORM_FILE_READ | | 133 base::PLATFORM_FILE_READ | |
129 base::PLATFORM_FILE_WRITE; | 134 base::PLATFORM_FILE_WRITE; |
130 base::CreatePlatformFile(path, creation_flags, NULL, &error); | 135 base::CreatePlatformFile(path, creation_flags, NULL, &error); |
131 return error == base::PLATFORM_FILE_OK || | 136 return error == base::PLATFORM_FILE_OK || |
132 error == base::PLATFORM_FILE_ERROR_EXISTS; | 137 error == base::PLATFORM_FILE_ERROR_EXISTS; |
133 } | 138 } |
134 | 139 |
| 140 // Expand the mime-types and extensions provided in an AcceptOption, returning |
| 141 // them within the passed extension vector. Returns false if no valid types |
| 142 // were found. |
| 143 bool GetFileTypesFromAcceptOption( |
| 144 const file_system::AcceptOption& accept_option, |
| 145 std::vector<FilePath::StringType>* extensions, |
| 146 string16* description) { |
| 147 std::set<FilePath::StringType> extension_set; |
| 148 int description_id = 0; |
| 149 bool found_description = false; |
| 150 |
| 151 if (accept_option.mime_types.get()) { |
| 152 std::vector<std::string>* list = accept_option.mime_types.get(); |
| 153 for (std::vector<std::string>::const_iterator iter = list->begin(); |
| 154 iter != list->end(); ++iter) { |
| 155 std::vector<FilePath::StringType> inner; |
| 156 std::string accept_type = *iter; |
| 157 StringToLowerASCII(&accept_type); |
| 158 |
| 159 if (*iter == "image/*") { |
| 160 description_id = IDS_IMAGE_FILES; |
| 161 net::GetImageExtensions(&inner); |
| 162 } else if (*iter == "audio/*") { |
| 163 description_id = IDS_AUDIO_FILES; |
| 164 net::GetAudioExtensions(&inner); |
| 165 } else if (*iter == "video/*") { |
| 166 description_id = IDS_VIDEO_FILES; |
| 167 net::GetVideoExtensions(&inner); |
| 168 } else { |
| 169 net::GetExtensionsForMimeType(*iter, &inner); |
| 170 } |
| 171 extension_set.insert(inner.begin(), inner.end()); |
| 172 |
| 173 if (description_id) { |
| 174 if (found_description) |
| 175 description_id = 0; // We already have an accept type with label; if |
| 176 // we find another, give up and use the default. |
| 177 found_description = true; |
| 178 } |
| 179 } |
| 180 } |
| 181 |
| 182 if (accept_option.extensions.get()) { |
| 183 std::vector<std::string>* list = accept_option.extensions.get(); |
| 184 for (std::vector<std::string>::const_iterator iter = list->begin(); |
| 185 iter != list->end(); ++iter) { |
| 186 std::string extension = *iter; |
| 187 StringToLowerASCII(&extension); |
| 188 extension_set.insert(FilePath::StringType(*iter)); |
| 189 } |
| 190 } |
| 191 |
| 192 extensions->assign(extension_set.begin(), extension_set.end()); |
| 193 if (extensions->empty()) |
| 194 return false; |
| 195 |
| 196 if (accept_option.description.get()) |
| 197 *description = UTF8ToUTF16(*accept_option.description.get()); |
| 198 else if (description_id) |
| 199 *description = l10n_util::GetStringUTF16(description_id); |
| 200 |
| 201 return true; |
| 202 } |
| 203 |
135 } // namespace | 204 } // namespace |
136 | 205 |
137 namespace extensions { | 206 namespace extensions { |
138 | 207 |
139 bool FileSystemGetDisplayPathFunction::RunImpl() { | 208 bool FileSystemGetDisplayPathFunction::RunImpl() { |
140 std::string filesystem_name; | 209 std::string filesystem_name; |
141 std::string filesystem_path; | 210 std::string filesystem_path; |
142 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &filesystem_name)); | 211 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &filesystem_name)); |
143 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &filesystem_path)); | 212 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &filesystem_path)); |
144 | 213 |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
257 } | 326 } |
258 | 327 |
259 // Handles showing a dialog to the user to ask for the filename for a file to | 328 // Handles showing a dialog to the user to ask for the filename for a file to |
260 // save or open. | 329 // save or open. |
261 class FileSystemChooseFileFunction::FilePicker | 330 class FileSystemChooseFileFunction::FilePicker |
262 : public ui::SelectFileDialog::Listener { | 331 : public ui::SelectFileDialog::Listener { |
263 public: | 332 public: |
264 FilePicker(FileSystemChooseFileFunction* function, | 333 FilePicker(FileSystemChooseFileFunction* function, |
265 content::WebContents* web_contents, | 334 content::WebContents* web_contents, |
266 const FilePath& suggested_name, | 335 const FilePath& suggested_name, |
| 336 const ui::SelectFileDialog::FileTypeInfo& file_type_info, |
267 ui::SelectFileDialog::Type picker_type, | 337 ui::SelectFileDialog::Type picker_type, |
268 EntryType entry_type) | 338 EntryType entry_type) |
269 : suggested_name_(suggested_name), | 339 : suggested_name_(suggested_name), |
270 entry_type_(entry_type), | 340 entry_type_(entry_type), |
271 function_(function) { | 341 function_(function) { |
272 select_file_dialog_ = ui::SelectFileDialog::Create( | 342 select_file_dialog_ = ui::SelectFileDialog::Create( |
273 this, new ChromeSelectFilePolicy(web_contents)); | 343 this, new ChromeSelectFilePolicy(web_contents)); |
274 ui::SelectFileDialog::FileTypeInfo file_type_info; | |
275 FilePath::StringType extension = suggested_name.Extension(); | |
276 if (!extension.empty()) { | |
277 extension.erase(extension.begin()); // drop the . | |
278 file_type_info.extensions.resize(1); | |
279 file_type_info.extensions[0].push_back(extension); | |
280 } | |
281 file_type_info.include_all_files = true; | |
282 gfx::NativeWindow owning_window = web_contents ? | 344 gfx::NativeWindow owning_window = web_contents ? |
283 platform_util::GetTopLevel(web_contents->GetNativeView()) : NULL; | 345 platform_util::GetTopLevel(web_contents->GetNativeView()) : NULL; |
284 | 346 |
285 if (g_skip_picker_for_test) { | 347 if (g_skip_picker_for_test) { |
286 if (g_path_to_be_picked_for_test) { | 348 if (g_path_to_be_picked_for_test) { |
287 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | 349 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
288 base::Bind( | 350 base::Bind( |
289 &FileSystemChooseFileFunction::FilePicker::FileSelected, | 351 &FileSystemChooseFileFunction::FilePicker::FileSelected, |
290 base::Unretained(this), *g_path_to_be_picked_for_test, 1, | 352 base::Unretained(this), *g_path_to_be_picked_for_test, 1, |
291 static_cast<void*>(NULL))); | 353 static_cast<void*>(NULL))); |
(...skipping 10 matching lines...) Expand all Loading... |
302 select_file_dialog_->SelectFile(picker_type, | 364 select_file_dialog_->SelectFile(picker_type, |
303 string16(), | 365 string16(), |
304 suggested_name, | 366 suggested_name, |
305 &file_type_info, 0, FILE_PATH_LITERAL(""), | 367 &file_type_info, 0, FILE_PATH_LITERAL(""), |
306 owning_window, NULL); | 368 owning_window, NULL); |
307 } | 369 } |
308 | 370 |
309 virtual ~FilePicker() {} | 371 virtual ~FilePicker() {} |
310 | 372 |
311 private: | 373 private: |
312 // SelectFileDialog::Listener implementation. | 374 // ui::SelectFileDialog::Listener implementation. |
313 virtual void FileSelected(const FilePath& path, | 375 virtual void FileSelected(const FilePath& path, |
314 int index, | 376 int index, |
315 void* params) OVERRIDE { | 377 void* params) OVERRIDE { |
316 function_->FileSelected(path, entry_type_); | 378 function_->FileSelected(path, entry_type_); |
317 delete this; | 379 delete this; |
318 } | 380 } |
319 | 381 |
320 virtual void FileSelectionCanceled(void* params) OVERRIDE { | 382 virtual void FileSelectionCanceled(void* params) OVERRIDE { |
321 function_->FileSelectionCanceled(); | 383 function_->FileSelectionCanceled(); |
322 delete this; | 384 delete this; |
323 } | 385 } |
324 | 386 |
325 FilePath suggested_name_; | 387 FilePath suggested_name_; |
326 | 388 |
327 EntryType entry_type_; | 389 EntryType entry_type_; |
328 | 390 |
329 scoped_refptr<ui::SelectFileDialog> select_file_dialog_; | 391 scoped_refptr<ui::SelectFileDialog> select_file_dialog_; |
330 scoped_refptr<FileSystemChooseFileFunction> function_; | 392 scoped_refptr<FileSystemChooseFileFunction> function_; |
331 | 393 |
332 DISALLOW_COPY_AND_ASSIGN(FilePicker); | 394 DISALLOW_COPY_AND_ASSIGN(FilePicker); |
333 }; | 395 }; |
334 | 396 |
335 bool FileSystemChooseFileFunction::ShowPicker( | 397 bool FileSystemChooseFileFunction::ShowPicker( |
336 const FilePath& suggested_name, | 398 const FilePath& suggested_name, |
| 399 const ui::SelectFileDialog::FileTypeInfo& file_type_info, |
337 ui::SelectFileDialog::Type picker_type, | 400 ui::SelectFileDialog::Type picker_type, |
338 EntryType entry_type) { | 401 EntryType entry_type) { |
339 ShellWindowRegistry* registry = ShellWindowRegistry::Get(profile()); | 402 ShellWindowRegistry* registry = ShellWindowRegistry::Get(profile()); |
340 DCHECK(registry); | 403 DCHECK(registry); |
341 ShellWindow* shell_window = registry->GetShellWindowForRenderViewHost( | 404 ShellWindow* shell_window = registry->GetShellWindowForRenderViewHost( |
342 render_view_host()); | 405 render_view_host()); |
343 if (!shell_window) { | 406 if (!shell_window) { |
344 error_ = kInvalidCallingPage; | 407 error_ = kInvalidCallingPage; |
345 return false; | 408 return false; |
346 } | 409 } |
347 | 410 |
348 // The file picker will hold a reference to this function instance, preventing | 411 // The file picker will hold a reference to this function instance, preventing |
349 // its destruction (and subsequent sending of the function response) until the | 412 // its destruction (and subsequent sending of the function response) until the |
350 // user has selected a file or cancelled the picker. At that point, the picker | 413 // user has selected a file or cancelled the picker. At that point, the picker |
351 // will delete itself, which will also free the function instance. | 414 // will delete itself, which will also free the function instance. |
352 new FilePicker(this, shell_window->web_contents(), suggested_name, | 415 new FilePicker(this, shell_window->web_contents(), suggested_name, |
353 picker_type, entry_type); | 416 file_type_info, picker_type, entry_type); |
354 return true; | 417 return true; |
355 } | 418 } |
356 | 419 |
357 // static | 420 // static |
358 void FileSystemChooseFileFunction::SkipPickerAndAlwaysSelectPathForTest( | 421 void FileSystemChooseFileFunction::SkipPickerAndAlwaysSelectPathForTest( |
359 FilePath* path) { | 422 FilePath* path) { |
360 g_skip_picker_for_test = true; | 423 g_skip_picker_for_test = true; |
361 g_path_to_be_picked_for_test = path; | 424 g_path_to_be_picked_for_test = path; |
362 } | 425 } |
363 | 426 |
(...skipping 19 matching lines...) Expand all Loading... |
383 | 446 |
384 // Don't need to check the file, it's for reading. | 447 // Don't need to check the file, it's for reading. |
385 RegisterFileSystemAndSendResponse(path, READ_ONLY); | 448 RegisterFileSystemAndSendResponse(path, READ_ONLY); |
386 } | 449 } |
387 | 450 |
388 void FileSystemChooseFileFunction::FileSelectionCanceled() { | 451 void FileSystemChooseFileFunction::FileSelectionCanceled() { |
389 error_ = kUserCancelled; | 452 error_ = kUserCancelled; |
390 SendResponse(false); | 453 SendResponse(false); |
391 } | 454 } |
392 | 455 |
| 456 void FileSystemChooseFileFunction::BuildFileTypeInfo( |
| 457 ui::SelectFileDialog::FileTypeInfo* file_type_info, |
| 458 const FilePath::StringType& suggested_extension, |
| 459 const std::vector<linked_ptr<file_system::AcceptOption> >* accepts, |
| 460 const bool* acceptsAllTypes) { |
| 461 file_type_info->include_all_files = true; |
| 462 if (acceptsAllTypes) |
| 463 file_type_info->include_all_files = *acceptsAllTypes; |
| 464 |
| 465 bool need_suggestion = !file_type_info->include_all_files && |
| 466 !suggested_extension.empty(); |
| 467 |
| 468 if (accepts) { |
| 469 typedef file_system::AcceptOption AcceptOption; |
| 470 for (std::vector<linked_ptr<AcceptOption> >::const_iterator iter = |
| 471 accepts->begin(); iter != accepts->end(); ++iter) { |
| 472 string16 description; |
| 473 std::vector<FilePath::StringType> extensions; |
| 474 |
| 475 if (!GetFileTypesFromAcceptOption(**iter, &extensions, &description)) |
| 476 continue; // No extensions were found. |
| 477 |
| 478 file_type_info->extensions.push_back(extensions); |
| 479 file_type_info->extension_description_overrides.push_back(description); |
| 480 |
| 481 // If we still need to find suggested_extension, hunt for it inside the |
| 482 // extensions returned from GetFileTypesFromAcceptOption. |
| 483 if (need_suggestion && std::find(extensions.begin(), |
| 484 extensions.end(), suggested_extension) != extensions.end()) { |
| 485 need_suggestion = false; |
| 486 } |
| 487 } |
| 488 } |
| 489 |
| 490 // If there's nothing in our accepted extension list or we couldn't find the |
| 491 // suggested extension required, then default to accepting all types. |
| 492 if (file_type_info->extensions.empty() || need_suggestion) |
| 493 file_type_info->include_all_files = true; |
| 494 } |
| 495 |
| 496 void FileSystemChooseFileFunction::BuildSuggestion( |
| 497 const std::string *opt_name, |
| 498 FilePath* suggested_name, |
| 499 FilePath::StringType* suggested_extension) { |
| 500 if (opt_name) { |
| 501 *suggested_name = FilePath::FromUTF8Unsafe(*opt_name); |
| 502 |
| 503 // Don't allow any path components; shorten to the base name. This should |
| 504 // result in a relative path, but in some cases may not. Clear the |
| 505 // suggestion for safety if this is the case. |
| 506 *suggested_name = suggested_name->BaseName(); |
| 507 if (suggested_name->IsAbsolute()) |
| 508 *suggested_name = FilePath(); |
| 509 |
| 510 *suggested_extension = suggested_name->Extension(); |
| 511 if (!suggested_extension->empty()) |
| 512 suggested_extension->erase(suggested_extension->begin()); // drop the . |
| 513 } |
| 514 } |
| 515 |
393 bool FileSystemChooseFileFunction::RunImpl() { | 516 bool FileSystemChooseFileFunction::RunImpl() { |
394 scoped_ptr<ChooseFile::Params> params(ChooseFile::Params::Create(*args_)); | 517 scoped_ptr<ChooseFile::Params> params(ChooseFile::Params::Create(*args_)); |
395 EXTENSION_FUNCTION_VALIDATE(params.get()); | 518 EXTENSION_FUNCTION_VALIDATE(params.get()); |
396 | 519 |
397 FilePath suggested_name; | 520 FilePath suggested_name; |
| 521 ui::SelectFileDialog::FileTypeInfo file_type_info; |
398 EntryType entry_type = READ_ONLY; | 522 EntryType entry_type = READ_ONLY; |
399 ui::SelectFileDialog::Type picker_type = | 523 ui::SelectFileDialog::Type picker_type = |
400 ui::SelectFileDialog::SELECT_OPEN_FILE; | 524 ui::SelectFileDialog::SELECT_OPEN_FILE; |
401 | 525 |
402 file_system::ChooseFileOptions* options = params->options.get(); | 526 file_system::ChooseFileOptions* options = params->options.get(); |
403 if (options) { | 527 if (options) { |
404 if (options->type.get()) { | 528 if (options->type.get()) { |
405 if (*options->type == kOpenWritableFileOption) { | 529 if (*options->type == kOpenWritableFileOption) { |
406 entry_type = WRITABLE; | 530 entry_type = WRITABLE; |
407 } else if (*options->type == kSaveFileOption) { | 531 } else if (*options->type == kSaveFileOption) { |
408 entry_type = WRITABLE; | 532 entry_type = WRITABLE; |
409 picker_type = ui::SelectFileDialog::SELECT_SAVEAS_FILE; | 533 picker_type = ui::SelectFileDialog::SELECT_SAVEAS_FILE; |
410 } else if (*options->type != kOpenFileOption) { | 534 } else if (*options->type != kOpenFileOption) { |
411 error_ = kUnknownChooseFileType; | 535 error_ = kUnknownChooseFileType; |
412 return false; | 536 return false; |
413 } | 537 } |
414 } | 538 } |
415 | 539 |
416 if (options->suggested_name.get()) { | 540 FilePath::StringType suggested_extension; |
417 suggested_name = FilePath::FromUTF8Unsafe( | 541 BuildSuggestion(options->suggested_name.get(), &suggested_name, |
418 *options->suggested_name.get()); | 542 &suggested_extension); |
419 | 543 |
420 // Don't allow any path components; shorten to the base name. This should | 544 BuildFileTypeInfo(&file_type_info, suggested_extension, |
421 // result in a relative path, but in some cases may not. Clear the | 545 options->accepts.get(), options->accepts_all_types.get()); |
422 // suggestion for safety if this is the case. | |
423 suggested_name = suggested_name.BaseName(); | |
424 if (suggested_name.IsAbsolute()) { | |
425 suggested_name = FilePath(); | |
426 } | |
427 } | |
428 } | 546 } |
429 | 547 |
430 if (entry_type == WRITABLE && !HasFileSystemWritePermission()) { | 548 if (entry_type == WRITABLE && !HasFileSystemWritePermission()) { |
431 error_ = kRequiresFileSystemWriteError; | 549 error_ = kRequiresFileSystemWriteError; |
432 return false; | 550 return false; |
433 } | 551 } |
434 | 552 |
435 return ShowPicker(suggested_name, picker_type, entry_type); | 553 return ShowPicker(suggested_name, file_type_info, picker_type, entry_type); |
436 } | 554 } |
437 | 555 |
438 } // namespace extensions | 556 } // namespace extensions |
OLD | NEW |