1 // Copyright (c) 2009 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.
4
5 #include "chrome/browser/platform_util.h"
6
7 #include <gtk/gtk.h>
8
9 #include "base/file_util.h"
10 #include "base/process_util.h"
11 #include "base/utf_string_conversions.h"
12 #include "content/common/process_watcher.h"
13 #include "googleurl/src/gurl.h"
14
15 namespace {
16
XDGUtil(const std::string & util,const std::string & arg)17 void XDGUtil(const std::string& util, const std::string& arg) {
18 std::vector<std::string> argv;
19 argv.push_back(util);
20 argv.push_back(arg);
21
22 base::environment_vector env;
23 // xdg-open can fall back on mailcap which eventually might plumb through
24 // to a command that needs a terminal. Set the environment variable telling
25 // it that we definitely don't have a terminal available and that it should
26 // bring up a new terminal if necessary. See "man mailcap".
27 env.push_back(std::make_pair("MM_NOTTTY", "1"));
28
29 // In Google Chrome, we do not let GNOME's bug-buddy intercept our crashes.
30 // However, we do not want this environment variable to propagate to external
31 // applications. See http://crbug.com/24120
32 char* disable_gnome_bug_buddy = getenv("GNOME_DISABLE_CRASH_DIALOG");
33 if (disable_gnome_bug_buddy &&
34 disable_gnome_bug_buddy == std::string("SET_BY_GOOGLE_CHROME")) {
35 env.push_back(std::make_pair("GNOME_DISABLE_CRASH_DIALOG", ""));
36 }
37
38 base::file_handle_mapping_vector no_files;
39 base::ProcessHandle handle;
40 if (base::LaunchApp(argv, env, no_files, false, &handle))
41 ProcessWatcher::EnsureProcessGetsReaped(handle);
42 }
43
XDGOpen(const std::string & path)44 void XDGOpen(const std::string& path) {
45 XDGUtil("xdg-open", path);
46 }
47
XDGEmail(const std::string & email)48 void XDGEmail(const std::string& email) {
49 XDGUtil("xdg-email", email);
50 }
51
52 } // namespace
53
54 namespace platform_util {
55
56 // TODO(estade): It would be nice to be able to select the file in the file
57 // manager, but that probably requires extending xdg-open. For now just
58 // show the folder.
ShowItemInFolder(const FilePath & full_path)59 void ShowItemInFolder(const FilePath& full_path) {
60 FilePath dir = full_path.DirName();
61 if (!file_util::DirectoryExists(dir))
62 return;
63
64 XDGOpen(dir.value());
65 }
66
OpenItem(const FilePath & full_path)67 void OpenItem(const FilePath& full_path) {
68 XDGOpen(full_path.value());
69 }
70
OpenExternal(const GURL & url)71 void OpenExternal(const GURL& url) {
72 if (url.SchemeIs("mailto"))
73 XDGEmail(url.spec());
74 else
75 XDGOpen(url.spec());
76 }
77
78 } // namespace platform_util
79