1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/platform_util.h"
8 #include "base/files/file_util.h"
9 #include "base/process/kill.h"
10 #include "base/process/launch.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/platform_util_internal.h"
13 #include "content/public/browser/browser_thread.h"
16 using content::BrowserThread
;
18 namespace platform_util
{
22 void XDGUtil(const std::string
& util
,
23 const base::FilePath
& working_directory
,
24 const std::string
& arg
) {
25 std::vector
<std::string
> argv
;
29 base::LaunchOptions options
;
30 options
.current_directory
= working_directory
;
31 options
.allow_new_privs
= true;
32 // xdg-open can fall back on mailcap which eventually might plumb through
33 // to a command that needs a terminal. Set the environment variable telling
34 // it that we definitely don't have a terminal available and that it should
35 // bring up a new terminal if necessary. See "man mailcap".
36 options
.environ
["MM_NOTTTY"] = "1";
38 // In Google Chrome, we do not let GNOME's bug-buddy intercept our crashes.
39 // However, we do not want this environment variable to propagate to external
40 // applications. See http://crbug.com/24120
41 char* disable_gnome_bug_buddy
= getenv("GNOME_DISABLE_CRASH_DIALOG");
42 if (disable_gnome_bug_buddy
&&
43 disable_gnome_bug_buddy
== std::string("SET_BY_GOOGLE_CHROME"))
44 options
.environ
["GNOME_DISABLE_CRASH_DIALOG"] = std::string();
46 base::Process process
= base::LaunchProcess(argv
, options
);
47 if (process
.IsValid())
48 base::EnsureProcessGetsReaped(process
.Pid());
51 void XDGOpen(const base::FilePath
& working_directory
, const std::string
& path
) {
52 XDGUtil("xdg-open", working_directory
, path
);
55 void XDGEmail(const std::string
& email
) {
56 XDGUtil("xdg-email", base::FilePath(), email
);
63 void PlatformOpenVerifiedItem(const base::FilePath
& path
, OpenItemType type
) {
66 XDGOpen(path
.DirName(), path
.value());
69 // The utility process checks the working directory prior to the
70 // invocation of xdg-open by changing the current directory into it. This
71 // operation only succeeds if |path| is a directory. Opening "." from
72 // there ensures that the target of the operation is a directory. Note
73 // that there remains a TOCTOU race where the directory could be unlinked
74 // between the time the utility process changes into the directory and the
75 // time the application invoked by xdg-open inspects the path by name.
80 } // namespace internal
82 void ShowItemInFolder(Profile
* profile
, const base::FilePath
& full_path
) {
83 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
84 // TODO(estade): It would be nice to be able to select the file in the file
85 // manager, but that probably requires extending xdg-open. For now just show
87 OpenItem(profile
, full_path
.DirName(), OPEN_FOLDER
, OpenOperationCallback());
90 void OpenExternal(Profile
* profile
, const GURL
& url
) {
91 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
92 if (url
.SchemeIs("mailto"))
95 XDGOpen(base::FilePath(), url
.spec());
98 } // namespace platform_util