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"
12 MockAsyncProxyResolver::Request::Request(MockAsyncProxyResolver
* resolver
,
15 const CompletionCallback
& callback
)
16 : resolver_(resolver
),
20 origin_loop_(base::MessageLoop::current()) {
23 void MockAsyncProxyResolver::Request::CompleteNow(int rv
) {
24 CompletionCallback callback
= callback_
;
27 resolver_
->RemovePendingRequest(this);
32 MockAsyncProxyResolver::Request::~Request() {
35 MockAsyncProxyResolver::~MockAsyncProxyResolver() {
38 int MockAsyncProxyResolver::GetProxyForURL(
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
);
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
)
81 script_data_(script_data
),
86 MockAsyncProxyResolverFactory::Request::~Request() {
89 void MockAsyncProxyResolverFactory::Request::CompleteNow(
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);
101 void MockAsyncProxyResolverFactory::Request::CompleteNowWithForwarder(
103 ProxyResolver
* resolver
) {
105 CompleteNow(rv
, make_scoped_ptr(new ForwardingProxyResolver(resolver
)));
108 void MockAsyncProxyResolverFactory::Request::FactoryDestroyed() {
112 class MockAsyncProxyResolverFactory::Job
113 : public ProxyResolverFactory::Request
{
116 const scoped_refptr
<MockAsyncProxyResolverFactory::Request
>& request
)
117 : request_(request
) {}
119 if (request_
->factory_
) {
120 request_
->factory_
->cancelled_requests_
.push_back(request_
);
121 request_
->factory_
->RemovePendingRequest(request_
.get());
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
)
166 int ForwardingProxyResolver::GetProxyForURL(const GURL
& query_url
,
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
);