Merge pull request #2200 from atom/input_accept_attr
Enable 'accept' attribute in 'input' label.
This commit is contained in:
commit
02cadde8de
1 changed files with 63 additions and 3 deletions
|
@ -4,17 +4,66 @@
|
||||||
|
|
||||||
#include "atom/browser/web_dialog_helper.h"
|
#include "atom/browser/web_dialog_helper.h"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include "atom/browser/atom_browser_context.h"
|
||||||
|
#include "atom/browser/native_window.h"
|
||||||
#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/prefs/pref_service.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 {
|
||||||
|
|
||||||
|
const char kSelectFileLastDirectory[] = "selectfile.last_directory";
|
||||||
|
|
||||||
|
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 +74,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;
|
||||||
|
@ -56,10 +108,14 @@ void WebDialogHelper::RunFileChooser(content::WebContents* web_contents,
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<base::FilePath> paths;
|
std::vector<base::FilePath> paths;
|
||||||
|
AtomBrowserContext* browser_context = static_cast<AtomBrowserContext*>(
|
||||||
|
window_->web_contents()->GetBrowserContext());
|
||||||
|
base::FilePath default_file_path = browser_context->prefs()->GetFilePath(
|
||||||
|
kSelectFileLastDirectory).Append(params.default_file_name);
|
||||||
if (file_dialog::ShowOpenDialog(window_,
|
if (file_dialog::ShowOpenDialog(window_,
|
||||||
base::UTF16ToUTF8(params.title),
|
base::UTF16ToUTF8(params.title),
|
||||||
params.default_file_name,
|
default_file_path,
|
||||||
file_dialog::Filters(),
|
filters,
|
||||||
flags,
|
flags,
|
||||||
&paths)) {
|
&paths)) {
|
||||||
for (auto& path : paths) {
|
for (auto& path : paths) {
|
||||||
|
@ -68,6 +124,10 @@ void WebDialogHelper::RunFileChooser(content::WebContents* web_contents,
|
||||||
info.display_name = path.BaseName().value();
|
info.display_name = path.BaseName().value();
|
||||||
result.push_back(info);
|
result.push_back(info);
|
||||||
}
|
}
|
||||||
|
if (!paths.empty()) {
|
||||||
|
browser_context->prefs()->SetFilePath(kSelectFileLastDirectory,
|
||||||
|
paths[0].DirName());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue