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

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: All code now inside file_system_api.cc Created 8 years, 5 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_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
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) {
140 // NOTE: Mostly stolen from ppb_file_chooser_impl.cc
141 std::vector<std::string> type_list;
142 if (accept_types.empty())
143 return type_list;
144 base::SplitString(accept_types, ',', &type_list);
145 std::vector<std::string> normalized_type_list;
146 normalized_type_list.reserve(type_list.size());
147
148 for (std::vector<std::string>::const_iterator iter = type_list.begin();
149 iter != type_list.end(); ++iter) {
150 std::string type;
151 TrimWhitespaceASCII(*iter, TRIM_ALL, &type);
152
153 if (type.empty())
154 continue;
155
156 StringToLowerASCII(&type);
157 normalized_type_list.push_back(type);
benwells 2012/07/24 06:01:27 Could this be made simpler by 1) converting accept
158 }
159
160 return normalized_type_list;
161 }
162
163 bool GetFileTypesFromAcceptType(const std::string& accept_type,
164 std::vector<FilePath::StringType>* extensions,
165 string16 *description) {
166 std::vector<std::string> type_list = ParseAcceptValue(accept_type);
167
168 int description_id = 0;
169 int valid_type_count = 0;
170 size_t old_extension_size = extensions->size();
171 for (std::vector<std::string>::const_iterator iter = type_list.begin();
172 iter != type_list.end(); ++iter) {
173
174 if ((*iter)[0] == '.') {
175 // Assume this is a file extension, and add it to its own place insie
benwells 2012/07/24 06:01:27 insie->inside
thorogood 2012/07/25 05:02:58 Done.
176 // the list.
177 FilePath::StringType ext(iter->begin(), iter->end());
178 extensions->push_back(ext.substr(1));
179 } else if (*iter == "image/*") {
180 description_id = IDS_IMAGE_FILES;
181 net::GetImageExtensions(extensions);
182 } else if (*iter == "audio/*") {
183 description_id = IDS_AUDIO_FILES;
184 net::GetAudioExtensions(extensions);
185 } else if (*iter == "video/*") {
186 description_id = IDS_VIDEO_FILES;
benwells 2012/07/24 06:01:27 This will overwrite description if it is already s
thorogood 2012/07/25 05:02:59 Yes. This isn't actually what we want, but the val
187 net::GetVideoExtensions(extensions);
188 } else {
189 net::GetExtensionsForMimeType(*iter, extensions);
190 }
191 // TODO(thorogood): It's possible to add the same extension multiple times.
benwells 2012/07/24 06:01:27 Are you planning on fixing this? Depending on what
thorogood 2012/07/25 05:02:59 I'm not sure. It either means modifying the net::G
192
193 if (extensions->size() > old_extension_size)
194 valid_type_count++;
195 }
196
197 if (valid_type_count == 0)
198 return false;
199
200 if (valid_type_count > 1 ||
201 (valid_type_count == 1 && description_id == 0 && extensions->size() > 1))
202 description_id = 0;
203
204 if (description_id) {
205 *description = l10n_util::GetStringUTF16(description_id);
206 } else {
207 // We don't have a description ID; generate a string like
208 // ".ext1, .ext2, .other-ext".
benwells 2012/07/24 06:01:27 Would be nicer to have "*.ext1, *.ext2..."
thorogood 2012/07/25 05:02:59 Done.
209 *description = string16();
210 for (std::vector<FilePath::StringType>::const_iterator iter
211 = extensions->begin(); iter != extensions->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 }
222
223 return true;
224 }
225
134 } // namespace 226 } // namespace
135 227
136 namespace extensions { 228 namespace extensions {
137 229
138 bool FileSystemGetDisplayPathFunction::RunImpl() { 230 bool FileSystemGetDisplayPathFunction::RunImpl() {
139 std::string filesystem_name; 231 std::string filesystem_name;
140 std::string filesystem_path; 232 std::string filesystem_path;
141 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &filesystem_name)); 233 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &filesystem_name));
142 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &filesystem_path)); 234 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &filesystem_path));
143 235
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
256 } 348 }
257 349
258 // Handles showing a dialog to the user to ask for the filename for a file to 350 // Handles showing a dialog to the user to ask for the filename for a file to
259 // save or open. 351 // save or open.
260 class FileSystemChooseFileFunction::FilePicker 352 class FileSystemChooseFileFunction::FilePicker
261 : public SelectFileDialog::Listener { 353 : public SelectFileDialog::Listener {
262 public: 354 public:
263 FilePicker(FileSystemChooseFileFunction* function, 355 FilePicker(FileSystemChooseFileFunction* function,
264 content::WebContents* web_contents, 356 content::WebContents* web_contents,
265 const FilePath& suggested_name, 357 const FilePath& suggested_name,
358 const SelectFileDialog::FileTypeInfo& file_type_info,
266 SelectFileDialog::Type picker_type, 359 SelectFileDialog::Type picker_type,
267 EntryType entry_type) 360 EntryType entry_type)
268 : suggested_name_(suggested_name), 361 : suggested_name_(suggested_name),
269 entry_type_(entry_type), 362 entry_type_(entry_type),
270 function_(function) { 363 function_(function) {
271 select_file_dialog_ = SelectFileDialog::Create( 364 select_file_dialog_ = SelectFileDialog::Create(
272 this, new ChromeSelectFilePolicy(web_contents)); 365 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 ? 366 gfx::NativeWindow owning_window = web_contents ?
282 platform_util::GetTopLevel(web_contents->GetNativeView()) : NULL; 367 platform_util::GetTopLevel(web_contents->GetNativeView()) : NULL;
283 368
284 if (g_skip_picker_for_test) { 369 if (g_skip_picker_for_test) {
285 if (g_path_to_be_picked_for_test) { 370 if (g_path_to_be_picked_for_test) {
286 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, 371 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
287 base::Bind( 372 base::Bind(
288 &FileSystemChooseFileFunction::FilePicker::FileSelected, 373 &FileSystemChooseFileFunction::FilePicker::FileSelected,
289 base::Unretained(this), *g_path_to_be_picked_for_test, 1, 374 base::Unretained(this), *g_path_to_be_picked_for_test, 1,
290 static_cast<void*>(NULL))); 375 static_cast<void*>(NULL)));
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
326 EntryType entry_type_; 411 EntryType entry_type_;
327 412
328 scoped_refptr<SelectFileDialog> select_file_dialog_; 413 scoped_refptr<SelectFileDialog> select_file_dialog_;
329 scoped_refptr<FileSystemChooseFileFunction> function_; 414 scoped_refptr<FileSystemChooseFileFunction> function_;
330 415
331 DISALLOW_COPY_AND_ASSIGN(FilePicker); 416 DISALLOW_COPY_AND_ASSIGN(FilePicker);
332 }; 417 };
333 418
334 bool FileSystemChooseFileFunction::ShowPicker( 419 bool FileSystemChooseFileFunction::ShowPicker(
335 const FilePath& suggested_name, 420 const FilePath& suggested_name,
421 const SelectFileDialog::FileTypeInfo& file_type_info,
336 SelectFileDialog::Type picker_type, 422 SelectFileDialog::Type picker_type,
337 EntryType entry_type) { 423 EntryType entry_type) {
338 ShellWindowRegistry* registry = ShellWindowRegistry::Get(profile()); 424 ShellWindowRegistry* registry = ShellWindowRegistry::Get(profile());
339 DCHECK(registry); 425 DCHECK(registry);
340 ShellWindow* shell_window = registry->GetShellWindowForRenderViewHost( 426 ShellWindow* shell_window = registry->GetShellWindowForRenderViewHost(
341 render_view_host()); 427 render_view_host());
342 if (!shell_window) { 428 if (!shell_window) {
343 error_ = kInvalidCallingPage; 429 error_ = kInvalidCallingPage;
344 return false; 430 return false;
345 } 431 }
346 432
347 // The file picker will hold a reference to this function instance, preventing 433 // The file picker will hold a reference to this function instance, preventing
348 // its destruction (and subsequent sending of the function response) until the 434 // 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 435 // 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. 436 // will delete itself, which will also free the function instance.
351 new FilePicker(this, shell_window->web_contents(), suggested_name, 437 new FilePicker(this, shell_window->web_contents(), suggested_name,
352 picker_type, entry_type); 438 file_type_info, picker_type, entry_type);
353 return true; 439 return true;
354 } 440 }
355 441
356 // static 442 // static
357 void FileSystemChooseFileFunction::SkipPickerAndAlwaysSelectPathForTest( 443 void FileSystemChooseFileFunction::SkipPickerAndAlwaysSelectPathForTest(
358 FilePath* path) { 444 FilePath* path) {
359 g_skip_picker_for_test = true; 445 g_skip_picker_for_test = true;
360 g_path_to_be_picked_for_test = path; 446 g_path_to_be_picked_for_test = path;
361 } 447 }
362 448
(...skipping 24 matching lines...) Expand all
387 void FileSystemChooseFileFunction::FileSelectionCanceled() { 473 void FileSystemChooseFileFunction::FileSelectionCanceled() {
388 error_ = kUserCancelled; 474 error_ = kUserCancelled;
389 SendResponse(false); 475 SendResponse(false);
390 } 476 }
391 477
392 bool FileSystemChooseFileFunction::RunImpl() { 478 bool FileSystemChooseFileFunction::RunImpl() {
393 scoped_ptr<ChooseFile::Params> params(ChooseFile::Params::Create(*args_)); 479 scoped_ptr<ChooseFile::Params> params(ChooseFile::Params::Create(*args_));
394 EXTENSION_FUNCTION_VALIDATE(params.get()); 480 EXTENSION_FUNCTION_VALIDATE(params.get());
395 481
396 FilePath suggested_name; 482 FilePath suggested_name;
483 scoped_ptr<SelectFileDialog::FileTypeInfo> file_type_info(
484 new SelectFileDialog::FileTypeInfo());
397 EntryType entry_type = READ_ONLY; 485 EntryType entry_type = READ_ONLY;
398 SelectFileDialog::Type picker_type = SelectFileDialog::SELECT_OPEN_FILE; 486 SelectFileDialog::Type picker_type = SelectFileDialog::SELECT_OPEN_FILE;
399 487
400 file_system::ChooseFileOptions* options = params->options.get(); 488 file_system::ChooseFileOptions* options = params->options.get();
401 if (options) { 489 if (options) {
402 if (options->type.get()) { 490 if (options->type.get()) {
403 if (*options->type == kOpenWritableFileOption) { 491 if (*options->type == kOpenWritableFileOption) {
404 entry_type = WRITABLE; 492 entry_type = WRITABLE;
405 } else if (*options->type == kSaveFileOption) { 493 } else if (*options->type == kSaveFileOption) {
406 entry_type = WRITABLE; 494 entry_type = WRITABLE;
407 picker_type = SelectFileDialog::SELECT_SAVEAS_FILE; 495 picker_type = SelectFileDialog::SELECT_SAVEAS_FILE;
408 } else if (*options->type != kOpenFileOption) { 496 } else if (*options->type != kOpenFileOption) {
409 error_ = kUnknownChooseFileType; 497 error_ = kUnknownChooseFileType;
410 return false; 498 return false;
411 } 499 }
412 } 500 }
413 501
502 FilePath::StringType suggested_extension;
414 if (options->suggested_name.get()) { 503 if (options->suggested_name.get()) {
415 suggested_name = FilePath::FromUTF8Unsafe( 504 suggested_name = FilePath::FromUTF8Unsafe(
416 *options->suggested_name.get()); 505 *options->suggested_name.get());
417 506
418 // Don't allow any path components; shorten to the base name. This should 507 // Don't allow any path components; shorten to the base name. This should
419 // result in a relative path, but in some cases may not. Clear the 508 // result in a relative path, but in some cases may not. Clear the
420 // suggestion for safety if this is the case. 509 // suggestion for safety if this is the case.
421 suggested_name = suggested_name.BaseName(); 510 suggested_name = suggested_name.BaseName();
422 if (suggested_name.IsAbsolute()) { 511 if (suggested_name.IsAbsolute()) {
423 suggested_name = FilePath(); 512 suggested_name = FilePath();
424 } 513 }
514 suggested_extension = suggested_name.Extension();
515 if (!suggested_extension.empty()) {
516 suggested_extension.erase(suggested_extension.begin()); // drop the .
517 }
518 }
519
520 if (options->accepts.get()) {
521 std::vector<std::string>* raw = options->accepts.get();
522 for (std::vector<std::string>::const_iterator iter = raw->begin();
523 iter != raw->end(); ++iter) {
524 string16 description;
525 std::vector<FilePath::StringType> extensions;
526 if (GetFileTypesFromAcceptType(*iter, &extensions, &description)) {
527 file_type_info->extensions.push_back(extensions);
528 file_type_info->extension_description_overrides.push_back(description) ;
529 }
530 // TODO(thorogood): If suggested_extension is non-empty, ensure that
531 // at least once of our extensions list includes it.
532 }
533 }
534
535 if (options->accepts_all_types.get() &&
536 !file_type_info->extensions.empty()) {
537 file_type_info->include_all_files = *options->accepts_all_types.get();
538 } else {
539 // There's nothing in our accepted extension list; default to accepting
540 // all types.
541 file_type_info->include_all_files = true;
425 } 542 }
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