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

Unified Diff: chrome/browser/tab_contents/tab_contents_file_select_helper.cc

Issue 3311016: Support accept attribute for an <input type=file> element.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 10 years, 3 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/tab_contents/tab_contents_file_select_helper.cc
===================================================================
--- chrome/browser/tab_contents/tab_contents_file_select_helper.cc (revision 58709)
+++ chrome/browser/tab_contents/tab_contents_file_select_helper.cc (working copy)
@@ -4,13 +4,18 @@
#include "chrome/browser/tab_contents/tab_contents_file_select_helper.h"
+#include "app/l10n_util.h"
#include "base/file_util.h"
+#include "base/string_split.h"
+#include "base/string_util.h"
+#include "net/base/mime_util.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/renderer_host/render_view_host.h"
#include "chrome/browser/shell_dialogs.h"
#include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/browser/tab_contents/tab_contents_view.h"
#include "chrome/common/render_messages_params.h"
+#include "grit/generated_resources.h"
TabContentsFileSelectHelper::TabContentsFileSelectHelper(
TabContents* tab_contents)
@@ -88,6 +93,73 @@
GetRenderViewHost()->FilesSelectedInChooser(std::vector<FilePath>());
}
+SelectFileDialog::FileTypeInfo*
+TabContentsFileSelectHelper::GetFileTypesFromAcceptType(
+ const string16& accept_types) {
+ if (accept_types.empty())
+ return NULL;
+
+ // Split the accept-type string on commas.
+ std::vector<string16> mime_types;
+ base::SplitStringUsingSubstr(accept_types, L",", &mime_types);
+ if (mime_types.empty())
+ return NULL;
+
+ // Create FileTypeInfo and pre-allocate for the first extension list.
+ scoped_ptr<SelectFileDialog::FileTypeInfo> file_type(
+ new SelectFileDialog::FileTypeInfo());
+ file_type->include_all_files = true;
+ file_type->extensions.resize(1);
+ std::vector<FilePath::StringType>* extensions = &file_type->extensions.back();
+
+ // Find the correspondinge extensions.
+ int valid_type_count = 0;
+ int description_id = 0;
+ for (size_t i = 0; i < mime_types.size(); ++i) {
+ string16 mime_type = mime_types[i];
+ std::string ascii_mime_type = StringToLowerASCII(UTF16ToASCII(mime_type));
+
+ TrimWhitespace(ascii_mime_type, TRIM_ALL, &ascii_mime_type);
+ if (ascii_mime_type.empty())
+ continue;
+
+ size_t old_extension_size = extensions->size();
+ if (ascii_mime_type == "image/*") {
+ description_id = IDS_IMAGE_FILES;
+ net::GetImageExtensions(extensions);
+ } else if (ascii_mime_type == "audio/*") {
+ description_id = IDS_AUDIO_FILES;
+ net::GetAudioExtensions(extensions);
+ } else if (ascii_mime_type == "video/*") {
+ description_id = IDS_VIDEO_FILES;
+ net::GetVideoExtensions(extensions);
+ } else {
+ net::GetExtensionsForMimeType(ascii_mime_type, extensions);
+ }
+
+ if (extensions->size() > old_extension_size)
+ valid_type_count++;
+ }
+
+ // Use a generic description "Custom Files" if either of the following is
+ // true:
+ // 1) There're multiple types specified, like "audio/*,video/*"
+ // 2) There're multiple extensions for a MIME type without parameter, like
+ // "ehtml,shtml,htm,html" for "text/html". On Windows, the select file
+ // dialog uses the first extension in the list to form the description,
+ // like "EHTML Files". This is not what we want.
+ if (valid_type_count > 1 ||
+ (valid_type_count == 1 && description_id == 0 && extensions->size() > 1))
+ description_id = IDS_CUSTOM_FILES;
+
+ if (description_id) {
+ file_type->extension_description_overrides.push_back(
+ l10n_util::GetStringUTF16(description_id));
+ }
+
+ return file_type.release();
+}
+
void TabContentsFileSelectHelper::RunFileChooser(
const ViewHostMsg_RunFileChooser_Params &params) {
if (!select_file_dialog_.get())
@@ -110,6 +182,8 @@
dialog_type_ = SelectFileDialog::SELECT_OPEN_FILE; // Prevent warning.
NOTREACHED();
}
+ scoped_ptr<SelectFileDialog::FileTypeInfo> file_types(
+ GetFileTypesFromAcceptType(params.accept_types));
FilePath default_file_name = params.default_file_name;
if (default_file_name.empty())
default_file_name = tab_contents_->profile()->last_selected_directory();
@@ -117,7 +191,7 @@
dialog_type_,
params.title,
default_file_name,
- NULL,
+ file_types.get(),
0,
FILE_PATH_LITERAL(""),
tab_contents_->view()->GetTopLevelNativeWindow(),

Powered by Google App Engine
This is Rietveld 408576698