Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / net / proxy / mock_proxy_resolver.h
blobaec843227b95e034bb4104ef49ea4d8f8c5f85a7
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 "net/proxy/proxy_resolver_factory.h"
14 #include "url/gurl.h"
16 namespace base {
17 class MessageLoop;
20 namespace net {
22 // Asynchronous mock proxy resolver. All requests complete asynchronously,
23 // user must call Request::CompleteNow() on a pending request to signal it.
24 class MockAsyncProxyResolver : public ProxyResolver {
25 public:
26 class Request : public base::RefCounted<Request> {
27 public:
28 Request(MockAsyncProxyResolver* resolver,
29 const GURL& url,
30 ProxyInfo* results,
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);
39 private:
40 friend class base::RefCounted<Request>;
42 virtual ~Request();
44 MockAsyncProxyResolver* resolver_;
45 const GURL url_;
46 ProxyInfo* results_;
47 CompletionCallback callback_;
48 base::MessageLoop* origin_loop_;
51 typedef std::vector<scoped_refptr<Request> > RequestsList;
53 MockAsyncProxyResolver();
54 ~MockAsyncProxyResolver() override;
56 // ProxyResolver implementation.
57 int GetProxyForURL(const GURL& url,
58 ProxyInfo* results,
59 const CompletionCallback& callback,
60 RequestHandle* request_handle,
61 const BoundNetLog& /*net_log*/) override;
62 void CancelRequest(RequestHandle request_handle) override;
63 LoadState GetLoadState(RequestHandle request_handle) const override;
64 const RequestsList& pending_requests() const {
65 return pending_requests_;
68 const RequestsList& cancelled_requests() const {
69 return cancelled_requests_;
72 void RemovePendingRequest(Request* request);
74 private:
75 RequestsList pending_requests_;
76 RequestsList cancelled_requests_;
79 // Asynchronous mock proxy resolver factory . All requests complete
80 // asynchronously; the user must call Request::CompleteNow() on a pending
81 // request to signal it.
82 class MockAsyncProxyResolverFactory : public ProxyResolverFactory {
83 public:
84 class Request;
85 using RequestsList = std::vector<scoped_refptr<Request>>;
87 explicit MockAsyncProxyResolverFactory(bool resolvers_expect_pac_bytes);
88 ~MockAsyncProxyResolverFactory() override;
90 int CreateProxyResolver(
91 const scoped_refptr<ProxyResolverScriptData>& pac_script,
92 scoped_ptr<ProxyResolver>* resolver,
93 const CompletionCallback& callback,
94 scoped_ptr<ProxyResolverFactory::Request>* request) override;
96 const RequestsList& pending_requests() const { return pending_requests_; }
98 const RequestsList& cancelled_requests() const { return cancelled_requests_; }
100 void RemovePendingRequest(Request* request);
102 private:
103 class Job;
104 RequestsList pending_requests_;
105 RequestsList cancelled_requests_;
108 class MockAsyncProxyResolverFactory::Request
109 : public base::RefCounted<Request> {
110 public:
111 Request(MockAsyncProxyResolverFactory* factory,
112 const scoped_refptr<ProxyResolverScriptData>& script_data,
113 scoped_ptr<ProxyResolver>* resolver,
114 const CompletionCallback& callback);
116 const scoped_refptr<ProxyResolverScriptData>& script_data() const {
117 return script_data_;
120 // Completes this request. A ForwardingProxyResolver that forwards to
121 // |resolver| will be returned to the requester. |resolver| must not be
122 // null and must remain as long as the resolver returned by this request
123 // remains in use.
124 void CompleteNowWithForwarder(int rv, ProxyResolver* resolver);
126 void CompleteNow(int rv, scoped_ptr<ProxyResolver> resolver);
128 private:
129 friend class base::RefCounted<Request>;
130 friend class MockAsyncProxyResolverFactory;
131 friend class MockAsyncProxyResolverFactory::Job;
133 ~Request();
135 void FactoryDestroyed();
137 MockAsyncProxyResolverFactory* factory_;
138 const scoped_refptr<ProxyResolverScriptData> script_data_;
139 scoped_ptr<ProxyResolver>* resolver_;
140 CompletionCallback callback_;
143 // ForwardingProxyResolver forwards all requests to |impl|. |impl| must remain
144 // so long as this remains in use.
145 class ForwardingProxyResolver : public ProxyResolver {
146 public:
147 explicit ForwardingProxyResolver(ProxyResolver* impl);
149 // ProxyResolver overrides.
150 int GetProxyForURL(const GURL& query_url,
151 ProxyInfo* results,
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;
158 private:
159 ProxyResolver* impl_;
161 DISALLOW_COPY_AND_ASSIGN(ForwardingProxyResolver);
164 } // namespace net
166 #endif // NET_PROXY_MOCK_PROXY_RESOLVER_H_