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 // Parses and normalizes a comma-seperated list of accept types into a |
| 140 // std::vector, e.g., from "foo /bar, .TXT, hello/*" into |
| 141 // ["foo/bar", ".txt", "hello/*"]. |
| 142 std::vector<std::string> ParseAcceptValue(const std::string& accept_types) { |
| 143 std::vector<std::string> type_list; |
| 144 std::string normalized_accept_types(accept_types); |
| 145 |
| 146 RemoveChars(normalized_accept_types, kWhitespaceASCII, |
| 147 &normalized_accept_types); |
| 148 StringToLowerASCII(&normalized_accept_types); |
| 149 |
| 150 // Note that type_list might contain blank entries, e.g., if the source data |
| 151 // was "foo,,bar", the output will be "foo", "", "bar". |
| 152 base::SplitString(normalized_accept_types, ',', &type_list); |
| 153 return type_list; |
| 154 } |
| 155 |
| 156 bool GetFileTypesFromAcceptType( |
| 157 const std::string& accept_type, |
| 158 std::vector<FilePath::StringType>* extensions, |
| 159 string16* description) { |
| 160 std::vector<std::string> type_list = ParseAcceptValue(accept_type); |
| 161 std::set<FilePath::StringType> extension_set; |
| 162 int description_id = 0; |
| 163 bool found_description = false; |
| 164 |
| 165 for (std::vector<std::string>::const_iterator iter = type_list.begin(); |
| 166 iter != type_list.end(); ++iter) { |
| 167 std::vector<FilePath::StringType> inner; |
| 168 |
| 169 if (iter->empty()) { |
| 170 // Do nothing. |
| 171 } else if ((*iter)[0] == '.') { |
| 172 // Assume this is a file extension, and update the vector with just a |
| 173 // single extension type. |
| 174 FilePath::StringType ext(iter->begin(), iter->end()); |
| 175 inner.push_back(ext.substr(1)); |
| 176 } else if (*iter == "image/*") { |
| 177 description_id = IDS_IMAGE_FILES; |
| 178 net::GetImageExtensions(&inner); |
| 179 } else if (*iter == "audio/*") { |
| 180 description_id = IDS_AUDIO_FILES; |
| 181 net::GetAudioExtensions(&inner); |
| 182 } else if (*iter == "video/*") { |
| 183 description_id = IDS_VIDEO_FILES; |
| 184 net::GetVideoExtensions(&inner); |
| 185 } else { |
| 186 net::GetExtensionsForMimeType(*iter, &inner); |
| 187 } |
| 188 extension_set.insert(inner.begin(), inner.end()); |
| 189 |
| 190 if (description_id) { |
| 191 if (found_description) |
| 192 description_id = 0; // We already have an accept type with description; |
| 193 // if we find another, give up and use the default. |
| 194 found_description = true; |
| 195 } |
| 196 } |
| 197 |
| 198 extensions->assign(extension_set.begin(), extension_set.end()); |
| 199 if (extensions->empty()) |
| 200 return false; |
| 201 |
| 202 if (description_id) |
| 203 *description = l10n_util::GetStringUTF16(description_id); |
| 204 return true; |
| 205 } |
| 206 |
134 } // namespace | 207 } // namespace |
135 | 208 |
136 namespace extensions { | 209 namespace extensions { |
137 | 210 |
138 bool FileSystemGetDisplayPathFunction::RunImpl() { | 211 bool FileSystemGetDisplayPathFunction::RunImpl() { |
139 std::string filesystem_name; | 212 std::string filesystem_name; |
140 std::string filesystem_path; | 213 std::string filesystem_path; |
141 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &filesystem_name)); | 214 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &filesystem_name)); |
142 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &filesystem_path)); | 215 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &filesystem_path)); |
143 | 216 |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 } | 329 } |
257 | 330 |
258 // Handles showing a dialog to the user to ask for the filename for a file to | 331 // Handles showing a dialog to the user to ask for the filename for a file to |
259 // save or open. | 332 // save or open. |
260 class FileSystemChooseFileFunction::FilePicker | 333 class FileSystemChooseFileFunction::FilePicker |
261 : public SelectFileDialog::Listener { | 334 : public SelectFileDialog::Listener { |
262 public: | 335 public: |
263 FilePicker(FileSystemChooseFileFunction* function, | 336 FilePicker(FileSystemChooseFileFunction* function, |
264 content::WebContents* web_contents, | 337 content::WebContents* web_contents, |
265 const FilePath& suggested_name, | 338 const FilePath& suggested_name, |
| 339 const SelectFileDialog::FileTypeInfo& file_type_info, |
266 SelectFileDialog::Type picker_type, | 340 SelectFileDialog::Type picker_type, |
267 EntryType entry_type) | 341 EntryType entry_type) |
268 : suggested_name_(suggested_name), | 342 : suggested_name_(suggested_name), |
269 entry_type_(entry_type), | 343 entry_type_(entry_type), |
270 function_(function) { | 344 function_(function) { |
271 select_file_dialog_ = SelectFileDialog::Create( | 345 select_file_dialog_ = SelectFileDialog::Create( |
272 this, new ChromeSelectFilePolicy(web_contents)); | 346 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 ? | 347 gfx::NativeWindow owning_window = web_contents ? |
282 platform_util::GetTopLevel(web_contents->GetNativeView()) : NULL; | 348 platform_util::GetTopLevel(web_contents->GetNativeView()) : NULL; |
283 | 349 |
284 if (g_skip_picker_for_test) { | 350 if (g_skip_picker_for_test) { |
285 if (g_path_to_be_picked_for_test) { | 351 if (g_path_to_be_picked_for_test) { |
286 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | 352 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
287 base::Bind( | 353 base::Bind( |
288 &FileSystemChooseFileFunction::FilePicker::FileSelected, | 354 &FileSystemChooseFileFunction::FilePicker::FileSelected, |
289 base::Unretained(this), *g_path_to_be_picked_for_test, 1, | 355 base::Unretained(this), *g_path_to_be_picked_for_test, 1, |
290 static_cast<void*>(NULL))); | 356 static_cast<void*>(NULL))); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
326 EntryType entry_type_; | 392 EntryType entry_type_; |
327 | 393 |
328 scoped_refptr<SelectFileDialog> select_file_dialog_; | 394 scoped_refptr<SelectFileDialog> select_file_dialog_; |
329 scoped_refptr<FileSystemChooseFileFunction> function_; | 395 scoped_refptr<FileSystemChooseFileFunction> function_; |
330 | 396 |
331 DISALLOW_COPY_AND_ASSIGN(FilePicker); | 397 DISALLOW_COPY_AND_ASSIGN(FilePicker); |
332 }; | 398 }; |
333 | 399 |
334 bool FileSystemChooseFileFunction::ShowPicker( | 400 bool FileSystemChooseFileFunction::ShowPicker( |
335 const FilePath& suggested_name, | 401 const FilePath& suggested_name, |
| 402 const SelectFileDialog::FileTypeInfo& file_type_info, |
336 SelectFileDialog::Type picker_type, | 403 SelectFileDialog::Type picker_type, |
337 EntryType entry_type) { | 404 EntryType entry_type) { |
338 ShellWindowRegistry* registry = ShellWindowRegistry::Get(profile()); | 405 ShellWindowRegistry* registry = ShellWindowRegistry::Get(profile()); |
339 DCHECK(registry); | 406 DCHECK(registry); |
340 ShellWindow* shell_window = registry->GetShellWindowForRenderViewHost( | 407 ShellWindow* shell_window = registry->GetShellWindowForRenderViewHost( |
341 render_view_host()); | 408 render_view_host()); |
342 if (!shell_window) { | 409 if (!shell_window) { |
343 error_ = kInvalidCallingPage; | 410 error_ = kInvalidCallingPage; |
344 return false; | 411 return false; |
345 } | 412 } |
346 | 413 |
347 // The file picker will hold a reference to this function instance, preventing | 414 // The file picker will hold a reference to this function instance, preventing |
348 // its destruction (and subsequent sending of the function response) until the | 415 // 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 | 416 // 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. | 417 // will delete itself, which will also free the function instance. |
351 new FilePicker(this, shell_window->web_contents(), suggested_name, | 418 new FilePicker(this, shell_window->web_contents(), suggested_name, |
352 picker_type, entry_type); | 419 file_type_info, picker_type, entry_type); |
353 return true; | 420 return true; |
354 } | 421 } |
355 | 422 |
356 // static | 423 // static |
357 void FileSystemChooseFileFunction::SkipPickerAndAlwaysSelectPathForTest( | 424 void FileSystemChooseFileFunction::SkipPickerAndAlwaysSelectPathForTest( |
358 FilePath* path) { | 425 FilePath* path) { |
359 g_skip_picker_for_test = true; | 426 g_skip_picker_for_test = true; |
360 g_path_to_be_picked_for_test = path; | 427 g_path_to_be_picked_for_test = path; |
361 } | 428 } |
362 | 429 |
(...skipping 19 matching lines...) Expand all Loading... |
382 | 449 |
383 // Don't need to check the file, it's for reading. | 450 // Don't need to check the file, it's for reading. |
384 RegisterFileSystemAndSendResponse(path, READ_ONLY); | 451 RegisterFileSystemAndSendResponse(path, READ_ONLY); |
385 } | 452 } |
386 | 453 |
387 void FileSystemChooseFileFunction::FileSelectionCanceled() { | 454 void FileSystemChooseFileFunction::FileSelectionCanceled() { |
388 error_ = kUserCancelled; | 455 error_ = kUserCancelled; |
389 SendResponse(false); | 456 SendResponse(false); |
390 } | 457 } |
391 | 458 |
| 459 void FileSystemChooseFileFunction::BuildFileTypeInfo( |
| 460 SelectFileDialog::FileTypeInfo* file_type_info, |
| 461 const FilePath::StringType& suggested_extension, |
| 462 const std::vector<std::string>* accepts, |
| 463 const bool* acceptsAllTypes) { |
| 464 file_type_info->include_all_files = true; |
| 465 if (acceptsAllTypes) |
| 466 file_type_info->include_all_files = *acceptsAllTypes; |
| 467 |
| 468 bool need_suggestion = !file_type_info->include_all_files && |
| 469 !suggested_extension.empty(); |
| 470 |
| 471 if (accepts) { |
| 472 for (std::vector<std::string>::const_iterator iter = accepts->begin(); |
| 473 iter != accepts->end(); ++iter) { |
| 474 string16 description; |
| 475 std::vector<FilePath::StringType> extensions; |
| 476 |
| 477 if (!GetFileTypesFromAcceptType(*iter, &extensions, &description)) |
| 478 continue; // No extensions were found. |
| 479 |
| 480 file_type_info->extensions.push_back(extensions); |
| 481 file_type_info->extension_description_overrides.push_back(description); |
| 482 |
| 483 // If we still need to find suggested_extension, hunt for it inside the |
| 484 // extensions returned from GetFileTypesFromAcceptType. |
| 485 if (need_suggestion && std::find(extensions.begin(), |
| 486 extensions.end(), suggested_extension) != extensions.end()) { |
| 487 need_suggestion = false; |
| 488 } |
| 489 } |
| 490 } |
| 491 |
| 492 // If there's nothing in our accepted extension list or we couldn't find the |
| 493 // suggested extension required, then default to accepting all types. |
| 494 if (file_type_info->extensions.empty() || need_suggestion) |
| 495 file_type_info->include_all_files = true; |
| 496 } |
| 497 |
| 498 void FileSystemChooseFileFunction::BuildSuggestion( |
| 499 const std::string *opt_name, |
| 500 FilePath* suggested_name, |
| 501 FilePath::StringType* suggested_extension) { |
| 502 if (opt_name) { |
| 503 *suggested_name = FilePath::FromUTF8Unsafe(*opt_name); |
| 504 |
| 505 // Don't allow any path components; shorten to the base name. This should |
| 506 // result in a relative path, but in some cases may not. Clear the |
| 507 // suggestion for safety if this is the case. |
| 508 *suggested_name = suggested_name->BaseName(); |
| 509 if (suggested_name->IsAbsolute()) |
| 510 *suggested_name = FilePath(); |
| 511 |
| 512 *suggested_extension = suggested_name->Extension(); |
| 513 if (!suggested_extension->empty()) |
| 514 suggested_extension->erase(suggested_extension->begin()); // drop the . |
| 515 } |
| 516 } |
| 517 |
392 bool FileSystemChooseFileFunction::RunImpl() { | 518 bool FileSystemChooseFileFunction::RunImpl() { |
393 scoped_ptr<ChooseFile::Params> params(ChooseFile::Params::Create(*args_)); | 519 scoped_ptr<ChooseFile::Params> params(ChooseFile::Params::Create(*args_)); |
394 EXTENSION_FUNCTION_VALIDATE(params.get()); | 520 EXTENSION_FUNCTION_VALIDATE(params.get()); |
395 | 521 |
396 FilePath suggested_name; | 522 FilePath suggested_name; |
| 523 SelectFileDialog::FileTypeInfo file_type_info; |
397 EntryType entry_type = READ_ONLY; | 524 EntryType entry_type = READ_ONLY; |
398 SelectFileDialog::Type picker_type = SelectFileDialog::SELECT_OPEN_FILE; | 525 SelectFileDialog::Type picker_type = SelectFileDialog::SELECT_OPEN_FILE; |
399 | 526 |
400 file_system::ChooseFileOptions* options = params->options.get(); | 527 file_system::ChooseFileOptions* options = params->options.get(); |
401 if (options) { | 528 if (options) { |
402 if (options->type.get()) { | 529 if (options->type.get()) { |
403 if (*options->type == kOpenWritableFileOption) { | 530 if (*options->type == kOpenWritableFileOption) { |
404 entry_type = WRITABLE; | 531 entry_type = WRITABLE; |
405 } else if (*options->type == kSaveFileOption) { | 532 } else if (*options->type == kSaveFileOption) { |
406 entry_type = WRITABLE; | 533 entry_type = WRITABLE; |
407 picker_type = SelectFileDialog::SELECT_SAVEAS_FILE; | 534 picker_type = SelectFileDialog::SELECT_SAVEAS_FILE; |
408 } else if (*options->type != kOpenFileOption) { | 535 } else if (*options->type != kOpenFileOption) { |
409 error_ = kUnknownChooseFileType; | 536 error_ = kUnknownChooseFileType; |
410 return false; | 537 return false; |
411 } | 538 } |
412 } | 539 } |
413 | 540 |
414 if (options->suggested_name.get()) { | 541 FilePath::StringType suggested_extension; |
415 suggested_name = FilePath::FromUTF8Unsafe( | 542 BuildSuggestion(options->suggested_name.get(), &suggested_name, |
416 *options->suggested_name.get()); | 543 &suggested_extension); |
417 | 544 |
418 // Don't allow any path components; shorten to the base name. This should | 545 BuildFileTypeInfo(&file_type_info, suggested_extension, |
419 // result in a relative path, but in some cases may not. Clear the | 546 options->accepts.get(), 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 } | 547 } |
427 | 548 |
428 if (entry_type == WRITABLE && !HasFileSystemWritePermission()) { | 549 if (entry_type == WRITABLE && !HasFileSystemWritePermission()) { |
429 error_ = kRequiresFileSystemWriteError; | 550 error_ = kRequiresFileSystemWriteError; |
430 return false; | 551 return false; |
431 } | 552 } |
432 | 553 |
433 return ShowPicker(suggested_name, picker_type, entry_type); | 554 return ShowPicker(suggested_name, file_type_info, picker_type, entry_type); |
434 } | 555 } |
435 | 556 |
436 } // namespace extensions | 557 } // namespace extensions |
OLD | NEW |