- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / platform_util_linux.cc
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.
4
5 #include "chrome/browser/platform_util.h"
6
7 #include "base/bind.h"
8 #include "base/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 "content/public/browser/browser_thread.h"
13 #include "url/gurl.h"
14
15 using content::BrowserThread;
16
17 namespace {
18
19 void XDGUtil(const std::string& util, const std::string& arg) {
20   std::vector<std::string> argv;
21   argv.push_back(util);
22   argv.push_back(arg);
23
24   base::LaunchOptions options;
25   // xdg-open can fall back on mailcap which eventually might plumb through
26   // to a command that needs a terminal.  Set the environment variable telling
27   // it that we definitely don't have a terminal available and that it should
28   // bring up a new terminal if necessary.  See "man mailcap".
29   options.environ["MM_NOTTTY"] = "1";
30
31   // In Google Chrome, we do not let GNOME's bug-buddy intercept our crashes.
32   // However, we do not want this environment variable to propagate to external
33   // applications. See http://crbug.com/24120
34   char* disable_gnome_bug_buddy = getenv("GNOME_DISABLE_CRASH_DIALOG");
35   if (disable_gnome_bug_buddy &&
36       disable_gnome_bug_buddy == std::string("SET_BY_GOOGLE_CHROME"))
37     options.environ["GNOME_DISABLE_CRASH_DIALOG"] = std::string();
38
39   base::ProcessHandle handle;
40   if (base::LaunchProcess(argv, options, &handle))
41     base::EnsureProcessGetsReaped(handle);
42 }
43
44 void XDGOpen(const std::string& path) {
45   XDGUtil("xdg-open", path);
46 }
47
48 void XDGEmail(const std::string& email) {
49   XDGUtil("xdg-email", email);
50 }
51
52 // TODO(estade): It would be nice to be able to select the file in the file
53 // manager, but that probably requires extending xdg-open. For now just
54 // show the folder.
55 void ShowItemInFolderOnFileThread(const base::FilePath& full_path) {
56   base::FilePath dir = full_path.DirName();
57   if (!base::DirectoryExists(dir))
58     return;
59
60   XDGOpen(dir.value());
61 }
62
63 }  // namespace
64
65 namespace platform_util {
66
67 void ShowItemInFolder(const base::FilePath& full_path) {
68   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
69   BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
70       base::Bind(&ShowItemInFolderOnFileThread, full_path));
71 }
72
73 void OpenItem(const base::FilePath& full_path) {
74   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
75   BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
76       base::Bind(&XDGOpen, full_path.value()));
77 }
78
79 void OpenExternal(const GURL& url) {
80   if (url.SchemeIs("mailto"))
81     XDGEmail(url.spec());
82   else
83     XDGOpen(url.spec());
84 }
85
86 }  // namespace platform_util