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

Side by Side Diff: chrome/browser/file_select_helper.cc

Issue 10414085: Modified the pepper file chooser API to support filtering files by extensions. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years, 7 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 | Annotate | Revision Log
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/file_select_helper.h" 5 #include "chrome/browser/file_select_helper.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/file_util.h" 10 #include "base/file_util.h"
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after
242 if (accept_types.empty()) 242 if (accept_types.empty())
243 return NULL; 243 return NULL;
244 244
245 // Create FileTypeInfo and pre-allocate for the first extension list. 245 // Create FileTypeInfo and pre-allocate for the first extension list.
246 scoped_ptr<SelectFileDialog::FileTypeInfo> file_type( 246 scoped_ptr<SelectFileDialog::FileTypeInfo> file_type(
247 new SelectFileDialog::FileTypeInfo()); 247 new SelectFileDialog::FileTypeInfo());
248 file_type->include_all_files = true; 248 file_type->include_all_files = true;
249 file_type->extensions.resize(1); 249 file_type->extensions.resize(1);
250 std::vector<FilePath::StringType>* extensions = &file_type->extensions.back(); 250 std::vector<FilePath::StringType>* extensions = &file_type->extensions.back();
251 251
252 // Find the correspondinge extensions. 252 // Find the corresponding extensions.
253 int valid_type_count = 0; 253 int valid_type_count = 0;
254 int description_id = 0; 254 int description_id = 0;
255 for (size_t i = 0; i < accept_types.size(); ++i) { 255 for (size_t i = 0; i < accept_types.size(); ++i) {
256 std::string ascii_mime_type = UTF16ToASCII(accept_types[i]); 256 std::string ascii_type = UTF16ToASCII(accept_types[i]);
257 // WebKit normalizes MIME types. See HTMLInputElement::acceptMIMETypes(). 257 // WebKit normalizes accept types.
258 DCHECK(StringToLowerASCII(ascii_mime_type) == ascii_mime_type) 258 DCHECK(ascii_type.length() > 0) << "An accept type is an empty string.";
259 << "A MIME type contains uppercase letter: " << ascii_mime_type; 259 DCHECK(ascii_type[0] == '.' ? ascii_type.length() > 1 : true)
Evan Stade 2012/05/25 20:19:57 DCHECK(ascii_type != ".")
raymes 2012/05/25 21:00:23 Umm yes :P. Done.
260 DCHECK(TrimWhitespaceASCII(ascii_mime_type, TRIM_ALL, &ascii_mime_type) 260 << "An accept type is a period.";
261 DCHECK(StringToLowerASCII(ascii_type) == ascii_type)
262 << "An accept type contains uppercase letter: " << ascii_type;
263 DCHECK(TrimWhitespaceASCII(ascii_type, TRIM_ALL, &ascii_type)
Evan Stade 2012/05/25 20:19:57 unit tests > DCHECKs?
raymes 2012/05/25 21:00:23 I agree, I was just extending the current behavior
Evan Stade 2012/05/25 21:10:15 the function should be robust to failures of these
261 == TRIM_NONE) 264 == TRIM_NONE)
262 << "A MIME type contains whitespace: '" << ascii_mime_type << "'"; 265 << "An accept type contains whitespace: '" << ascii_type << "'";
263 266
264 size_t old_extension_size = extensions->size(); 267 size_t old_extension_size = extensions->size();
265 if (ascii_mime_type == "image/*") { 268 if (ascii_type[0] == '.') {
269 // If the type starts with a period it is assumed to be a file extension
270 // so we just have to add it to the list.
271 FilePath::StringType ext(ascii_type.begin(), ascii_type.end());
272 extensions->push_back(ext.substr(1));
273 } else if (ascii_type == "image/*") {
266 description_id = IDS_IMAGE_FILES; 274 description_id = IDS_IMAGE_FILES;
267 net::GetImageExtensions(extensions); 275 net::GetImageExtensions(extensions);
268 } else if (ascii_mime_type == "audio/*") { 276 } else if (ascii_type == "audio/*") {
269 description_id = IDS_AUDIO_FILES; 277 description_id = IDS_AUDIO_FILES;
270 net::GetAudioExtensions(extensions); 278 net::GetAudioExtensions(extensions);
271 } else if (ascii_mime_type == "video/*") { 279 } else if (ascii_type == "video/*") {
272 description_id = IDS_VIDEO_FILES; 280 description_id = IDS_VIDEO_FILES;
273 net::GetVideoExtensions(extensions); 281 net::GetVideoExtensions(extensions);
274 } else { 282 } else {
275 net::GetExtensionsForMimeType(ascii_mime_type, extensions); 283 net::GetExtensionsForMimeType(ascii_type, extensions);
276 } 284 }
277 285
278 if (extensions->size() > old_extension_size) 286 if (extensions->size() > old_extension_size)
279 valid_type_count++; 287 valid_type_count++;
280 } 288 }
281 289
282 // If no valid extension is added, bail out. 290 // If no valid extension is added, bail out.
283 if (valid_type_count == 0) 291 if (valid_type_count == 0)
284 return NULL; 292 return NULL;
285 293
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
454 case content::NOTIFICATION_WEB_CONTENTS_DESTROYED: { 462 case content::NOTIFICATION_WEB_CONTENTS_DESTROYED: {
455 DCHECK(content::Source<WebContents>(source).ptr() == web_contents_); 463 DCHECK(content::Source<WebContents>(source).ptr() == web_contents_);
456 web_contents_ = NULL; 464 web_contents_ = NULL;
457 break; 465 break;
458 } 466 }
459 467
460 default: 468 default:
461 NOTREACHED(); 469 NOTREACHED();
462 } 470 }
463 } 471 }
OLDNEW
« no previous file with comments | « chrome/browser/file_select_helper.h ('k') | chrome/browser/ui/gtk/select_file_dialog_impl_gtk.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698