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 "googleurl/src/gurl.h"
12 #include "net/base/net_errors.h"
13 #include "net/proxy/proxy_resolver.h"
19 // Asynchronous mock proxy resolver. All requests complete asynchronously,
20 // user must call Request::CompleteNow() on a pending request to signal it.
21 class MockAsyncProxyResolverBase
: public ProxyResolver
{
23 class Request
: public base::RefCounted
<Request
> {
25 Request(MockAsyncProxyResolverBase
* resolver
,
28 const net::CompletionCallback
& callback
);
30 const GURL
& url() const { return url_
; }
31 ProxyInfo
* results() const { return results_
; }
32 const net::CompletionCallback
& callback() const { return callback_
; }
34 void CompleteNow(int rv
);
37 friend class base::RefCounted
<Request
>;
41 MockAsyncProxyResolverBase
* resolver_
;
44 net::CompletionCallback callback_
;
45 MessageLoop
* origin_loop_
;
48 class SetPacScriptRequest
{
51 MockAsyncProxyResolverBase
* resolver
,
52 const scoped_refptr
<ProxyResolverScriptData
>& script_data
,
53 const net::CompletionCallback
& callback
);
54 ~SetPacScriptRequest();
56 const ProxyResolverScriptData
* script_data() const { return script_data_
; }
58 void CompleteNow(int rv
);
61 MockAsyncProxyResolverBase
* resolver_
;
62 const scoped_refptr
<ProxyResolverScriptData
> script_data_
;
63 net::CompletionCallback callback_
;
64 MessageLoop
* origin_loop_
;
67 typedef std::vector
<scoped_refptr
<Request
> > RequestsList
;
69 virtual ~MockAsyncProxyResolverBase();
71 // ProxyResolver implementation.
72 virtual int GetProxyForURL(const GURL
& url
,
74 const net::CompletionCallback
& callback
,
75 RequestHandle
* request_handle
,
76 const BoundNetLog
& /*net_log*/) OVERRIDE
;
77 virtual void CancelRequest(RequestHandle request_handle
) OVERRIDE
;
78 virtual LoadState
GetLoadState(RequestHandle request_handle
) const OVERRIDE
;
79 virtual LoadState
GetLoadStateThreadSafe(
80 RequestHandle request_handle
) const OVERRIDE
;
81 virtual int SetPacScript(
82 const scoped_refptr
<ProxyResolverScriptData
>& script_data
,
83 const net::CompletionCallback
& callback
) OVERRIDE
;
84 virtual void CancelSetPacScript() OVERRIDE
;
86 const RequestsList
& pending_requests() const {
87 return pending_requests_
;
90 const RequestsList
& cancelled_requests() const {
91 return cancelled_requests_
;
94 SetPacScriptRequest
* pending_set_pac_script_request() const;
96 bool has_pending_set_pac_script_request() const {
97 return pending_set_pac_script_request_
.get() != NULL
;
100 void RemovePendingRequest(Request
* request
);
102 void RemovePendingSetPacScriptRequest(SetPacScriptRequest
* request
);
105 explicit MockAsyncProxyResolverBase(bool expects_pac_bytes
);
108 RequestsList pending_requests_
;
109 RequestsList cancelled_requests_
;
110 scoped_ptr
<SetPacScriptRequest
> pending_set_pac_script_request_
;
113 class MockAsyncProxyResolver
: public MockAsyncProxyResolverBase
{
115 MockAsyncProxyResolver()
116 : MockAsyncProxyResolverBase(false /*expects_pac_bytes*/) {}
119 class MockAsyncProxyResolverExpectsBytes
: public MockAsyncProxyResolverBase
{
121 MockAsyncProxyResolverExpectsBytes()
122 : MockAsyncProxyResolverBase(true /*expects_pac_bytes*/) {}
127 #endif // NET_PROXY_MOCK_PROXY_RESOLVER_H_