blob: 8b8a9c8cfdde5a3a4b79012578c59ab85f810928 [file] [log] [blame]
Andrew de los Reyes9cd120d2010-11-18 17:50:03 -08001// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "update_engine/proxy_resolver.h"
6
Alex Vakulenko4906c1c2014-08-21 13:17:44 -07007#include <base/bind.h>
Alex Deymo60ca1a72015-06-18 18:19:15 -07008#include <base/location.h>
Alex Vakulenko4906c1c2014-08-21 13:17:44 -07009
Alex Deymo60ca1a72015-06-18 18:19:15 -070010using chromeos::MessageLoop;
Andrew de los Reyes45168102010-11-22 11:13:50 -080011using std::deque;
Andrew de los Reyes9cd120d2010-11-18 17:50:03 -080012using std::string;
Andrew de los Reyes9cd120d2010-11-18 17:50:03 -080013
14namespace chromeos_update_engine {
15
16const char kNoProxy[] = "direct://";
17
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -080018DirectProxyResolver::~DirectProxyResolver() {
Alex Deymo60ca1a72015-06-18 18:19:15 -070019 if (idle_callback_id_ != MessageLoop::kTaskIdNull) {
20 // The DirectProxyResolver is instantiated as part of the UpdateAttempter
21 // which is also instantiated by default by the FakeSystemState, even when
22 // it is not used. We check the manage_shares_id_ before calling the
23 // MessageLoop::current() since the unit test using a FakeSystemState may
24 // have not define a MessageLoop for the current thread.
25 MessageLoop::current()->CancelTask(idle_callback_id_);
26 idle_callback_id_ = MessageLoop::kTaskIdNull;
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -080027 }
28}
29
Alex Deymof329b932014-10-30 01:37:48 -070030bool DirectProxyResolver::GetProxiesForUrl(const string& url,
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -080031 ProxiesResolvedFn callback,
32 void* data) {
Alex Deymo60ca1a72015-06-18 18:19:15 -070033 idle_callback_id_ = MessageLoop::current()->PostTask(
34 FROM_HERE,
35 base::Bind(
36 &DirectProxyResolver::ReturnCallback,
37 base::Unretained(this),
38 callback,
39 data));
Andrew de los Reyes9cd120d2010-11-18 17:50:03 -080040 return true;
41}
42
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -080043void DirectProxyResolver::ReturnCallback(ProxiesResolvedFn callback,
44 void* data) {
Alex Deymo60ca1a72015-06-18 18:19:15 -070045 idle_callback_id_ = MessageLoop::kTaskIdNull;
Gilad Arnold9bedeb52011-11-17 16:19:57 -080046
47 // Initialize proxy pool with as many proxies as indicated (all identical).
Alex Deymof329b932014-10-30 01:37:48 -070048 deque<string> proxies(num_proxies_, kNoProxy);
Gilad Arnold9bedeb52011-11-17 16:19:57 -080049
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -080050 (*callback)(proxies, data);
51}
52
53
Andrew de los Reyes9cd120d2010-11-18 17:50:03 -080054} // namespace chromeos_update_engine