blob: 932fb9cb0bfc27f5b9be8cf3a504eefe1848f417 [file] [log] [blame]
Andrew de los Reyes000d8952011-03-02 15:21:14 -08001// 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 Vakulenkod2779df2014-06-16 13:19:00 -07007#include <deque>
Andrew de los Reyes000d8952011-03-02 15:21:14 -08008#include <map>
9#include <string>
Alex Vakulenkod2779df2014-06-16 13:19:00 -070010#include <utility>
Andrew de los Reyes000d8952011-03-02 15:21:14 -080011
Alex Vakulenko4906c1c2014-08-21 13:17:44 -070012#include <base/bind.h>
Chris Sosafc661a12013-02-26 14:43:21 -080013#include <base/strings/string_tokenizer.h>
Alex Deymoc4acdf42014-05-28 21:07:10 -070014#include <base/strings/string_util.h>
Andrew de los Reyes000d8952011-03-02 15:21:14 -080015
Andrew de los Reyes000d8952011-03-02 15:21:14 -080016#include "update_engine/utils.h"
17
18namespace chromeos_update_engine {
19
Chris Sosafc661a12013-02-26 14:43:21 -080020using base::StringTokenizer;
Alex Deymo60ca1a72015-06-18 18:19:15 -070021using base::TimeDelta;
22using chromeos::MessageLoop;
Andrew de los Reyes000d8952011-03-02 15:21:14 -080023using std::deque;
24using std::make_pair;
Andrew de los Reyes000d8952011-03-02 15:21:14 -080025using std::pair;
26using std::string;
27
Andrew de los Reyes000d8952011-03-02 15:21:14 -080028const char kLibCrosServiceName[] = "org.chromium.LibCrosService";
Alex Deymo30534502015-07-20 15:06:33 -070029const char kLibCrosProxyResolveName[] = "ProxyResolved";
Andrew de los Reyes000d8952011-03-02 15:21:14 -080030const char kLibCrosProxyResolveSignalInterface[] =
Alex Deymo30534502015-07-20 15:06:33 -070031 "org.chromium.UpdateEngineLibcrosProxyResolvedInterface";
Andrew de los Reyes000d8952011-03-02 15:21:14 -080032
33namespace {
Gilad Arnoldb752fb32014-03-03 12:23:39 -080034
Andrew de los Reyes000d8952011-03-02 15:21:14 -080035const int kTimeout = 5; // seconds
36
Alex Vakulenkod2779df2014-06-16 13:19:00 -070037} // namespace
Andrew de los Reyes000d8952011-03-02 15:21:14 -080038
Gilad Arnold1b9d6ae2014-03-03 13:46:07 -080039ChromeBrowserProxyResolver::ChromeBrowserProxyResolver(
Alex Deymo30534502015-07-20 15:06:33 -070040 LibCrosProxy* libcros_proxy)
41 : libcros_proxy_(libcros_proxy), timeout_(kTimeout) {}
Andrew de los Reyes000d8952011-03-02 15:21:14 -080042
43bool ChromeBrowserProxyResolver::Init() {
Alex Deymo30534502015-07-20 15:06:33 -070044 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 Reyes000d8952011-03-02 15:21:14 -080050 return true;
51}
52
53ChromeBrowserProxyResolver::~ChromeBrowserProxyResolver() {
Alex Deymo30534502015-07-20 15:06:33 -070054 // Kill outstanding timers.
Alex Deymo020600d2014-11-05 21:05:55 -080055 for (auto& timer : timers_) {
Alex Deymo60ca1a72015-06-18 18:19:15 -070056 MessageLoop::current()->CancelTask(timer.second);
57 timer.second = MessageLoop::kTaskIdNull;
Andrew de los Reyes000d8952011-03-02 15:21:14 -080058 }
59}
60
61bool ChromeBrowserProxyResolver::GetProxiesForUrl(const string& url,
62 ProxiesResolvedFn callback,
63 void* data) {
Alex Deymo30534502015-07-20 15:06:33 -070064 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 Reyes518502a2011-03-14 14:19:39 -070072 timeout = 0;
Andrew de los Reyes000d8952011-03-02 15:21:14 -080073 }
Gilad Arnold1877c392012-02-10 11:34:33 -080074
Andrew de los Reyes000d8952011-03-02 15:21:14 -080075 callbacks_.insert(make_pair(url, make_pair(callback, data)));
Alex Deymo60ca1a72015-06-18 18:19:15 -070076 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 Reyes000d8952011-03-02 15:21:14 -080082 timers_.insert(make_pair(url, timer));
83 return true;
84}
85
Andrew de los Reyes000d8952011-03-02 15:21:14 -080086bool 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 Deymo60ca1a72015-06-18 18:19:15 -0700103 MessageLoop::current()->CancelTask(it->second);
Andrew de los Reyes000d8952011-03-02 15:21:14 -0800104 timers_.erase(it);
105 }
106 return true;
107}
108
Alex Deymo30534502015-07-20 15:06:33 -0700109void 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
118void ChromeBrowserProxyResolver::OnProxyResolvedSignal(
119 const string& source_url,
120 const string& proxy_info,
121 const string& error_message) {
Andrew de los Reyes000d8952011-03-02 15:21:14 -0800122 pair<ProxiesResolvedFn, void*> callback;
123 TEST_AND_RETURN(DeleteUrlState(source_url, true, &callback));
Alex Deymo30534502015-07-20 15:06:33 -0700124 if (!error_message.empty()) {
125 LOG(WARNING) << "ProxyResolved error: " << error_message;
126 }
127 (*callback.first)(ParseProxyString(proxy_info), callback.second);
Andrew de los Reyes000d8952011-03-02 15:21:14 -0800128}
129
130void 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
139deque<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 Chan736fcb52014-05-21 18:28:22 -0700148 base::TrimWhitespaceASCII(token, base::TRIM_ALL, &token);
Andrew de los Reyes000d8952011-03-02 15:21:14 -0800149
150 // Start by finding the first space (if any).
Alex Deymof329b932014-10-30 01:37:48 -0700151 string::iterator space;
Andrew de los Reyes000d8952011-03-02 15:21:14 -0800152 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 Chan93aabd02014-09-05 08:20:59 -0700159 base::StringToLowerASCII(&scheme);
Andrew de los Reyes000d8952011-03-02 15:21:14 -0800160 // 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 Chan736fcb52014-05-21 18:28:22 -0700172 base::TrimWhitespaceASCII(host_and_port, base::TRIM_ALL, &host_and_port);
Andrew de los Reyes000d8952011-03-02 15:21:14 -0800173 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