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 MockAsyncProxyResolverBase
: public ProxyResolver
{
26 class Request
: public base::RefCounted
<Request
> {
28 Request(MockAsyncProxyResolverBase
* 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 MockAsyncProxyResolverBase
* resolver_
;
47 CompletionCallback callback_
;
48 base::MessageLoop
* origin_loop_
;
51 class SetPacScriptRequest
{
54 MockAsyncProxyResolverBase
* resolver
,
55 const scoped_refptr
<ProxyResolverScriptData
>& script_data
,
56 const CompletionCallback
& callback
);
57 ~SetPacScriptRequest();
59 const ProxyResolverScriptData
* script_data() const {
60 return script_data_
.get();
63 void CompleteNow(int rv
);
66 MockAsyncProxyResolverBase
* resolver_
;
67 const scoped_refptr
<ProxyResolverScriptData
> script_data_
;
68 CompletionCallback callback_
;
69 base::MessageLoop
* origin_loop_
;
72 typedef std::vector
<scoped_refptr
<Request
> > RequestsList
;
74 ~MockAsyncProxyResolverBase() override
;
76 // ProxyResolver implementation.
77 int GetProxyForURL(const GURL
& url
,
79 const CompletionCallback
& callback
,
80 RequestHandle
* request_handle
,
81 const BoundNetLog
& /*net_log*/) override
;
82 void CancelRequest(RequestHandle request_handle
) override
;
83 LoadState
GetLoadState(RequestHandle request_handle
) const override
;
84 int SetPacScript(const scoped_refptr
<ProxyResolverScriptData
>& script_data
,
85 const CompletionCallback
& callback
) override
;
86 void CancelSetPacScript() override
;
88 const RequestsList
& pending_requests() const {
89 return pending_requests_
;
92 const RequestsList
& cancelled_requests() const {
93 return cancelled_requests_
;
96 SetPacScriptRequest
* pending_set_pac_script_request() const;
98 bool has_pending_set_pac_script_request() const {
99 return pending_set_pac_script_request_
.get() != NULL
;
102 void RemovePendingRequest(Request
* request
);
104 void RemovePendingSetPacScriptRequest(SetPacScriptRequest
* request
);
107 explicit MockAsyncProxyResolverBase(bool expects_pac_bytes
);
110 RequestsList pending_requests_
;
111 RequestsList cancelled_requests_
;
112 scoped_ptr
<SetPacScriptRequest
> pending_set_pac_script_request_
;
115 class MockAsyncProxyResolver
: public MockAsyncProxyResolverBase
{
117 MockAsyncProxyResolver()
118 : MockAsyncProxyResolverBase(false /*expects_pac_bytes*/) {}
121 class MockAsyncProxyResolverExpectsBytes
: public MockAsyncProxyResolverBase
{
123 MockAsyncProxyResolverExpectsBytes()
124 : MockAsyncProxyResolverBase(true /*expects_pac_bytes*/) {}
127 // This factory returns ProxyResolvers that forward all requests to
128 // |resolver|. |resolver| must remain so long as any value returned from
129 // CreateProxyResolver remains in use.
130 class ForwardingProxyResolverFactory
: public LegacyProxyResolverFactory
{
132 explicit ForwardingProxyResolverFactory(ProxyResolver
* resolver
);
134 // LegacyProxyResolverFactory override.
135 scoped_ptr
<ProxyResolver
> CreateProxyResolver() override
;
138 ProxyResolver
* resolver_
;
140 DISALLOW_COPY_AND_ASSIGN(ForwardingProxyResolverFactory
);
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
;
157 void CancelSetPacScript() override
;
158 int SetPacScript(const scoped_refptr
<ProxyResolverScriptData
>& script_data
,
159 const CompletionCallback
& callback
) override
;
162 ProxyResolver
* impl_
;
164 DISALLOW_COPY_AND_ASSIGN(ForwardingProxyResolver
);
169 #endif // NET_PROXY_MOCK_PROXY_RESOLVER_H_