blob: bfb58f7bfbbbf12ead3f5da8560339688cf5fb82 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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 Reyes000d8952011-03-02 15:21:14 -080016
17#include "update_engine/chrome_browser_proxy_resolver.h"
18
Daniel Erat941cf232017-04-20 12:09:58 -060019#include <utility>
Andrew de los Reyes000d8952011-03-02 15:21:14 -080020
Alex Vakulenko4906c1c2014-08-21 13:17:44 -070021#include <base/bind.h>
Daniel Erat941cf232017-04-20 12:09:58 -060022#include <base/memory/ptr_util.h>
Alex Deymoc4acdf42014-05-28 21:07:10 -070023#include <base/strings/string_util.h>
Jeffrey Kardatzkecf5f1f12017-10-02 16:08:44 -070024#include <brillo/http/http_proxy.h>
Andrew de los Reyes000d8952011-03-02 15:21:14 -080025
Jeffrey Kardatzkecf5f1f12017-10-02 16:08:44 -070026#include "update_engine/dbus_connection.h"
Andrew de los Reyes000d8952011-03-02 15:21:14 -080027
28namespace chromeos_update_engine {
29
Jeffrey Kardatzkecf5f1f12017-10-02 16:08:44 -070030ChromeBrowserProxyResolver::ChromeBrowserProxyResolver()
Amin Hassani7cc8bb02019-01-14 16:29:47 -080031 : next_request_id_(kProxyRequestIdNull + 1), weak_ptr_factory_(this) {}
Andrew de los Reyes000d8952011-03-02 15:21:14 -080032
Daniel Erat941cf232017-04-20 12:09:58 -060033ChromeBrowserProxyResolver::~ChromeBrowserProxyResolver() = default;
Andrew de los Reyes000d8952011-03-02 15:21:14 -080034
Daniel Erat941cf232017-04-20 12:09:58 -060035ProxyRequestId ChromeBrowserProxyResolver::GetProxiesForUrl(
Jeffrey Kardatzkecf5f1f12017-10-02 16:08:44 -070036 const std::string& url, const ProxiesResolvedFn& callback) {
Daniel Erat941cf232017-04-20 12:09:58 -060037 const ProxyRequestId id = next_request_id_++;
Jeffrey Kardatzkecf5f1f12017-10-02 16:08:44 -070038 brillo::http::GetChromeProxyServersAsync(
Amin Hassani7cc8bb02019-01-14 16:29:47 -080039 DBusConnection::Get()->GetDBus(),
40 url,
Jeffrey Kardatzkecf5f1f12017-10-02 16:08:44 -070041 base::Bind(&ChromeBrowserProxyResolver::OnGetChromeProxyServers,
Amin Hassani7cc8bb02019-01-14 16:29:47 -080042 weak_ptr_factory_.GetWeakPtr(),
43 id));
Daniel Erat941cf232017-04-20 12:09:58 -060044 pending_callbacks_[id] = callback;
45 return id;
46}
47
48bool ChromeBrowserProxyResolver::CancelProxyRequest(ProxyRequestId request) {
49 return pending_callbacks_.erase(request) != 0;
50}
51
Jeffrey Kardatzkecf5f1f12017-10-02 16:08:44 -070052void ChromeBrowserProxyResolver::OnGetChromeProxyServers(
Amin Hassani7cc8bb02019-01-14 16:29:47 -080053 ProxyRequestId request_id,
54 bool success,
Jeffrey Kardatzkecf5f1f12017-10-02 16:08:44 -070055 const std::vector<std::string>& proxies) {
56 // If |success| is false, |proxies| will still hold the direct proxy option
57 // which is what we do in our error case.
Daniel Erat941cf232017-04-20 12:09:58 -060058 auto it = pending_callbacks_.find(request_id);
59 if (it == pending_callbacks_.end())
60 return;
61
62 ProxiesResolvedFn callback = it->second;
63 pending_callbacks_.erase(it);
Jeffrey Kardatzkecf5f1f12017-10-02 16:08:44 -070064 callback.Run(std::deque<std::string>(proxies.begin(), proxies.end()));
Daniel Erat941cf232017-04-20 12:09:58 -060065}
66
Jeffrey Kardatzkecf5f1f12017-10-02 16:08:44 -070067} // namespace chromeos_update_engine