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