/
platform_util_linux.cc
421 lines (347 loc) · 13.3 KB
/
platform_util_linux.cc
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
// Copyright (c) 2013 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/common/platform_util.h"
#include <fcntl.h>
#include <stdio.h>
#include <string>
#include <vector>
#include "base/cancelable_callback.h"
#include "base/containers/contains.h"
#include "base/environment.h"
#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/nix/xdg_util.h"
#include "base/no_destructor.h"
#include "base/posix/eintr_wrapper.h"
#include "base/process/kill.h"
#include "base/process/launch.h"
#include "base/strings/string_util.h"
#include "base/threading/thread_restrictions.h"
#include "components/dbus/thread_linux/dbus_thread_linux.h"
#include "content/public/browser/browser_thread.h"
#include "dbus/bus.h"
#include "dbus/message.h"
#include "dbus/object_proxy.h"
#include "shell/common/platform_util_internal.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "ui/gtk/gtk_util.h" // nogncheck
#include "url/gurl.h"
#define ELECTRON_TRASH "ELECTRON_TRASH"
namespace platform_util {
void OpenFolder(const base::FilePath& full_path);
}
namespace {
const char kMethodListActivatableNames[] = "ListActivatableNames";
const char kMethodNameHasOwner[] = "NameHasOwner";
const char kFreedesktopFileManagerName[] = "org.freedesktop.FileManager1";
const char kFreedesktopFileManagerPath[] = "/org/freedesktop/FileManager1";
const char kMethodShowItems[] = "ShowItems";
const char kFreedesktopPortalName[] = "org.freedesktop.portal.Desktop";
const char kFreedesktopPortalPath[] = "/org/freedesktop/portal/desktop";
const char kFreedesktopPortalOpenURI[] = "org.freedesktop.portal.OpenURI";
const char kMethodOpenDirectory[] = "OpenDirectory";
class ShowItemHelper {
public:
static ShowItemHelper& GetInstance() {
static base::NoDestructor<ShowItemHelper> instance;
return *instance;
}
ShowItemHelper() {}
ShowItemHelper(const ShowItemHelper&) = delete;
ShowItemHelper& operator=(const ShowItemHelper&) = delete;
void ShowItemInFolder(const base::FilePath& full_path) {
if (!bus_) {
// Sets up the D-Bus connection.
dbus::Bus::Options bus_options;
bus_options.bus_type = dbus::Bus::SESSION;
bus_options.connection_type = dbus::Bus::PRIVATE;
bus_options.dbus_task_runner = dbus_thread_linux::GetTaskRunner();
bus_ = base::MakeRefCounted<dbus::Bus>(bus_options);
}
if (!dbus_proxy_) {
dbus_proxy_ = bus_->GetObjectProxy(DBUS_SERVICE_DBUS,
dbus::ObjectPath(DBUS_PATH_DBUS));
}
if (prefer_filemanager_interface_.has_value()) {
if (prefer_filemanager_interface_.value()) {
ShowItemUsingFileManager(full_path);
} else {
ShowItemUsingFreedesktopPortal(full_path);
}
} else {
CheckFileManagerRunning(full_path);
}
}
private:
void CheckFileManagerRunning(const base::FilePath& full_path) {
dbus::MethodCall method_call(DBUS_INTERFACE_DBUS, kMethodNameHasOwner);
dbus::MessageWriter writer(&method_call);
writer.AppendString(kFreedesktopFileManagerName);
dbus_proxy_->CallMethod(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::BindOnce(&ShowItemHelper::CheckFileManagerRunningResponse,
base::Unretained(this), full_path));
}
void CheckFileManagerRunningResponse(const base::FilePath& full_path,
dbus::Response* response) {
if (prefer_filemanager_interface_.has_value()) {
ShowItemInFolder(full_path);
return;
}
bool is_running = false;
if (!response) {
LOG(ERROR) << "Failed to call " << kMethodNameHasOwner;
} else {
dbus::MessageReader reader(response);
bool owned = false;
if (!reader.PopBool(&owned)) {
LOG(ERROR) << "Failed to read " << kMethodNameHasOwner << " response";
} else if (owned) {
is_running = true;
}
}
if (is_running) {
prefer_filemanager_interface_ = true;
ShowItemInFolder(full_path);
} else {
CheckFileManagerActivatable(full_path);
}
}
void CheckFileManagerActivatable(const base::FilePath& full_path) {
dbus::MethodCall method_call(DBUS_INTERFACE_DBUS,
kMethodListActivatableNames);
dbus_proxy_->CallMethod(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::BindOnce(&ShowItemHelper::CheckFileManagerActivatableResponse,
base::Unretained(this), full_path));
}
void CheckFileManagerActivatableResponse(const base::FilePath& full_path,
dbus::Response* response) {
if (prefer_filemanager_interface_.has_value()) {
ShowItemInFolder(full_path);
return;
}
bool is_activatable = false;
if (!response) {
LOG(ERROR) << "Failed to call " << kMethodListActivatableNames;
} else {
dbus::MessageReader reader(response);
std::vector<std::string> names;
if (!reader.PopArrayOfStrings(&names)) {
LOG(ERROR) << "Failed to read " << kMethodListActivatableNames
<< " response";
} else if (base::Contains(names, kFreedesktopFileManagerName)) {
is_activatable = true;
}
}
prefer_filemanager_interface_ = is_activatable;
ShowItemInFolder(full_path);
}
void ShowItemUsingFreedesktopPortal(const base::FilePath& full_path) {
if (!object_proxy_) {
object_proxy_ = bus_->GetObjectProxy(
kFreedesktopPortalName, dbus::ObjectPath(kFreedesktopPortalPath));
}
base::ScopedFD fd(
HANDLE_EINTR(open(full_path.value().c_str(), O_RDONLY | O_CLOEXEC)));
if (!fd.is_valid()) {
LOG(ERROR) << "Failed to open " << full_path << " for URI portal";
// If the call fails, at least open the parent folder.
platform_util::OpenFolder(full_path.DirName());
return;
}
dbus::MethodCall open_directory_call(kFreedesktopPortalOpenURI,
kMethodOpenDirectory);
dbus::MessageWriter writer(&open_directory_call);
writer.AppendString("");
// Note that AppendFileDescriptor() duplicates the fd, so we shouldn't
// release ownership of it here.
writer.AppendFileDescriptor(fd.get());
dbus::MessageWriter options_writer(nullptr);
writer.OpenArray("{sv}", &options_writer);
writer.CloseContainer(&options_writer);
ShowItemUsingBusCall(&open_directory_call, full_path);
}
void ShowItemUsingFileManager(const base::FilePath& full_path) {
if (!object_proxy_) {
object_proxy_ =
bus_->GetObjectProxy(kFreedesktopFileManagerName,
dbus::ObjectPath(kFreedesktopFileManagerPath));
}
dbus::MethodCall show_items_call(kFreedesktopFileManagerName,
kMethodShowItems);
dbus::MessageWriter writer(&show_items_call);
writer.AppendArrayOfStrings(
{"file://" + full_path.value()}); // List of file(s) to highlight.
writer.AppendString({}); // startup-id
ShowItemUsingBusCall(&show_items_call, full_path);
}
void ShowItemUsingBusCall(dbus::MethodCall* call,
const base::FilePath& full_path) {
object_proxy_->CallMethod(
call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::BindOnce(&ShowItemHelper::ShowItemInFolderResponse,
base::Unretained(this), full_path, call->GetMember()));
}
void ShowItemInFolderResponse(const base::FilePath& full_path,
const std::string& method,
dbus::Response* response) {
if (response)
return;
LOG(ERROR) << "Error calling " << method;
// If the bus call fails, at least open the parent folder.
platform_util::OpenFolder(full_path.DirName());
}
scoped_refptr<dbus::Bus> bus_;
dbus::ObjectProxy* dbus_proxy_ = nullptr;
dbus::ObjectProxy* object_proxy_ = nullptr;
absl::optional<bool> prefer_filemanager_interface_;
};
// Descriptions pulled from https://linux.die.net/man/1/xdg-open
std::string GetErrorDescription(int error_code) {
switch (error_code) {
case 1:
return "Error in command line syntax";
case 2:
return "The item does not exist";
case 3:
return "A required tool could not be found";
case 4:
return "The action failed";
default:
return "";
}
}
bool XDGUtil(const std::vector<std::string>& argv,
const base::FilePath& working_directory,
const bool wait_for_exit,
platform_util::OpenCallback callback) {
base::LaunchOptions options;
options.current_directory = working_directory;
options.allow_new_privs = true;
// xdg-open can fall back on mailcap which eventually might plumb through
// to a command that needs a terminal. Set the environment variable telling
// it that we definitely don't have a terminal available and that it should
// bring up a new terminal if necessary. See "man mailcap".
options.environment["MM_NOTTTY"] = "1";
base::Process process = base::LaunchProcess(argv, options);
if (!process.IsValid())
return false;
if (wait_for_exit) {
base::ScopedAllowBaseSyncPrimitivesForTesting
allow_sync; // required by WaitForExit
int exit_code = -1;
bool success = process.WaitForExit(&exit_code);
if (!callback.is_null())
std::move(callback).Run(GetErrorDescription(exit_code));
return success ? (exit_code == 0) : false;
}
base::EnsureProcessGetsReaped(std::move(process));
return true;
}
bool XDGOpen(const base::FilePath& working_directory,
const std::string& path,
const bool wait_for_exit,
platform_util::OpenCallback callback) {
return XDGUtil({"xdg-open", path}, working_directory, wait_for_exit,
std::move(callback));
}
bool XDGEmail(const std::string& email, const bool wait_for_exit) {
return XDGUtil({"xdg-email", email}, base::FilePath(), wait_for_exit,
platform_util::OpenCallback());
}
} // namespace
namespace platform_util {
void ShowItemInFolder(const base::FilePath& full_path) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
ShowItemHelper::GetInstance().ShowItemInFolder(full_path);
}
void OpenPath(const base::FilePath& full_path, OpenCallback callback) {
// This is async, so we don't care about the return value.
XDGOpen(full_path.DirName(), full_path.value(), true, std::move(callback));
}
void OpenFolder(const base::FilePath& full_path) {
if (!base::DirectoryExists(full_path))
return;
XDGOpen(full_path.DirName(), ".", false, platform_util::OpenCallback());
}
void OpenExternal(const GURL& url,
const OpenExternalOptions& options,
OpenCallback callback) {
// Don't wait for exit, since we don't want to wait for the browser/email
// client window to close before returning
if (url.SchemeIs("mailto")) {
bool success = XDGEmail(url.spec(), false);
std::move(callback).Run(success ? "" : "Failed to open path");
} else {
bool success = XDGOpen(base::FilePath(), url.spec(), false,
platform_util::OpenCallback());
std::move(callback).Run(success ? "" : "Failed to open path");
}
}
bool MoveItemToTrash(const base::FilePath& full_path, bool delete_on_fail) {
auto env = base::Environment::Create();
// find the trash method
std::string trash;
if (!env->GetVar(ELECTRON_TRASH, &trash)) {
// Determine desktop environment and set accordingly.
const auto desktop_env(base::nix::GetDesktopEnvironment(env.get()));
if (desktop_env == base::nix::DESKTOP_ENVIRONMENT_KDE4 ||
desktop_env == base::nix::DESKTOP_ENVIRONMENT_KDE5) {
trash = "kioclient5";
} else if (desktop_env == base::nix::DESKTOP_ENVIRONMENT_KDE3) {
trash = "kioclient";
}
}
// build the invocation
std::vector<std::string> argv;
const auto& filename = full_path.value();
if (trash == "kioclient5" || trash == "kioclient") {
argv = {trash, "move", filename, "trash:/"};
} else if (trash == "trash-cli") {
argv = {"trash-put", filename};
} else if (trash == "gvfs-trash") {
argv = {"gvfs-trash", filename}; // deprecated, but still exists
} else {
argv = {"gio", "trash", filename};
}
return XDGUtil(argv, base::FilePath(), true, platform_util::OpenCallback());
}
namespace internal {
bool PlatformTrashItem(const base::FilePath& full_path, std::string* error) {
if (!MoveItemToTrash(full_path, false)) {
// TODO(nornagon): at least include the exit code?
*error = "Failed to move item to trash";
return false;
}
return true;
}
} // namespace internal
void Beep() {
// echo '\a' > /dev/console
FILE* fp = fopen("/dev/console", "a");
if (fp == nullptr) {
fp = fopen("/dev/tty", "a");
}
if (fp != nullptr) {
fprintf(fp, "\a");
fclose(fp);
}
}
bool GetDesktopName(std::string* setme) {
return base::Environment::Create()->GetVar("CHROME_DESKTOP", setme);
}
std::string GetXdgAppId() {
std::string desktop_file_name;
if (GetDesktopName(&desktop_file_name)) {
const std::string kDesktopExtension{".desktop"};
if (base::EndsWith(desktop_file_name, kDesktopExtension,
base::CompareCase::INSENSITIVE_ASCII)) {
desktop_file_name.resize(desktop_file_name.size() -
kDesktopExtension.size());
}
}
return desktop_file_name;
}
} // namespace platform_util