Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(933)

Side by Side Diff: net/proxy/mock_proxy_resolver.cc

Issue 1102463002: Add a MockAsyncProxyResolverFactory and update some tests to use it. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@proxy-factory-refactor
Patch Set: rebase Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/proxy/mock_proxy_resolver.h ('k') | net/proxy/multi_threaded_proxy_resolver_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/proxy/mock_proxy_resolver.h" 5 #include "net/proxy/mock_proxy_resolver.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/message_loop/message_loop.h" 8 #include "base/message_loop/message_loop.h"
9 9
10 namespace net { 10 namespace net {
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 105
106 void MockAsyncProxyResolverBase::RemovePendingSetPacScriptRequest( 106 void MockAsyncProxyResolverBase::RemovePendingSetPacScriptRequest(
107 SetPacScriptRequest* request) { 107 SetPacScriptRequest* request) {
108 DCHECK_EQ(request, pending_set_pac_script_request()); 108 DCHECK_EQ(request, pending_set_pac_script_request());
109 pending_set_pac_script_request_.reset(); 109 pending_set_pac_script_request_.reset();
110 } 110 }
111 111
112 MockAsyncProxyResolverBase::MockAsyncProxyResolverBase(bool expects_pac_bytes) 112 MockAsyncProxyResolverBase::MockAsyncProxyResolverBase(bool expects_pac_bytes)
113 : ProxyResolver(expects_pac_bytes) {} 113 : ProxyResolver(expects_pac_bytes) {}
114 114
115 MockAsyncProxyResolverFactory::Request::Request(
116 MockAsyncProxyResolverFactory* factory,
117 const scoped_refptr<ProxyResolverScriptData>& script_data,
118 scoped_ptr<ProxyResolver>* resolver,
119 const CompletionCallback& callback)
120 : factory_(factory),
121 script_data_(script_data),
122 resolver_(resolver),
123 callback_(callback) {
124 }
125
126 MockAsyncProxyResolverFactory::Request::~Request() {
127 }
128
129 void MockAsyncProxyResolverFactory::Request::CompleteNow(
130 int rv,
131 scoped_ptr<ProxyResolver> resolver) {
132 *resolver_ = resolver.Pass();
133
134 // RemovePendingRequest may remove the last external reference to |this|.
135 scoped_refptr<MockAsyncProxyResolverFactory::Request> keep_alive(this);
136 factory_->RemovePendingRequest(this);
137 factory_ = nullptr;
138 callback_.Run(rv);
139 }
140
141 void MockAsyncProxyResolverFactory::Request::CompleteNowWithForwarder(
142 int rv,
143 ProxyResolver* resolver) {
144 DCHECK(resolver);
145 CompleteNow(rv, make_scoped_ptr(new ForwardingProxyResolver(resolver)));
146 }
147
148 void MockAsyncProxyResolverFactory::Request::FactoryDestroyed() {
149 factory_ = nullptr;
150 }
151
152 class MockAsyncProxyResolverFactory::Job
153 : public ProxyResolverFactory::Request {
154 public:
155 explicit Job(
156 const scoped_refptr<MockAsyncProxyResolverFactory::Request>& request)
157 : request_(request) {}
158 ~Job() override {
159 if (request_->factory_) {
160 request_->factory_->cancelled_requests_.push_back(request_);
161 request_->factory_->RemovePendingRequest(request_.get());
162 }
163 }
164
165 private:
166 scoped_refptr<MockAsyncProxyResolverFactory::Request> request_;
167 };
168
169 MockAsyncProxyResolverFactory::MockAsyncProxyResolverFactory(
170 bool resolvers_expect_pac_bytes)
171 : ProxyResolverFactory(resolvers_expect_pac_bytes) {
172 }
173
174 int MockAsyncProxyResolverFactory::CreateProxyResolver(
175 const scoped_refptr<ProxyResolverScriptData>& pac_script,
176 scoped_ptr<ProxyResolver>* resolver,
177 const net::CompletionCallback& callback,
178 scoped_ptr<ProxyResolverFactory::Request>* request_handle) {
179 scoped_refptr<Request> request =
180 new Request(this, pac_script, resolver, callback);
181 pending_requests_.push_back(request);
182
183 request_handle->reset(new Job(request));
184
185 // Test code completes the request by calling request->CompleteNow().
186 return ERR_IO_PENDING;
187 }
188
189 void MockAsyncProxyResolverFactory::RemovePendingRequest(Request* request) {
190 RequestsList::iterator it =
191 std::find(pending_requests_.begin(), pending_requests_.end(), request);
192 DCHECK(it != pending_requests_.end());
193 pending_requests_.erase(it);
194 }
195
196 MockAsyncProxyResolverFactory::~MockAsyncProxyResolverFactory() {
197 for (auto& request : pending_requests_) {
198 request->FactoryDestroyed();
199 }
200 }
201
115 ForwardingProxyResolver::ForwardingProxyResolver(ProxyResolver* impl) 202 ForwardingProxyResolver::ForwardingProxyResolver(ProxyResolver* impl)
116 : ProxyResolver(impl->expects_pac_bytes()), impl_(impl) { 203 : ProxyResolver(impl->expects_pac_bytes()), impl_(impl) {
117 } 204 }
118 205
119 int ForwardingProxyResolver::GetProxyForURL(const GURL& query_url, 206 int ForwardingProxyResolver::GetProxyForURL(const GURL& query_url,
120 ProxyInfo* results, 207 ProxyInfo* results,
121 const CompletionCallback& callback, 208 const CompletionCallback& callback,
122 RequestHandle* request, 209 RequestHandle* request,
123 const BoundNetLog& net_log) { 210 const BoundNetLog& net_log) {
124 return impl_->GetProxyForURL(query_url, results, callback, request, net_log); 211 return impl_->GetProxyForURL(query_url, results, callback, request, net_log);
(...skipping 10 matching lines...) Expand all
135 void ForwardingProxyResolver::CancelSetPacScript() { 222 void ForwardingProxyResolver::CancelSetPacScript() {
136 impl_->CancelSetPacScript(); 223 impl_->CancelSetPacScript();
137 } 224 }
138 225
139 int ForwardingProxyResolver::SetPacScript( 226 int ForwardingProxyResolver::SetPacScript(
140 const scoped_refptr<ProxyResolverScriptData>& script_data, 227 const scoped_refptr<ProxyResolverScriptData>& script_data,
141 const CompletionCallback& callback) { 228 const CompletionCallback& callback) {
142 return impl_->SetPacScript(script_data, callback); 229 return impl_->SetPacScript(script_data, callback);
143 } 230 }
144 231
145 ForwardingProxyResolverFactory::ForwardingProxyResolverFactory(
146 ProxyResolver* resolver)
147 : LegacyProxyResolverFactory(resolver->expects_pac_bytes()),
148 resolver_(resolver) {
149 }
150
151 scoped_ptr<ProxyResolver>
152 ForwardingProxyResolverFactory::CreateProxyResolver() {
153 return make_scoped_ptr(new ForwardingProxyResolver(resolver_));
154 }
155
156 } // namespace net 232 } // namespace net
OLDNEW
« no previous file with comments | « net/proxy/mock_proxy_resolver.h ('k') | net/proxy/multi_threaded_proxy_resolver_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698