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.
|
|
|
|
|
|
|
|
#include "atom/browser/web_dialog_helper.h"
|
|
|
|
|
2015-07-13 05:51:18 +00:00
|
|
|
#include <string>
|
2014-10-31 09:37:32 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2015-07-13 08:19:30 +00:00
|
|
|
#include "atom/browser/atom_browser_context.h"
|
|
|
|
#include "atom/browser/native_window.h"
|
2014-10-30 16:37:14 +00:00
|
|
|
#include "atom/browser/ui/file_dialog.h"
|
|
|
|
#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"
|
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"
|
2015-01-10 02:02:44 +00:00
|
|
|
#include "content/public/common/file_chooser_file_info.h"
|
2016-09-06 08:24:37 +00:00
|
|
|
#include "content/public/common/file_chooser_params.h"
|
2015-07-13 05:51:18 +00:00
|
|
|
#include "net/base/mime_util.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
|
|
|
|
2015-07-13 05:51:18 +00:00
|
|
|
namespace {
|
|
|
|
|
2017-02-23 16:38:29 +00:00
|
|
|
class FileSelectHelper : public base::RefCounted<FileSelectHelper>,
|
2017-02-22 23:58:46 +00:00
|
|
|
public content::WebContentsObserver {
|
|
|
|
public:
|
|
|
|
FileSelectHelper(content::RenderFrameHost* render_frame_host,
|
|
|
|
const content::FileChooserParams::Mode& mode)
|
|
|
|
: render_frame_host_(render_frame_host), mode_(mode) {
|
2018-04-18 01:55:30 +00:00
|
|
|
auto web_contents =
|
|
|
|
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) {
|
2017-02-23 16:38:29 +00:00
|
|
|
auto callback = base::Bind(&FileSelectHelper::OnOpenDialogDone, this);
|
2017-02-22 23:58:46 +00:00
|
|
|
file_dialog::ShowOpenDialog(settings, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShowSaveDialog(const file_dialog::DialogSettings& settings) {
|
2017-02-23 16:38:29 +00:00
|
|
|
auto callback = base::Bind(&FileSelectHelper::OnSaveDialogDone, this);
|
2017-02-22 23:58:46 +00:00
|
|
|
file_dialog::ShowSaveDialog(settings, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-02-23 16:38:29 +00:00
|
|
|
friend class base::RefCounted<FileSelectHelper>;
|
|
|
|
|
2017-03-30 21:13:23 +00:00
|
|
|
~FileSelectHelper() override {}
|
2017-02-23 16:38:29 +00:00
|
|
|
|
2018-02-12 18:25:06 +00:00
|
|
|
#if defined(MAS_BUILD)
|
2018-04-18 01:55:30 +00:00
|
|
|
void OnOpenDialogDone(bool result,
|
|
|
|
const std::vector<base::FilePath>& paths,
|
2018-02-12 18:25:06 +00:00
|
|
|
const std::vector<std::string>& bookmarks)
|
|
|
|
#else
|
|
|
|
void OnOpenDialogDone(bool result, const std::vector<base::FilePath>& paths)
|
|
|
|
#endif
|
|
|
|
{
|
2017-02-22 23:58:46 +00:00
|
|
|
std::vector<content::FileChooserFileInfo> file_info;
|
|
|
|
if (result) {
|
|
|
|
for (auto& path : paths) {
|
|
|
|
content::FileChooserFileInfo info;
|
|
|
|
info.file_path = path;
|
|
|
|
info.display_name = path.BaseName().value();
|
|
|
|
file_info.push_back(info);
|
|
|
|
}
|
|
|
|
|
2017-02-24 15:49:26 +00:00
|
|
|
if (render_frame_host_ && !paths.empty()) {
|
2017-02-22 23:58:46 +00:00
|
|
|
auto browser_context = static_cast<atom::AtomBrowserContext*>(
|
|
|
|
render_frame_host_->GetProcess()->GetBrowserContext());
|
|
|
|
browser_context->prefs()->SetFilePath(prefs::kSelectFileLastDirectory,
|
|
|
|
paths[0].DirName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
OnFilesSelected(file_info);
|
|
|
|
}
|
|
|
|
|
2018-02-12 18:25:06 +00:00
|
|
|
#if defined(MAS_BUILD)
|
2018-04-18 01:55:30 +00:00
|
|
|
void OnSaveDialogDone(bool result,
|
|
|
|
const base::FilePath& path,
|
2018-02-12 18:25:06 +00:00
|
|
|
const std::string& bookmark)
|
|
|
|
#else
|
|
|
|
void OnSaveDialogDone(bool result, const base::FilePath& path)
|
|
|
|
#endif
|
|
|
|
{
|
2017-02-22 23:58:46 +00:00
|
|
|
std::vector<content::FileChooserFileInfo> file_info;
|
|
|
|
if (result) {
|
|
|
|
content::FileChooserFileInfo info;
|
|
|
|
info.file_path = path;
|
|
|
|
info.display_name = path.BaseName().value();
|
|
|
|
file_info.push_back(info);
|
|
|
|
}
|
|
|
|
OnFilesSelected(file_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnFilesSelected(
|
|
|
|
const std::vector<content::FileChooserFileInfo>& file_info) {
|
|
|
|
if (render_frame_host_)
|
|
|
|
render_frame_host_->FilesSelectedInChooser(file_info, mode_);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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_;
|
|
|
|
content::FileChooserParams::Mode mode_;
|
|
|
|
};
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-07-28 02:30:36 +00:00
|
|
|
// If no valid exntesion is added, return empty filters.
|
|
|
|
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
|
|
|
|
|
|
|
if (valid_type_count > 1 ||
|
|
|
|
(valid_type_count == 1 && description.empty() && extensions.size() > 1))
|
|
|
|
description = "Custom Files";
|
|
|
|
|
|
|
|
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";
|
|
|
|
filters.back().second.push_back("*");
|
|
|
|
|
2015-07-13 05:51:18 +00:00
|
|
|
return filters;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2014-10-30 16:37:14 +00:00
|
|
|
namespace atom {
|
|
|
|
|
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
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
WebDialogHelper::~WebDialogHelper() {}
|
2015-07-13 05:51:18 +00:00
|
|
|
|
2016-09-06 08:24:37 +00:00
|
|
|
void WebDialogHelper::RunFileChooser(
|
|
|
|
content::RenderFrameHost* render_frame_host,
|
|
|
|
const content::FileChooserParams& params) {
|
2015-01-10 02:02:44 +00:00
|
|
|
std::vector<content::FileChooserFileInfo> result;
|
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);
|
|
|
|
|
2017-02-22 23:58:46 +00:00
|
|
|
scoped_refptr<FileSelectHelper> file_select_helper(
|
|
|
|
new FileSelectHelper(render_frame_host, params.mode));
|
2014-10-31 11:27:01 +00:00
|
|
|
if (params.mode == content::FileChooserParams::Save) {
|
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 {
|
|
|
|
int flags = file_dialog::FILE_DIALOG_CREATE_DIRECTORY;
|
|
|
|
switch (params.mode) {
|
|
|
|
case content::FileChooserParams::OpenMultiple:
|
|
|
|
flags |= file_dialog::FILE_DIALOG_MULTI_SELECTIONS;
|
|
|
|
case content::FileChooserParams::Open:
|
|
|
|
flags |= file_dialog::FILE_DIALOG_OPEN_FILE;
|
|
|
|
break;
|
|
|
|
case content::FileChooserParams::UploadFolder:
|
|
|
|
flags |= file_dialog::FILE_DIALOG_OPEN_DIRECTORY;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NOTREACHED();
|
|
|
|
}
|
|
|
|
|
2018-03-06 06:32:00 +00:00
|
|
|
auto* browser_context = static_cast<atom::AtomBrowserContext*>(
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-10-31 09:37:32 +00:00
|
|
|
void WebDialogHelper::EnumerateDirectory(content::WebContents* web_contents,
|
|
|
|
int request_id,
|
|
|
|
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;
|
|
|
|
std::vector<base::FilePath> paths;
|
|
|
|
while (!(path = file_enum.Next()).empty())
|
|
|
|
paths.push_back(path);
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
web_contents->GetRenderViewHost()->DirectoryEnumerationFinished(request_id,
|
|
|
|
paths);
|
2014-10-30 16:37:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace atom
|