2014-10-31 00:37:14 +08:00
|
|
|
// Copyright (c) 2014 GitHub, Inc. All rights reserved.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-06-19 13:46:59 -07:00
|
|
|
#include "shell/browser/web_dialog_helper.h"
|
2014-10-31 00:37:14 +08:00
|
|
|
|
2015-07-13 13:51:18 +08:00
|
|
|
#include <string>
|
2019-01-12 06:30:43 +05:30
|
|
|
#include <utility>
|
2014-10-31 17:37:32 +08:00
|
|
|
#include <vector>
|
|
|
|
|
2014-10-31 00:37:14 +08:00
|
|
|
#include "base/bind.h"
|
2014-10-31 17:37:32 +08:00
|
|
|
#include "base/files/file_enumerator.h"
|
2015-07-13 13:51:18 +08:00
|
|
|
#include "base/files/file_path.h"
|
2014-10-31 19:27:01 +08:00
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2015-07-26 16:08:29 +08:00
|
|
|
#include "chrome/common/pref_names.h"
|
2016-08-26 15:30:02 -07:00
|
|
|
#include "components/prefs/pref_service.h"
|
2019-01-12 06:30:43 +05:30
|
|
|
#include "content/public/browser/file_select_listener.h"
|
2016-09-06 17:24:37 +09:00
|
|
|
#include "content/public/browser/render_frame_host.h"
|
2017-02-22 15:58:46 -08:00
|
|
|
#include "content/public/browser/render_process_host.h"
|
2014-10-31 17:37:32 +08:00
|
|
|
#include "content/public/browser/render_view_host.h"
|
|
|
|
#include "content/public/browser/web_contents.h"
|
2015-07-13 13:51:18 +08:00
|
|
|
#include "net/base/mime_util.h"
|
2020-02-04 12:19:40 -08:00
|
|
|
#include "shell/browser/electron_browser_context.h"
|
2020-10-27 17:05:28 -07:00
|
|
|
#include "shell/browser/file_select_helper.h"
|
2019-06-19 13:46:59 -07:00
|
|
|
#include "shell/browser/native_window.h"
|
|
|
|
#include "shell/browser/ui/file_dialog.h"
|
2014-10-31 00:37:14 +08:00
|
|
|
|
2019-01-12 06:30:43 +05:30
|
|
|
using blink::mojom::FileChooserFileInfo;
|
|
|
|
using blink::mojom::FileChooserFileInfoPtr;
|
|
|
|
using blink::mojom::FileChooserParams;
|
2020-10-27 17:05:28 -07:00
|
|
|
using blink::mojom::NativeFileInfo;
|
2019-01-12 06:30:43 +05:30
|
|
|
|
2015-07-13 13:51:18 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
file_dialog::Filters GetFileTypesFromAcceptType(
|
2021-03-16 12:18:45 -04:00
|
|
|
const std::vector<std::u16string>& accept_types) {
|
2015-07-13 13:51:18 +08:00
|
|
|
file_dialog::Filters filters;
|
|
|
|
if (accept_types.empty())
|
|
|
|
return filters;
|
|
|
|
|
|
|
|
std::vector<base::FilePath::StringType> extensions;
|
|
|
|
|
2017-07-14 11:04:06 -07:00
|
|
|
int valid_type_count = 0;
|
|
|
|
std::string description;
|
|
|
|
|
2015-07-13 13:51:18 +08:00
|
|
|
for (const auto& accept_type : accept_types) {
|
|
|
|
std::string ascii_type = base::UTF16ToASCII(accept_type);
|
2017-07-14 11:04:06 -07:00
|
|
|
auto old_extension_size = extensions.size();
|
|
|
|
|
2015-07-13 13:51:18 +08:00
|
|
|
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.
|
2018-04-17 21:55:30 -04:00
|
|
|
base::FilePath::StringType extension(ascii_type.begin(),
|
|
|
|
ascii_type.end());
|
2015-07-13 13:51:18 +08:00
|
|
|
// Skip the first character.
|
|
|
|
extensions.push_back(extension.substr(1));
|
|
|
|
} else {
|
2017-07-14 11:04:06 -07:00
|
|
|
if (ascii_type == "image/*")
|
|
|
|
description = "Image Files";
|
|
|
|
else if (ascii_type == "audio/*")
|
|
|
|
description = "Audio Files";
|
|
|
|
else if (ascii_type == "video/*")
|
|
|
|
description = "Video Files";
|
|
|
|
|
|
|
|
// For MIME Type, `audio/*, video/*, image/*
|
2015-07-28 10:30:36 +08:00
|
|
|
net::GetExtensionsForMimeType(ascii_type, &extensions);
|
2015-07-13 13:51:18 +08:00
|
|
|
}
|
2017-07-14 11:04:06 -07:00
|
|
|
|
|
|
|
if (extensions.size() > old_extension_size)
|
|
|
|
valid_type_count++;
|
2015-07-13 13:51:18 +08:00
|
|
|
}
|
|
|
|
|
2019-07-01 07:58:06 -07:00
|
|
|
// If no valid extension is added, return empty filters.
|
2015-07-28 10:30:36 +08:00
|
|
|
if (extensions.empty())
|
|
|
|
return filters;
|
|
|
|
|
2015-07-13 13:51:18 +08:00
|
|
|
filters.push_back(file_dialog::Filter());
|
2017-07-14 11:04:06 -07:00
|
|
|
|
2018-05-15 22:28:28 +02:00
|
|
|
if (valid_type_count > 1 || (valid_type_count == 1 && description.empty()))
|
2017-07-14 11:04:06 -07:00
|
|
|
description = "Custom Files";
|
|
|
|
|
2018-05-15 22:28:28 +02:00
|
|
|
DCHECK(!description.empty());
|
2017-07-14 11:04:06 -07:00
|
|
|
filters[0].first = description;
|
|
|
|
|
2015-07-13 13:51:18 +08:00
|
|
|
for (const auto& extension : extensions) {
|
|
|
|
#if defined(OS_WIN)
|
2021-03-18 15:55:51 -04:00
|
|
|
filters[0].second.push_back(base::WideToASCII(extension));
|
2015-07-13 13:51:18 +08:00
|
|
|
#else
|
|
|
|
filters[0].second.push_back(extension);
|
|
|
|
#endif
|
|
|
|
}
|
2017-06-10 15:12:04 +09:00
|
|
|
|
|
|
|
// Allow all files when extension is specified.
|
|
|
|
filters.push_back(file_dialog::Filter());
|
|
|
|
filters.back().first = "All Files";
|
2019-09-13 10:26:59 -04:00
|
|
|
filters.back().second.emplace_back("*");
|
2017-06-10 15:12:04 +09:00
|
|
|
|
2015-07-13 13:51:18 +08:00
|
|
|
return filters;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2019-06-19 14:23:04 -07:00
|
|
|
namespace electron {
|
2014-10-31 00:37:14 +08:00
|
|
|
|
2017-11-02 22:50:04 +01:00
|
|
|
WebDialogHelper::WebDialogHelper(NativeWindow* window, bool offscreen)
|
2021-01-26 19:16:21 +01:00
|
|
|
: window_(window), offscreen_(offscreen) {}
|
2014-10-31 00:37:14 +08:00
|
|
|
|
2019-09-16 18:12:00 -04:00
|
|
|
WebDialogHelper::~WebDialogHelper() = default;
|
2015-07-13 13:51:18 +08:00
|
|
|
|
2016-09-06 17:24:37 +09:00
|
|
|
void WebDialogHelper::RunFileChooser(
|
|
|
|
content::RenderFrameHost* render_frame_host,
|
2020-07-13 18:13:34 -07:00
|
|
|
scoped_refptr<content::FileSelectListener> listener,
|
2020-10-27 17:05:28 -07:00
|
|
|
const FileChooserParams& params) {
|
2017-02-07 17:32:58 -08:00
|
|
|
file_dialog::DialogSettings settings;
|
2017-11-02 22:50:04 +01:00
|
|
|
settings.force_detached = offscreen_;
|
2017-02-07 17:32:58 -08:00
|
|
|
settings.filters = GetFileTypesFromAcceptType(params.accept_types);
|
|
|
|
settings.parent_window = window_;
|
|
|
|
settings.title = base::UTF16ToUTF8(params.title);
|
|
|
|
|
2020-10-27 17:05:28 -07:00
|
|
|
auto* fsc =
|
|
|
|
new FileSelectHelper(render_frame_host, std::move(listener), params.mode);
|
|
|
|
|
2019-01-12 06:30:43 +05:30
|
|
|
if (params.mode == FileChooserParams::Mode::kSave) {
|
2017-02-07 17:32:58 -08:00
|
|
|
settings.default_path = params.default_file_name;
|
2020-10-27 17:05:28 -07:00
|
|
|
fsc->ShowSaveDialog(settings);
|
2014-10-31 19:27:01 +08:00
|
|
|
} else {
|
2019-08-13 13:40:07 -07:00
|
|
|
int flags = file_dialog::OPEN_DIALOG_CREATE_DIRECTORY;
|
2014-10-31 19:27:01 +08:00
|
|
|
switch (params.mode) {
|
2019-01-12 06:30:43 +05:30
|
|
|
case FileChooserParams::Mode::kOpenMultiple:
|
2019-08-13 13:40:07 -07:00
|
|
|
flags |= file_dialog::OPEN_DIALOG_MULTI_SELECTIONS;
|
2018-06-20 12:54:42 -07:00
|
|
|
FALLTHROUGH;
|
2019-01-12 06:30:43 +05:30
|
|
|
case FileChooserParams::Mode::kOpen:
|
2019-08-13 13:40:07 -07:00
|
|
|
flags |= file_dialog::OPEN_DIALOG_OPEN_FILE;
|
2014-10-31 19:27:01 +08:00
|
|
|
break;
|
2019-01-12 06:30:43 +05:30
|
|
|
case FileChooserParams::Mode::kUploadFolder:
|
2019-08-13 13:40:07 -07:00
|
|
|
flags |= file_dialog::OPEN_DIALOG_OPEN_DIRECTORY;
|
2014-10-31 19:27:01 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NOTREACHED();
|
|
|
|
}
|
|
|
|
|
2020-02-04 12:19:40 -08:00
|
|
|
auto* browser_context = static_cast<electron::ElectronBrowserContext*>(
|
2018-03-06 15:32:00 +09:00
|
|
|
render_frame_host->GetProcess()->GetBrowserContext());
|
2018-04-17 21:55:30 -04:00
|
|
|
settings.default_path = browser_context->prefs()
|
|
|
|
->GetFilePath(prefs::kSelectFileLastDirectory)
|
|
|
|
.Append(params.default_file_name);
|
2017-02-07 17:32:58 -08:00
|
|
|
settings.properties = flags;
|
2020-10-27 17:05:28 -07:00
|
|
|
fsc->ShowOpenDialog(settings);
|
2014-10-31 19:27:01 +08:00
|
|
|
}
|
2014-10-31 00:37:14 +08:00
|
|
|
}
|
|
|
|
|
2019-01-12 06:30:43 +05:30
|
|
|
void WebDialogHelper::EnumerateDirectory(
|
|
|
|
content::WebContents* web_contents,
|
2020-07-13 18:13:34 -07:00
|
|
|
scoped_refptr<content::FileSelectListener> listener,
|
2019-01-12 06:30:43 +05:30
|
|
|
const base::FilePath& dir) {
|
2018-04-17 21:55:30 -04:00
|
|
|
int types = base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES |
|
2014-10-31 17:37:32 +08:00
|
|
|
base::FileEnumerator::INCLUDE_DOT_DOT;
|
|
|
|
base::FileEnumerator file_enum(dir, false, types);
|
|
|
|
|
|
|
|
base::FilePath path;
|
2019-01-12 06:30:43 +05:30
|
|
|
std::vector<FileChooserFileInfoPtr> file_info;
|
|
|
|
while (!(path = file_enum.Next()).empty()) {
|
|
|
|
file_info.push_back(FileChooserFileInfo::NewNativeFile(
|
2021-03-16 12:18:45 -04:00
|
|
|
NativeFileInfo::New(path, std::u16string())));
|
2019-01-12 06:30:43 +05:30
|
|
|
}
|
2014-10-31 17:37:32 +08:00
|
|
|
|
2019-01-09 16:36:01 -08:00
|
|
|
listener->FileSelected(std::move(file_info), dir,
|
2019-01-12 06:30:43 +05:30
|
|
|
FileChooserParams::Mode::kUploadFolder);
|
2014-10-31 00:37:14 +08:00
|
|
|
}
|
|
|
|
|
2019-06-19 14:23:04 -07:00
|
|
|
} // namespace electron
|