blob: abd6f76b3296d4a4a5bf994c4df5ce15774e7d9d [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://";
29
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -080030DirectProxyResolver::~DirectProxyResolver() {
Alex Deymo60ca1a72015-06-18 18:19:15 -070031 if (idle_callback_id_ != MessageLoop::kTaskIdNull) {
32 // The DirectProxyResolver is instantiated as part of the UpdateAttempter
33 // which is also instantiated by default by the FakeSystemState, even when
34 // it is not used. We check the manage_shares_id_ before calling the
35 // MessageLoop::current() since the unit test using a FakeSystemState may
36 // have not define a MessageLoop for the current thread.
37 MessageLoop::current()->CancelTask(idle_callback_id_);
38 idle_callback_id_ = MessageLoop::kTaskIdNull;
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -080039 }
40}
41
Alex Deymof329b932014-10-30 01:37:48 -070042bool DirectProxyResolver::GetProxiesForUrl(const string& url,
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -080043 ProxiesResolvedFn callback,
44 void* data) {
Alex Deymo60ca1a72015-06-18 18:19:15 -070045 idle_callback_id_ = MessageLoop::current()->PostTask(
46 FROM_HERE,
47 base::Bind(
48 &DirectProxyResolver::ReturnCallback,
49 base::Unretained(this),
50 callback,
51 data));
Andrew de los Reyes9cd120d2010-11-18 17:50:03 -080052 return true;
53}
54
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -080055void DirectProxyResolver::ReturnCallback(ProxiesResolvedFn callback,
56 void* data) {
Alex Deymo60ca1a72015-06-18 18:19:15 -070057 idle_callback_id_ = MessageLoop::kTaskIdNull;
Gilad Arnold9bedeb52011-11-17 16:19:57 -080058
59 // Initialize proxy pool with as many proxies as indicated (all identical).
Alex Deymof329b932014-10-30 01:37:48 -070060 deque<string> proxies(num_proxies_, kNoProxy);
Gilad Arnold9bedeb52011-11-17 16:19:57 -080061
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -080062 (*callback)(proxies, data);
63}
64
65
Andrew de los Reyes9cd120d2010-11-18 17:50:03 -080066} // namespace chromeos_update_engine