blob: 2c8824ff268faeb5f2f706109089064657acac08 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2010 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 Reyes9cd120d2010-11-18 17:50:03 -080016
Gilad Arnoldcf175a02014-07-10 16:48:47 -070017#ifndef UPDATE_ENGINE_PROXY_RESOLVER_H_
18#define UPDATE_ENGINE_PROXY_RESOLVER_H_
Andrew de los Reyes9cd120d2010-11-18 17:50:03 -080019
Andrew de los Reyes45168102010-11-22 11:13:50 -080020#include <deque>
Andrew de los Reyes9cd120d2010-11-18 17:50:03 -080021#include <string>
Andrew de los Reyes9cd120d2010-11-18 17:50:03 -080022
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -080023#include <base/logging.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070024#include <brillo/message_loops/message_loop.h>
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -080025
Alex Deymo39910dc2015-11-09 17:04:30 -080026#include "update_engine/common/utils.h"
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -080027
Andrew de los Reyes9cd120d2010-11-18 17:50:03 -080028namespace chromeos_update_engine {
29
30extern const char kNoProxy[];
31
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -080032// Callback for a call to GetProxiesForUrl().
33// Resultant proxies are in |out_proxy|. Each will be in one of the
34// following forms:
35// http://<host>[:<port>] - HTTP proxy
36// socks{4,5}://<host>[:<port>] - SOCKS4/5 proxy
37// kNoProxy - no proxy
38typedef void (*ProxiesResolvedFn)(const std::deque<std::string>& proxies,
39 void* data);
40
Andrew de los Reyes9cd120d2010-11-18 17:50:03 -080041class ProxyResolver {
42 public:
43 ProxyResolver() {}
44 virtual ~ProxyResolver() {}
45
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -080046 // Finds proxies for the given URL and returns them via the callback.
47 // |data| will be passed to the callback.
48 // Returns true on success.
Andrew de los Reyes9cd120d2010-11-18 17:50:03 -080049 virtual bool GetProxiesForUrl(const std::string& url,
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -080050 ProxiesResolvedFn callback,
51 void* data) = 0;
Andrew de los Reyes9cd120d2010-11-18 17:50:03 -080052
53 private:
54 DISALLOW_COPY_AND_ASSIGN(ProxyResolver);
55};
56
57// Always says to not use a proxy
58class DirectProxyResolver : public ProxyResolver {
59 public:
Alex Deymo60ca1a72015-06-18 18:19:15 -070060 DirectProxyResolver() = default;
Alex Deymo610277e2014-11-11 21:18:11 -080061 ~DirectProxyResolver() override;
62 bool GetProxiesForUrl(const std::string& url,
63 ProxiesResolvedFn callback,
64 void* data) override;
Andrew de los Reyes9cd120d2010-11-18 17:50:03 -080065
Gilad Arnold9bedeb52011-11-17 16:19:57 -080066 // Set the number of direct (non-) proxies to be returned by resolver.
67 // The default value is 1; higher numbers are currently used in testing.
68 inline void set_num_proxies(size_t num_proxies) {
69 num_proxies_ = num_proxies;
70 }
71
Andrew de los Reyes9cd120d2010-11-18 17:50:03 -080072 private:
Alex Deymo60ca1a72015-06-18 18:19:15 -070073 // The ID of the main loop callback.
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070074 brillo::MessageLoop::TaskId idle_callback_id_{
75 brillo::MessageLoop::kTaskIdNull};
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -080076
Gilad Arnold9bedeb52011-11-17 16:19:57 -080077 // Number of direct proxies to return on resolved list; currently used for
78 // testing.
Alex Deymo60ca1a72015-06-18 18:19:15 -070079 size_t num_proxies_{1};
Gilad Arnold9bedeb52011-11-17 16:19:57 -080080
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -080081 // The MainLoop callback, from here we return to the client.
82 void ReturnCallback(ProxiesResolvedFn callback, void* data);
Andrew de los Reyes9cd120d2010-11-18 17:50:03 -080083 DISALLOW_COPY_AND_ASSIGN(DirectProxyResolver);
84};
85
86} // namespace chromeos_update_engine
87
Gilad Arnoldcf175a02014-07-10 16:48:47 -070088#endif // UPDATE_ENGINE_PROXY_RESOLVER_H_