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/common/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 const ProxyRequestId kProxyRequestIdNull = brillo::MessageLoop::kTaskIdNull;
30 
~DirectProxyResolver()31 DirectProxyResolver::~DirectProxyResolver() {
32   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;
40   }
41 }
42 
GetProxiesForUrl(const string & url,const ProxiesResolvedFn & callback)43 ProxyRequestId DirectProxyResolver::GetProxiesForUrl(
44     const string& url, const ProxiesResolvedFn& callback) {
45   idle_callback_id_ = MessageLoop::current()->PostTask(
46       FROM_HERE,
47       base::Bind(&DirectProxyResolver::ReturnCallback,
48                  base::Unretained(this),
49                  callback));
50   return idle_callback_id_;
51 }
52 
CancelProxyRequest(ProxyRequestId request)53 bool DirectProxyResolver::CancelProxyRequest(ProxyRequestId request) {
54   return MessageLoop::current()->CancelTask(request);
55 }
56 
ReturnCallback(const ProxiesResolvedFn & callback)57 void DirectProxyResolver::ReturnCallback(const ProxiesResolvedFn& callback) {
58   idle_callback_id_ = MessageLoop::kTaskIdNull;
59 
60   // Initialize proxy pool with as many proxies as indicated (all identical).
61   deque<string> proxies(num_proxies_, kNoProxy);
62 
63   callback.Run(proxies);
64 }
65 
66 }  // namespace chromeos_update_engine
67