1 // Copyright (c) 2012 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 #ifndef NET_PROXY_MOCK_PROXY_RESOLVER_H_
6 #define NET_PROXY_MOCK_PROXY_RESOLVER_H_
10 #include "base/memory/scoped_ptr.h"
11 #include "net/base/net_errors.h"
12 #include "net/proxy/proxy_resolver.h"
13 #include "net/proxy/proxy_resolver_factory.h"
22 // Asynchronous mock proxy resolver. All requests complete asynchronously,
23 // user must call Request::CompleteNow() on a pending request to signal it.
24 class MockAsyncProxyResolver
: public ProxyResolver
{
26 class Request
: public base::RefCounted
<Request
> {
28 Request(MockAsyncProxyResolver
* resolver
,
31 const CompletionCallback
& callback
);
33 const GURL
& url() const { return url_
; }
34 ProxyInfo
* results() const { return results_
; }
35 const CompletionCallback
& callback() const { return callback_
; }
37 void CompleteNow(int rv
);
40 friend class base::RefCounted
<Request
>;
44 MockAsyncProxyResolver
* resolver_
;
47 CompletionCallback callback_
;
48 base::MessageLoop
* origin_loop_
;
51 typedef std::vector
<scoped_refptr
<Request
> > RequestsList
;
53 MockAsyncProxyResolver();
54 ~MockAsyncProxyResolver() override
;
56 // ProxyResolver implementation.
57 int GetProxyForURL(const GURL
& url
,
59 const CompletionCallback
& callback
,
60 RequestHandle
* request_handle
,
61 const BoundNetLog
& /*net_log*/) override
;
62 void CancelRequest(RequestHandle request_handle
) override
;
63 LoadState
GetLoadState(RequestHandle request_handle
) const override
;
64 const RequestsList
& pending_requests() const {
65 return pending_requests_
;
68 const RequestsList
& cancelled_requests() const {
69 return cancelled_requests_
;
72 void RemovePendingRequest(Request
* request
);
75 RequestsList pending_requests_
;
76 RequestsList cancelled_requests_
;
79 // Asynchronous mock proxy resolver factory . All requests complete
80 // asynchronously; the user must call Request::CompleteNow() on a pending
81 // request to signal it.
82 class MockAsyncProxyResolverFactory
: public ProxyResolverFactory
{
85 using RequestsList
= std::vector
<scoped_refptr
<Request
>>;
87 explicit MockAsyncProxyResolverFactory(bool resolvers_expect_pac_bytes
);
88 ~MockAsyncProxyResolverFactory() override
;
90 int CreateProxyResolver(
91 const scoped_refptr
<ProxyResolverScriptData
>& pac_script
,
92 scoped_ptr
<ProxyResolver
>* resolver
,
93 const CompletionCallback
& callback
,
94 scoped_ptr
<ProxyResolverFactory::Request
>* request
) override
;
96 const RequestsList
& pending_requests() const { return pending_requests_
; }
98 const RequestsList
& cancelled_requests() const { return cancelled_requests_
; }
100 void RemovePendingRequest(Request
* request
);
104 RequestsList pending_requests_
;
105 RequestsList cancelled_requests_
;
108 class MockAsyncProxyResolverFactory::Request
109 : public base::RefCounted
<Request
> {
111 Request(MockAsyncProxyResolverFactory
* factory
,
112 const scoped_refptr
<ProxyResolverScriptData
>& script_data
,
113 scoped_ptr
<ProxyResolver
>* resolver
,
114 const CompletionCallback
& callback
);
116 const scoped_refptr
<ProxyResolverScriptData
>& script_data() const {
120 // Completes this request. A ForwardingProxyResolver that forwards to
121 // |resolver| will be returned to the requester. |resolver| must not be
122 // null and must remain as long as the resolver returned by this request
124 void CompleteNowWithForwarder(int rv
, ProxyResolver
* resolver
);
126 void CompleteNow(int rv
, scoped_ptr
<ProxyResolver
> resolver
);
129 friend class base::RefCounted
<Request
>;
130 friend class MockAsyncProxyResolverFactory
;
131 friend class MockAsyncProxyResolverFactory::Job
;
135 void FactoryDestroyed();
137 MockAsyncProxyResolverFactory
* factory_
;
138 const scoped_refptr
<ProxyResolverScriptData
> script_data_
;
139 scoped_ptr
<ProxyResolver
>* resolver_
;
140 CompletionCallback callback_
;
143 // ForwardingProxyResolver forwards all requests to |impl|. |impl| must remain
144 // so long as this remains in use.
145 class ForwardingProxyResolver
: public ProxyResolver
{
147 explicit ForwardingProxyResolver(ProxyResolver
* impl
);
149 // ProxyResolver overrides.
150 int GetProxyForURL(const GURL
& query_url
,
152 const CompletionCallback
& callback
,
153 RequestHandle
* request
,
154 const BoundNetLog
& net_log
) override
;
155 void CancelRequest(RequestHandle request
) override
;
156 LoadState
GetLoadState(RequestHandle request
) const override
;
159 ProxyResolver
* impl_
;
161 DISALLOW_COPY_AND_ASSIGN(ForwardingProxyResolver
);
166 #endif // NET_PROXY_MOCK_PROXY_RESOLVER_H_