Probably broke Win7 Tests (dbg)(6). http://build.chromium.org/p/chromium.win/builders...
[chromium-blink-merge.git] / net / proxy / mock_proxy_resolver.h
blob3864d530de94e6eb6d35ab2941153a13105955bc
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_
8 #include <vector>
10 #include "base/memory/scoped_ptr.h"
11 #include "net/base/net_errors.h"
12 #include "net/proxy/proxy_resolver.h"
13 #include "url/gurl.h"
15 namespace base {
16 class MessageLoop;
19 namespace net {
21 // Asynchronous mock proxy resolver. All requests complete asynchronously,
22 // user must call Request::CompleteNow() on a pending request to signal it.
23 class MockAsyncProxyResolverBase : public ProxyResolver {
24 public:
25 class Request : public base::RefCounted<Request> {
26 public:
27 Request(MockAsyncProxyResolverBase* resolver,
28 const GURL& url,
29 ProxyInfo* results,
30 const net::CompletionCallback& callback);
32 const GURL& url() const { return url_; }
33 ProxyInfo* results() const { return results_; }
34 const net::CompletionCallback& callback() const { return callback_; }
36 void CompleteNow(int rv);
38 private:
39 friend class base::RefCounted<Request>;
41 virtual ~Request();
43 MockAsyncProxyResolverBase* resolver_;
44 const GURL url_;
45 ProxyInfo* results_;
46 net::CompletionCallback callback_;
47 base::MessageLoop* origin_loop_;
50 class SetPacScriptRequest {
51 public:
52 SetPacScriptRequest(
53 MockAsyncProxyResolverBase* resolver,
54 const scoped_refptr<ProxyResolverScriptData>& script_data,
55 const net::CompletionCallback& callback);
56 ~SetPacScriptRequest();
58 const ProxyResolverScriptData* script_data() const {
59 return script_data_.get();
62 void CompleteNow(int rv);
64 private:
65 MockAsyncProxyResolverBase* resolver_;
66 const scoped_refptr<ProxyResolverScriptData> script_data_;
67 net::CompletionCallback callback_;
68 base::MessageLoop* origin_loop_;
71 typedef std::vector<scoped_refptr<Request> > RequestsList;
73 virtual ~MockAsyncProxyResolverBase();
75 // ProxyResolver implementation.
76 virtual int GetProxyForURL(const GURL& url,
77 ProxyInfo* results,
78 const net::CompletionCallback& callback,
79 RequestHandle* request_handle,
80 const BoundNetLog& /*net_log*/) OVERRIDE;
81 virtual void CancelRequest(RequestHandle request_handle) OVERRIDE;
82 virtual LoadState GetLoadState(RequestHandle request_handle) const OVERRIDE;
83 virtual int SetPacScript(
84 const scoped_refptr<ProxyResolverScriptData>& script_data,
85 const net::CompletionCallback& callback) OVERRIDE;
86 virtual 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);
106 protected:
107 explicit MockAsyncProxyResolverBase(bool expects_pac_bytes);
109 private:
110 RequestsList pending_requests_;
111 RequestsList cancelled_requests_;
112 scoped_ptr<SetPacScriptRequest> pending_set_pac_script_request_;
115 class MockAsyncProxyResolver : public MockAsyncProxyResolverBase {
116 public:
117 MockAsyncProxyResolver()
118 : MockAsyncProxyResolverBase(false /*expects_pac_bytes*/) {}
121 class MockAsyncProxyResolverExpectsBytes : public MockAsyncProxyResolverBase {
122 public:
123 MockAsyncProxyResolverExpectsBytes()
124 : MockAsyncProxyResolverBase(true /*expects_pac_bytes*/) {}
127 } // namespace net
129 #endif // NET_PROXY_MOCK_PROXY_RESOLVER_H_