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 <memory> | 8 #include <memory> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
13 #include "net/proxy/proxy_resolver.h" | 13 #include "net/proxy/proxy_resolver.h" |
14 #include "net/proxy/proxy_resolver_factory.h" | 14 #include "net/proxy/proxy_resolver_factory.h" |
15 #include "url/gurl.h" | 15 #include "url/gurl.h" |
16 | 16 |
17 namespace net { | 17 namespace net { |
18 | 18 |
19 // Asynchronous mock proxy resolver. All requests complete asynchronously, | 19 // Asynchronous mock proxy resolver. All requests complete asynchronously, |
20 // user must call Request::CompleteNow() on a pending request to signal it. | 20 // user must call Job::CompleteNow() on a pending request to signal it. |
21 class MockAsyncProxyResolver : public ProxyResolver { | 21 class MockAsyncProxyResolver : public ProxyResolver { |
22 public: | 22 public: |
23 class Request : public base::RefCounted<Request> { | 23 class Job { |
24 public: | 24 public: |
25 Request(MockAsyncProxyResolver* resolver, | 25 Job(MockAsyncProxyResolver* resolver, |
26 const GURL& url, | 26 const GURL& url, |
27 ProxyInfo* results, | 27 ProxyInfo* results, |
28 const CompletionCallback& callback); | 28 const CompletionCallback& callback); |
29 | 29 |
30 const GURL& url() const { return url_; } | 30 const GURL& url() const { return url_; } |
31 ProxyInfo* results() const { return results_; } | 31 ProxyInfo* results() const { return results_; } |
32 const CompletionCallback& callback() const { return callback_; } | 32 const CompletionCallback& callback() const { return callback_; } |
| 33 MockAsyncProxyResolver* Resolver() const { return resolver_; }; |
33 | 34 |
34 void CompleteNow(int rv); | 35 void CompleteNow(int rv); |
35 | 36 |
| 37 ~Job(); |
| 38 |
36 private: | 39 private: |
37 friend class base::RefCounted<Request>; | |
38 | |
39 virtual ~Request(); | |
40 | |
41 MockAsyncProxyResolver* resolver_; | 40 MockAsyncProxyResolver* resolver_; |
42 const GURL url_; | 41 const GURL url_; |
43 ProxyInfo* results_; | 42 ProxyInfo* results_; |
44 CompletionCallback callback_; | 43 CompletionCallback callback_; |
45 }; | 44 }; |
46 | 45 |
47 typedef std::vector<scoped_refptr<Request> > RequestsList; | 46 class RequestImpl : public ProxyResolver::Request { |
| 47 public: |
| 48 explicit RequestImpl(std::unique_ptr<Job> job); |
| 49 |
| 50 ~RequestImpl() override; |
| 51 |
| 52 LoadState GetLoadState() override; |
| 53 |
| 54 private: |
| 55 std::unique_ptr<Job> job_; |
| 56 }; |
48 | 57 |
49 MockAsyncProxyResolver(); | 58 MockAsyncProxyResolver(); |
50 ~MockAsyncProxyResolver() override; | 59 ~MockAsyncProxyResolver() override; |
51 | 60 |
52 // ProxyResolver implementation. | 61 // ProxyResolver implementation. |
53 int GetProxyForURL(const GURL& url, | 62 int GetProxyForURL(const GURL& url, |
54 ProxyInfo* results, | 63 ProxyInfo* results, |
55 const CompletionCallback& callback, | 64 const CompletionCallback& callback, |
56 RequestHandle* request_handle, | 65 std::unique_ptr<Request>* request, |
57 const NetLogWithSource& /*net_log*/) override; | 66 const NetLogWithSource& /*net_log*/) override; |
58 void CancelRequest(RequestHandle request_handle) override; | 67 const std::vector<Job*>& pending_jobs() const { return pending_jobs_; } |
59 LoadState GetLoadState(RequestHandle request_handle) const override; | 68 |
60 const RequestsList& pending_requests() const { | 69 const std::vector<std::unique_ptr<Job>>& cancelled_jobs() const { |
61 return pending_requests_; | 70 return cancelled_jobs_; |
62 } | 71 } |
63 | 72 |
64 const RequestsList& cancelled_requests() const { | 73 void AddCancelledJob(std::unique_ptr<Job> job); |
65 return cancelled_requests_; | 74 void RemovePendingJob(Job* job); |
66 } | |
67 | |
68 void RemovePendingRequest(Request* request); | |
69 | 75 |
70 private: | 76 private: |
71 RequestsList pending_requests_; | 77 std::vector<Job*> pending_jobs_; |
72 RequestsList cancelled_requests_; | 78 std::vector<std::unique_ptr<Job>> cancelled_jobs_; |
73 }; | 79 }; |
74 | 80 |
75 // Asynchronous mock proxy resolver factory . All requests complete | 81 // Asynchronous mock proxy resolver factory . All requests complete |
76 // asynchronously; the user must call Request::CompleteNow() on a pending | 82 // asynchronously; the user must call Request::CompleteNow() on a pending |
77 // request to signal it. | 83 // request to signal it. |
78 class MockAsyncProxyResolverFactory : public ProxyResolverFactory { | 84 class MockAsyncProxyResolverFactory : public ProxyResolverFactory { |
79 public: | 85 public: |
80 class Request; | 86 class Request; |
81 using RequestsList = std::vector<scoped_refptr<Request>>; | 87 using RequestsList = std::vector<scoped_refptr<Request>>; |
82 | 88 |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 // ForwardingProxyResolver forwards all requests to |impl|. |impl| must remain | 145 // ForwardingProxyResolver forwards all requests to |impl|. |impl| must remain |
140 // so long as this remains in use. | 146 // so long as this remains in use. |
141 class ForwardingProxyResolver : public ProxyResolver { | 147 class ForwardingProxyResolver : public ProxyResolver { |
142 public: | 148 public: |
143 explicit ForwardingProxyResolver(ProxyResolver* impl); | 149 explicit ForwardingProxyResolver(ProxyResolver* impl); |
144 | 150 |
145 // ProxyResolver overrides. | 151 // ProxyResolver overrides. |
146 int GetProxyForURL(const GURL& query_url, | 152 int GetProxyForURL(const GURL& query_url, |
147 ProxyInfo* results, | 153 ProxyInfo* results, |
148 const CompletionCallback& callback, | 154 const CompletionCallback& callback, |
149 RequestHandle* request, | 155 std::unique_ptr<Request>* request, |
150 const NetLogWithSource& net_log) override; | 156 const NetLogWithSource& net_log) override; |
151 void CancelRequest(RequestHandle request) override; | |
152 LoadState GetLoadState(RequestHandle request) const override; | |
153 | 157 |
154 private: | 158 private: |
155 ProxyResolver* impl_; | 159 ProxyResolver* impl_; |
156 | 160 |
157 DISALLOW_COPY_AND_ASSIGN(ForwardingProxyResolver); | 161 DISALLOW_COPY_AND_ASSIGN(ForwardingProxyResolver); |
158 }; | 162 }; |
159 | 163 |
160 } // namespace net | 164 } // namespace net |
161 | 165 |
162 #endif // NET_PROXY_MOCK_PROXY_RESOLVER_H_ | 166 #endif // NET_PROXY_MOCK_PROXY_RESOLVER_H_ |
OLD | NEW |