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
|
// Copyright 2012 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/platform_util.h"
#include <fcntl.h>
#include <memory>
#include <optional>
#include <queue>
#include <string>
#include <vector>
#include "base/callback_list.h"
#include "base/check.h"
#include "base/check_op.h"
#include "base/containers/contains.h"
#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/logging.h"
#include "base/memory/raw_ptr.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/strings/utf_string_conversions.h"
#include "base/task/thread_pool.h"
#include "base/types/expected.h"
#include "chrome/browser/platform_util_internal.h"
#include "components/dbus/properties/types.h"
#include "components/dbus/thread_linux/dbus_thread_linux.h"
#include "components/dbus/utils/check_for_service_and_start.h"
#include "components/dbus/xdg/request.h"
#include "content/public/browser/browser_thread.h"
#include "dbus/bus.h"
#include "dbus/message.h"
#include "dbus/object_proxy.h"
#include "url/gurl.h"
using content::BrowserThread;
namespace platform_util {
namespace {
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";
const char kActivationTokenKey[] = "activation_token";
class ShowItemHelper {
public:
static ShowItemHelper& GetInstance() {
static base::NoDestructor<ShowItemHelper> instance;
return *instance;
}
ShowItemHelper() = default;
ShowItemHelper(const ShowItemHelper&) = delete;
ShowItemHelper& operator=(const ShowItemHelper&) = delete;
void ShowItemInFolder(const base::FilePath& full_path) {
// Skip opening the folder during browser tests, to avoid leaving an open
// file explorer window behind.
if (!internal::AreShellOperationsAllowed()) {
return;
}
if (!bus_) {
bus_ = dbus_thread_linux::GetSharedSessionBus();
}
if (api_type_.has_value()) {
ShowItemInFolderOnApiTypeSet(full_path);
return;
}
bool api_availability_check_in_progress = !pending_requests_.empty();
pending_requests_.push(full_path);
if (!api_availability_check_in_progress) {
// Initiate check to determine if portal or the FileManager API should
// be used. The portal API is always preferred if available.
dbus_utils::CheckForServiceAndStart(
bus_.get(), kFreedesktopPortalName,
base::BindOnce(&ShowItemHelper::CheckPortalRunningResponse,
// Unretained is safe, the ShowItemHelper instance is
// never destroyed.
base::Unretained(this)));
}
}
private:
enum class ApiType { kNone, kPortal, kFileManager };
void ShowItemInFolderOnApiTypeSet(const base::FilePath& full_path) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
CHECK(api_type_.has_value());
switch (*api_type_) {
case ApiType::kPortal:
ShowItemUsingPortal(full_path);
break;
case ApiType::kFileManager:
ShowItemUsingFileManager(full_path);
break;
case ApiType::kNone:
OpenParentFolderFallback(full_path);
break;
}
}
void ProcessPendingRequests() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (!bus_) {
return;
}
CHECK(!pending_requests_.empty());
while (!pending_requests_.empty()) {
ShowItemInFolderOnApiTypeSet(pending_requests_.front());
pending_requests_.pop();
}
}
void CheckPortalRunningResponse(std::optional<bool> is_running) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (is_running.value_or(false)) {
api_type_ = ApiType::kPortal;
ProcessPendingRequests();
} else {
// Portal is unavailable.
// Check if FileManager is available.
dbus_utils::CheckForServiceAndStart(
bus_.get(), kFreedesktopFileManagerName,
base::BindOnce(&ShowItemHelper::CheckFileManagerRunningResponse,
// Unretained is safe, the ShowItemHelper instance is
// never destroyed.
base::Unretained(this)));
}
}
void CheckFileManagerRunningResponse(std::optional<bool> is_running) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (is_running.value_or(false)) {
api_type_ = ApiType::kFileManager;
} else {
// Neither portal nor FileManager is available.
api_type_ = ApiType::kNone;
}
ProcessPendingRequests();
}
void ShowItemUsingPortal(const base::FilePath& full_path) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
CHECK(api_type_.has_value());
CHECK_EQ(*api_type_, ApiType::kPortal);
base::ThreadPool::PostTaskAndReplyWithResult(
FROM_HERE, {base::MayBlock()},
base::BindOnce(
[](const base::FilePath& full_path) {
base::ScopedFD fd(HANDLE_EINTR(
open(full_path.value().c_str(), O_RDONLY | O_CLOEXEC)));
return fd;
},
full_path),
base::BindOnce(&ShowItemHelper::ShowItemUsingPortalFdOpened,
// Unretained is safe, the ShowItemHelper instance is
// never destroyed.
base::Unretained(this), full_path));
}
void ShowItemUsingPortalFdOpened(const base::FilePath& full_path,
base::ScopedFD fd) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (!bus_) {
return;
}
if (!fd.is_valid()) {
// At least open the parent folder, as long as we're not in the unit
// tests.
OpenParentFolderFallback(full_path);
return;
}
base::nix::CreateXdgActivationToken(base::BindOnce(
&ShowItemHelper::ShowItemUsingPortalWithToken,
// Unretained is safe, the ShowItemHelper instance is never destroyed.
base::Unretained(this), full_path, std::move(fd)));
}
void ShowItemUsingPortalWithToken(const base::FilePath& full_path,
base::ScopedFD fd,
std::string activation_token) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (!bus_) {
return;
}
if (!portal_object_proxy_) {
portal_object_proxy_ = bus_->GetObjectProxy(
kFreedesktopPortalName, dbus::ObjectPath(kFreedesktopPortalPath));
}
DbusDictionary options;
options.PutAs(kActivationTokenKey, DbusString(activation_token));
// In the rare occasion that another request comes in before the response is
// received, we will end up overwriting this request object with the new one
// and the response from the first request will not be handled in that case.
// This should be acceptable as it means the two requests were received too
// close to each other from the user and the first one was handled on a best
// effort basis.
portal_open_directory_request_ = std::make_unique<dbus_xdg::Request>(
bus_, portal_object_proxy_, kFreedesktopPortalOpenURI,
kMethodOpenDirectory,
MakeDbusParameters(DbusString(""), DbusUnixFd(std::move(fd))),
std::move(options),
base::BindOnce(&ShowItemHelper::ShowItemUsingPortalResponse,
// Unretained is safe, the ShowItemHelper instance is
// never destroyed.
base::Unretained(this), full_path));
}
void ShowItemUsingPortalResponse(
const base::FilePath& full_path,
base::expected<DbusDictionary, dbus_xdg::ResponseError> results) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
portal_open_directory_request_.reset();
if (!results.has_value()) {
OpenParentFolderFallback(full_path);
}
}
void ShowItemUsingFileManager(const base::FilePath& full_path) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (!bus_) {
return;
}
CHECK(api_type_.has_value());
CHECK_EQ(*api_type_, ApiType::kFileManager);
if (!file_manager_object_proxy_) {
file_manager_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
file_manager_object_proxy_->CallMethod(
&show_items_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::BindOnce(&ShowItemHelper::ShowItemUsingFileManagerResponse,
// Unretained is safe, the ShowItemHelper instance is
// never destroyed.
base::Unretained(this), full_path));
}
void ShowItemUsingFileManagerResponse(const base::FilePath& full_path,
dbus::Response* response) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (!response) {
// If the bus call fails, at least open the parent folder.
OpenParentFolderFallback(full_path);
}
}
void OpenParentFolderFallback(const base::FilePath& full_path) {
OpenItem(
// profile is not used in linux
/*profile=*/nullptr, full_path.DirName(), OPEN_FOLDER,
OpenOperationCallback());
}
scoped_refptr<dbus::Bus> bus_;
std::optional<ApiType> api_type_;
// The proxy objects are owned by `bus_`.
raw_ptr<dbus::ObjectProxy> portal_object_proxy_ = nullptr;
raw_ptr<dbus::ObjectProxy> file_manager_object_proxy_ = nullptr;
std::unique_ptr<dbus_xdg::Request> portal_open_directory_request_;
// Requests that are queued until the API availability is determined.
std::queue<base::FilePath> pending_requests_;
};
void OnLaunchOptionsCreated(const std::string& command,
const base::FilePath& working_directory,
const std::string& arg,
base::LaunchOptions options) {
std::vector<std::string> argv;
argv.push_back(command);
argv.push_back(arg);
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";
// In Google Chrome, we do not let GNOME's bug-buddy intercept our crashes.
// However, we do not want this environment variable to propagate to external
// applications. See http://crbug.com/24120
char* disable_gnome_bug_buddy = getenv("GNOME_DISABLE_CRASH_DIALOG");
if (disable_gnome_bug_buddy &&
disable_gnome_bug_buddy == std::string("SET_BY_GOOGLE_CHROME")) {
options.environment["GNOME_DISABLE_CRASH_DIALOG"] = std::string();
}
base::Process process = base::LaunchProcess(argv, options);
if (process.IsValid()) {
base::EnsureProcessGetsReaped(std::move(process));
}
}
void RunCommand(const std::string& command,
const base::FilePath& working_directory,
const std::string& arg) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
base::nix::CreateLaunchOptionsWithXdgActivation(
base::BindOnce(&OnLaunchOptionsCreated, command, working_directory, arg));
}
void XDGOpen(const base::FilePath& working_directory, const std::string& path) {
RunCommand("xdg-open", working_directory, path);
}
void XDGEmail(const std::string& email) {
RunCommand("xdg-email", base::FilePath(), email);
}
} // namespace
namespace internal {
void PlatformOpenVerifiedItem(const base::FilePath& path, OpenItemType type) {
switch (type) {
case OPEN_FILE:
// Launch options with xdg activation token can only be obtained on the UI
// thread.
content::GetUIThreadTaskRunner()->PostTask(
FROM_HERE, base::BindOnce(&XDGOpen, path.DirName(), path.value()));
break;
case OPEN_FOLDER:
// The utility process checks the working directory prior to the
// invocation of xdg-open by changing the current directory into it. This
// operation only succeeds if |path| is a directory. Opening "." from
// there ensures that the target of the operation is a directory. Note
// that there remains a TOCTOU race where the directory could be unlinked
// between the time the utility process changes into the directory and the
// time the application invoked by xdg-open inspects the path by name.
// Launch options with xdg activation token can only be obtained on the UI
// thread.
content::GetUIThreadTaskRunner()->PostTask(
FROM_HERE, base::BindOnce(&XDGOpen, path, "."));
break;
}
}
} // namespace internal
void ShowItemInFolder(Profile*, const base::FilePath& full_path) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
ShowItemHelper::GetInstance().ShowItemInFolder(full_path);
}
void OpenExternal(const GURL& url) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (url.SchemeIs("mailto")) {
XDGEmail(url.spec());
} else {
XDGOpen(base::FilePath(), url.spec());
}
}
} // namespace platform_util
|