2014-10-30 16:37:14 +00: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 20:46:59 +00:00
|
|
|
#include "shell/browser/web_dialog_helper.h"
|
2014-10-30 16:37:14 +00:00
|
|
|
|
2019-09-16 22:12:00 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2015-07-13 05:51:18 +00:00
|
|
|
#include <string>
|
2019-01-12 01:00:43 +00:00
|
|
|
#include <utility>
|
2014-10-31 09:37:32 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2014-10-30 16:37:14 +00:00
|
|
|
#include "base/bind.h"
|
2014-10-31 09:37:32 +00:00
|
|
|
#include "base/files/file_enumerator.h"
|
2015-07-13 05:51:18 +00:00
|
|
|
#include "base/files/file_path.h"
|
2014-10-31 11:27:01 +00:00
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2015-07-26 08:08:29 +00:00
|
|
|
#include "chrome/common/pref_names.h"
|
2016-08-26 22:30:02 +00:00
|
|
|
#include "components/prefs/pref_service.h"
|
2019-01-12 01:00:43 +00:00
|
|
|
#include "content/public/browser/file_select_listener.h"
|
2016-09-06 08:24:37 +00:00
|
|
|
#include "content/public/browser/render_frame_host.h"
|
2017-02-22 23:58:46 +00:00
|
|
|
#include "content/public/browser/render_process_host.h"
|
2014-10-31 09:37:32 +00:00
|
|
|
#include "content/public/browser/render_view_host.h"
|
|
|
|
#include "content/public/browser/web_contents.h"
|
2019-02-21 17:29:06 +00:00
|
|
|
#include "content/public/browser/web_contents_observer.h"
|
2019-09-06 05:52:54 +00:00
|
|
|
#include "gin/dictionary.h"
|
2019-07-01 14:58:06 +00:00
|
|
|
#include "net/base/directory_lister.h"
|
2015-07-13 05:51:18 +00:00
|
|
|
#include "net/base/mime_util.h"
|
2020-02-04 20:19:40 +00:00
|
|
|
#include "shell/browser/electron_browser_context.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/native_window.h"
|
|
|
|
#include "shell/browser/ui/file_dialog.h"
|
2019-09-06 05:52:54 +00:00
|
|
|
#include "shell/common/gin_converters/callback_converter.h"
|
|
|
|
#include "shell/common/gin_converters/file_path_converter.h"
|
2019-09-08 15:10:18 +00:00
|
|
|
#include "shell/common/gin_helper/dictionary.h"
|
2014-10-31 11:27:01 +00:00
|
|
|
#include "ui/shell_dialogs/selected_file_info.h"
|
2014-10-30 16:37:14 +00:00
|
|
|
|
2019-01-12 01:00:43 +00:00
|
|
|
using blink::mojom::FileChooserFileInfo;
|
|
|
|
using blink::mojom::FileChooserFileInfoPtr;
|
|
|
|
using blink::mojom::FileChooserParams;
|
|
|
|
|
2015-07-13 05:51:18 +00:00
|
|
|
namespace {
|
|
|
|
|
2017-02-23 16:38:29 +00:00
|
|
|
class FileSelectHelper : public base::RefCounted<FileSelectHelper>,
|
2019-05-21 16:18:22 +00:00
|
|
|
public content::WebContentsObserver,
|
2019-07-01 14:58:06 +00:00
|
|
|
public net::DirectoryLister::DirectoryListerDelegate {
|
2017-02-22 23:58:46 +00:00
|
|
|
public:
|
2019-07-01 14:58:06 +00:00
|
|
|
REQUIRE_ADOPTION_FOR_REFCOUNTED_TYPE();
|
|
|
|
|
2017-02-22 23:58:46 +00:00
|
|
|
FileSelectHelper(content::RenderFrameHost* render_frame_host,
|
2019-01-12 01:00:43 +00:00
|
|
|
std::unique_ptr<content::FileSelectListener> listener,
|
|
|
|
blink::mojom::FileChooserParams::Mode mode)
|
|
|
|
: render_frame_host_(render_frame_host),
|
|
|
|
listener_(std::move(listener)),
|
|
|
|
mode_(mode) {
|
2018-04-17 22:41:47 +00:00
|
|
|
auto* web_contents =
|
2018-04-18 01:55:30 +00:00
|
|
|
content::WebContents::FromRenderFrameHost(render_frame_host);
|
2017-02-22 23:58:46 +00:00
|
|
|
content::WebContentsObserver::Observe(web_contents);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShowOpenDialog(const file_dialog::DialogSettings& settings) {
|
2019-03-05 13:54:48 +00:00
|
|
|
v8::Isolate* isolate = v8::Isolate::GetCurrent();
|
2020-03-27 03:37:28 +00:00
|
|
|
v8::HandleScope scope(isolate);
|
2019-11-01 06:10:32 +00:00
|
|
|
gin_helper::Promise<gin_helper::Dictionary> promise(isolate);
|
2019-03-05 13:54:48 +00:00
|
|
|
|
2019-05-02 23:49:27 +00:00
|
|
|
auto callback = base::BindOnce(&FileSelectHelper::OnOpenDialogDone, this);
|
|
|
|
ignore_result(promise.Then(std::move(callback)));
|
2019-05-14 22:46:53 +00:00
|
|
|
|
|
|
|
file_dialog::ShowOpenDialog(settings, std::move(promise));
|
2017-02-22 23:58:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ShowSaveDialog(const file_dialog::DialogSettings& settings) {
|
2019-03-05 21:48:20 +00:00
|
|
|
v8::Isolate* isolate = v8::Isolate::GetCurrent();
|
2020-03-27 03:37:28 +00:00
|
|
|
v8::HandleScope scope(isolate);
|
2019-11-01 06:10:32 +00:00
|
|
|
gin_helper::Promise<gin_helper::Dictionary> promise(isolate);
|
2019-08-26 11:12:09 +00:00
|
|
|
|
|
|
|
auto callback = base::BindOnce(&FileSelectHelper::OnSaveDialogDone, this);
|
|
|
|
ignore_result(promise.Then(std::move(callback)));
|
2019-03-05 21:48:20 +00:00
|
|
|
|
|
|
|
file_dialog::ShowSaveDialog(settings, std::move(promise));
|
2017-02-22 23:58:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-02-23 16:38:29 +00:00
|
|
|
friend class base::RefCounted<FileSelectHelper>;
|
|
|
|
|
2019-09-16 22:12:00 +00:00
|
|
|
~FileSelectHelper() override = default;
|
2017-02-23 16:38:29 +00:00
|
|
|
|
2019-07-01 14:58:06 +00:00
|
|
|
// net::DirectoryLister::DirectoryListerDelegate
|
|
|
|
void OnListFile(
|
|
|
|
const net::DirectoryLister::DirectoryListerData& data) override {
|
|
|
|
// We don't want to return directory paths, only file paths
|
|
|
|
if (data.info.IsDirectory())
|
|
|
|
return;
|
|
|
|
|
|
|
|
lister_paths_.push_back(data.path);
|
|
|
|
}
|
|
|
|
|
|
|
|
// net::DirectoryLister::DirectoryListerDelegate
|
|
|
|
void OnListDone(int error) override {
|
|
|
|
std::vector<FileChooserFileInfoPtr> file_info;
|
|
|
|
for (const auto& path : lister_paths_)
|
|
|
|
file_info.push_back(FileChooserFileInfo::NewNativeFile(
|
|
|
|
blink::mojom::NativeFileInfo::New(path, base::string16())));
|
|
|
|
|
|
|
|
OnFilesSelected(std::move(file_info), lister_base_dir_);
|
|
|
|
Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EnumerateDirectory() {
|
|
|
|
// Ensure that this fn is only called once
|
|
|
|
DCHECK(!lister_);
|
|
|
|
DCHECK(!lister_base_dir_.empty());
|
|
|
|
DCHECK(lister_paths_.empty());
|
|
|
|
|
2019-09-16 22:12:00 +00:00
|
|
|
lister_ = std::make_unique<net::DirectoryLister>(
|
|
|
|
lister_base_dir_, net::DirectoryLister::NO_SORT_RECURSIVE, this);
|
2019-07-01 14:58:06 +00:00
|
|
|
lister_->Start();
|
2019-05-21 16:18:22 +00:00
|
|
|
// It is difficult for callers to know how long to keep a reference to
|
|
|
|
// this instance. We AddRef() here to keep the instance alive after we
|
|
|
|
// return to the caller. Once the directory lister is complete we
|
2019-07-01 14:58:06 +00:00
|
|
|
// Release() & at that point we run OnFilesSelected() which will
|
|
|
|
// deref the last reference held by the listener.
|
2019-05-21 16:18:22 +00:00
|
|
|
AddRef();
|
|
|
|
}
|
|
|
|
|
2019-09-08 15:10:18 +00:00
|
|
|
void OnOpenDialogDone(gin_helper::Dictionary result) {
|
2019-01-12 01:00:43 +00:00
|
|
|
std::vector<FileChooserFileInfoPtr> file_info;
|
2019-05-14 22:46:53 +00:00
|
|
|
bool canceled = true;
|
|
|
|
result.Get("canceled", &canceled);
|
2019-05-21 16:18:22 +00:00
|
|
|
// For certain file chooser modes (kUploadFolder) we need to do some async
|
|
|
|
// work before calling back to the listener. In that particular case the
|
|
|
|
// listener is called from the directory enumerator.
|
|
|
|
bool ready_to_call_listener = false;
|
2019-05-14 22:46:53 +00:00
|
|
|
|
2019-08-23 21:18:30 +00:00
|
|
|
if (canceled) {
|
|
|
|
OnSelectionCancelled();
|
|
|
|
} else {
|
2019-05-14 22:46:53 +00:00
|
|
|
std::vector<base::FilePath> paths;
|
|
|
|
if (result.Get("filePaths", &paths)) {
|
2019-05-21 16:18:22 +00:00
|
|
|
// If we are uploading a folder we need to enumerate its contents
|
|
|
|
if (mode_ == FileChooserParams::Mode::kUploadFolder &&
|
|
|
|
paths.size() >= 1) {
|
2019-07-01 14:58:06 +00:00
|
|
|
lister_base_dir_ = paths[0];
|
|
|
|
EnumerateDirectory();
|
2019-05-21 16:18:22 +00:00
|
|
|
} else {
|
|
|
|
for (auto& path : paths) {
|
|
|
|
file_info.push_back(FileChooserFileInfo::NewNativeFile(
|
|
|
|
blink::mojom::NativeFileInfo::New(
|
|
|
|
path, path.BaseName().AsUTF16Unsafe())));
|
|
|
|
}
|
|
|
|
|
|
|
|
ready_to_call_listener = true;
|
2019-05-14 22:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (render_frame_host_ && !paths.empty()) {
|
2020-02-04 20:19:40 +00:00
|
|
|
auto* browser_context =
|
|
|
|
static_cast<electron::ElectronBrowserContext*>(
|
|
|
|
render_frame_host_->GetProcess()->GetBrowserContext());
|
2019-05-14 22:46:53 +00:00
|
|
|
browser_context->prefs()->SetFilePath(prefs::kSelectFileLastDirectory,
|
|
|
|
paths[0].DirName());
|
|
|
|
}
|
2017-02-22 23:58:46 +00:00
|
|
|
}
|
2019-08-23 21:18:30 +00:00
|
|
|
// We should only call this if we have not cancelled the dialog
|
|
|
|
if (ready_to_call_listener)
|
|
|
|
OnFilesSelected(std::move(file_info), lister_base_dir_);
|
2017-02-22 23:58:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-08 15:10:18 +00:00
|
|
|
void OnSaveDialogDone(gin_helper::Dictionary result) {
|
2019-01-12 01:00:43 +00:00
|
|
|
std::vector<FileChooserFileInfoPtr> file_info;
|
2019-05-14 22:46:53 +00:00
|
|
|
bool canceled = true;
|
|
|
|
result.Get("canceled", &canceled);
|
|
|
|
|
2019-08-23 21:18:30 +00:00
|
|
|
if (canceled) {
|
|
|
|
OnSelectionCancelled();
|
|
|
|
} else {
|
2019-05-14 22:46:53 +00:00
|
|
|
base::FilePath path;
|
|
|
|
if (result.Get("filePath", &path)) {
|
|
|
|
file_info.push_back(FileChooserFileInfo::NewNativeFile(
|
|
|
|
blink::mojom::NativeFileInfo::New(
|
|
|
|
path, path.BaseName().AsUTF16Unsafe())));
|
|
|
|
}
|
2019-08-23 21:18:30 +00:00
|
|
|
// We should only call this if we have not cancelled the dialog
|
|
|
|
OnFilesSelected(std::move(file_info), base::FilePath());
|
2017-02-22 23:58:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-21 16:18:22 +00:00
|
|
|
void OnFilesSelected(std::vector<FileChooserFileInfoPtr> file_info,
|
|
|
|
base::FilePath base_dir) {
|
2019-01-12 01:00:43 +00:00
|
|
|
if (listener_) {
|
2019-05-21 16:18:22 +00:00
|
|
|
listener_->FileSelected(std::move(file_info), base_dir, mode_);
|
2019-01-12 01:00:43 +00:00
|
|
|
listener_.reset();
|
|
|
|
}
|
|
|
|
render_frame_host_ = nullptr;
|
2017-02-22 23:58:46 +00:00
|
|
|
}
|
|
|
|
|
2019-08-23 21:18:30 +00:00
|
|
|
void OnSelectionCancelled() {
|
|
|
|
if (listener_) {
|
|
|
|
listener_->FileSelectionCanceled();
|
|
|
|
listener_.reset();
|
|
|
|
}
|
|
|
|
render_frame_host_ = nullptr;
|
|
|
|
}
|
|
|
|
|
2017-02-22 23:58:46 +00:00
|
|
|
// content::WebContentsObserver:
|
|
|
|
void RenderFrameHostChanged(content::RenderFrameHost* old_host,
|
|
|
|
content::RenderFrameHost* new_host) override {
|
|
|
|
if (old_host == render_frame_host_)
|
|
|
|
render_frame_host_ = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// content::WebContentsObserver:
|
|
|
|
void RenderFrameDeleted(content::RenderFrameHost* deleted_host) override {
|
|
|
|
if (deleted_host == render_frame_host_)
|
|
|
|
render_frame_host_ = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// content::WebContentsObserver:
|
2018-04-18 01:55:30 +00:00
|
|
|
void WebContentsDestroyed() override { render_frame_host_ = nullptr; }
|
2017-02-22 23:58:46 +00:00
|
|
|
|
|
|
|
content::RenderFrameHost* render_frame_host_;
|
2019-01-12 01:00:43 +00:00
|
|
|
std::unique_ptr<content::FileSelectListener> listener_;
|
|
|
|
blink::mojom::FileChooserParams::Mode mode_;
|
2019-07-01 14:58:06 +00:00
|
|
|
|
|
|
|
// DirectoryLister-specific members
|
|
|
|
std::unique_ptr<net::DirectoryLister> lister_;
|
|
|
|
base::FilePath lister_base_dir_;
|
|
|
|
std::vector<base::FilePath> lister_paths_;
|
2017-02-22 23:58:46 +00:00
|
|
|
};
|
|
|
|
|
2015-07-13 05:51:18 +00:00
|
|
|
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;
|
|
|
|
|
2017-07-14 18:04:06 +00:00
|
|
|
int valid_type_count = 0;
|
|
|
|
std::string description;
|
|
|
|
|
2015-07-13 05:51:18 +00:00
|
|
|
for (const auto& accept_type : accept_types) {
|
|
|
|
std::string ascii_type = base::UTF16ToASCII(accept_type);
|
2017-07-14 18:04:06 +00:00
|
|
|
auto old_extension_size = extensions.size();
|
|
|
|
|
2015-07-13 05:51:18 +00: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-18 01:55:30 +00:00
|
|
|
base::FilePath::StringType extension(ascii_type.begin(),
|
|
|
|
ascii_type.end());
|
2015-07-13 05:51:18 +00:00
|
|
|
// Skip the first character.
|
|
|
|
extensions.push_back(extension.substr(1));
|
|
|
|
} else {
|
2017-07-14 18:04:06 +00: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 02:30:36 +00:00
|
|
|
net::GetExtensionsForMimeType(ascii_type, &extensions);
|
2015-07-13 05:51:18 +00:00
|
|
|
}
|
2017-07-14 18:04:06 +00:00
|
|
|
|
|
|
|
if (extensions.size() > old_extension_size)
|
|
|
|
valid_type_count++;
|
2015-07-13 05:51:18 +00:00
|
|
|
}
|
|
|
|
|
2019-07-01 14:58:06 +00:00
|
|
|
// If no valid extension is added, return empty filters.
|
2015-07-28 02:30:36 +00:00
|
|
|
if (extensions.empty())
|
|
|
|
return filters;
|
|
|
|
|
2015-07-13 05:51:18 +00:00
|
|
|
filters.push_back(file_dialog::Filter());
|
2017-07-14 18:04:06 +00:00
|
|
|
|
2018-05-15 20:28:28 +00:00
|
|
|
if (valid_type_count > 1 || (valid_type_count == 1 && description.empty()))
|
2017-07-14 18:04:06 +00:00
|
|
|
description = "Custom Files";
|
|
|
|
|
2018-05-15 20:28:28 +00:00
|
|
|
DCHECK(!description.empty());
|
2017-07-14 18:04:06 +00:00
|
|
|
filters[0].first = description;
|
|
|
|
|
2015-07-13 05:51:18 +00:00
|
|
|
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
|
|
|
|
}
|
2017-06-10 06:12:04 +00:00
|
|
|
|
|
|
|
// Allow all files when extension is specified.
|
|
|
|
filters.push_back(file_dialog::Filter());
|
|
|
|
filters.back().first = "All Files";
|
2019-09-13 14:26:59 +00:00
|
|
|
filters.back().second.emplace_back("*");
|
2017-06-10 06:12:04 +00:00
|
|
|
|
2015-07-13 05:51:18 +00:00
|
|
|
return filters;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
namespace electron {
|
2014-10-30 16:37:14 +00:00
|
|
|
|
2017-11-02 21:50:04 +00:00
|
|
|
WebDialogHelper::WebDialogHelper(NativeWindow* window, bool offscreen)
|
2018-04-18 01:55:30 +00:00
|
|
|
: window_(window), offscreen_(offscreen), weak_factory_(this) {}
|
2014-10-30 16:37:14 +00:00
|
|
|
|
2019-09-16 22:12:00 +00:00
|
|
|
WebDialogHelper::~WebDialogHelper() = default;
|
2015-07-13 05:51:18 +00:00
|
|
|
|
2016-09-06 08:24:37 +00:00
|
|
|
void WebDialogHelper::RunFileChooser(
|
|
|
|
content::RenderFrameHost* render_frame_host,
|
2019-01-12 01:00:43 +00:00
|
|
|
std::unique_ptr<content::FileSelectListener> listener,
|
|
|
|
const blink::mojom::FileChooserParams& params) {
|
2017-02-08 01:32:58 +00:00
|
|
|
file_dialog::DialogSettings settings;
|
2017-11-02 21:50:04 +00:00
|
|
|
settings.force_detached = offscreen_;
|
2017-02-08 01:32:58 +00:00
|
|
|
settings.filters = GetFileTypesFromAcceptType(params.accept_types);
|
|
|
|
settings.parent_window = window_;
|
|
|
|
settings.title = base::UTF16ToUTF8(params.title);
|
|
|
|
|
2019-07-01 14:58:06 +00:00
|
|
|
auto file_select_helper = base::MakeRefCounted<FileSelectHelper>(
|
|
|
|
render_frame_host, std::move(listener), params.mode);
|
2019-01-12 01:00:43 +00:00
|
|
|
if (params.mode == FileChooserParams::Mode::kSave) {
|
2017-02-08 01:32:58 +00:00
|
|
|
settings.default_path = params.default_file_name;
|
2017-02-22 23:58:46 +00:00
|
|
|
file_select_helper->ShowSaveDialog(settings);
|
2014-10-31 11:27:01 +00:00
|
|
|
} else {
|
2019-08-13 20:40:07 +00:00
|
|
|
int flags = file_dialog::OPEN_DIALOG_CREATE_DIRECTORY;
|
2014-10-31 11:27:01 +00:00
|
|
|
switch (params.mode) {
|
2019-01-12 01:00:43 +00:00
|
|
|
case FileChooserParams::Mode::kOpenMultiple:
|
2019-08-13 20:40:07 +00:00
|
|
|
flags |= file_dialog::OPEN_DIALOG_MULTI_SELECTIONS;
|
2018-06-20 19:54:42 +00:00
|
|
|
FALLTHROUGH;
|
2019-01-12 01:00:43 +00:00
|
|
|
case FileChooserParams::Mode::kOpen:
|
2019-08-13 20:40:07 +00:00
|
|
|
flags |= file_dialog::OPEN_DIALOG_OPEN_FILE;
|
|
|
|
flags |= file_dialog::OPEN_DIALOG_TREAT_PACKAGE_APP_AS_DIRECTORY;
|
2014-10-31 11:27:01 +00:00
|
|
|
break;
|
2019-01-12 01:00:43 +00:00
|
|
|
case FileChooserParams::Mode::kUploadFolder:
|
2019-08-13 20:40:07 +00:00
|
|
|
flags |= file_dialog::OPEN_DIALOG_OPEN_DIRECTORY;
|
2014-10-31 11:27:01 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NOTREACHED();
|
|
|
|
}
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
auto* browser_context = static_cast<electron::ElectronBrowserContext*>(
|
2018-03-06 06:32:00 +00:00
|
|
|
render_frame_host->GetProcess()->GetBrowserContext());
|
2018-04-18 01:55:30 +00:00
|
|
|
settings.default_path = browser_context->prefs()
|
|
|
|
->GetFilePath(prefs::kSelectFileLastDirectory)
|
|
|
|
.Append(params.default_file_name);
|
2017-02-08 01:32:58 +00:00
|
|
|
settings.properties = flags;
|
2017-02-22 23:58:46 +00:00
|
|
|
file_select_helper->ShowOpenDialog(settings);
|
2014-10-31 11:27:01 +00:00
|
|
|
}
|
2014-10-30 16:37:14 +00:00
|
|
|
}
|
|
|
|
|
2019-01-12 01:00:43 +00:00
|
|
|
void WebDialogHelper::EnumerateDirectory(
|
|
|
|
content::WebContents* web_contents,
|
|
|
|
std::unique_ptr<content::FileSelectListener> listener,
|
|
|
|
const base::FilePath& dir) {
|
2018-04-18 01:55:30 +00:00
|
|
|
int types = base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES |
|
2014-10-31 09:37:32 +00:00
|
|
|
base::FileEnumerator::INCLUDE_DOT_DOT;
|
|
|
|
base::FileEnumerator file_enum(dir, false, types);
|
|
|
|
|
|
|
|
base::FilePath path;
|
2019-01-12 01:00:43 +00:00
|
|
|
std::vector<FileChooserFileInfoPtr> file_info;
|
|
|
|
while (!(path = file_enum.Next()).empty()) {
|
|
|
|
file_info.push_back(FileChooserFileInfo::NewNativeFile(
|
|
|
|
blink::mojom::NativeFileInfo::New(path, base::string16())));
|
|
|
|
}
|
2014-10-31 09:37:32 +00:00
|
|
|
|
2019-01-10 00:36:01 +00:00
|
|
|
listener->FileSelected(std::move(file_info), dir,
|
2019-01-12 01:00:43 +00:00
|
|
|
FileChooserParams::Mode::kUploadFolder);
|
2014-10-30 16:37:14 +00:00
|
|
|
}
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
} // namespace electron
|