Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(83)

Side by Side Diff: chrome/browser/extensions/api/file_system/file_system_api.cc

Issue 10692105: Updates file type selector for fileSystem API (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Mihai's suggestions Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_util.h" 26 #include "webkit/fileapi/file_system_util.h"
24 #include "webkit/fileapi/isolated_context.h" 27 #include "webkit/fileapi/isolated_context.h"
28 #include "ui/base/l10n/l10n_util.h"
25 29
26 using fileapi::IsolatedContext; 30 using fileapi::IsolatedContext;
27 31
28 const char kInvalidParameters[] = "Invalid parameters"; 32 const char kInvalidParameters[] = "Invalid parameters";
29 const char kSecurityError[] = "Security error"; 33 const char kSecurityError[] = "Security error";
30 const char kInvalidCallingPage[] = "Invalid calling page"; 34 const char kInvalidCallingPage[] = "Invalid calling page";
31 const char kUserCancelled[] = "User cancelled"; 35 const char kUserCancelled[] = "User cancelled";
32 const char kWritableFileError[] = "Invalid file for writing"; 36 const char kWritableFileError[] = "Invalid file for writing";
33 const char kRequiresFileSystemWriteError[] = 37 const char kRequiresFileSystemWriteError[] =
34 "Operation requires fileSystemWrite permission"; 38 "Operation requires fileSystemWrite permission";
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 // Create the file if it doesn't already exist. 128 // Create the file if it doesn't already exist.
125 base::PlatformFileError error = base::PLATFORM_FILE_OK; 129 base::PlatformFileError error = base::PLATFORM_FILE_OK;
126 int creation_flags = base::PLATFORM_FILE_CREATE | 130 int creation_flags = base::PLATFORM_FILE_CREATE |
127 base::PLATFORM_FILE_READ | 131 base::PLATFORM_FILE_READ |
128 base::PLATFORM_FILE_WRITE; 132 base::PLATFORM_FILE_WRITE;
129 base::CreatePlatformFile(path, creation_flags, NULL, &error); 133 base::CreatePlatformFile(path, creation_flags, NULL, &error);
130 return error == base::PLATFORM_FILE_OK || 134 return error == base::PLATFORM_FILE_OK ||
131 error == base::PLATFORM_FILE_ERROR_EXISTS; 135 error == base::PLATFORM_FILE_ERROR_EXISTS;
132 } 136 }
133 137
138 // Expand the mime-types and extensions provided in an AcceptOption, returning
139 // them within the passed extension vector. Returns false if no valid types
140 // were found.
141 bool GetFileTypesFromAcceptOption(
142 const file_system::AcceptOption& accept_option,
143 std::vector<FilePath::StringType>* extensions,
144 string16* description) {
145 std::set<FilePath::StringType> extension_set;
146 int description_id = 0;
147 bool found_description = false;
148
149 if (accept_option.mime_types.get()) {
150 std::vector<std::string>* list = accept_option.mime_types.get();
151 for (std::vector<std::string>::const_iterator iter = list->begin();
152 iter != list->end(); ++iter) {
153 std::vector<FilePath::StringType> inner;
154 std::string accept_type = *iter;
155 StringToLowerASCII(&accept_type);
156
157 if (*iter == "image/*") {
158 description_id = IDS_IMAGE_FILES;
159 net::GetImageExtensions(&inner);
160 } else if (*iter == "audio/*") {
161 description_id = IDS_AUDIO_FILES;
162 net::GetAudioExtensions(&inner);
163 } else if (*iter == "video/*") {
164 description_id = IDS_VIDEO_FILES;
165 net::GetVideoExtensions(&inner);
166 } else {
167 net::GetExtensionsForMimeType(*iter, &inner);
168 }
169 extension_set.insert(inner.begin(), inner.end());
170
171 if (description_id) {
benwells 2012/07/31 23:01:19 This logic is confusing, and probably not correct.
thorogood 2012/08/03 02:58:02 This was a bug. However, I've made it clear the de
172 if (found_description)
173 description_id = 0; // We already have an accept type with label; if
174 // we find another, give up and use the default.
175 found_description = true;
176 }
177 }
178 }
179
180 if (accept_option.extensions.get()) {
181 std::vector<std::string>* list = accept_option.extensions.get();
182 for (std::vector<std::string>::const_iterator iter = list->begin();
183 iter != list->end(); ++iter) {
184 std::string extension = *iter;
185 StringToLowerASCII(&extension);
186 extension_set.insert(FilePath::StringType(*iter));
187 }
188 }
189
190 extensions->assign(extension_set.begin(), extension_set.end());
191 if (extensions->empty())
192 return false;
193
194 if (accept_option.description.get())
195 *description = UTF8ToUTF16(*accept_option.description.get());
196 else if (description_id)
197 *description = l10n_util::GetStringUTF16(description_id);
198
199 return true;
200 }
201
134 } // namespace 202 } // namespace
135 203
136 namespace extensions { 204 namespace extensions {
137 205
138 bool FileSystemGetDisplayPathFunction::RunImpl() { 206 bool FileSystemGetDisplayPathFunction::RunImpl() {
139 std::string filesystem_name; 207 std::string filesystem_name;
140 std::string filesystem_path; 208 std::string filesystem_path;
141 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &filesystem_name)); 209 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &filesystem_name));
142 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &filesystem_path)); 210 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &filesystem_path));
143 211
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
256 } 324 }
257 325
258 // Handles showing a dialog to the user to ask for the filename for a file to 326 // Handles showing a dialog to the user to ask for the filename for a file to
259 // save or open. 327 // save or open.
260 class FileSystemChooseFileFunction::FilePicker 328 class FileSystemChooseFileFunction::FilePicker
261 : public SelectFileDialog::Listener { 329 : public SelectFileDialog::Listener {
262 public: 330 public:
263 FilePicker(FileSystemChooseFileFunction* function, 331 FilePicker(FileSystemChooseFileFunction* function,
264 content::WebContents* web_contents, 332 content::WebContents* web_contents,
265 const FilePath& suggested_name, 333 const FilePath& suggested_name,
334 const SelectFileDialog::FileTypeInfo& file_type_info,
266 SelectFileDialog::Type picker_type, 335 SelectFileDialog::Type picker_type,
267 EntryType entry_type) 336 EntryType entry_type)
268 : suggested_name_(suggested_name), 337 : suggested_name_(suggested_name),
269 entry_type_(entry_type), 338 entry_type_(entry_type),
270 function_(function) { 339 function_(function) {
271 select_file_dialog_ = SelectFileDialog::Create( 340 select_file_dialog_ = SelectFileDialog::Create(
272 this, new ChromeSelectFilePolicy(web_contents)); 341 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 ? 342 gfx::NativeWindow owning_window = web_contents ?
282 platform_util::GetTopLevel(web_contents->GetNativeView()) : NULL; 343 platform_util::GetTopLevel(web_contents->GetNativeView()) : NULL;
283 344
284 if (g_skip_picker_for_test) { 345 if (g_skip_picker_for_test) {
285 if (g_path_to_be_picked_for_test) { 346 if (g_path_to_be_picked_for_test) {
286 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, 347 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
287 base::Bind( 348 base::Bind(
288 &FileSystemChooseFileFunction::FilePicker::FileSelected, 349 &FileSystemChooseFileFunction::FilePicker::FileSelected,
289 base::Unretained(this), *g_path_to_be_picked_for_test, 1, 350 base::Unretained(this), *g_path_to_be_picked_for_test, 1,
290 static_cast<void*>(NULL))); 351 static_cast<void*>(NULL)));
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
326 EntryType entry_type_; 387 EntryType entry_type_;
327 388
328 scoped_refptr<SelectFileDialog> select_file_dialog_; 389 scoped_refptr<SelectFileDialog> select_file_dialog_;
329 scoped_refptr<FileSystemChooseFileFunction> function_; 390 scoped_refptr<FileSystemChooseFileFunction> function_;
330 391
331 DISALLOW_COPY_AND_ASSIGN(FilePicker); 392 DISALLOW_COPY_AND_ASSIGN(FilePicker);
332 }; 393 };
333 394
334 bool FileSystemChooseFileFunction::ShowPicker( 395 bool FileSystemChooseFileFunction::ShowPicker(
335 const FilePath& suggested_name, 396 const FilePath& suggested_name,
397 const SelectFileDialog::FileTypeInfo& file_type_info,
336 SelectFileDialog::Type picker_type, 398 SelectFileDialog::Type picker_type,
337 EntryType entry_type) { 399 EntryType entry_type) {
338 ShellWindowRegistry* registry = ShellWindowRegistry::Get(profile()); 400 ShellWindowRegistry* registry = ShellWindowRegistry::Get(profile());
339 DCHECK(registry); 401 DCHECK(registry);
340 ShellWindow* shell_window = registry->GetShellWindowForRenderViewHost( 402 ShellWindow* shell_window = registry->GetShellWindowForRenderViewHost(
341 render_view_host()); 403 render_view_host());
342 if (!shell_window) { 404 if (!shell_window) {
343 error_ = kInvalidCallingPage; 405 error_ = kInvalidCallingPage;
344 return false; 406 return false;
345 } 407 }
346 408
347 // The file picker will hold a reference to this function instance, preventing 409 // The file picker will hold a reference to this function instance, preventing
348 // its destruction (and subsequent sending of the function response) until the 410 // 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 411 // 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. 412 // will delete itself, which will also free the function instance.
351 new FilePicker(this, shell_window->web_contents(), suggested_name, 413 new FilePicker(this, shell_window->web_contents(), suggested_name,
352 picker_type, entry_type); 414 file_type_info, picker_type, entry_type);
353 return true; 415 return true;
354 } 416 }
355 417
356 // static 418 // static
357 void FileSystemChooseFileFunction::SkipPickerAndAlwaysSelectPathForTest( 419 void FileSystemChooseFileFunction::SkipPickerAndAlwaysSelectPathForTest(
358 FilePath* path) { 420 FilePath* path) {
359 g_skip_picker_for_test = true; 421 g_skip_picker_for_test = true;
360 g_path_to_be_picked_for_test = path; 422 g_path_to_be_picked_for_test = path;
361 } 423 }
362 424
(...skipping 19 matching lines...) Expand all
382 444
383 // Don't need to check the file, it's for reading. 445 // Don't need to check the file, it's for reading.
384 RegisterFileSystemAndSendResponse(path, READ_ONLY); 446 RegisterFileSystemAndSendResponse(path, READ_ONLY);
385 } 447 }
386 448
387 void FileSystemChooseFileFunction::FileSelectionCanceled() { 449 void FileSystemChooseFileFunction::FileSelectionCanceled() {
388 error_ = kUserCancelled; 450 error_ = kUserCancelled;
389 SendResponse(false); 451 SendResponse(false);
390 } 452 }
391 453
454 void FileSystemChooseFileFunction::BuildFileTypeInfo(
455 SelectFileDialog::FileTypeInfo* file_type_info,
456 const FilePath::StringType& suggested_extension,
457 const std::vector<linked_ptr<file_system::AcceptOption> >* accepts,
458 const bool* acceptsAllTypes) {
459 file_type_info->include_all_files = true;
460 if (acceptsAllTypes)
461 file_type_info->include_all_files = *acceptsAllTypes;
462
463 bool need_suggestion = !file_type_info->include_all_files &&
464 !suggested_extension.empty();
465
466 if (accepts) {
467 typedef file_system::AcceptOption AcceptOption;
468 for (std::vector<linked_ptr<AcceptOption> >::const_iterator iter =
469 accepts->begin(); iter != accepts->end(); ++iter) {
470 string16 description;
471 std::vector<FilePath::StringType> extensions;
472
473 if (!GetFileTypesFromAcceptOption(**iter, &extensions, &description))
474 continue; // No extensions were found.
475
476 file_type_info->extensions.push_back(extensions);
477 file_type_info->extension_description_overrides.push_back(description);
478
479 // If we still need to find suggested_extension, hunt for it inside the
480 // extensions returned from GetFileTypesFromAcceptOption.
481 if (need_suggestion && std::find(extensions.begin(),
482 extensions.end(), suggested_extension) != extensions.end()) {
483 need_suggestion = false;
484 }
485 }
486 }
487
488 // If there's nothing in our accepted extension list or we couldn't find the
489 // suggested extension required, then default to accepting all types.
490 if (file_type_info->extensions.empty() || need_suggestion)
491 file_type_info->include_all_files = true;
492 }
493
494 void FileSystemChooseFileFunction::BuildSuggestion(
495 const std::string *opt_name,
496 FilePath* suggested_name,
497 FilePath::StringType* suggested_extension) {
498 if (opt_name) {
499 *suggested_name = FilePath::FromUTF8Unsafe(*opt_name);
500
501 // Don't allow any path components; shorten to the base name. This should
502 // result in a relative path, but in some cases may not. Clear the
503 // suggestion for safety if this is the case.
504 *suggested_name = suggested_name->BaseName();
505 if (suggested_name->IsAbsolute())
506 *suggested_name = FilePath();
507
508 *suggested_extension = suggested_name->Extension();
509 if (!suggested_extension->empty())
510 suggested_extension->erase(suggested_extension->begin()); // drop the .
511 }
512 }
513
392 bool FileSystemChooseFileFunction::RunImpl() { 514 bool FileSystemChooseFileFunction::RunImpl() {
393 scoped_ptr<ChooseFile::Params> params(ChooseFile::Params::Create(*args_)); 515 scoped_ptr<ChooseFile::Params> params(ChooseFile::Params::Create(*args_));
394 EXTENSION_FUNCTION_VALIDATE(params.get()); 516 EXTENSION_FUNCTION_VALIDATE(params.get());
395 517
396 FilePath suggested_name; 518 FilePath suggested_name;
519 SelectFileDialog::FileTypeInfo file_type_info;
397 EntryType entry_type = READ_ONLY; 520 EntryType entry_type = READ_ONLY;
398 SelectFileDialog::Type picker_type = SelectFileDialog::SELECT_OPEN_FILE; 521 SelectFileDialog::Type picker_type = SelectFileDialog::SELECT_OPEN_FILE;
399 522
400 file_system::ChooseFileOptions* options = params->options.get(); 523 file_system::ChooseFileOptions* options = params->options.get();
401 if (options) { 524 if (options) {
402 if (options->type.get()) { 525 if (options->type.get()) {
403 if (*options->type == kOpenWritableFileOption) { 526 if (*options->type == kOpenWritableFileOption) {
404 entry_type = WRITABLE; 527 entry_type = WRITABLE;
405 } else if (*options->type == kSaveFileOption) { 528 } else if (*options->type == kSaveFileOption) {
406 entry_type = WRITABLE; 529 entry_type = WRITABLE;
407 picker_type = SelectFileDialog::SELECT_SAVEAS_FILE; 530 picker_type = SelectFileDialog::SELECT_SAVEAS_FILE;
408 } else if (*options->type != kOpenFileOption) { 531 } else if (*options->type != kOpenFileOption) {
409 error_ = kUnknownChooseFileType; 532 error_ = kUnknownChooseFileType;
410 return false; 533 return false;
411 } 534 }
412 } 535 }
413 536
414 if (options->suggested_name.get()) { 537 FilePath::StringType suggested_extension;
415 suggested_name = FilePath::FromUTF8Unsafe( 538 BuildSuggestion(options->suggested_name.get(), &suggested_name,
416 *options->suggested_name.get()); 539 &suggested_extension);
417 540
418 // Don't allow any path components; shorten to the base name. This should 541 BuildFileTypeInfo(&file_type_info, suggested_extension,
419 // result in a relative path, but in some cases may not. Clear the 542 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 } 543 }
427 544
428 if (entry_type == WRITABLE && !HasFileSystemWritePermission()) { 545 if (entry_type == WRITABLE && !HasFileSystemWritePermission()) {
429 error_ = kRequiresFileSystemWriteError; 546 error_ = kRequiresFileSystemWriteError;
430 return false; 547 return false;
431 } 548 }
432 549
433 return ShowPicker(suggested_name, picker_type, entry_type); 550 return ShowPicker(suggested_name, file_type_info, picker_type, entry_type);
434 } 551 }
435 552
436 } // namespace extensions 553 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698