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