Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / net / proxy / mock_proxy_resolver.cc
blob2ff09007eb116822b819dfa78806771987a5b7bd
1 // Copyright (c) 2011 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 #include "net/proxy/mock_proxy_resolver.h"
7 #include "base/logging.h"
8 #include "base/message_loop/message_loop.h"
10 namespace net {
12 MockAsyncProxyResolverBase::Request::Request(
13 MockAsyncProxyResolverBase* resolver,
14 const GURL& url,
15 ProxyInfo* results,
16 const CompletionCallback& callback)
17 : resolver_(resolver),
18 url_(url),
19 results_(results),
20 callback_(callback),
21 origin_loop_(base::MessageLoop::current()) {}
23 void MockAsyncProxyResolverBase::Request::CompleteNow(int rv) {
24 CompletionCallback callback = callback_;
26 // May delete |this|.
27 resolver_->RemovePendingRequest(this);
29 callback.Run(rv);
32 MockAsyncProxyResolverBase::Request::~Request() {}
34 MockAsyncProxyResolverBase::SetPacScriptRequest::SetPacScriptRequest(
35 MockAsyncProxyResolverBase* resolver,
36 const scoped_refptr<ProxyResolverScriptData>& script_data,
37 const CompletionCallback& callback)
38 : resolver_(resolver),
39 script_data_(script_data),
40 callback_(callback),
41 origin_loop_(base::MessageLoop::current()) {}
43 MockAsyncProxyResolverBase::SetPacScriptRequest::~SetPacScriptRequest() {}
45 void MockAsyncProxyResolverBase::SetPacScriptRequest::CompleteNow(int rv) {
46 CompletionCallback callback = callback_;
48 // Will delete |this|.
49 resolver_->RemovePendingSetPacScriptRequest(this);
51 callback.Run(rv);
54 MockAsyncProxyResolverBase::~MockAsyncProxyResolverBase() {}
56 int MockAsyncProxyResolverBase::GetProxyForURL(
57 const GURL& url, ProxyInfo* results, const CompletionCallback& callback,
58 RequestHandle* request_handle, const BoundNetLog& /*net_log*/) {
59 scoped_refptr<Request> request = new Request(this, url, results, callback);
60 pending_requests_.push_back(request);
62 if (request_handle)
63 *request_handle = reinterpret_cast<RequestHandle>(request.get());
65 // Test code completes the request by calling request->CompleteNow().
66 return ERR_IO_PENDING;
69 void MockAsyncProxyResolverBase::CancelRequest(RequestHandle request_handle) {
70 scoped_refptr<Request> request = reinterpret_cast<Request*>(request_handle);
71 cancelled_requests_.push_back(request);
72 RemovePendingRequest(request.get());
75 LoadState MockAsyncProxyResolverBase::GetLoadState(
76 RequestHandle request_handle) const {
77 return LOAD_STATE_RESOLVING_PROXY_FOR_URL;
80 int MockAsyncProxyResolverBase::SetPacScript(
81 const scoped_refptr<ProxyResolverScriptData>& script_data,
82 const CompletionCallback& callback) {
83 DCHECK(!pending_set_pac_script_request_.get());
84 pending_set_pac_script_request_.reset(
85 new SetPacScriptRequest(this, script_data, callback));
86 // Finished when user calls SetPacScriptRequest::CompleteNow().
87 return ERR_IO_PENDING;
90 void MockAsyncProxyResolverBase::CancelSetPacScript() {
91 // Do nothing (caller was responsible for completing the request).
94 MockAsyncProxyResolverBase::SetPacScriptRequest*
95 MockAsyncProxyResolverBase::pending_set_pac_script_request() const {
96 return pending_set_pac_script_request_.get();
99 void MockAsyncProxyResolverBase::RemovePendingRequest(Request* request) {
100 RequestsList::iterator it = std::find(
101 pending_requests_.begin(), pending_requests_.end(), request);
102 DCHECK(it != pending_requests_.end());
103 pending_requests_.erase(it);
106 void MockAsyncProxyResolverBase::RemovePendingSetPacScriptRequest(
107 SetPacScriptRequest* request) {
108 DCHECK_EQ(request, pending_set_pac_script_request());
109 pending_set_pac_script_request_.reset();
112 MockAsyncProxyResolverBase::MockAsyncProxyResolverBase(bool expects_pac_bytes)
113 : ProxyResolver(expects_pac_bytes) {}
115 ForwardingProxyResolver::ForwardingProxyResolver(ProxyResolver* impl)
116 : ProxyResolver(impl->expects_pac_bytes()), impl_(impl) {
119 int ForwardingProxyResolver::GetProxyForURL(const GURL& query_url,
120 ProxyInfo* results,
121 const CompletionCallback& callback,
122 RequestHandle* request,
123 const BoundNetLog& net_log) {
124 return impl_->GetProxyForURL(query_url, results, callback, request, net_log);
127 void ForwardingProxyResolver::CancelRequest(RequestHandle request) {
128 impl_->CancelRequest(request);
131 LoadState ForwardingProxyResolver::GetLoadState(RequestHandle request) const {
132 return impl_->GetLoadState(request);
135 void ForwardingProxyResolver::CancelSetPacScript() {
136 impl_->CancelSetPacScript();
139 int ForwardingProxyResolver::SetPacScript(
140 const scoped_refptr<ProxyResolverScriptData>& script_data,
141 const CompletionCallback& callback) {
142 return impl_->SetPacScript(script_data, callback);
145 ForwardingProxyResolverFactory::ForwardingProxyResolverFactory(
146 ProxyResolver* resolver)
147 : LegacyProxyResolverFactory(resolver->expects_pac_bytes()),
148 resolver_(resolver) {
151 scoped_ptr<ProxyResolver>
152 ForwardingProxyResolverFactory::CreateProxyResolver() {
153 return make_scoped_ptr(new ForwardingProxyResolver(resolver_));
156 } // namespace net