[email protected] | a82af39 | 2012-02-24 04:40:20 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 14a000d | 2010-04-29 21:44:24 | [diff] [blame] | 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 | |
[email protected] | 7c3228a | 2011-11-11 21:35:22 | [diff] [blame] | 7 | #include "base/bind.h" |
[email protected] | 14a000d | 2010-04-29 21:44:24 | [diff] [blame] | 8 | #include "base/file_util.h" |
| 9 | #include "base/process_util.h" |
| 10 | #include "base/utf_string_conversions.h" |
[email protected] | 7c3228a | 2011-11-11 21:35:22 | [diff] [blame] | 11 | #include "content/public/browser/browser_thread.h" |
[email protected] | 14a000d | 2010-04-29 21:44:24 | [diff] [blame] | 12 | #include "googleurl/src/gurl.h" |
| 13 | |
[email protected] | 7c3228a | 2011-11-11 21:35:22 | [diff] [blame] | 14 | using content::BrowserThread; |
| 15 | |
[email protected] | 14a000d | 2010-04-29 21:44:24 | [diff] [blame] | 16 | namespace { |
| 17 | |
[email protected] | e9ce67a1 | 2010-11-11 20:58:49 | [diff] [blame] | 18 | void XDGUtil(const std::string& util, const std::string& arg) { |
[email protected] | 14a000d | 2010-04-29 21:44:24 | [diff] [blame] | 19 | std::vector<std::string> argv; |
[email protected] | e9ce67a1 | 2010-11-11 20:58:49 | [diff] [blame] | 20 | argv.push_back(util); |
| 21 | argv.push_back(arg); |
[email protected] | 14a000d | 2010-04-29 21:44:24 | [diff] [blame] | 22 | |
[email protected] | a82af39 | 2012-02-24 04:40:20 | [diff] [blame] | 23 | base::EnvironmentVector env; |
[email protected] | 14a000d | 2010-04-29 21:44:24 | [diff] [blame] | 24 | // 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] | 14a000d | 2010-04-29 21:44:24 | [diff] [blame] | 39 | base::ProcessHandle handle; |
[email protected] | 1e41c38 | 2011-07-12 18:09:46 | [diff] [blame] | 40 | base::LaunchOptions options; |
[email protected] | 1e41c38 | 2011-07-12 18:09:46 | [diff] [blame] | 41 | options.environ = &env; |
[email protected] | e599218 | 2011-07-15 16:47:02 | [diff] [blame] | 42 | if (base::LaunchProcess(argv, options, &handle)) |
[email protected] | eaac7159 | 2011-11-23 18:32:00 | [diff] [blame] | 43 | base::EnsureProcessGetsReaped(handle); |
[email protected] | 14a000d | 2010-04-29 21:44:24 | [diff] [blame] | 44 | } |
| 45 | |
[email protected] | e9ce67a1 | 2010-11-11 20:58:49 | [diff] [blame] | 46 | void XDGOpen(const std::string& path) { |
| 47 | XDGUtil("xdg-open", path); |
| 48 | } |
| 49 | |
| 50 | void XDGEmail(const std::string& email) { |
| 51 | XDGUtil("xdg-email", email); |
| 52 | } |
| 53 | |
[email protected] | 14a000d | 2010-04-29 21:44:24 | [diff] [blame] | 54 | // 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] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 57 | void ShowItemInFolderOnFileThread(const base::FilePath& full_path) { |
| 58 | base::FilePath dir = full_path.DirName(); |
[email protected] | 14a000d | 2010-04-29 21:44:24 | [diff] [blame] | 59 | if (!file_util::DirectoryExists(dir)) |
| 60 | return; |
| 61 | |
| 62 | XDGOpen(dir.value()); |
| 63 | } |
| 64 | |
[email protected] | 7c3228a | 2011-11-11 21:35:22 | [diff] [blame] | 65 | } // namespace |
| 66 | |
| 67 | namespace platform_util { |
| 68 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 69 | void ShowItemInFolder(const base::FilePath& full_path) { |
[email protected] | 7c3228a | 2011-11-11 21:35:22 | [diff] [blame] | 70 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 71 | BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 72 | base::Bind(&ShowItemInFolderOnFileThread, full_path)); |
| 73 | } |
| 74 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 75 | void OpenItem(const base::FilePath& full_path) { |
[email protected] | 7c3228a | 2011-11-11 21:35:22 | [diff] [blame] | 76 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 77 | BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 78 | base::Bind(&XDGOpen, full_path.value())); |
[email protected] | 14a000d | 2010-04-29 21:44:24 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | void OpenExternal(const GURL& url) { |
[email protected] | e9ce67a1 | 2010-11-11 20:58:49 | [diff] [blame] | 82 | if (url.SchemeIs("mailto")) |
| 83 | XDGEmail(url.spec()); |
| 84 | else |
| 85 | XDGOpen(url.spec()); |
[email protected] | 14a000d | 2010-04-29 21:44:24 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | } // namespace platform_util |