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

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: Patch #23 broken 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_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
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 valid_type = 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
172 if (inner.empty())
173 continue;
174
175 if (description_id && valid_type)
176 description_id = 0; // We already have an accept type with label; if
177 // we find another, give up and use the default.
178
179 extension_set.insert(inner.begin(), inner.end());
180 valid_type = true;
181 }
182 }
183
184 if (accept_option.extensions.get()) {
185 std::vector<std::string>* list = accept_option.extensions.get();
186 for (std::vector<std::string>::const_iterator iter = list->begin();
187 iter != list->end(); ++iter) {
188 std::string extension = *iter;
189 StringToLowerASCII(&extension);
190 #if defined(OS_WIN)
191 extension_set.insert(UTF8ToWide(*iter));
192 #else
193 extension_set.insert(*iter);
194 #endif
195 }
196 }
197
198 extensions->assign(extension_set.begin(), extension_set.end());
199 if (extensions->empty())
200 return false;
201
202 if (accept_option.description.get())
203 *description = UTF8ToUTF16(*accept_option.description.get());
204 else if (description_id)
205 *description = l10n_util::GetStringUTF16(description_id);
206
207 return true;
208 }
209
135 } // namespace 210 } // namespace
136 211
137 namespace extensions { 212 namespace extensions {
138 213
139 bool FileSystemGetDisplayPathFunction::RunImpl() { 214 bool FileSystemGetDisplayPathFunction::RunImpl() {
140 std::string filesystem_name; 215 std::string filesystem_name;
141 std::string filesystem_path; 216 std::string filesystem_path;
142 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &filesystem_name)); 217 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &filesystem_name));
143 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &filesystem_path)); 218 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &filesystem_path));
144 219
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
257 } 332 }
258 333
259 // Handles showing a dialog to the user to ask for the filename for a file to 334 // Handles showing a dialog to the user to ask for the filename for a file to
260 // save or open. 335 // save or open.
261 class FileSystemChooseFileFunction::FilePicker 336 class FileSystemChooseFileFunction::FilePicker
262 : public ui::SelectFileDialog::Listener { 337 : public ui::SelectFileDialog::Listener {
263 public: 338 public:
264 FilePicker(FileSystemChooseFileFunction* function, 339 FilePicker(FileSystemChooseFileFunction* function,
265 content::WebContents* web_contents, 340 content::WebContents* web_contents,
266 const FilePath& suggested_name, 341 const FilePath& suggested_name,
342 const ui::SelectFileDialog::FileTypeInfo& file_type_info,
267 ui::SelectFileDialog::Type picker_type, 343 ui::SelectFileDialog::Type picker_type,
268 EntryType entry_type) 344 EntryType entry_type)
269 : suggested_name_(suggested_name), 345 : suggested_name_(suggested_name),
270 entry_type_(entry_type), 346 entry_type_(entry_type),
271 function_(function) { 347 function_(function) {
272 select_file_dialog_ = ui::SelectFileDialog::Create( 348 select_file_dialog_ = ui::SelectFileDialog::Create(
273 this, new ChromeSelectFilePolicy(web_contents)); 349 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 ? 350 gfx::NativeWindow owning_window = web_contents ?
283 platform_util::GetTopLevel(web_contents->GetNativeView()) : NULL; 351 platform_util::GetTopLevel(web_contents->GetNativeView()) : NULL;
284 352
285 if (g_skip_picker_for_test) { 353 if (g_skip_picker_for_test) {
286 if (g_path_to_be_picked_for_test) { 354 if (g_path_to_be_picked_for_test) {
287 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, 355 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
288 base::Bind( 356 base::Bind(
289 &FileSystemChooseFileFunction::FilePicker::FileSelected, 357 &FileSystemChooseFileFunction::FilePicker::FileSelected,
290 base::Unretained(this), *g_path_to_be_picked_for_test, 1, 358 base::Unretained(this), *g_path_to_be_picked_for_test, 1,
291 static_cast<void*>(NULL))); 359 static_cast<void*>(NULL)));
(...skipping 10 matching lines...) Expand all
302 select_file_dialog_->SelectFile(picker_type, 370 select_file_dialog_->SelectFile(picker_type,
303 string16(), 371 string16(),
304 suggested_name, 372 suggested_name,
305 &file_type_info, 0, FILE_PATH_LITERAL(""), 373 &file_type_info, 0, FILE_PATH_LITERAL(""),
306 owning_window, NULL); 374 owning_window, NULL);
307 } 375 }
308 376
309 virtual ~FilePicker() {} 377 virtual ~FilePicker() {}
310 378
311 private: 379 private:
312 // SelectFileDialog::Listener implementation. 380 // ui::SelectFileDialog::Listener implementation.
313 virtual void FileSelected(const FilePath& path, 381 virtual void FileSelected(const FilePath& path,
314 int index, 382 int index,
315 void* params) OVERRIDE { 383 void* params) OVERRIDE {
316 function_->FileSelected(path, entry_type_); 384 function_->FileSelected(path, entry_type_);
317 delete this; 385 delete this;
318 } 386 }
319 387
320 virtual void FileSelectionCanceled(void* params) OVERRIDE { 388 virtual void FileSelectionCanceled(void* params) OVERRIDE {
321 function_->FileSelectionCanceled(); 389 function_->FileSelectionCanceled();
322 delete this; 390 delete this;
323 } 391 }
324 392
325 FilePath suggested_name_; 393 FilePath suggested_name_;
326 394
327 EntryType entry_type_; 395 EntryType entry_type_;
328 396
329 scoped_refptr<ui::SelectFileDialog> select_file_dialog_; 397 scoped_refptr<ui::SelectFileDialog> select_file_dialog_;
330 scoped_refptr<FileSystemChooseFileFunction> function_; 398 scoped_refptr<FileSystemChooseFileFunction> function_;
331 399
332 DISALLOW_COPY_AND_ASSIGN(FilePicker); 400 DISALLOW_COPY_AND_ASSIGN(FilePicker);
333 }; 401 };
334 402
335 bool FileSystemChooseFileFunction::ShowPicker( 403 bool FileSystemChooseFileFunction::ShowPicker(
336 const FilePath& suggested_name, 404 const FilePath& suggested_name,
405 const ui::SelectFileDialog::FileTypeInfo& file_type_info,
337 ui::SelectFileDialog::Type picker_type, 406 ui::SelectFileDialog::Type picker_type,
338 EntryType entry_type) { 407 EntryType entry_type) {
339 ShellWindowRegistry* registry = ShellWindowRegistry::Get(profile()); 408 ShellWindowRegistry* registry = ShellWindowRegistry::Get(profile());
340 DCHECK(registry); 409 DCHECK(registry);
341 ShellWindow* shell_window = registry->GetShellWindowForRenderViewHost( 410 ShellWindow* shell_window = registry->GetShellWindowForRenderViewHost(
342 render_view_host()); 411 render_view_host());
343 if (!shell_window) { 412 if (!shell_window) {
344 error_ = kInvalidCallingPage; 413 error_ = kInvalidCallingPage;
345 return false; 414 return false;
346 } 415 }
347 416
348 // The file picker will hold a reference to this function instance, preventing 417 // The file picker will hold a reference to this function instance, preventing
349 // its destruction (and subsequent sending of the function response) until the 418 // 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 419 // 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. 420 // will delete itself, which will also free the function instance.
352 new FilePicker(this, shell_window->web_contents(), suggested_name, 421 new FilePicker(this, shell_window->web_contents(), suggested_name,
353 picker_type, entry_type); 422 file_type_info, picker_type, entry_type);
354 return true; 423 return true;
355 } 424 }
356 425
357 // static 426 // static
358 void FileSystemChooseFileFunction::SkipPickerAndAlwaysSelectPathForTest( 427 void FileSystemChooseFileFunction::SkipPickerAndAlwaysSelectPathForTest(
359 FilePath* path) { 428 FilePath* path) {
360 g_skip_picker_for_test = true; 429 g_skip_picker_for_test = true;
361 g_path_to_be_picked_for_test = path; 430 g_path_to_be_picked_for_test = path;
362 } 431 }
363 432
(...skipping 19 matching lines...) Expand all
383 452
384 // Don't need to check the file, it's for reading. 453 // Don't need to check the file, it's for reading.
385 RegisterFileSystemAndSendResponse(path, READ_ONLY); 454 RegisterFileSystemAndSendResponse(path, READ_ONLY);
386 } 455 }
387 456
388 void FileSystemChooseFileFunction::FileSelectionCanceled() { 457 void FileSystemChooseFileFunction::FileSelectionCanceled() {
389 error_ = kUserCancelled; 458 error_ = kUserCancelled;
390 SendResponse(false); 459 SendResponse(false);
391 } 460 }
392 461
462 void FileSystemChooseFileFunction::BuildFileTypeInfo(
463 ui::SelectFileDialog::FileTypeInfo* file_type_info,
464 const FilePath::StringType& suggested_extension,
465 const AcceptOptions* accepts,
466 const bool* acceptsAllTypes) {
467 file_type_info->include_all_files = true;
468 if (acceptsAllTypes)
469 file_type_info->include_all_files = *acceptsAllTypes;
470
471 bool need_suggestion = !file_type_info->include_all_files &&
472 !suggested_extension.empty();
473
474 if (accepts) {
475 typedef file_system::AcceptOption AcceptOption;
476 for (std::vector<linked_ptr<AcceptOption> >::const_iterator iter =
477 accepts->begin(); iter != accepts->end(); ++iter) {
478 string16 description;
479 std::vector<FilePath::StringType> extensions;
480
481 if (!GetFileTypesFromAcceptOption(**iter, &extensions, &description))
482 continue; // No extensions were found.
483
484 file_type_info->extensions.push_back(extensions);
485 file_type_info->extension_description_overrides.push_back(description);
486
487 // If we still need to find suggested_extension, hunt for it inside the
488 // extensions returned from GetFileTypesFromAcceptOption.
489 if (need_suggestion && std::find(extensions.begin(),
490 extensions.end(), suggested_extension) != extensions.end()) {
491 need_suggestion = false;
492 }
493 }
494 }
495
496 // If there's nothing in our accepted extension list or we couldn't find the
497 // suggested extension required, then default to accepting all types.
498 if (file_type_info->extensions.empty() || need_suggestion)
499 file_type_info->include_all_files = true;
500 }
501
502 void FileSystemChooseFileFunction::BuildSuggestion(
503 const std::string *opt_name,
504 FilePath* suggested_name,
505 FilePath::StringType* suggested_extension) {
506 if (opt_name) {
507 *suggested_name = FilePath::FromUTF8Unsafe(*opt_name);
508
509 // Don't allow any path components; shorten to the base name. This should
510 // result in a relative path, but in some cases may not. Clear the
511 // suggestion for safety if this is the case.
512 *suggested_name = suggested_name->BaseName();
513 if (suggested_name->IsAbsolute())
514 *suggested_name = FilePath();
515
516 *suggested_extension = suggested_name->Extension();
517 if (!suggested_extension->empty())
518 suggested_extension->erase(suggested_extension->begin()); // drop the .
519 }
520 }
521
393 bool FileSystemChooseFileFunction::RunImpl() { 522 bool FileSystemChooseFileFunction::RunImpl() {
394 scoped_ptr<ChooseFile::Params> params(ChooseFile::Params::Create(*args_)); 523 scoped_ptr<ChooseFile::Params> params(ChooseFile::Params::Create(*args_));
395 EXTENSION_FUNCTION_VALIDATE(params.get()); 524 EXTENSION_FUNCTION_VALIDATE(params.get());
396 525
397 FilePath suggested_name; 526 FilePath suggested_name;
527 ui::SelectFileDialog::FileTypeInfo file_type_info;
398 EntryType entry_type = READ_ONLY; 528 EntryType entry_type = READ_ONLY;
399 ui::SelectFileDialog::Type picker_type = 529 ui::SelectFileDialog::Type picker_type =
400 ui::SelectFileDialog::SELECT_OPEN_FILE; 530 ui::SelectFileDialog::SELECT_OPEN_FILE;
401 531
402 file_system::ChooseFileOptions* options = params->options.get(); 532 file_system::ChooseFileOptions* options = params->options.get();
403 if (options) { 533 if (options) {
404 if (options->type.get()) { 534 if (options->type.get()) {
405 if (*options->type == kOpenWritableFileOption) { 535 if (*options->type == kOpenWritableFileOption) {
406 entry_type = WRITABLE; 536 entry_type = WRITABLE;
407 } else if (*options->type == kSaveFileOption) { 537 } else if (*options->type == kSaveFileOption) {
408 entry_type = WRITABLE; 538 entry_type = WRITABLE;
409 picker_type = ui::SelectFileDialog::SELECT_SAVEAS_FILE; 539 picker_type = ui::SelectFileDialog::SELECT_SAVEAS_FILE;
410 } else if (*options->type != kOpenFileOption) { 540 } else if (*options->type != kOpenFileOption) {
411 error_ = kUnknownChooseFileType; 541 error_ = kUnknownChooseFileType;
412 return false; 542 return false;
413 } 543 }
414 } 544 }
415 545
416 if (options->suggested_name.get()) { 546 FilePath::StringType suggested_extension;
417 suggested_name = FilePath::FromUTF8Unsafe( 547 BuildSuggestion(options->suggested_name.get(), &suggested_name,
418 *options->suggested_name.get()); 548 &suggested_extension);
419 549
420 // Don't allow any path components; shorten to the base name. This should 550 BuildFileTypeInfo(&file_type_info, suggested_extension,
421 // result in a relative path, but in some cases may not. Clear the 551 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 } 552 }
429 553
430 if (entry_type == WRITABLE && !HasFileSystemWritePermission()) { 554 if (entry_type == WRITABLE && !HasFileSystemWritePermission()) {
431 error_ = kRequiresFileSystemWriteError; 555 error_ = kRequiresFileSystemWriteError;
432 return false; 556 return false;
433 } 557 }
434 558
435 return ShowPicker(suggested_name, picker_type, entry_type); 559 return ShowPicker(suggested_name, file_type_info, picker_type, entry_type);
436 } 560 }
437 561
438 } // namespace extensions 562 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698