| 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/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 Loading... |
| 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 if (!IsAcceptTypeValid(ascii_type)) |
| 258 DCHECK(StringToLowerASCII(ascii_mime_type) == ascii_mime_type) | 258 continue; |
| 259 << "A MIME type contains uppercase letter: " << ascii_mime_type; | |
| 260 DCHECK(TrimWhitespaceASCII(ascii_mime_type, TRIM_ALL, &ascii_mime_type) | |
| 261 == TRIM_NONE) | |
| 262 << "A MIME type contains whitespace: '" << ascii_mime_type << "'"; | |
| 263 | 259 |
| 264 size_t old_extension_size = extensions->size(); | 260 size_t old_extension_size = extensions->size(); |
| 265 if (ascii_mime_type == "image/*") { | 261 if (ascii_type[0] == '.') { |
| 262 // If the type starts with a period it is assumed to be a file extension |
| 263 // so we just have to add it to the list. |
| 264 FilePath::StringType ext(ascii_type.begin(), ascii_type.end()); |
| 265 extensions->push_back(ext.substr(1)); |
| 266 } else if (ascii_type == "image/*") { |
| 266 description_id = IDS_IMAGE_FILES; | 267 description_id = IDS_IMAGE_FILES; |
| 267 net::GetImageExtensions(extensions); | 268 net::GetImageExtensions(extensions); |
| 268 } else if (ascii_mime_type == "audio/*") { | 269 } else if (ascii_type == "audio/*") { |
| 269 description_id = IDS_AUDIO_FILES; | 270 description_id = IDS_AUDIO_FILES; |
| 270 net::GetAudioExtensions(extensions); | 271 net::GetAudioExtensions(extensions); |
| 271 } else if (ascii_mime_type == "video/*") { | 272 } else if (ascii_type == "video/*") { |
| 272 description_id = IDS_VIDEO_FILES; | 273 description_id = IDS_VIDEO_FILES; |
| 273 net::GetVideoExtensions(extensions); | 274 net::GetVideoExtensions(extensions); |
| 274 } else { | 275 } else { |
| 275 net::GetExtensionsForMimeType(ascii_mime_type, extensions); | 276 net::GetExtensionsForMimeType(ascii_type, extensions); |
| 276 } | 277 } |
| 277 | 278 |
| 278 if (extensions->size() > old_extension_size) | 279 if (extensions->size() > old_extension_size) |
| 279 valid_type_count++; | 280 valid_type_count++; |
| 280 } | 281 } |
| 281 | 282 |
| 282 // If no valid extension is added, bail out. | 283 // If no valid extension is added, bail out. |
| 283 if (valid_type_count == 0) | 284 if (valid_type_count == 0) |
| 284 return NULL; | 285 return NULL; |
| 285 | 286 |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 454 case content::NOTIFICATION_WEB_CONTENTS_DESTROYED: { | 455 case content::NOTIFICATION_WEB_CONTENTS_DESTROYED: { |
| 455 DCHECK(content::Source<WebContents>(source).ptr() == web_contents_); | 456 DCHECK(content::Source<WebContents>(source).ptr() == web_contents_); |
| 456 web_contents_ = NULL; | 457 web_contents_ = NULL; |
| 457 break; | 458 break; |
| 458 } | 459 } |
| 459 | 460 |
| 460 default: | 461 default: |
| 461 NOTREACHED(); | 462 NOTREACHED(); |
| 462 } | 463 } |
| 463 } | 464 } |
| 465 |
| 466 // static |
| 467 bool FileSelectHelper::IsAcceptTypeValid(const std::string& accept_type) { |
| 468 // TODO(raymes): This only does some basic checks, extend to test more cases. |
| 469 // A 1 character accept type will always be invalid (either a "." in the case |
| 470 // of an extension or a "/" in the case of a MIME type). |
| 471 std::string unused; |
| 472 if (accept_type.length() <= 1 || |
| 473 StringToLowerASCII(accept_type) != accept_type || |
| 474 TrimWhitespaceASCII(accept_type, TRIM_ALL, &unused) != TRIM_NONE) { |
| 475 return false; |
| 476 } |
| 477 return true; |
| 478 } |
| OLD | NEW |