#include "atom/browser/web_dialog_helper.h"
+#include <string>
#include <vector>
#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<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 {
WebDialogHelper::WebDialogHelper(NativeWindow* window)
WebDialogHelper::~WebDialogHelper() {
}
+
void WebDialogHelper::RunFileChooser(content::WebContents* web_contents,
const content::FileChooserParams& params) {
std::vector<content::FileChooserFileInfo> 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;
if (file_dialog::ShowOpenDialog(window_,
base::UTF16ToUTF8(params.title),
params.default_file_name,
- file_dialog::Filters(),
+ filters,
flags,
&paths)) {
for (auto& path : paths) {