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
22using brillo::MessageLoop;
23using std::deque;
24using std::string;
25
26namespace chromeos_update_engine {
27
28const char kNoProxy[] = "direct://";
29
30DirectProxyResolver::~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
42bool 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
55void 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