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