Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / net / proxy / mock_proxy_resolver.cc
blob9c5cb036107e782f48372d596cebbb036f155fe1
1 // Copyright (c) 2011 The Chromium 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.
5 #include "net/proxy/mock_proxy_resolver.h"
7 #include "base/logging.h"
8 #include "base/message_loop/message_loop.h"
10 namespace net {
12 MockAsyncProxyResolver::Request::Request(MockAsyncProxyResolver* resolver,
13 const GURL& url,
14 ProxyInfo* results,
15 const CompletionCallback& callback)
16 : resolver_(resolver),
17 url_(url),
18 results_(results),
19 callback_(callback),
20 origin_loop_(base::MessageLoop::current()) {
23 void MockAsyncProxyResolver::Request::CompleteNow(int rv) {
24 CompletionCallback callback = callback_;
26 // May delete |this|.
27 resolver_->RemovePendingRequest(this);
29 callback.Run(rv);
32 MockAsyncProxyResolver::Request::~Request() {
35 MockAsyncProxyResolver::~MockAsyncProxyResolver() {
38 int MockAsyncProxyResolver::GetProxyForURL(
39 const GURL& url,
40 ProxyInfo* results,
41 const CompletionCallback& callback,
42 RequestHandle* request_handle,
43 const BoundNetLog& /*net_log*/) {
44 scoped_refptr<Request> request = new Request(this, url, results, callback);
45 pending_requests_.push_back(request);
47 if (request_handle)
48 *request_handle = reinterpret_cast<RequestHandle>(request.get());
50 // Test code completes the request by calling request->CompleteNow().
51 return ERR_IO_PENDING;
54 void MockAsyncProxyResolver::CancelRequest(RequestHandle request_handle) {
55 scoped_refptr<Request> request = reinterpret_cast<Request*>(request_handle);
56 cancelled_requests_.push_back(request);
57 RemovePendingRequest(request.get());
60 LoadState MockAsyncProxyResolver::GetLoadState(
61 RequestHandle request_handle) const {
62 return LOAD_STATE_RESOLVING_PROXY_FOR_URL;
65 void MockAsyncProxyResolver::RemovePendingRequest(Request* request) {
66 RequestsList::iterator it = std::find(
67 pending_requests_.begin(), pending_requests_.end(), request);
68 DCHECK(it != pending_requests_.end());
69 pending_requests_.erase(it);
72 MockAsyncProxyResolver::MockAsyncProxyResolver() {
75 MockAsyncProxyResolverFactory::Request::Request(
76 MockAsyncProxyResolverFactory* factory,
77 const scoped_refptr<ProxyResolverScriptData>& script_data,
78 scoped_ptr<ProxyResolver>* resolver,
79 const CompletionCallback& callback)
80 : factory_(factory),
81 script_data_(script_data),
82 resolver_(resolver),
83 callback_(callback) {
86 MockAsyncProxyResolverFactory::Request::~Request() {
89 void MockAsyncProxyResolverFactory::Request::CompleteNow(
90 int rv,
91 scoped_ptr<ProxyResolver> resolver) {
92 *resolver_ = resolver.Pass();
94 // RemovePendingRequest may remove the last external reference to |this|.
95 scoped_refptr<MockAsyncProxyResolverFactory::Request> keep_alive(this);
96 factory_->RemovePendingRequest(this);
97 factory_ = nullptr;
98 callback_.Run(rv);
101 void MockAsyncProxyResolverFactory::Request::CompleteNowWithForwarder(
102 int rv,
103 ProxyResolver* resolver) {
104 DCHECK(resolver);
105 CompleteNow(rv, make_scoped_ptr(new ForwardingProxyResolver(resolver)));
108 void MockAsyncProxyResolverFactory::Request::FactoryDestroyed() {
109 factory_ = nullptr;
112 class MockAsyncProxyResolverFactory::Job
113 : public ProxyResolverFactory::Request {
114 public:
115 explicit Job(
116 const scoped_refptr<MockAsyncProxyResolverFactory::Request>& request)
117 : request_(request) {}
118 ~Job() override {
119 if (request_->factory_) {
120 request_->factory_->cancelled_requests_.push_back(request_);
121 request_->factory_->RemovePendingRequest(request_.get());
125 private:
126 scoped_refptr<MockAsyncProxyResolverFactory::Request> request_;
129 MockAsyncProxyResolverFactory::MockAsyncProxyResolverFactory(
130 bool resolvers_expect_pac_bytes)
131 : ProxyResolverFactory(resolvers_expect_pac_bytes) {
134 int MockAsyncProxyResolverFactory::CreateProxyResolver(
135 const scoped_refptr<ProxyResolverScriptData>& pac_script,
136 scoped_ptr<ProxyResolver>* resolver,
137 const net::CompletionCallback& callback,
138 scoped_ptr<ProxyResolverFactory::Request>* request_handle) {
139 scoped_refptr<Request> request =
140 new Request(this, pac_script, resolver, callback);
141 pending_requests_.push_back(request);
143 request_handle->reset(new Job(request));
145 // Test code completes the request by calling request->CompleteNow().
146 return ERR_IO_PENDING;
149 void MockAsyncProxyResolverFactory::RemovePendingRequest(Request* request) {
150 RequestsList::iterator it =
151 std::find(pending_requests_.begin(), pending_requests_.end(), request);
152 DCHECK(it != pending_requests_.end());
153 pending_requests_.erase(it);
156 MockAsyncProxyResolverFactory::~MockAsyncProxyResolverFactory() {
157 for (auto& request : pending_requests_) {
158 request->FactoryDestroyed();
162 ForwardingProxyResolver::ForwardingProxyResolver(ProxyResolver* impl)
163 : impl_(impl) {
166 int ForwardingProxyResolver::GetProxyForURL(const GURL& query_url,
167 ProxyInfo* results,
168 const CompletionCallback& callback,
169 RequestHandle* request,
170 const BoundNetLog& net_log) {
171 return impl_->GetProxyForURL(query_url, results, callback, request, net_log);
174 void ForwardingProxyResolver::CancelRequest(RequestHandle request) {
175 impl_->CancelRequest(request);
178 LoadState ForwardingProxyResolver::GetLoadState(RequestHandle request) const {
179 return impl_->GetLoadState(request);
182 } // namespace net