Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / ui / webui / options / advanced_options_utils_linux.cc
bloba05845bb26cf62273173b939610a24aef6e9b499
1 // Copyright 2013 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.
5 #if !defined(OS_CHROMEOS)
7 #include "chrome/browser/ui/webui/options/advanced_options_utils.h"
9 #include "base/bind.h"
10 #include "base/environment.h"
11 #include "base/files/file_path.h"
12 #include "base/files/file_util.h"
13 #include "base/nix/xdg_util.h"
14 #include "base/process/launch.h"
15 #include "base/strings/string_split.h"
16 #include "base/strings/string_util.h"
17 #include "chrome/browser/tab_contents/tab_util.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "content/public/browser/render_process_host.h"
20 #include "content/public/browser/render_view_host.h"
21 #include "content/public/browser/web_contents.h"
23 using content::BrowserThread;
24 using content::OpenURLParams;
25 using content::Referrer;
26 using content::WebContents;
28 namespace options {
30 // Command used to configure GNOME 2 proxy settings.
31 const char* kGNOME2ProxyConfigCommand[] = {"gnome-network-properties", NULL};
32 // In GNOME 3, we might need to run gnome-control-center instead. We try this
33 // only after gnome-network-properties is not found, because older GNOME also
34 // has this but it doesn't do the same thing. See below where we use it.
35 const char* kGNOME3ProxyConfigCommand[] = {"gnome-control-center", "network",
36 NULL};
37 // KDE3 and KDE4 are only slightly different, but incompatible. Go figure.
38 const char* kKDE3ProxyConfigCommand[] = {"kcmshell", "proxy", NULL};
39 const char* kKDE4ProxyConfigCommand[] = {"kcmshell4", "proxy", NULL};
41 // The URL for Linux proxy configuration help when not running under a
42 // supported desktop environment.
43 const char kLinuxProxyConfigUrl[] = "about:linux-proxy-config";
45 namespace {
47 // Show the proxy config URL in the given tab.
48 void ShowLinuxProxyConfigUrl(int render_process_id, int render_view_id) {
49 DCHECK_CURRENTLY_ON(BrowserThread::UI);
50 scoped_ptr<base::Environment> env(base::Environment::Create());
51 const char* name = base::nix::GetDesktopEnvironmentName(env.get());
52 if (name)
53 LOG(ERROR) << "Could not find " << name << " network settings in $PATH";
54 OpenURLParams params(
55 GURL(kLinuxProxyConfigUrl), Referrer(), NEW_FOREGROUND_TAB,
56 ui::PAGE_TRANSITION_LINK, false);
58 WebContents* web_contents =
59 tab_util::GetWebContentsByID(render_process_id, render_view_id);
60 if (web_contents)
61 web_contents->OpenURL(params);
64 // Start the given proxy configuration utility.
65 bool StartProxyConfigUtil(const char* command[]) {
66 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
67 // base::LaunchProcess() returns true ("success") if the fork()
68 // succeeds, but not necessarily the exec(). We'd like to be able to
69 // use StartProxyConfigUtil() to search possible options and stop on
70 // success, so we search $PATH first to predict whether the exec is
71 // expected to succeed.
72 // TODO(mdm): this is a useful check, and is very similar to some
73 // code in proxy_config_service_linux.cc. It should probably be in
74 // base:: somewhere.
75 scoped_ptr<base::Environment> env(base::Environment::Create());
76 std::string path;
77 if (!env->GetVar("PATH", &path)) {
78 LOG(ERROR) << "No $PATH variable. Assuming no " << command[0] << ".";
79 return false;
82 bool found = false;
83 for (const base::StringPiece& cur_path :
84 base::SplitStringPiece(path, ":", base::KEEP_WHITESPACE,
85 base::SPLIT_WANT_NONEMPTY)) {
86 base::FilePath file(cur_path);
87 if (base::PathExists(file.Append(command[0]))) {
88 found = true;
89 break;
92 if (!found)
93 return false;
95 std::vector<std::string> argv;
96 for (size_t i = 0; command[i]; ++i)
97 argv.push_back(command[i]);
98 base::Process process = base::LaunchProcess(argv, base::LaunchOptions());
99 if (!process.IsValid()) {
100 LOG(ERROR) << "StartProxyConfigUtil failed to start " << command[0];
101 return false;
103 base::EnsureProcessGetsReaped(process.Pid());
104 return true;
107 // Detect, and if possible, start the appropriate proxy config utility. On
108 // failure to do so, show the Linux proxy config URL in a new tab instead.
109 void DetectAndStartProxyConfigUtil(int render_process_id,
110 int render_view_id) {
111 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
112 scoped_ptr<base::Environment> env(base::Environment::Create());
114 bool launched = false;
115 switch (base::nix::GetDesktopEnvironment(env.get())) {
116 case base::nix::DESKTOP_ENVIRONMENT_GNOME:
117 case base::nix::DESKTOP_ENVIRONMENT_UNITY: {
118 launched = StartProxyConfigUtil(kGNOME2ProxyConfigCommand);
119 if (!launched) {
120 // We try this second, even though it's the newer way, because this
121 // command existed in older versions of GNOME, but it didn't do the
122 // same thing. The older command is gone though, so this should do
123 // the right thing. (Also some distributions have blurred the lines
124 // between GNOME 2 and 3, so we can't necessarily detect what the
125 // right thing is based on indications of which version we have.)
126 launched = StartProxyConfigUtil(kGNOME3ProxyConfigCommand);
128 break;
131 case base::nix::DESKTOP_ENVIRONMENT_KDE3:
132 launched = StartProxyConfigUtil(kKDE3ProxyConfigCommand);
133 break;
135 case base::nix::DESKTOP_ENVIRONMENT_KDE4:
136 launched = StartProxyConfigUtil(kKDE4ProxyConfigCommand);
137 break;
139 case base::nix::DESKTOP_ENVIRONMENT_XFCE:
140 case base::nix::DESKTOP_ENVIRONMENT_OTHER:
141 break;
144 if (launched)
145 return;
146 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
147 base::Bind(&ShowLinuxProxyConfigUrl, render_process_id, render_view_id));
150 } // anonymous namespace
152 void AdvancedOptionsUtilities::ShowNetworkProxySettings(
153 WebContents* web_contents) {
154 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
155 base::Bind(&DetectAndStartProxyConfigUtil,
156 web_contents->GetRenderProcessHost()->GetID(),
157 web_contents->GetRenderViewHost()->GetRoutingID()));
160 } // namespace options
162 #endif // !defined(OS_CHROMEOS)