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.h"
12 MockAsyncProxyResolverBase::Request::Request(
13 MockAsyncProxyResolverBase
* resolver
, const GURL
& url
, ProxyInfo
* results
,
14 const CompletionCallback
& callback
)
15 : resolver_(resolver
),
19 origin_loop_(MessageLoop::current()) {
22 void MockAsyncProxyResolverBase::Request::CompleteNow(int rv
) {
23 CompletionCallback callback
= callback_
;
26 resolver_
->RemovePendingRequest(this);
31 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_(MessageLoop::current()) {
44 MockAsyncProxyResolverBase::SetPacScriptRequest::~SetPacScriptRequest() {}
46 void MockAsyncProxyResolverBase::SetPacScriptRequest::CompleteNow(int rv
) {
47 CompletionCallback callback
= callback_
;
49 // Will delete |this|.
50 resolver_
->RemovePendingSetPacScriptRequest(this);
55 MockAsyncProxyResolverBase::~MockAsyncProxyResolverBase() {}
57 int MockAsyncProxyResolverBase::GetProxyForURL(
58 const GURL
& url
, ProxyInfo
* results
, const CompletionCallback
& callback
,
59 RequestHandle
* request_handle
, const BoundNetLog
& /*net_log*/) {
60 scoped_refptr
<Request
> request
= new Request(this, url
, results
, callback
);
61 pending_requests_
.push_back(request
);
64 *request_handle
= reinterpret_cast<RequestHandle
>(request
.get());
66 // Test code completes the request by calling request->CompleteNow().
67 return ERR_IO_PENDING
;
70 void MockAsyncProxyResolverBase::CancelRequest(RequestHandle request_handle
) {
71 scoped_refptr
<Request
> request
= reinterpret_cast<Request
*>(request_handle
);
72 cancelled_requests_
.push_back(request
);
73 RemovePendingRequest(request
);
76 LoadState
MockAsyncProxyResolverBase::GetLoadState(
77 RequestHandle request_handle
) const {
79 return LOAD_STATE_IDLE
;
82 LoadState
MockAsyncProxyResolverBase::GetLoadStateThreadSafe(
83 RequestHandle request_handle
) const {
85 return LOAD_STATE_IDLE
;
88 int MockAsyncProxyResolverBase::SetPacScript(
89 const scoped_refptr
<ProxyResolverScriptData
>& script_data
,
90 const CompletionCallback
& callback
) {
91 DCHECK(!pending_set_pac_script_request_
.get());
92 pending_set_pac_script_request_
.reset(
93 new SetPacScriptRequest(this, script_data
, callback
));
94 // Finished when user calls SetPacScriptRequest::CompleteNow().
95 return ERR_IO_PENDING
;
98 void MockAsyncProxyResolverBase::CancelSetPacScript() {
99 // Do nothing (caller was responsible for completing the request).
102 MockAsyncProxyResolverBase::SetPacScriptRequest
*
103 MockAsyncProxyResolverBase::pending_set_pac_script_request() const {
104 return pending_set_pac_script_request_
.get();
107 void MockAsyncProxyResolverBase::RemovePendingRequest(Request
* request
) {
108 RequestsList::iterator it
= std::find(
109 pending_requests_
.begin(), pending_requests_
.end(), request
);
110 DCHECK(it
!= pending_requests_
.end());
111 pending_requests_
.erase(it
);
114 void MockAsyncProxyResolverBase::RemovePendingSetPacScriptRequest(
115 SetPacScriptRequest
* request
) {
116 DCHECK_EQ(request
, pending_set_pac_script_request());
117 pending_set_pac_script_request_
.reset();
120 MockAsyncProxyResolverBase::MockAsyncProxyResolverBase(bool expects_pac_bytes
)
121 : ProxyResolver(expects_pac_bytes
) {}