2015-11-17 19:20:13 +01:00
|
|
|
// Copyright (c) 2012 The Chromium Embedded Framework Authors.
|
|
|
|
// Portions copyright (c) 2012 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "libcef/browser/file_dialog_manager.h"
|
|
|
|
|
2016-01-06 20:20:54 +01:00
|
|
|
#include <utility>
|
|
|
|
|
2015-11-17 19:20:13 +01:00
|
|
|
#include "include/cef_dialog_handler.h"
|
|
|
|
#include "libcef/browser/browser_host_impl.h"
|
|
|
|
#include "libcef/browser/thread_util.h"
|
|
|
|
|
2016-06-21 00:59:23 +02:00
|
|
|
#include "content/public/browser/render_frame_host.h"
|
2015-11-17 19:20:13 +01:00
|
|
|
#include "content/public/common/file_chooser_file_info.h"
|
|
|
|
#include "net/base/directory_lister.h"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class CefFileDialogCallbackImpl : public CefFileDialogCallback {
|
|
|
|
public:
|
|
|
|
explicit CefFileDialogCallbackImpl(
|
|
|
|
const CefFileDialogRunner::RunFileChooserCallback& callback)
|
|
|
|
: callback_(callback) {
|
|
|
|
}
|
|
|
|
~CefFileDialogCallbackImpl() override {
|
|
|
|
if (!callback_.is_null()) {
|
|
|
|
// The callback is still pending. Cancel it now.
|
|
|
|
if (CEF_CURRENTLY_ON_UIT()) {
|
|
|
|
CancelNow(callback_);
|
|
|
|
} else {
|
|
|
|
CEF_POST_TASK(CEF_UIT,
|
|
|
|
base::Bind(&CefFileDialogCallbackImpl::CancelNow, callback_));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Continue(int selected_accept_filter,
|
|
|
|
const std::vector<CefString>& file_paths) override {
|
|
|
|
if (CEF_CURRENTLY_ON_UIT()) {
|
|
|
|
if (!callback_.is_null()) {
|
|
|
|
std::vector<base::FilePath> vec;
|
|
|
|
if (!file_paths.empty()) {
|
|
|
|
std::vector<CefString>::const_iterator it = file_paths.begin();
|
|
|
|
for (; it != file_paths.end(); ++it)
|
|
|
|
vec.push_back(base::FilePath(*it));
|
|
|
|
}
|
|
|
|
callback_.Run(selected_accept_filter, vec);
|
|
|
|
callback_.Reset();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
CEF_POST_TASK(CEF_UIT,
|
|
|
|
base::Bind(&CefFileDialogCallbackImpl::Continue, this,
|
|
|
|
selected_accept_filter, file_paths));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Cancel() override {
|
|
|
|
if (CEF_CURRENTLY_ON_UIT()) {
|
|
|
|
if (!callback_.is_null()) {
|
|
|
|
CancelNow(callback_);
|
|
|
|
callback_.Reset();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
CEF_POST_TASK(CEF_UIT,
|
|
|
|
base::Bind(&CefFileDialogCallbackImpl::Cancel, this));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsConnected() {
|
|
|
|
return !callback_.is_null();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Disconnect() {
|
|
|
|
callback_.Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static void CancelNow(
|
|
|
|
const CefFileDialogRunner::RunFileChooserCallback& callback) {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
std::vector<base::FilePath> file_paths;
|
|
|
|
callback.Run(0, file_paths);
|
|
|
|
}
|
|
|
|
|
|
|
|
CefFileDialogRunner::RunFileChooserCallback callback_;
|
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(CefFileDialogCallbackImpl);
|
|
|
|
};
|
|
|
|
|
|
|
|
void RunFileDialogDismissed(
|
|
|
|
CefRefPtr<CefRunFileDialogCallback> callback,
|
|
|
|
int selected_accept_filter,
|
|
|
|
const std::vector<base::FilePath>& file_paths) {
|
|
|
|
std::vector<CefString> paths;
|
|
|
|
if (file_paths.size() > 0) {
|
|
|
|
for (size_t i = 0; i < file_paths.size(); ++i)
|
|
|
|
paths.push_back(file_paths[i].value());
|
|
|
|
}
|
|
|
|
callback->OnFileDialogDismissed(selected_accept_filter, paths);
|
|
|
|
}
|
|
|
|
|
|
|
|
class UploadFolderHelper :
|
|
|
|
public net::DirectoryLister::DirectoryListerDelegate {
|
|
|
|
public:
|
|
|
|
explicit UploadFolderHelper(
|
|
|
|
const CefFileDialogRunner::RunFileChooserCallback& callback)
|
|
|
|
: callback_(callback) {
|
|
|
|
}
|
|
|
|
|
|
|
|
~UploadFolderHelper() override {
|
|
|
|
if (!callback_.is_null()) {
|
|
|
|
if (CEF_CURRENTLY_ON_UIT()) {
|
|
|
|
CancelNow(callback_);
|
|
|
|
} else {
|
|
|
|
CEF_POST_TASK(CEF_UIT,
|
|
|
|
base::Bind(&UploadFolderHelper::CancelNow, callback_));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnListFile(
|
|
|
|
const net::DirectoryLister::DirectoryListerData& data) override {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
if (!data.info.IsDirectory())
|
|
|
|
select_files_.push_back(data.path);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnListDone(int error) override {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
if (!callback_.is_null()) {
|
|
|
|
callback_.Run(0, select_files_);
|
|
|
|
callback_.Reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static void CancelNow(
|
|
|
|
const CefFileDialogRunner::RunFileChooserCallback& callback) {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
std::vector<base::FilePath> file_paths;
|
|
|
|
callback.Run(0, file_paths);
|
|
|
|
}
|
|
|
|
|
|
|
|
CefFileDialogRunner::RunFileChooserCallback callback_;
|
|
|
|
std::vector<base::FilePath> select_files_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(UploadFolderHelper);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
CefFileDialogManager::CefFileDialogManager(
|
|
|
|
CefBrowserHostImpl* browser,
|
2016-04-27 22:38:52 +02:00
|
|
|
std::unique_ptr<CefFileDialogRunner> runner)
|
2015-11-17 19:20:13 +01:00
|
|
|
: content::WebContentsObserver(browser->web_contents()),
|
|
|
|
browser_(browser),
|
2016-01-06 20:20:54 +01:00
|
|
|
runner_(std::move(runner)),
|
2015-11-17 19:20:13 +01:00
|
|
|
file_chooser_pending_(false),
|
2016-06-21 00:59:23 +02:00
|
|
|
render_frame_host_(nullptr),
|
2015-11-17 19:20:13 +01:00
|
|
|
weak_ptr_factory_(this) {
|
|
|
|
DCHECK(web_contents());
|
|
|
|
}
|
|
|
|
|
|
|
|
CefFileDialogManager::~CefFileDialogManager() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefFileDialogManager::Destroy() {
|
|
|
|
DCHECK(!file_chooser_pending_);
|
|
|
|
runner_.reset(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefFileDialogManager::RunFileDialog(
|
|
|
|
cef_file_dialog_mode_t mode,
|
|
|
|
const CefString& title,
|
|
|
|
const CefString& default_file_path,
|
|
|
|
const std::vector<CefString>& accept_filters,
|
|
|
|
int selected_accept_filter,
|
|
|
|
CefRefPtr<CefRunFileDialogCallback> callback) {
|
|
|
|
DCHECK(callback.get());
|
|
|
|
if (!callback.get())
|
|
|
|
return;
|
|
|
|
|
|
|
|
CefFileDialogRunner::FileChooserParams params;
|
|
|
|
switch (mode & FILE_DIALOG_TYPE_MASK) {
|
|
|
|
case FILE_DIALOG_OPEN:
|
|
|
|
params.mode = content::FileChooserParams::Open;
|
|
|
|
break;
|
|
|
|
case FILE_DIALOG_OPEN_MULTIPLE:
|
|
|
|
params.mode = content::FileChooserParams::OpenMultiple;
|
|
|
|
break;
|
|
|
|
case FILE_DIALOG_OPEN_FOLDER:
|
|
|
|
params.mode = content::FileChooserParams::UploadFolder;
|
|
|
|
break;
|
|
|
|
case FILE_DIALOG_SAVE:
|
|
|
|
params.mode = content::FileChooserParams::Save;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
DCHECK_GE(selected_accept_filter, 0);
|
|
|
|
params.selected_accept_filter = selected_accept_filter;
|
|
|
|
|
|
|
|
params.overwriteprompt = !!(mode & FILE_DIALOG_OVERWRITEPROMPT_FLAG);
|
|
|
|
params.hidereadonly = !!(mode & FILE_DIALOG_HIDEREADONLY_FLAG);
|
|
|
|
|
|
|
|
params.title = title;
|
|
|
|
if (!default_file_path.empty())
|
|
|
|
params.default_file_name = base::FilePath(default_file_path);
|
|
|
|
|
|
|
|
if (!accept_filters.empty()) {
|
|
|
|
std::vector<CefString>::const_iterator it = accept_filters.begin();
|
|
|
|
for (; it != accept_filters.end(); ++it)
|
|
|
|
params.accept_types.push_back(*it);
|
|
|
|
}
|
|
|
|
|
|
|
|
RunFileChooser(params, base::Bind(RunFileDialogDismissed, callback));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefFileDialogManager::RunFileChooser(
|
2016-06-21 00:59:23 +02:00
|
|
|
content::RenderFrameHost* render_frame_host,
|
2015-11-17 19:20:13 +01:00
|
|
|
const content::FileChooserParams& params) {
|
|
|
|
CEF_REQUIRE_UIT();
|
2016-06-21 00:59:23 +02:00
|
|
|
DCHECK(render_frame_host);
|
2015-11-17 19:20:13 +01:00
|
|
|
|
|
|
|
CefFileDialogRunner::FileChooserParams cef_params;
|
|
|
|
static_cast<content::FileChooserParams&>(cef_params) = params;
|
|
|
|
|
2016-06-21 00:59:23 +02:00
|
|
|
CefFileDialogRunner::RunFileChooserCallback callback;
|
2015-11-17 19:20:13 +01:00
|
|
|
if (params.mode == content::FileChooserParams::UploadFolder) {
|
2016-06-21 00:59:23 +02:00
|
|
|
callback = base::Bind(
|
|
|
|
&CefFileDialogManager::OnRunFileChooserUploadFolderDelegateCallback,
|
|
|
|
weak_ptr_factory_.GetWeakPtr(), params.mode);
|
|
|
|
} else {
|
|
|
|
callback = base::Bind(
|
|
|
|
&CefFileDialogManager::OnRunFileChooserDelegateCallback,
|
|
|
|
weak_ptr_factory_.GetWeakPtr(), params.mode);
|
2015-11-17 19:20:13 +01:00
|
|
|
}
|
|
|
|
|
2016-06-21 00:59:23 +02:00
|
|
|
RunFileChooserInternal(render_frame_host, cef_params, callback);
|
2015-11-17 19:20:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefFileDialogManager::RunFileChooser(
|
|
|
|
const CefFileDialogRunner::FileChooserParams& params,
|
|
|
|
const CefFileDialogRunner::RunFileChooserCallback& callback) {
|
2016-06-21 00:59:23 +02:00
|
|
|
const CefFileDialogRunner::RunFileChooserCallback& host_callback =
|
|
|
|
base::Bind(&CefFileDialogManager::OnRunFileChooserCallback,
|
|
|
|
weak_ptr_factory_.GetWeakPtr(), callback);
|
|
|
|
RunFileChooserInternal(nullptr, params, host_callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefFileDialogManager::RunFileChooserInternal(
|
|
|
|
content::RenderFrameHost* render_frame_host,
|
|
|
|
const CefFileDialogRunner::FileChooserParams& params,
|
|
|
|
const CefFileDialogRunner::RunFileChooserCallback& callback) {
|
2015-11-17 19:20:13 +01:00
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
|
|
|
|
if (file_chooser_pending_) {
|
|
|
|
// Dismiss the new dialog immediately.
|
|
|
|
callback.Run(0, std::vector<base::FilePath>());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
file_chooser_pending_ = true;
|
2016-06-21 00:59:23 +02:00
|
|
|
render_frame_host_ = render_frame_host;
|
2015-11-17 19:20:13 +01:00
|
|
|
|
|
|
|
bool handled = false;
|
|
|
|
|
|
|
|
if (browser_->client().get()) {
|
|
|
|
CefRefPtr<CefDialogHandler> handler =
|
|
|
|
browser_->client()->GetDialogHandler();
|
|
|
|
if (handler.get()) {
|
|
|
|
int mode = FILE_DIALOG_OPEN;
|
|
|
|
switch (params.mode) {
|
|
|
|
case content::FileChooserParams::Open:
|
|
|
|
mode = FILE_DIALOG_OPEN;
|
|
|
|
break;
|
|
|
|
case content::FileChooserParams::OpenMultiple:
|
|
|
|
mode = FILE_DIALOG_OPEN_MULTIPLE;
|
|
|
|
break;
|
|
|
|
case content::FileChooserParams::UploadFolder:
|
|
|
|
mode = FILE_DIALOG_OPEN_FOLDER;
|
|
|
|
break;
|
|
|
|
case content::FileChooserParams::Save:
|
|
|
|
mode = FILE_DIALOG_SAVE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NOTREACHED();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (params.overwriteprompt)
|
|
|
|
mode |= FILE_DIALOG_OVERWRITEPROMPT_FLAG;
|
|
|
|
if (params.hidereadonly)
|
|
|
|
mode |= FILE_DIALOG_HIDEREADONLY_FLAG;
|
|
|
|
|
|
|
|
std::vector<base::string16>::const_iterator it;
|
|
|
|
|
|
|
|
std::vector<CefString> accept_filters;
|
|
|
|
it = params.accept_types.begin();
|
|
|
|
for (; it != params.accept_types.end(); ++it)
|
|
|
|
accept_filters.push_back(*it);
|
|
|
|
|
|
|
|
CefRefPtr<CefFileDialogCallbackImpl> callbackImpl(
|
2016-06-21 00:59:23 +02:00
|
|
|
new CefFileDialogCallbackImpl(callback));
|
2015-11-17 19:20:13 +01:00
|
|
|
handled = handler->OnFileDialog(
|
|
|
|
browser_,
|
|
|
|
static_cast<cef_file_dialog_mode_t>(mode),
|
|
|
|
params.title,
|
|
|
|
params.default_file_name.value(),
|
|
|
|
accept_filters,
|
|
|
|
params.selected_accept_filter,
|
|
|
|
callbackImpl.get());
|
|
|
|
if (!handled) {
|
|
|
|
if (callbackImpl->IsConnected()) {
|
|
|
|
callbackImpl->Disconnect();
|
|
|
|
} else {
|
|
|
|
// User executed the callback even though they returned false.
|
|
|
|
NOTREACHED();
|
|
|
|
handled = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!handled) {
|
|
|
|
if (runner_.get()) {
|
2016-06-21 00:59:23 +02:00
|
|
|
runner_->Run(browser_, params, callback);
|
2015-11-17 19:20:13 +01:00
|
|
|
} else {
|
|
|
|
LOG(WARNING) << "No file dialog runner available for this platform";
|
2016-06-21 00:59:23 +02:00
|
|
|
callback.Run(0, std::vector<base::FilePath>());
|
2015-11-17 19:20:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefFileDialogManager::OnRunFileChooserCallback(
|
|
|
|
const CefFileDialogRunner::RunFileChooserCallback& callback,
|
|
|
|
int selected_accept_filter,
|
|
|
|
const std::vector<base::FilePath>& file_paths) {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
|
2016-06-21 00:59:23 +02:00
|
|
|
Cleanup();
|
2015-11-17 19:20:13 +01:00
|
|
|
|
|
|
|
// Execute the callback asynchronously.
|
|
|
|
CEF_POST_TASK(CEF_UIT,
|
|
|
|
base::Bind(callback, selected_accept_filter, file_paths));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefFileDialogManager::OnRunFileChooserUploadFolderDelegateCallback(
|
|
|
|
const content::FileChooserParams::Mode mode,
|
|
|
|
int selected_accept_filter,
|
|
|
|
const std::vector<base::FilePath>& file_paths) {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
DCHECK_EQ(mode, content::FileChooserParams::UploadFolder);
|
|
|
|
|
|
|
|
if (file_paths.size() == 0) {
|
|
|
|
// Client canceled the file chooser.
|
|
|
|
OnRunFileChooserDelegateCallback(mode, selected_accept_filter, file_paths);
|
|
|
|
} else {
|
|
|
|
lister_.reset(new net::DirectoryLister(
|
|
|
|
file_paths[0],
|
2016-09-08 17:38:05 +02:00
|
|
|
net::DirectoryLister::NO_SORT_RECURSIVE,
|
2015-11-17 19:20:13 +01:00
|
|
|
new UploadFolderHelper(
|
|
|
|
base::Bind(&CefFileDialogManager::OnRunFileChooserDelegateCallback,
|
|
|
|
weak_ptr_factory_.GetWeakPtr(), mode))));
|
2017-03-03 23:37:23 +01:00
|
|
|
lister_->Start();
|
2015-11-17 19:20:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefFileDialogManager::OnRunFileChooserDelegateCallback(
|
|
|
|
content::FileChooserParams::Mode mode,
|
|
|
|
int selected_accept_filter,
|
|
|
|
const std::vector<base::FilePath>& file_paths) {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
|
|
|
|
// Convert FilePath list to SelectedFileInfo list.
|
|
|
|
std::vector<content::FileChooserFileInfo> selected_files;
|
|
|
|
for (size_t i = 0; i < file_paths.size(); ++i) {
|
|
|
|
content::FileChooserFileInfo info;
|
|
|
|
info.file_path = file_paths[i];
|
|
|
|
selected_files.push_back(info);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify our RenderViewHost in all cases.
|
2016-06-21 00:59:23 +02:00
|
|
|
if (render_frame_host_)
|
|
|
|
render_frame_host_->FilesSelectedInChooser(selected_files, mode);
|
|
|
|
|
|
|
|
Cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefFileDialogManager::Cleanup() {
|
|
|
|
if (lister_)
|
|
|
|
lister_.reset();
|
|
|
|
|
|
|
|
render_frame_host_ = nullptr;
|
|
|
|
file_chooser_pending_ = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefFileDialogManager::RenderFrameDeleted(
|
|
|
|
content::RenderFrameHost* render_frame_host) {
|
|
|
|
if (render_frame_host == render_frame_host_)
|
|
|
|
render_frame_host_ = nullptr;
|
2015-11-17 19:20:13 +01:00
|
|
|
}
|