blob: 4afe8fd9632d54d947d8d35fe27dd747f3afe900 [file] [log] [blame]
[email protected]a82af392012-02-24 04:40:201// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]14a000d2010-04-29 21:44:242// 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
[email protected]7c3228a2011-11-11 21:35:227#include "base/bind.h"
[email protected]14a000d2010-04-29 21:44:248#include "base/file_util.h"
9#include "base/process_util.h"
10#include "base/utf_string_conversions.h"
[email protected]7c3228a2011-11-11 21:35:2211#include "content/public/browser/browser_thread.h"
[email protected]14a000d2010-04-29 21:44:2412#include "googleurl/src/gurl.h"
13
[email protected]7c3228a2011-11-11 21:35:2214using content::BrowserThread;
15
[email protected]14a000d2010-04-29 21:44:2416namespace {
17
[email protected]e9ce67a12010-11-11 20:58:4918void XDGUtil(const std::string& util, const std::string& arg) {
[email protected]14a000d2010-04-29 21:44:2419 std::vector<std::string> argv;
[email protected]e9ce67a12010-11-11 20:58:4920 argv.push_back(util);
21 argv.push_back(arg);
[email protected]14a000d2010-04-29 21:44:2422
[email protected]a82af392012-02-24 04:40:2023 base::EnvironmentVector env;
[email protected]14a000d2010-04-29 21:44:2424 // xdg-open can fall back on mailcap which eventually might plumb through
25 // to a command that needs a terminal. Set the environment variable telling
26 // it that we definitely don't have a terminal available and that it should
27 // bring up a new terminal if necessary. See "man mailcap".
28 env.push_back(std::make_pair("MM_NOTTTY", "1"));
29
30 // In Google Chrome, we do not let GNOME's bug-buddy intercept our crashes.
31 // However, we do not want this environment variable to propagate to external
32 // applications. See http://crbug.com/24120
33 char* disable_gnome_bug_buddy = getenv("GNOME_DISABLE_CRASH_DIALOG");
34 if (disable_gnome_bug_buddy &&
35 disable_gnome_bug_buddy == std::string("SET_BY_GOOGLE_CHROME")) {
36 env.push_back(std::make_pair("GNOME_DISABLE_CRASH_DIALOG", ""));
37 }
38
[email protected]14a000d2010-04-29 21:44:2439 base::ProcessHandle handle;
[email protected]1e41c382011-07-12 18:09:4640 base::LaunchOptions options;
[email protected]1e41c382011-07-12 18:09:4641 options.environ = &env;
[email protected]e5992182011-07-15 16:47:0242 if (base::LaunchProcess(argv, options, &handle))
[email protected]eaac71592011-11-23 18:32:0043 base::EnsureProcessGetsReaped(handle);
[email protected]14a000d2010-04-29 21:44:2444}
45
[email protected]e9ce67a12010-11-11 20:58:4946void XDGOpen(const std::string& path) {
47 XDGUtil("xdg-open", path);
48}
49
50void XDGEmail(const std::string& email) {
51 XDGUtil("xdg-email", email);
52}
53
[email protected]14a000d2010-04-29 21:44:2454// TODO(estade): It would be nice to be able to select the file in the file
55// manager, but that probably requires extending xdg-open. For now just
56// show the folder.
[email protected]650b2d52013-02-10 03:41:4557void ShowItemInFolderOnFileThread(const base::FilePath& full_path) {
58 base::FilePath dir = full_path.DirName();
[email protected]14a000d2010-04-29 21:44:2459 if (!file_util::DirectoryExists(dir))
60 return;
61
62 XDGOpen(dir.value());
63}
64
[email protected]7c3228a2011-11-11 21:35:2265} // namespace
66
67namespace platform_util {
68
[email protected]650b2d52013-02-10 03:41:4569void ShowItemInFolder(const base::FilePath& full_path) {
[email protected]7c3228a2011-11-11 21:35:2270 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
71 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
72 base::Bind(&ShowItemInFolderOnFileThread, full_path));
73}
74
[email protected]650b2d52013-02-10 03:41:4575void OpenItem(const base::FilePath& full_path) {
[email protected]7c3228a2011-11-11 21:35:2276 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
77 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
78 base::Bind(&XDGOpen, full_path.value()));
[email protected]14a000d2010-04-29 21:44:2479}
80
81void OpenExternal(const GURL& url) {
[email protected]e9ce67a12010-11-11 20:58:4982 if (url.SchemeIs("mailto"))
83 XDGEmail(url.spec());
84 else
85 XDGOpen(url.spec());
[email protected]14a000d2010-04-29 21:44:2486}
87
88} // namespace platform_util