| Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // | 
|  | 2 | // Copyright (C) 2011 The Android Open Source Project | 
|  | 3 | // | 
|  | 4 | // Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | // you may not use this file except in compliance with the License. | 
|  | 6 | // You may obtain a copy of the License at | 
|  | 7 | // | 
|  | 8 | //      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | // | 
|  | 10 | // Unless required by applicable law or agreed to in writing, software | 
|  | 11 | // distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | // See the License for the specific language governing permissions and | 
|  | 14 | // limitations under the License. | 
|  | 15 | // | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 16 |  | 
|  | 17 | #include "update_engine/chrome_browser_proxy_resolver.h" | 
|  | 18 |  | 
| Daniel Erat | 941cf23 | 2017-04-20 12:09:58 -0600 | [diff] [blame] | 19 | #include <utility> | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 20 |  | 
| Alex Vakulenko | 4906c1c | 2014-08-21 13:17:44 -0700 | [diff] [blame] | 21 | #include <base/bind.h> | 
| Daniel Erat | 941cf23 | 2017-04-20 12:09:58 -0600 | [diff] [blame] | 22 | #include <base/memory/ptr_util.h> | 
| Alex Deymo | c4acdf4 | 2014-05-28 21:07:10 -0700 | [diff] [blame] | 23 | #include <base/strings/string_util.h> | 
| Jeffrey Kardatzke | cf5f1f1 | 2017-10-02 16:08:44 -0700 | [diff] [blame] | 24 | #include <brillo/http/http_proxy.h> | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 25 |  | 
| Jeffrey Kardatzke | cf5f1f1 | 2017-10-02 16:08:44 -0700 | [diff] [blame] | 26 | #include "update_engine/dbus_connection.h" | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 27 |  | 
|  | 28 | namespace chromeos_update_engine { | 
|  | 29 |  | 
| Jeffrey Kardatzke | cf5f1f1 | 2017-10-02 16:08:44 -0700 | [diff] [blame] | 30 | ChromeBrowserProxyResolver::ChromeBrowserProxyResolver() | 
|  | 31 | : next_request_id_(kProxyRequestIdNull + 1), | 
| Daniel Erat | 941cf23 | 2017-04-20 12:09:58 -0600 | [diff] [blame] | 32 | weak_ptr_factory_(this) {} | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 33 |  | 
| Daniel Erat | 941cf23 | 2017-04-20 12:09:58 -0600 | [diff] [blame] | 34 | ChromeBrowserProxyResolver::~ChromeBrowserProxyResolver() = default; | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 35 |  | 
| Daniel Erat | 941cf23 | 2017-04-20 12:09:58 -0600 | [diff] [blame] | 36 | ProxyRequestId ChromeBrowserProxyResolver::GetProxiesForUrl( | 
| Jeffrey Kardatzke | cf5f1f1 | 2017-10-02 16:08:44 -0700 | [diff] [blame] | 37 | const std::string& url, const ProxiesResolvedFn& callback) { | 
| Daniel Erat | 941cf23 | 2017-04-20 12:09:58 -0600 | [diff] [blame] | 38 | const ProxyRequestId id = next_request_id_++; | 
| Jeffrey Kardatzke | cf5f1f1 | 2017-10-02 16:08:44 -0700 | [diff] [blame] | 39 | brillo::http::GetChromeProxyServersAsync( | 
|  | 40 | DBusConnection::Get()->GetDBus(), url, | 
|  | 41 | base::Bind(&ChromeBrowserProxyResolver::OnGetChromeProxyServers, | 
|  | 42 | weak_ptr_factory_.GetWeakPtr(), id)); | 
| Daniel Erat | 941cf23 | 2017-04-20 12:09:58 -0600 | [diff] [blame] | 43 | pending_callbacks_[id] = callback; | 
|  | 44 | return id; | 
|  | 45 | } | 
|  | 46 |  | 
|  | 47 | bool ChromeBrowserProxyResolver::CancelProxyRequest(ProxyRequestId request) { | 
|  | 48 | return pending_callbacks_.erase(request) != 0; | 
|  | 49 | } | 
|  | 50 |  | 
| Jeffrey Kardatzke | cf5f1f1 | 2017-10-02 16:08:44 -0700 | [diff] [blame] | 51 | void ChromeBrowserProxyResolver::OnGetChromeProxyServers( | 
|  | 52 | ProxyRequestId request_id, bool success, | 
|  | 53 | const std::vector<std::string>& proxies) { | 
|  | 54 | // If |success| is false, |proxies| will still hold the direct proxy option | 
|  | 55 | // which is what we do in our error case. | 
| Daniel Erat | 941cf23 | 2017-04-20 12:09:58 -0600 | [diff] [blame] | 56 | auto it = pending_callbacks_.find(request_id); | 
|  | 57 | if (it == pending_callbacks_.end()) | 
|  | 58 | return; | 
|  | 59 |  | 
|  | 60 | ProxiesResolvedFn callback = it->second; | 
|  | 61 | pending_callbacks_.erase(it); | 
| Jeffrey Kardatzke | cf5f1f1 | 2017-10-02 16:08:44 -0700 | [diff] [blame] | 62 | callback.Run(std::deque<std::string>(proxies.begin(), proxies.end())); | 
| Daniel Erat | 941cf23 | 2017-04-20 12:09:58 -0600 | [diff] [blame] | 63 | } | 
|  | 64 |  | 
| Jeffrey Kardatzke | cf5f1f1 | 2017-10-02 16:08:44 -0700 | [diff] [blame] | 65 | }  // namespace chromeos_update_engine |