OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_PROXY_MOCK_PROXY_RESOLVER_H_ | 5 #ifndef NET_PROXY_MOCK_PROXY_RESOLVER_H_ |
6 #define NET_PROXY_MOCK_PROXY_RESOLVER_H_ | 6 #define NET_PROXY_MOCK_PROXY_RESOLVER_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
12 #include "net/proxy/proxy_resolver.h" | 12 #include "net/proxy/proxy_resolver.h" |
13 #include "net/proxy/proxy_resolver_factory.h" | 13 #include "net/proxy/proxy_resolver_factory.h" |
14 #include "url/gurl.h" | 14 #include "url/gurl.h" |
15 | 15 |
16 namespace base { | 16 namespace base { |
17 class MessageLoop; | 17 class MessageLoop; |
18 } | 18 } |
19 | 19 |
20 namespace net { | 20 namespace net { |
21 | 21 |
22 // Asynchronous mock proxy resolver. All requests complete asynchronously, | 22 // Asynchronous mock proxy resolver. All requests complete asynchronously, |
23 // user must call Request::CompleteNow() on a pending request to signal it. | 23 // user must call Request::CompleteNow() on a pending request to signal it. |
24 class MockAsyncProxyResolverBase : public ProxyResolver { | 24 class MockAsyncProxyResolver : public ProxyResolver { |
25 public: | 25 public: |
26 class Request : public base::RefCounted<Request> { | 26 class Request : public base::RefCounted<Request> { |
27 public: | 27 public: |
28 Request(MockAsyncProxyResolverBase* resolver, | 28 Request(MockAsyncProxyResolver* resolver, |
29 const GURL& url, | 29 const GURL& url, |
30 ProxyInfo* results, | 30 ProxyInfo* results, |
31 const CompletionCallback& callback); | 31 const CompletionCallback& callback); |
32 | 32 |
33 const GURL& url() const { return url_; } | 33 const GURL& url() const { return url_; } |
34 ProxyInfo* results() const { return results_; } | 34 ProxyInfo* results() const { return results_; } |
35 const CompletionCallback& callback() const { return callback_; } | 35 const CompletionCallback& callback() const { return callback_; } |
36 | 36 |
37 void CompleteNow(int rv); | 37 void CompleteNow(int rv); |
38 | 38 |
39 private: | 39 private: |
40 friend class base::RefCounted<Request>; | 40 friend class base::RefCounted<Request>; |
41 | 41 |
42 virtual ~Request(); | 42 virtual ~Request(); |
43 | 43 |
44 MockAsyncProxyResolverBase* resolver_; | 44 MockAsyncProxyResolver* resolver_; |
45 const GURL url_; | 45 const GURL url_; |
46 ProxyInfo* results_; | 46 ProxyInfo* results_; |
47 CompletionCallback callback_; | 47 CompletionCallback callback_; |
48 base::MessageLoop* origin_loop_; | 48 base::MessageLoop* origin_loop_; |
49 }; | 49 }; |
50 | 50 |
51 class SetPacScriptRequest { | |
52 public: | |
53 SetPacScriptRequest( | |
54 MockAsyncProxyResolverBase* resolver, | |
55 const scoped_refptr<ProxyResolverScriptData>& script_data, | |
56 const CompletionCallback& callback); | |
57 ~SetPacScriptRequest(); | |
58 | |
59 const ProxyResolverScriptData* script_data() const { | |
60 return script_data_.get(); | |
61 } | |
62 | |
63 void CompleteNow(int rv); | |
64 | |
65 private: | |
66 MockAsyncProxyResolverBase* resolver_; | |
67 const scoped_refptr<ProxyResolverScriptData> script_data_; | |
68 CompletionCallback callback_; | |
69 base::MessageLoop* origin_loop_; | |
70 }; | |
71 | |
72 typedef std::vector<scoped_refptr<Request> > RequestsList; | 51 typedef std::vector<scoped_refptr<Request> > RequestsList; |
73 | 52 |
74 ~MockAsyncProxyResolverBase() override; | 53 MockAsyncProxyResolver(); |
| 54 ~MockAsyncProxyResolver() override; |
75 | 55 |
76 // ProxyResolver implementation. | 56 // ProxyResolver implementation. |
77 int GetProxyForURL(const GURL& url, | 57 int GetProxyForURL(const GURL& url, |
78 ProxyInfo* results, | 58 ProxyInfo* results, |
79 const CompletionCallback& callback, | 59 const CompletionCallback& callback, |
80 RequestHandle* request_handle, | 60 RequestHandle* request_handle, |
81 const BoundNetLog& /*net_log*/) override; | 61 const BoundNetLog& /*net_log*/) override; |
82 void CancelRequest(RequestHandle request_handle) override; | 62 void CancelRequest(RequestHandle request_handle) override; |
83 LoadState GetLoadState(RequestHandle request_handle) const override; | 63 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; | |
87 | |
88 const RequestsList& pending_requests() const { | 64 const RequestsList& pending_requests() const { |
89 return pending_requests_; | 65 return pending_requests_; |
90 } | 66 } |
91 | 67 |
92 const RequestsList& cancelled_requests() const { | 68 const RequestsList& cancelled_requests() const { |
93 return cancelled_requests_; | 69 return cancelled_requests_; |
94 } | 70 } |
95 | 71 |
96 SetPacScriptRequest* pending_set_pac_script_request() const; | |
97 | |
98 bool has_pending_set_pac_script_request() const { | |
99 return pending_set_pac_script_request_.get() != NULL; | |
100 } | |
101 | |
102 void RemovePendingRequest(Request* request); | 72 void RemovePendingRequest(Request* request); |
103 | 73 |
104 void RemovePendingSetPacScriptRequest(SetPacScriptRequest* request); | |
105 | |
106 protected: | |
107 explicit MockAsyncProxyResolverBase(bool expects_pac_bytes); | |
108 | |
109 private: | 74 private: |
110 RequestsList pending_requests_; | 75 RequestsList pending_requests_; |
111 RequestsList cancelled_requests_; | 76 RequestsList cancelled_requests_; |
112 scoped_ptr<SetPacScriptRequest> pending_set_pac_script_request_; | |
113 }; | |
114 | |
115 class MockAsyncProxyResolver : public MockAsyncProxyResolverBase { | |
116 public: | |
117 MockAsyncProxyResolver() | |
118 : MockAsyncProxyResolverBase(false /*expects_pac_bytes*/) {} | |
119 }; | |
120 | |
121 class MockAsyncProxyResolverExpectsBytes : public MockAsyncProxyResolverBase { | |
122 public: | |
123 MockAsyncProxyResolverExpectsBytes() | |
124 : MockAsyncProxyResolverBase(true /*expects_pac_bytes*/) {} | |
125 }; | 77 }; |
126 | 78 |
127 // Asynchronous mock proxy resolver factory . All requests complete | 79 // Asynchronous mock proxy resolver factory . All requests complete |
128 // asynchronously; the user must call Request::CompleteNow() on a pending | 80 // asynchronously; the user must call Request::CompleteNow() on a pending |
129 // request to signal it. | 81 // request to signal it. |
130 class MockAsyncProxyResolverFactory : public ProxyResolverFactory { | 82 class MockAsyncProxyResolverFactory : public ProxyResolverFactory { |
131 public: | 83 public: |
132 class Request; | 84 class Request; |
133 using RequestsList = std::vector<scoped_refptr<Request>>; | 85 using RequestsList = std::vector<scoped_refptr<Request>>; |
134 | 86 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 explicit ForwardingProxyResolver(ProxyResolver* impl); | 147 explicit ForwardingProxyResolver(ProxyResolver* impl); |
196 | 148 |
197 // ProxyResolver overrides. | 149 // ProxyResolver overrides. |
198 int GetProxyForURL(const GURL& query_url, | 150 int GetProxyForURL(const GURL& query_url, |
199 ProxyInfo* results, | 151 ProxyInfo* results, |
200 const CompletionCallback& callback, | 152 const CompletionCallback& callback, |
201 RequestHandle* request, | 153 RequestHandle* request, |
202 const BoundNetLog& net_log) override; | 154 const BoundNetLog& net_log) override; |
203 void CancelRequest(RequestHandle request) override; | 155 void CancelRequest(RequestHandle request) override; |
204 LoadState GetLoadState(RequestHandle request) const override; | 156 LoadState GetLoadState(RequestHandle request) const override; |
205 void CancelSetPacScript() override; | |
206 int SetPacScript(const scoped_refptr<ProxyResolverScriptData>& script_data, | |
207 const CompletionCallback& callback) override; | |
208 | 157 |
209 private: | 158 private: |
210 ProxyResolver* impl_; | 159 ProxyResolver* impl_; |
211 | 160 |
212 DISALLOW_COPY_AND_ASSIGN(ForwardingProxyResolver); | 161 DISALLOW_COPY_AND_ASSIGN(ForwardingProxyResolver); |
213 }; | 162 }; |
214 | 163 |
215 } // namespace net | 164 } // namespace net |
216 | 165 |
217 #endif // NET_PROXY_MOCK_PROXY_RESOLVER_H_ | 166 #endif // NET_PROXY_MOCK_PROXY_RESOLVER_H_ |
OLD | NEW |