Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS 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 | #include "update_engine/chrome_browser_proxy_resolver.h" |
| 6 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 7 | #include <deque> |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 8 | #include <map> |
| 9 | #include <string> |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 10 | #include <utility> |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 11 | |
Alex Vakulenko | 4906c1c | 2014-08-21 13:17:44 -0700 | [diff] [blame] | 12 | #include <base/bind.h> |
Chris Sosa | fc661a1 | 2013-02-26 14:43:21 -0800 | [diff] [blame] | 13 | #include <base/strings/string_tokenizer.h> |
Alex Deymo | c4acdf4 | 2014-05-28 21:07:10 -0700 | [diff] [blame] | 14 | #include <base/strings/string_util.h> |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 15 | |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 16 | #include "update_engine/utils.h" |
| 17 | |
| 18 | namespace chromeos_update_engine { |
| 19 | |
Chris Sosa | fc661a1 | 2013-02-26 14:43:21 -0800 | [diff] [blame] | 20 | using base::StringTokenizer; |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 21 | using base::TimeDelta; |
| 22 | using chromeos::MessageLoop; |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 23 | using std::deque; |
| 24 | using std::make_pair; |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 25 | using std::pair; |
| 26 | using std::string; |
| 27 | |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 28 | const char kLibCrosServiceName[] = "org.chromium.LibCrosService"; |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame^] | 29 | const char kLibCrosProxyResolveName[] = "ProxyResolved"; |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 30 | const char kLibCrosProxyResolveSignalInterface[] = |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame^] | 31 | "org.chromium.UpdateEngineLibcrosProxyResolvedInterface"; |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 32 | |
| 33 | namespace { |
Gilad Arnold | b752fb3 | 2014-03-03 12:23:39 -0800 | [diff] [blame] | 34 | |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 35 | const int kTimeout = 5; // seconds |
| 36 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 37 | } // namespace |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 38 | |
Gilad Arnold | 1b9d6ae | 2014-03-03 13:46:07 -0800 | [diff] [blame] | 39 | ChromeBrowserProxyResolver::ChromeBrowserProxyResolver( |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame^] | 40 | LibCrosProxy* libcros_proxy) |
| 41 | : libcros_proxy_(libcros_proxy), timeout_(kTimeout) {} |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 42 | |
| 43 | bool ChromeBrowserProxyResolver::Init() { |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame^] | 44 | libcros_proxy_->ue_proxy_resolved_interface() |
| 45 | ->RegisterProxyResolvedSignalHandler( |
| 46 | base::Bind(&ChromeBrowserProxyResolver::OnProxyResolvedSignal, |
| 47 | base::Unretained(this)), |
| 48 | base::Bind(&ChromeBrowserProxyResolver::OnSignalConnected, |
| 49 | base::Unretained(this))); |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 50 | return true; |
| 51 | } |
| 52 | |
| 53 | ChromeBrowserProxyResolver::~ChromeBrowserProxyResolver() { |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame^] | 54 | // Kill outstanding timers. |
Alex Deymo | 020600d | 2014-11-05 21:05:55 -0800 | [diff] [blame] | 55 | for (auto& timer : timers_) { |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 56 | MessageLoop::current()->CancelTask(timer.second); |
| 57 | timer.second = MessageLoop::kTaskIdNull; |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
| 61 | bool ChromeBrowserProxyResolver::GetProxiesForUrl(const string& url, |
| 62 | ProxiesResolvedFn callback, |
| 63 | void* data) { |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame^] | 64 | int timeout = timeout_; |
| 65 | chromeos::ErrorPtr error; |
| 66 | if (!libcros_proxy_->service_interface_proxy()->ResolveNetworkProxy( |
| 67 | url.c_str(), |
| 68 | kLibCrosProxyResolveSignalInterface, |
| 69 | kLibCrosProxyResolveName, |
| 70 | &error)) { |
| 71 | LOG(WARNING) << "Can't resolve the proxy. Continuing with no proxy."; |
Andrew de los Reyes | 518502a | 2011-03-14 14:19:39 -0700 | [diff] [blame] | 72 | timeout = 0; |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 73 | } |
Gilad Arnold | 1877c39 | 2012-02-10 11:34:33 -0800 | [diff] [blame] | 74 | |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 75 | callbacks_.insert(make_pair(url, make_pair(callback, data))); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 76 | MessageLoop::TaskId timer = MessageLoop::current()->PostDelayedTask( |
| 77 | FROM_HERE, |
| 78 | base::Bind(&ChromeBrowserProxyResolver::HandleTimeout, |
| 79 | base::Unretained(this), |
| 80 | url), |
| 81 | TimeDelta::FromSeconds(timeout)); |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 82 | timers_.insert(make_pair(url, timer)); |
| 83 | return true; |
| 84 | } |
| 85 | |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 86 | bool ChromeBrowserProxyResolver::DeleteUrlState( |
| 87 | const string& source_url, |
| 88 | bool delete_timer, |
| 89 | pair<ProxiesResolvedFn, void*>* callback) { |
| 90 | { |
| 91 | CallbacksMap::iterator it = callbacks_.lower_bound(source_url); |
| 92 | TEST_AND_RETURN_FALSE(it != callbacks_.end()); |
| 93 | TEST_AND_RETURN_FALSE(it->first == source_url); |
| 94 | if (callback) |
| 95 | *callback = it->second; |
| 96 | callbacks_.erase(it); |
| 97 | } |
| 98 | { |
| 99 | TimeoutsMap::iterator it = timers_.lower_bound(source_url); |
| 100 | TEST_AND_RETURN_FALSE(it != timers_.end()); |
| 101 | TEST_AND_RETURN_FALSE(it->first == source_url); |
| 102 | if (delete_timer) |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 103 | MessageLoop::current()->CancelTask(it->second); |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 104 | timers_.erase(it); |
| 105 | } |
| 106 | return true; |
| 107 | } |
| 108 | |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame^] | 109 | void ChromeBrowserProxyResolver::OnSignalConnected(const string& interface_name, |
| 110 | const string& signal_name, |
| 111 | bool successful) { |
| 112 | if (!successful) { |
| 113 | LOG(ERROR) << "Couldn't connect to the signal " << interface_name << "." |
| 114 | << signal_name; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | void ChromeBrowserProxyResolver::OnProxyResolvedSignal( |
| 119 | const string& source_url, |
| 120 | const string& proxy_info, |
| 121 | const string& error_message) { |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 122 | pair<ProxiesResolvedFn, void*> callback; |
| 123 | TEST_AND_RETURN(DeleteUrlState(source_url, true, &callback)); |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame^] | 124 | if (!error_message.empty()) { |
| 125 | LOG(WARNING) << "ProxyResolved error: " << error_message; |
| 126 | } |
| 127 | (*callback.first)(ParseProxyString(proxy_info), callback.second); |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | void ChromeBrowserProxyResolver::HandleTimeout(string source_url) { |
| 131 | LOG(INFO) << "Timeout handler called. Seems Chrome isn't responding."; |
| 132 | pair<ProxiesResolvedFn, void*> callback; |
| 133 | TEST_AND_RETURN(DeleteUrlState(source_url, false, &callback)); |
| 134 | deque<string> proxies; |
| 135 | proxies.push_back(kNoProxy); |
| 136 | (*callback.first)(proxies, callback.second); |
| 137 | } |
| 138 | |
| 139 | deque<string> ChromeBrowserProxyResolver::ParseProxyString( |
| 140 | const string& input) { |
| 141 | deque<string> ret; |
| 142 | // Some of this code taken from |
| 143 | // http://src.chromium.org/svn/trunk/src/net/proxy/proxy_server.cc and |
| 144 | // http://src.chromium.org/svn/trunk/src/net/proxy/proxy_list.cc |
| 145 | StringTokenizer entry_tok(input, ";"); |
| 146 | while (entry_tok.GetNext()) { |
| 147 | string token = entry_tok.token(); |
Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame] | 148 | base::TrimWhitespaceASCII(token, base::TRIM_ALL, &token); |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 149 | |
| 150 | // Start by finding the first space (if any). |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 151 | string::iterator space; |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 152 | for (space = token.begin(); space != token.end(); ++space) { |
| 153 | if (IsAsciiWhitespace(*space)) { |
| 154 | break; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | string scheme = string(token.begin(), space); |
Ben Chan | 93aabd0 | 2014-09-05 08:20:59 -0700 | [diff] [blame] | 159 | base::StringToLowerASCII(&scheme); |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 160 | // Chrome uses "socks" to mean socks4 and "proxy" to mean http. |
| 161 | if (scheme == "socks") |
| 162 | scheme += "4"; |
| 163 | else if (scheme == "proxy") |
| 164 | scheme = "http"; |
| 165 | else if (scheme != "https" && |
| 166 | scheme != "socks4" && |
| 167 | scheme != "socks5" && |
| 168 | scheme != "direct") |
| 169 | continue; // Invalid proxy scheme |
| 170 | |
| 171 | string host_and_port = string(space, token.end()); |
Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame] | 172 | base::TrimWhitespaceASCII(host_and_port, base::TRIM_ALL, &host_and_port); |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 173 | if (scheme != "direct" && host_and_port.empty()) |
| 174 | continue; // Must supply host/port when non-direct proxy used. |
| 175 | ret.push_back(scheme + "://" + host_and_port); |
| 176 | } |
| 177 | if (ret.empty() || *ret.rbegin() != kNoProxy) |
| 178 | ret.push_back(kNoProxy); |
| 179 | return ret; |
| 180 | } |
| 181 | |
| 182 | } // namespace chromeos_update_engine |