net cleanup: Remove unnecessary namespace prefixes.
[chromium-blink-merge.git] / net / proxy / mock_proxy_resolver.h
blobee24da41cec85c445873ccf874019aea4fab4cdb
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 MockAsyncProxyResolverBase : public ProxyResolver {
25 public:
26 class Request : public base::RefCounted<Request> {
27 public:
28 Request(MockAsyncProxyResolverBase* 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 MockAsyncProxyResolverBase* resolver_;
45 const GURL url_;
46 ProxyInfo* results_;
47 CompletionCallback callback_;
48 base::MessageLoop* origin_loop_;
51 class SetPacScriptRequest {
52 public:
53 SetPacScriptRequest(
54 MockAsyncProxyResolverBase* resolver,
55 const scoped_refptr<ProxyResolverScriptData>& script_data,
56 const CompletionCallback& callback);
57 ~SetPacScriptRequest();
59 const ProxyResolverScriptData* script_data() const {
60 return script_data_.get();
63 void CompleteNow(int rv);
65 private:
66 MockAsyncProxyResolverBase* resolver_;
67 const scoped_refptr<ProxyResolverScriptData> script_data_;
68 CompletionCallback callback_;
69 base::MessageLoop* origin_loop_;
72 typedef std::vector<scoped_refptr<Request> > RequestsList;
74 ~MockAsyncProxyResolverBase() override;
76 // ProxyResolver implementation.
77 int GetProxyForURL(const GURL& url,
78 ProxyInfo* results,
79 const CompletionCallback& callback,
80 RequestHandle* request_handle,
81 const BoundNetLog& /*net_log*/) override;
82 void CancelRequest(RequestHandle request_handle) override;
83 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;
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 // This factory returns ProxyResolvers that forward all requests to
128 // |resolver|. |resolver| must remain so long as any value returned from
129 // CreateProxyResolver remains in use.
130 class ForwardingProxyResolverFactory : public LegacyProxyResolverFactory {
131 public:
132 explicit ForwardingProxyResolverFactory(ProxyResolver* resolver);
134 // LegacyProxyResolverFactory override.
135 scoped_ptr<ProxyResolver> CreateProxyResolver() override;
137 private:
138 ProxyResolver* resolver_;
140 DISALLOW_COPY_AND_ASSIGN(ForwardingProxyResolverFactory);
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;
157 void CancelSetPacScript() override;
158 int SetPacScript(const scoped_refptr<ProxyResolverScriptData>& script_data,
159 const CompletionCallback& callback) override;
161 private:
162 ProxyResolver* impl_;
164 DISALLOW_COPY_AND_ASSIGN(ForwardingProxyResolver);
167 } // namespace net
169 #endif // NET_PROXY_MOCK_PROXY_RESOLVER_H_