From 04606a9f97b37d3563f365f48258f3fe9eeb4f6a Mon Sep 17 00:00:00 2001 From: Haojian Wu Date: Mon, 13 Jul 2015 13:51:18 +0800 Subject: [PATCH] Enable 'accept' attribute in 'input' label. --- atom/browser/web_dialog_helper.cc | 51 +++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/atom/browser/web_dialog_helper.cc b/atom/browser/web_dialog_helper.cc index f6f2334535..bbf16b5fc2 100644 --- a/atom/browser/web_dialog_helper.cc +++ b/atom/browser/web_dialog_helper.cc @@ -4,17 +4,61 @@ #include "atom/browser/web_dialog_helper.h" +#include #include #include "atom/browser/ui/file_dialog.h" #include "base/bind.h" #include "base/files/file_enumerator.h" +#include "base/files/file_path.h" #include "base/strings/utf_string_conversions.h" #include "content/public/browser/render_view_host.h" #include "content/public/browser/web_contents.h" #include "content/public/common/file_chooser_file_info.h" +#include "net/base/mime_util.h" #include "ui/shell_dialogs/selected_file_info.h" +namespace { + +file_dialog::Filters GetFileTypesFromAcceptType( + const std::vector& accept_types) { + file_dialog::Filters filters; + if (accept_types.empty()) + return filters; + + std::vector extensions; + + for (const auto& accept_type : accept_types) { + std::string ascii_type = base::UTF16ToASCII(accept_type); + if (ascii_type[0] == '.') { + // If the type starts with a period it is assumed to be a file extension, + // like `.txt`, // so we just have to add it to the list. + base::FilePath::StringType extension( + ascii_type.begin(), ascii_type.end()); + // Skip the first character. + extensions.push_back(extension.substr(1)); + } else { + if (ascii_type == "image/*" || ascii_type == "audio/*" || + ascii_type == "video/*") { + // For MIME Type + net::GetExtensionsForMimeType(ascii_type, &extensions); + } + } + } + + filters.push_back(file_dialog::Filter()); + for (const auto& extension : extensions) { +#if defined(OS_WIN) + filters[0].second.push_back(base::UTF16ToASCII(extension)); +#else + filters[0].second.push_back(extension); +#endif + } + return filters; +} + +} // namespace + namespace atom { WebDialogHelper::WebDialogHelper(NativeWindow* window) @@ -25,15 +69,18 @@ WebDialogHelper::WebDialogHelper(NativeWindow* window) WebDialogHelper::~WebDialogHelper() { } + void WebDialogHelper::RunFileChooser(content::WebContents* web_contents, const content::FileChooserParams& params) { std::vector result; + file_dialog::Filters filters = GetFileTypesFromAcceptType( + params.accept_types); if (params.mode == content::FileChooserParams::Save) { base::FilePath path; if (file_dialog::ShowSaveDialog(window_, base::UTF16ToUTF8(params.title), params.default_file_name, - file_dialog::Filters(), + filters, &path)) { content::FileChooserFileInfo info; info.file_path = path; @@ -59,7 +106,7 @@ void WebDialogHelper::RunFileChooser(content::WebContents* web_contents, if (file_dialog::ShowOpenDialog(window_, base::UTF16ToUTF8(params.title), params.default_file_name, - file_dialog::Filters(), + filters, flags, &paths)) { for (auto& path : paths) {