Enable 'accept' attribute in 'input' label.

This commit is contained in:
Haojian Wu 2015-07-13 13:51:18 +08:00
parent 4379d24e9d
commit 04606a9f97

View file

@ -4,17 +4,61 @@
#include "atom/browser/web_dialog_helper.h" #include "atom/browser/web_dialog_helper.h"
#include <string>
#include <vector> #include <vector>
#include "atom/browser/ui/file_dialog.h" #include "atom/browser/ui/file_dialog.h"
#include "base/bind.h" #include "base/bind.h"
#include "base/files/file_enumerator.h" #include "base/files/file_enumerator.h"
#include "base/files/file_path.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "content/public/browser/render_view_host.h" #include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents.h"
#include "content/public/common/file_chooser_file_info.h" #include "content/public/common/file_chooser_file_info.h"
#include "net/base/mime_util.h"
#include "ui/shell_dialogs/selected_file_info.h" #include "ui/shell_dialogs/selected_file_info.h"
namespace {
file_dialog::Filters GetFileTypesFromAcceptType(
const std::vector<base::string16>& accept_types) {
file_dialog::Filters filters;
if (accept_types.empty())
return filters;
std::vector<base::FilePath::StringType> 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 { namespace atom {
WebDialogHelper::WebDialogHelper(NativeWindow* window) WebDialogHelper::WebDialogHelper(NativeWindow* window)
@ -25,15 +69,18 @@ WebDialogHelper::WebDialogHelper(NativeWindow* window)
WebDialogHelper::~WebDialogHelper() { WebDialogHelper::~WebDialogHelper() {
} }
void WebDialogHelper::RunFileChooser(content::WebContents* web_contents, void WebDialogHelper::RunFileChooser(content::WebContents* web_contents,
const content::FileChooserParams& params) { const content::FileChooserParams& params) {
std::vector<content::FileChooserFileInfo> result; std::vector<content::FileChooserFileInfo> result;
file_dialog::Filters filters = GetFileTypesFromAcceptType(
params.accept_types);
if (params.mode == content::FileChooserParams::Save) { if (params.mode == content::FileChooserParams::Save) {
base::FilePath path; base::FilePath path;
if (file_dialog::ShowSaveDialog(window_, if (file_dialog::ShowSaveDialog(window_,
base::UTF16ToUTF8(params.title), base::UTF16ToUTF8(params.title),
params.default_file_name, params.default_file_name,
file_dialog::Filters(), filters,
&path)) { &path)) {
content::FileChooserFileInfo info; content::FileChooserFileInfo info;
info.file_path = path; info.file_path = path;
@ -59,7 +106,7 @@ void WebDialogHelper::RunFileChooser(content::WebContents* web_contents,
if (file_dialog::ShowOpenDialog(window_, if (file_dialog::ShowOpenDialog(window_,
base::UTF16ToUTF8(params.title), base::UTF16ToUTF8(params.title),
params.default_file_name, params.default_file_name,
file_dialog::Filters(), filters,
flags, flags,
&paths)) { &paths)) {
for (auto& path : paths) { for (auto& path : paths) {