forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselect_file_dialog_linux_portal.h
215 lines (176 loc) · 8.57 KB
/
select_file_dialog_linux_portal.h
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef UI_SHELL_DIALOGS_SELECT_FILE_DIALOG_LINUX_PORTAL_H_
#define UI_SHELL_DIALOGS_SELECT_FILE_DIALOG_LINUX_PORTAL_H_
#include "base/functional/callback_forward.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_refptr.h"
#include "base/synchronization/atomic_flag.h"
#include "base/task/sequenced_task_runner.h"
#include "dbus/bus.h"
#include "dbus/message.h"
#include "dbus/object_proxy.h"
#include "ui/shell_dialogs/select_file_dialog_linux.h"
namespace ui {
using OnSelectFileExecutedCallback =
base::OnceCallback<void(std::vector<base::FilePath> paths,
std::string current_filter)>;
using OnSelectFileCanceledCallback = base::OnceCallback<void()>;
// Implementation of SelectFileDialog that has the XDG file chooser portal show
// a platform-dependent file selection dialog. This acts as a modal dialog.
class SelectFileDialogLinuxPortal : public SelectFileDialogLinux {
public:
SelectFileDialogLinuxPortal(Listener* listener,
std::unique_ptr<ui::SelectFilePolicy> policy);
SelectFileDialogLinuxPortal(const SelectFileDialogLinuxPortal& other) =
delete;
SelectFileDialogLinuxPortal& operator=(
const SelectFileDialogLinuxPortal& other) = delete;
// Starts running a test to check for the presence of the file chooser portal
// on the D-Bus task runner. This should only be called once, preferably
// around program start.
static void StartAvailabilityTestInBackground();
// Checks if the file chooser portal is available. Blocks if the availability
// test from above has not yet completed (which should generally not happen).
static bool IsPortalAvailable();
// Destroys the connection to the bus.
static void DestroyPortalConnection();
protected:
~SelectFileDialogLinuxPortal() override;
// BaseShellDialog implementation:
bool IsRunning(gfx::NativeWindow parent_window) const override;
// SelectFileDialog implementation.
// |params| is user data we pass back via the Listener interface.
void SelectFileImpl(Type type,
const std::u16string& title,
const base::FilePath& default_path,
const FileTypeInfo* file_types,
int file_type_index,
const base::FilePath::StringType& default_extension,
gfx::NativeWindow owning_window,
void* params,
const GURL* caller) override;
bool HasMultipleFileTypeChoicesImpl() override;
private:
// A named set of patterns used as a dialog filter.
struct PortalFilter {
PortalFilter();
PortalFilter(const PortalFilter& other);
PortalFilter(PortalFilter&& other);
~PortalFilter();
PortalFilter& operator=(const PortalFilter& other) = default;
PortalFilter& operator=(PortalFilter&& other) = default;
std::string name;
std::vector<std::string> patterns;
};
// A set of PortalFilters, potentially with a default.
struct PortalFilterSet {
PortalFilterSet();
PortalFilterSet(const PortalFilterSet& other);
PortalFilterSet(PortalFilterSet&& other);
~PortalFilterSet();
PortalFilterSet& operator=(const PortalFilterSet& other) = default;
PortalFilterSet& operator=(PortalFilterSet&& other) = default;
std::vector<PortalFilter> filters;
absl::optional<PortalFilter> default_filter;
};
// A wrapper over some shared contextual information that needs to be passed
// around between SelectFileDialogLinuxPortal and the Portal via D-Bus.
// This is ref-counted due to sharing between the 2 sequences: dbus sequence
// and main sequence.
// Usage: SelectFileDialogLinuxPortal instantiates DialogInfo with the 2
// callbacks, sets the public members and then call SelectFileImplOnDbus().
// DialogInfo notifies the end result via one of the callbacks.
class DialogInfo : public base::RefCountedThreadSafe<DialogInfo> {
public:
DialogInfo(OnSelectFileExecutedCallback selected_callback,
OnSelectFileCanceledCallback canceled_callback);
// Sets up listeners for the response handle's signals.
void SelectFileImplOnBusThread(std::u16string title,
base::FilePath default_path,
const bool default_path_exists,
PortalFilterSet filter_set,
base::FilePath::StringType default_extension,
std::string parent_handle);
Type type;
// The task runner the SelectFileImpl method was called on.
scoped_refptr<base::SequencedTaskRunner> main_task_runner;
private:
friend class base::RefCountedThreadSafe<DialogInfo>;
~DialogInfo();
// Should run on D-Bus thread.
void ConnectToHandle();
void OnCallResponse(dbus::Bus* bus,
dbus::Response* response,
dbus::ErrorResponse* error_response);
void OnResponseSignalEmitted(dbus::Signal* signal);
bool CheckResponseCode(dbus::MessageReader* reader);
bool ReadResponseResults(dbus::MessageReader* reader,
std::vector<std::string>* uris,
std::string* current_filter);
void OnResponseSignalConnected(const std::string& interface,
const std::string& signal,
bool connected);
void AppendFiltersOption(dbus::MessageWriter* writer,
const std::vector<PortalFilter>& filters);
void AppendOptions(dbus::MessageWriter* writer,
const std::string& response_handle_token,
const base::FilePath& default_path,
const bool derfault_path_exists,
const PortalFilterSet& filter_set);
void AppendFilterStruct(dbus::MessageWriter* writer,
const PortalFilter& filter);
std::vector<base::FilePath> ConvertUrisToPaths(
const std::vector<std::string>& uris);
// Completes an open call, notifying the listener with the given paths, and
// marks the dialog as closed.
void CompleteOpen(std::vector<base::FilePath> paths,
std::string current_filter);
// Completes an open call, notifying the listener with a cancellation, and
// marks the dialog as closed.
void CancelOpen();
// These callbacks should run on main thread.
// It will point to SelectFileDialogPortal::CompleteOpenOnMainThread.
OnSelectFileExecutedCallback selected_callback_;
// It will point to SelectFileDialogPortal::CancelOpenOnMainThread.
OnSelectFileCanceledCallback canceled_callback_;
// The response object handle that the portal will send a signal to upon the
// dialog's completion.
raw_ptr<dbus::ObjectProxy, DanglingUntriaged> response_handle_ = nullptr;
};
// D-Bus configuration and initialization.
static void CheckPortalAvailabilityOnBusThread();
static bool IsPortalRunningOnBusThread(dbus::ObjectProxy* dbus_proxy);
static bool IsPortalActivatableOnBusThread(dbus::ObjectProxy* dbus_proxy);
// Returns a flag, written by the D-Bus thread and read by the UI thread,
// indicating whether or not the availability test has completed.
static base::AtomicFlag* GetAvailabilityTestCompletionFlag();
PortalFilterSet BuildFilterSet();
void SelectFileImplWithParentHandle(
std::u16string title,
base::FilePath default_path,
PortalFilterSet filter_set,
base::FilePath::StringType default_extension,
std::string parent_handle);
void CompleteOpenOnMainThread(std::vector<base::FilePath> paths,
std::string current_filter);
void CancelOpenOnMainThread();
// Removes the DialogInfo parent. Must be called on the UI task runner.
void UnparentOnMainThread();
// This should be used in the main thread.
absl::optional<gfx::AcceleratedWidget> parent_;
// The untyped params to pass to the listener, it should be used in the main
// thread.
raw_ptr<void> listener_params_ = nullptr;
// Data shared across main thread and D-Bus thread.
scoped_refptr<DialogInfo> info_;
// Written by the D-Bus thread and read by the UI thread.
static bool is_portal_available_;
// Used by the D-Bus thread to generate unique handle tokens.
static int handle_token_counter_;
std::vector<PortalFilter> filters_;
};
} // namespace ui
#endif // UI_SHELL_DIALOGS_SELECT_FILE_DIALOG_LINUX_PORTAL_H_