blob: 2ec59dbf7b35cf752c10b5f67b746aeb619cb69e [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
17#include "update_engine/proxy_resolver.h"
18
Alex Vakulenko4906c1c2014-08-21 13:17:44 -070019#include <base/bind.h>
Alex Deymo60ca1a72015-06-18 18:19:15 -070020#include <base/location.h>
Alex Vakulenko4906c1c2014-08-21 13:17:44 -070021
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070022using brillo::MessageLoop;
Andrew de los Reyes45168102010-11-22 11:13:50 -080023using std::deque;
Andrew de los Reyes9cd120d2010-11-18 17:50:03 -080024using std::string;
Andrew de los Reyes9cd120d2010-11-18 17:50:03 -080025
26namespace chromeos_update_engine {
27
28const char kNoProxy[] = "direct://";
Alex Deymo35821942017-02-05 04:36:02 +000029const ProxyRequestId kProxyRequestIdNull = brillo::MessageLoop::kTaskIdNull;
Andrew de los Reyes9cd120d2010-11-18 17:50:03 -080030
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -080031DirectProxyResolver::~DirectProxyResolver() {
Alex Deymo60ca1a72015-06-18 18:19:15 -070032 if (idle_callback_id_ != MessageLoop::kTaskIdNull) {
33 // The DirectProxyResolver is instantiated as part of the UpdateAttempter
34 // which is also instantiated by default by the FakeSystemState, even when
35 // it is not used. We check the manage_shares_id_ before calling the
36 // MessageLoop::current() since the unit test using a FakeSystemState may
37 // have not define a MessageLoop for the current thread.
38 MessageLoop::current()->CancelTask(idle_callback_id_);
39 idle_callback_id_ = MessageLoop::kTaskIdNull;
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -080040 }
41}
42
Alex Deymo35821942017-02-05 04:36:02 +000043ProxyRequestId DirectProxyResolver::GetProxiesForUrl(
44 const string& url, const ProxiesResolvedFn& callback) {
Alex Deymo60ca1a72015-06-18 18:19:15 -070045 idle_callback_id_ = MessageLoop::current()->PostTask(
46 FROM_HERE,
Alex Deymo35821942017-02-05 04:36:02 +000047 base::Bind(&DirectProxyResolver::ReturnCallback,
48 base::Unretained(this),
49 callback));
50 return idle_callback_id_;
Andrew de los Reyes9cd120d2010-11-18 17:50:03 -080051}
52
Alex Deymo35821942017-02-05 04:36:02 +000053bool DirectProxyResolver::CancelProxyRequest(ProxyRequestId request) {
54 return MessageLoop::current()->CancelTask(request);
55}
56
57void DirectProxyResolver::ReturnCallback(const ProxiesResolvedFn& callback) {
Alex Deymo60ca1a72015-06-18 18:19:15 -070058 idle_callback_id_ = MessageLoop::kTaskIdNull;
Gilad Arnold9bedeb52011-11-17 16:19:57 -080059
60 // Initialize proxy pool with as many proxies as indicated (all identical).
Alex Deymof329b932014-10-30 01:37:48 -070061 deque<string> proxies(num_proxies_, kNoProxy);
Gilad Arnold9bedeb52011-11-17 16:19:57 -080062
Alex Deymo35821942017-02-05 04:36:02 +000063 callback.Run(proxies);
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -080064}
65
66
Andrew de los Reyes9cd120d2010-11-18 17:50:03 -080067} // namespace chromeos_update_engine