1 //
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 //
16
17 #include "update_engine/proxy_resolver.h"
18
19 #include <base/bind.h>
20 #include <base/location.h>
21
22 using brillo::MessageLoop;
23 using std::deque;
24 using std::string;
25
26 namespace chromeos_update_engine {
27
28 const char kNoProxy[] = "direct://";
29
~DirectProxyResolver()30 DirectProxyResolver::~DirectProxyResolver() {
31 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;
39 }
40 }
41
GetProxiesForUrl(const string & url,ProxiesResolvedFn callback,void * data)42 bool DirectProxyResolver::GetProxiesForUrl(const string& url,
43 ProxiesResolvedFn callback,
44 void* data) {
45 idle_callback_id_ = MessageLoop::current()->PostTask(
46 FROM_HERE,
47 base::Bind(
48 &DirectProxyResolver::ReturnCallback,
49 base::Unretained(this),
50 callback,
51 data));
52 return true;
53 }
54
ReturnCallback(ProxiesResolvedFn callback,void * data)55 void DirectProxyResolver::ReturnCallback(ProxiesResolvedFn callback,
56 void* data) {
57 idle_callback_id_ = MessageLoop::kTaskIdNull;
58
59 // Initialize proxy pool with as many proxies as indicated (all identical).
60 deque<string> proxies(num_proxies_, kNoProxy);
61
62 (*callback)(proxies, data);
63 }
64
65
66 } // namespace chromeos_update_engine
67