1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/win/chrome_select_file_dialog_factory.h"
#include <utility>
#include <vector>
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/strings/utf_string_conversions.h"
#include "base/win/win_util.h"
#include "chrome/browser/win/util_win_service.h"
#include "chrome/services/util_win/public/mojom/util_win.mojom.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "mojo/public/cpp/bindings/sync_call_restrictions.h"
#include "ui/shell_dialogs/execute_select_file_win.h"
#include "ui/shell_dialogs/select_file_dialog_win.h"
#include "ui/shell_dialogs/select_file_policy.h"
// Helper class to execute a select file operation on a utility process. It
// hides the complexity of managing the lifetime of the connection to the
// UtilWin service.
class UtilWinHelper {
public:
UtilWinHelper(const UtilWinHelper&) = delete;
UtilWinHelper& operator=(const UtilWinHelper&) = delete;
// Executes the select file operation and returns the result via
// |on_select_file_executed_callback|.
static void ExecuteSelectFile(
ui::SelectFileDialog::Type type,
const std::u16string& title,
const base::FilePath& default_path,
const std::vector<ui::FileFilterSpec>& filter,
int file_type_index,
const std::wstring& default_extension,
HWND owner,
ui::OnSelectFileExecutedCallback on_select_file_executed_callback);
private:
UtilWinHelper(
ui::SelectFileDialog::Type type,
const std::u16string& title,
const base::FilePath& default_path,
const std::vector<ui::FileFilterSpec>& filter,
int file_type_index,
const std::wstring& default_extension,
HWND owner,
ui::OnSelectFileExecutedCallback on_select_file_executed_callback);
// Connection error handler for the interface pipe.
void OnConnectionError();
// Forwards the result of the file operation to the
// |on_select_file_executed_callback_|.
void OnSelectFileExecuted(const std::vector<base::FilePath>& paths,
int index);
// The pointer to the UtilWin interface. This must be kept alive while waiting
// for the response.
mojo::Remote<chrome::mojom::UtilWin> remote_util_win_;
// The callback that is invoked when the file operation is finished.
ui::OnSelectFileExecutedCallback on_select_file_executed_callback_;
SEQUENCE_CHECKER(sequence_checker_);
};
// static
void UtilWinHelper::ExecuteSelectFile(
ui::SelectFileDialog::Type type,
const std::u16string& title,
const base::FilePath& default_path,
const std::vector<ui::FileFilterSpec>& filter,
int file_type_index,
const std::wstring& default_extension,
HWND owner,
ui::OnSelectFileExecutedCallback on_select_file_executed_callback) {
// Self-deleting when the select file operation completes.
new UtilWinHelper(type, title, default_path, filter, file_type_index,
default_extension, owner,
std::move(on_select_file_executed_callback));
}
UtilWinHelper::UtilWinHelper(
ui::SelectFileDialog::Type type,
const std::u16string& title,
const base::FilePath& default_path,
const std::vector<ui::FileFilterSpec>& filter,
int file_type_index,
const std::wstring& default_extension,
HWND owner,
ui::OnSelectFileExecutedCallback on_select_file_executed_callback)
: on_select_file_executed_callback_(
std::move(on_select_file_executed_callback)) {
remote_util_win_ = LaunchUtilWinServiceInstance();
// |remote_util_win_| owns the callbacks and is guaranteed to be destroyed
// before |this|, therefore making base::Unretained() safe to use.
remote_util_win_.set_disconnect_handler(base::BindOnce(
&UtilWinHelper::OnConnectionError, base::Unretained(this)));
remote_util_win_->CallExecuteSelectFile(
type, base::win::HandleToUint32(owner), title, default_path, filter,
file_type_index, base::WideToUTF16(default_extension),
base::BindOnce(&UtilWinHelper::OnSelectFileExecuted,
base::Unretained(this)));
}
void UtilWinHelper::OnConnectionError() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
std::move(on_select_file_executed_callback_).Run({}, 0);
delete this;
}
void UtilWinHelper::OnSelectFileExecuted(
const std::vector<base::FilePath>& paths,
int index) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
std::move(on_select_file_executed_callback_).Run(paths, index);
delete this;
}
void ExecuteSelectFileImpl(
ui::SelectFileDialog::Type type,
const std::u16string& title,
const base::FilePath& default_path,
const std::vector<ui::FileFilterSpec>& filter,
int file_type_index,
const std::wstring& default_extension,
HWND owner,
ui::OnSelectFileExecutedCallback on_select_file_executed_callback) {
UtilWinHelper::ExecuteSelectFile(type, title, default_path, filter,
file_type_index, default_extension, owner,
std::move(on_select_file_executed_callback));
}
ChromeSelectFileDialogFactory::ChromeSelectFileDialogFactory() = default;
ChromeSelectFileDialogFactory::~ChromeSelectFileDialogFactory() = default;
ui::SelectFileDialog* ChromeSelectFileDialogFactory::Create(
ui::SelectFileDialog::Listener* listener,
std::unique_ptr<ui::SelectFilePolicy> policy) {
return ui::CreateWinSelectFileDialog(
listener, std::move(policy), base::BindRepeating(&ExecuteSelectFileImpl));
}
|