Commit 5caadda2 authored by thestig's avatar thestig Committed by Commit bot

Modernize SelectFileDialogImplKDE.

Use base::Bind and content::BrowserThread::PostTaskAndReplyWithResults()
instead of custom function pointer based callbacks.

Review-Url: https://codereview.chromium.org/2283823003
Cr-Commit-Position: refs/heads/master@{#414792}
parent 09a373d9
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include <stddef.h> #include <stddef.h>
#include <X11/Xlib.h> #include <X11/Xlib.h>
#include <memory>
#include <set> #include <set>
#include "base/bind.h" #include "base/bind.h"
...@@ -74,18 +75,18 @@ class SelectFileDialogImplKDE : public SelectFileDialogImpl { ...@@ -74,18 +75,18 @@ class SelectFileDialogImplKDE : public SelectFileDialogImpl {
bool HasMultipleFileTypeChoicesImpl() override; bool HasMultipleFileTypeChoicesImpl() override;
struct KDialogParams { struct KDialogParams {
KDialogParams(const std::string& type, const std::string& title, KDialogParams(const std::string& type,
const base::FilePath& default_path, XID parent, const std::string& title,
bool file_operation, bool multiple_selection, const base::FilePath& default_path,
void* kdialog_params, XID parent,
void (SelectFileDialogImplKDE::*callback)(XID, bool file_operation,
const std::string&, bool multiple_selection)
int, void*)) : type(type),
: type(type), title(title), default_path(default_path), parent(parent), title(title),
default_path(default_path),
parent(parent),
file_operation(file_operation), file_operation(file_operation),
multiple_selection(multiple_selection), multiple_selection(multiple_selection) {}
kdialog_params(kdialog_params), callback(callback) {
}
std::string type; std::string type;
std::string title; std::string title;
...@@ -93,9 +94,11 @@ class SelectFileDialogImplKDE : public SelectFileDialogImpl { ...@@ -93,9 +94,11 @@ class SelectFileDialogImplKDE : public SelectFileDialogImpl {
XID parent; XID parent;
bool file_operation; bool file_operation;
bool multiple_selection; bool multiple_selection;
void* kdialog_params; };
void (SelectFileDialogImplKDE::*callback)(XID, const std::string&,
int, void*); struct KDialogOutputParams {
std::string output;
int exit_code;
}; };
// Get the filters from |file_types_| and concatenate them into // Get the filters from |file_types_| and concatenate them into
...@@ -111,8 +114,9 @@ class SelectFileDialogImplKDE : public SelectFileDialogImpl { ...@@ -111,8 +114,9 @@ class SelectFileDialogImplKDE : public SelectFileDialogImpl {
bool multiple_selection, bool multiple_selection,
base::CommandLine* command_line); base::CommandLine* command_line);
// Call KDialog on the FILE thread and post results back to the UI thread. // Call KDialog on the FILE thread and return the results.
void CallKDialogOutput(const KDialogParams& params); std::unique_ptr<KDialogOutputParams> CallKDialogOutput(
const KDialogParams& params);
// Notifies the listener that a single file was chosen. // Notifies the listener that a single file was chosen.
void FileSelected(const base::FilePath& path, void* params); void FileSelected(const base::FilePath& path, void* params);
...@@ -145,18 +149,22 @@ class SelectFileDialogImplKDE : public SelectFileDialogImpl { ...@@ -145,18 +149,22 @@ class SelectFileDialogImplKDE : public SelectFileDialogImpl {
// Common function for OnSelectSingleFileDialogResponse and // Common function for OnSelectSingleFileDialogResponse and
// OnSelectSingleFolderDialogResponse. // OnSelectSingleFolderDialogResponse.
void SelectSingleFileHelper(const std::string& output, int exit_code, void SelectSingleFileHelper(void* params,
void* params, bool allow_folder); bool allow_folder,
std::unique_ptr<KDialogOutputParams> results);
void OnSelectSingleFileDialogResponse(XID parent,
const std::string& output, void OnSelectSingleFileDialogResponse(
int exit_code, void* params); XID parent,
void OnSelectMultiFileDialogResponse(XID parent, void* params,
const std::string& output, std::unique_ptr<KDialogOutputParams> results);
int exit_code, void* params); void OnSelectMultiFileDialogResponse(
void OnSelectSingleFolderDialogResponse(XID parent, XID parent,
const std::string& output, void* params,
int exit_code, void* params); std::unique_ptr<KDialogOutputParams> results);
void OnSelectSingleFolderDialogResponse(
XID parent,
void* params,
std::unique_ptr<KDialogOutputParams> results);
// Should be either DESKTOP_ENVIRONMENT_KDE3, KDE4, or KDE5. // Should be either DESKTOP_ENVIRONMENT_KDE3, KDE4, or KDE5.
base::nix::DesktopEnvironment desktop_; base::nix::DesktopEnvironment desktop_;
...@@ -296,7 +304,8 @@ std::string SelectFileDialogImplKDE::GetMimeTypeFilterString() { ...@@ -296,7 +304,8 @@ std::string SelectFileDialogImplKDE::GetMimeTypeFilterString() {
return filter_string; return filter_string;
} }
void SelectFileDialogImplKDE::CallKDialogOutput(const KDialogParams& params) { std::unique_ptr<SelectFileDialogImplKDE::KDialogOutputParams>
SelectFileDialogImplKDE::CallKDialogOutput(const KDialogParams& params) {
DCHECK_CURRENTLY_ON(BrowserThread::FILE); DCHECK_CURRENTLY_ON(BrowserThread::FILE);
base::CommandLine::StringVector cmd_vector; base::CommandLine::StringVector cmd_vector;
cmd_vector.push_back(kKdialogBinary); cmd_vector.push_back(kKdialogBinary);
...@@ -304,19 +313,14 @@ void SelectFileDialogImplKDE::CallKDialogOutput(const KDialogParams& params) { ...@@ -304,19 +313,14 @@ void SelectFileDialogImplKDE::CallKDialogOutput(const KDialogParams& params) {
GetKDialogCommandLine(params.type, params.title, params.default_path, GetKDialogCommandLine(params.type, params.title, params.default_path,
params.parent, params.file_operation, params.parent, params.file_operation,
params.multiple_selection, &command_line); params.multiple_selection, &command_line);
std::string output;
int exit_code; auto results = base::MakeUnique<KDialogOutputParams>();
// Get output from KDialog // Get output from KDialog
base::GetAppOutputWithExitCode(command_line, &output, &exit_code); base::GetAppOutputWithExitCode(command_line, &results->output,
if (!output.empty()) &results->exit_code);
output.erase(output.size() - 1); if (!results->output.empty())
results->output.erase(results->output.size() - 1);
// Now the dialog is no longer showing, but we can't erase its parent from the return results;
// parent set yet because we're on the FILE thread.
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::Bind(params.callback, this, params.parent, output, exit_code,
params.kdialog_params));
} }
void SelectFileDialogImplKDE::GetKDialogCommandLine( void SelectFileDialogImplKDE::GetKDialogCommandLine(
...@@ -392,76 +396,74 @@ void SelectFileDialogImplKDE::CreateSelectFolderDialog( ...@@ -392,76 +396,74 @@ void SelectFileDialogImplKDE::CreateSelectFolderDialog(
int title_message_id = (type == SELECT_UPLOAD_FOLDER) int title_message_id = (type == SELECT_UPLOAD_FOLDER)
? IDS_SELECT_UPLOAD_FOLDER_DIALOG_TITLE ? IDS_SELECT_UPLOAD_FOLDER_DIALOG_TITLE
: IDS_SELECT_FOLDER_DIALOG_TITLE; : IDS_SELECT_FOLDER_DIALOG_TITLE;
BrowserThread::PostTask( BrowserThread::PostTaskAndReplyWithResult(
BrowserThread::FILE, FROM_HERE, BrowserThread::FILE, FROM_HERE,
base::Bind( base::Bind(
&SelectFileDialogImplKDE::CallKDialogOutput, &SelectFileDialogImplKDE::CallKDialogOutput, this,
this,
KDialogParams( KDialogParams(
"--getexistingdirectory", "--getexistingdirectory", GetTitle(title, title_message_id),
GetTitle(title, title_message_id), default_path.empty() ? *last_opened_path_ : default_path, parent,
default_path.empty() ? *last_opened_path_ : default_path, false, false)),
parent, false, false, params, base::Bind(&SelectFileDialogImplKDE::OnSelectSingleFolderDialogResponse,
&SelectFileDialogImplKDE::OnSelectSingleFolderDialogResponse))); this, parent, params));
} }
void SelectFileDialogImplKDE::CreateFileOpenDialog( void SelectFileDialogImplKDE::CreateFileOpenDialog(
const std::string& title, const base::FilePath& default_path, const std::string& title, const base::FilePath& default_path,
XID parent, void* params) { XID parent, void* params) {
BrowserThread::PostTask( BrowserThread::PostTaskAndReplyWithResult(
BrowserThread::FILE, FROM_HERE, BrowserThread::FILE, FROM_HERE,
base::Bind( base::Bind(
&SelectFileDialogImplKDE::CallKDialogOutput, &SelectFileDialogImplKDE::CallKDialogOutput, this,
this,
KDialogParams( KDialogParams(
"--getopenfilename", "--getopenfilename", GetTitle(title, IDS_OPEN_FILE_DIALOG_TITLE),
GetTitle(title, IDS_OPEN_FILE_DIALOG_TITLE), default_path.empty() ? *last_opened_path_ : default_path, parent,
default_path.empty() ? *last_opened_path_ : default_path, true, false)),
parent, true, false, params, base::Bind(&SelectFileDialogImplKDE::OnSelectSingleFileDialogResponse,
&SelectFileDialogImplKDE::OnSelectSingleFileDialogResponse))); this, parent, params));
} }
void SelectFileDialogImplKDE::CreateMultiFileOpenDialog( void SelectFileDialogImplKDE::CreateMultiFileOpenDialog(
const std::string& title, const base::FilePath& default_path, const std::string& title, const base::FilePath& default_path,
XID parent, void* params) { XID parent, void* params) {
BrowserThread::PostTask( BrowserThread::PostTaskAndReplyWithResult(
BrowserThread::FILE, FROM_HERE, BrowserThread::FILE, FROM_HERE,
base::Bind( base::Bind(
&SelectFileDialogImplKDE::CallKDialogOutput, &SelectFileDialogImplKDE::CallKDialogOutput, this,
this,
KDialogParams( KDialogParams(
"--getopenfilename", "--getopenfilename", GetTitle(title, IDS_OPEN_FILES_DIALOG_TITLE),
GetTitle(title, IDS_OPEN_FILES_DIALOG_TITLE), default_path.empty() ? *last_opened_path_ : default_path, parent,
default_path.empty() ? *last_opened_path_ : default_path, true, true)),
parent, true, true, params, base::Bind(&SelectFileDialogImplKDE::OnSelectMultiFileDialogResponse,
&SelectFileDialogImplKDE::OnSelectMultiFileDialogResponse))); this, parent, params));
} }
void SelectFileDialogImplKDE::CreateSaveAsDialog( void SelectFileDialogImplKDE::CreateSaveAsDialog(
const std::string& title, const base::FilePath& default_path, const std::string& title, const base::FilePath& default_path,
XID parent, void* params) { XID parent, void* params) {
BrowserThread::PostTask( BrowserThread::PostTaskAndReplyWithResult(
BrowserThread::FILE, FROM_HERE, BrowserThread::FILE, FROM_HERE,
base::Bind( base::Bind(
&SelectFileDialogImplKDE::CallKDialogOutput, &SelectFileDialogImplKDE::CallKDialogOutput, this,
this, KDialogParams("--getsavefilename",
KDialogParams(
"--getsavefilename",
GetTitle(title, IDS_SAVE_AS_DIALOG_TITLE), GetTitle(title, IDS_SAVE_AS_DIALOG_TITLE),
default_path.empty() ? *last_saved_path_ : default_path, default_path.empty() ? *last_saved_path_ : default_path,
parent, true, false, params, parent, true, false)),
&SelectFileDialogImplKDE::OnSelectSingleFileDialogResponse))); base::Bind(&SelectFileDialogImplKDE::OnSelectSingleFileDialogResponse,
this, parent, params));
} }
void SelectFileDialogImplKDE::SelectSingleFileHelper(const std::string& output, void SelectFileDialogImplKDE::SelectSingleFileHelper(
int exit_code, void* params, bool allow_folder) { void* params,
VLOG(1) << "[kdialog] SingleFileResponse: " << output; bool allow_folder,
if (exit_code != 0 || output.empty()) { std::unique_ptr<KDialogOutputParams> results) {
VLOG(1) << "[kdialog] SingleFileResponse: " << results->output;
if (results->exit_code || results->output.empty()) {
FileNotSelected(params); FileNotSelected(params);
return; return;
} }
base::FilePath path(output); base::FilePath path(results->output);
if (allow_folder) { if (allow_folder) {
FileSelected(path, params); FileSelected(path, params);
return; return;
...@@ -474,34 +476,40 @@ void SelectFileDialogImplKDE::SelectSingleFileHelper(const std::string& output, ...@@ -474,34 +476,40 @@ void SelectFileDialogImplKDE::SelectSingleFileHelper(const std::string& output,
} }
void SelectFileDialogImplKDE::OnSelectSingleFileDialogResponse( void SelectFileDialogImplKDE::OnSelectSingleFileDialogResponse(
XID parent, const std::string& output, int exit_code, void* params) { XID parent,
void* params,
std::unique_ptr<KDialogOutputParams> results) {
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
parents_.erase(parent); parents_.erase(parent);
SelectSingleFileHelper(output, exit_code, params, false); SelectSingleFileHelper(params, false, std::move(results));
} }
void SelectFileDialogImplKDE::OnSelectSingleFolderDialogResponse( void SelectFileDialogImplKDE::OnSelectSingleFolderDialogResponse(
XID parent, const std::string& output, int exit_code, void* params) { XID parent,
void* params,
std::unique_ptr<KDialogOutputParams> results) {
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
parents_.erase(parent); parents_.erase(parent);
SelectSingleFileHelper(output, exit_code, params, true); SelectSingleFileHelper(params, true, std::move(results));
} }
void SelectFileDialogImplKDE::OnSelectMultiFileDialogResponse( void SelectFileDialogImplKDE::OnSelectMultiFileDialogResponse(
XID parent, const std::string& output, int exit_code, void* params) { XID parent,
void* params,
std::unique_ptr<KDialogOutputParams> results) {
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
VLOG(1) << "[kdialog] MultiFileResponse: " << output; VLOG(1) << "[kdialog] MultiFileResponse: " << results->output;
parents_.erase(parent); parents_.erase(parent);
if (exit_code != 0 || output.empty()) { if (results->exit_code || results->output.empty()) {
FileNotSelected(params); FileNotSelected(params);
return; return;
} }
std::vector<base::FilePath> filenames_fp; std::vector<base::FilePath> filenames_fp;
for (const base::StringPiece& line : for (const base::StringPiece& line :
base::SplitStringPiece(output, "\n", base::KEEP_WHITESPACE, base::SplitStringPiece(results->output, "\n", base::KEEP_WHITESPACE,
base::SPLIT_WANT_NONEMPTY)) { base::SPLIT_WANT_NONEMPTY)) {
base::FilePath path(line); base::FilePath path(line);
if (CallDirectoryExistsOnUIThread(path)) if (CallDirectoryExistsOnUIThread(path))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment