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"
12 MockAsyncProxyResolverBase::Request::Request(
13 MockAsyncProxyResolverBase
* resolver
,
16 const CompletionCallback
& callback
)
17 : resolver_(resolver
),
21 origin_loop_(base::MessageLoop::current()) {}
23 void MockAsyncProxyResolverBase::Request::CompleteNow(int rv
) {
24 CompletionCallback callback
= callback_
;
27 resolver_
->RemovePendingRequest(this);
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
),
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);
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
);
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
) {}