summaryrefslogtreecommitdiffstats
path: root/chrome/browser/shell_integration_linux.cc
blob: 2d2cb05cfa9733b8935cce3f39a4a7c5bbdb0e4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright (c) 2006-2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/shell_integration.h"

#include <fcntl.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include <vector>

#include "base/process_util.h"

#if defined(GOOGLE_CHROME_BUILD)
#define DESKTOP_APP_NAME "google-chrome.desktop"
#else  // CHROMIUM_BUILD
#define DESKTOP_APP_NAME "chromium-browser.desktop"
#endif

// We delegate the difficult of setting the default browser in Linux desktop
// environments to a new xdg utility, xdg-settings. We'll have to include a copy
// of it for this to work, obviously, but that's actually the suggested approach
// for xdg utilities anyway.

bool ShellIntegration::SetAsDefaultBrowser() {
  std::vector<std::string> argv;
  argv.push_back("xdg-settings");
  argv.push_back("set");
  argv.push_back("default-web-browser");
  argv.push_back(DESKTOP_APP_NAME);

  // xdg-settings internally runs xdg-mime, which uses mv to move newly-created
  // files on top of originals after making changes to them. In the event that
  // the original files are owned by another user (e.g. root, which can happen
  // if they are updated within sudo), mv will prompt the user to confirm if
  // standard input is a terminal (otherwise it just does it). So make sure it's
  // not, to avoid locking everything up waiting for mv.
  int devnull = open("/dev/null", O_RDONLY);
  if (devnull < 0)
    return false;
  base::file_handle_mapping_vector no_stdin;
  no_stdin.push_back(std::make_pair(devnull, STDIN_FILENO));

  base::ProcessHandle handle;
  if (!base::LaunchApp(argv, no_stdin, false, &handle)) {
    close(devnull);
    return false;
  }
  close(devnull);

  int success_code;
  base::WaitForExitCode(handle, &success_code);
  return success_code == EXIT_SUCCESS;
}

static bool GetDefaultBrowser(std::string* result) {
  std::vector<std::string> argv;
  argv.push_back("xdg-settings");
  argv.push_back("get");
  argv.push_back("default-web-browser");
  return base::GetAppOutput(CommandLine(argv), result);
}

bool ShellIntegration::IsDefaultBrowser() {
  std::string browser;
  if (!GetDefaultBrowser(&browser)) {
    // If GetDefaultBrowser() fails, we assume xdg-settings does not work for
    // some reason, and that we should pretend we're the default browser to
    // avoid giving repeated prompts to set ourselves as the default browser.
    // TODO: Really, being the default browser should be a ternary query: yes,
    // no, and "don't know" so that the UI can reflect this more accurately.
    return true;
  }
  // Allow for an optional newline at the end.
  if (browser.length() > 0 && browser[browser.length() - 1] == '\n')
    browser.resize(browser.length() - 1);
  return !browser.compare(DESKTOP_APP_NAME);
}

bool ShellIntegration::IsFirefoxDefaultBrowser() {
  std::string browser;
  // We don't care about the return value here.
  GetDefaultBrowser(&browser);
  return browser.find("irefox") != std::string::npos;
}