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

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

Issue 1439053002: Change ProxyResolver::GetProxyForURL() to take a scoped_ptr<Request>* rather than a RequestHandle* (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Restore refptr due windows error Created 4 years, 10 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
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/proxy_resolver_factory_mojo.h" 5 #include "net/proxy/proxy_resolver_factory_mojo.h"
6 6
7 #include <set> 7 #include <set>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
111 HostResolver* host_resolver, 111 HostResolver* host_resolver,
112 scoped_ptr<base::ScopedClosureRunner> on_delete_callback_runner, 112 scoped_ptr<base::ScopedClosureRunner> on_delete_callback_runner,
113 scoped_ptr<ProxyResolverErrorObserver> error_observer, 113 scoped_ptr<ProxyResolverErrorObserver> error_observer,
114 NetLog* net_log); 114 NetLog* net_log);
115 ~ProxyResolverMojo() override; 115 ~ProxyResolverMojo() override;
116 116
117 // ProxyResolver implementation: 117 // ProxyResolver implementation:
118 int GetProxyForURL(const GURL& url, 118 int GetProxyForURL(const GURL& url,
119 ProxyInfo* results, 119 ProxyInfo* results,
120 const net::CompletionCallback& callback, 120 const net::CompletionCallback& callback,
121 RequestHandle* request, 121 scoped_ptr<Request>* request,
122 const BoundNetLog& net_log) override; 122 const BoundNetLog& net_log) override;
123 void CancelRequest(RequestHandle request) override;
124 LoadState GetLoadState(RequestHandle request) const override;
125 123
126 private: 124 private:
127 class Job; 125 class Job;
126 class RequestImpl;
127
128 base::ThreadChecker thread_checker_;
128 129
129 // Mojo error handler. 130 // Mojo error handler.
130 void OnConnectionError(); 131 void OnConnectionError();
131 132
132 void RemoveJob(Job* job); 133 void RemoveJob(Job* job);
133 134
134 // Connection to the Mojo proxy resolver. 135 // Connection to the Mojo proxy resolver.
135 interfaces::ProxyResolverPtr mojo_proxy_resolver_ptr_; 136 interfaces::ProxyResolverPtr mojo_proxy_resolver_ptr_;
136 137
137 HostResolver* host_resolver_; 138 HostResolver* host_resolver_;
138 139
139 scoped_ptr<ProxyResolverErrorObserver> error_observer_; 140 scoped_ptr<ProxyResolverErrorObserver> error_observer_;
140 141
141 NetLog* net_log_; 142 NetLog* net_log_;
142 143
143 std::set<Job*> pending_jobs_; 144 std::set<Job*> pending_jobs_;
144 145
145 base::ThreadChecker thread_checker_;
146 146
147 scoped_ptr<base::ScopedClosureRunner> on_delete_callback_runner_; 147 scoped_ptr<base::ScopedClosureRunner> on_delete_callback_runner_;
148 148
149 DISALLOW_COPY_AND_ASSIGN(ProxyResolverMojo); 149 DISALLOW_COPY_AND_ASSIGN(ProxyResolverMojo);
150 }; 150 };
151 151
152 class ProxyResolverMojo::RequestImpl : public ProxyResolver::Request {
153 public:
154 RequestImpl(scoped_ptr<Job> job);
eroman 2016/02/24 03:08:06 explicit
155
156 ~RequestImpl() override;
157
158 LoadState GetLoadState() override;
159
160 private:
161 scoped_ptr<Job> job_;
162 };
163
152 class ProxyResolverMojo::Job 164 class ProxyResolverMojo::Job
153 : public ClientMixin<interfaces::ProxyResolverRequestClient> { 165 : public ClientMixin<interfaces::ProxyResolverRequestClient> {
154 public: 166 public:
155 Job(ProxyResolverMojo* resolver, 167 Job(ProxyResolverMojo* resolver,
156 const GURL& url, 168 const GURL& url,
157 ProxyInfo* results, 169 ProxyInfo* results,
158 const CompletionCallback& callback, 170 const CompletionCallback& callback,
159 const BoundNetLog& net_log); 171 const BoundNetLog& net_log);
160 ~Job() override; 172 ~Job() override;
161 173
162 // Cancels the job and prevents the callback from being run. 174 bool IsCallback();
163 void Cancel(); 175 void PacScriptTerminated();
164
165 // Returns the LoadState of this job. 176 // Returns the LoadState of this job.
166 LoadState GetLoadState(); 177 LoadState GetLoadState();
167 178
179 ProxyResolverMojo* GetResolver();
eroman 2016/02/24 03:08:06 resolver()
180
168 private: 181 private:
182 friend class base::RefCounted<Job>;
169 // Mojo error handler. 183 // Mojo error handler.
170 void OnConnectionError(); 184 void OnConnectionError();
171 185
172 // Overridden from interfaces::ProxyResolverRequestClient: 186 // Overridden from interfaces::ProxyResolverRequestClient:
173 void ReportResult( 187 void ReportResult(
174 int32_t error, 188 int32_t error,
175 mojo::Array<interfaces::ProxyServerPtr> proxy_servers) override; 189 mojo::Array<interfaces::ProxyServerPtr> proxy_servers) override;
176 190
177 ProxyResolverMojo* resolver_; 191 ProxyResolverMojo* resolver_;
178 const GURL url_; 192 const GURL url_;
179 ProxyInfo* results_; 193 ProxyInfo* results_;
180 CompletionCallback callback_; 194 CompletionCallback callback_;
181 195
182 base::ThreadChecker thread_checker_; 196 base::ThreadChecker thread_checker_;
183 mojo::Binding<interfaces::ProxyResolverRequestClient> binding_; 197 mojo::Binding<interfaces::ProxyResolverRequestClient> binding_;
184 }; 198 };
185 199
200 ProxyResolverMojo::RequestImpl::RequestImpl(scoped_ptr<Job> job)
201 : job_(std::move(job)) {}
202
203 ProxyResolverMojo::RequestImpl::~RequestImpl() {
204 ProxyResolverMojo* resolver = job_->GetResolver();
205 if (resolver) {
206 resolver->RemoveJob(job_.get());
207 }
208 }
209
210 LoadState ProxyResolverMojo::RequestImpl::GetLoadState() {
211 CHECK_EQ(1u, job_->GetResolver()->pending_jobs_.count(job_.get()));
212 return job_->GetLoadState();
213 }
214
186 ProxyResolverMojo::Job::Job(ProxyResolverMojo* resolver, 215 ProxyResolverMojo::Job::Job(ProxyResolverMojo* resolver,
187 const GURL& url, 216 const GURL& url,
188 ProxyInfo* results, 217 ProxyInfo* results,
189 const CompletionCallback& callback, 218 const CompletionCallback& callback,
190 const BoundNetLog& net_log) 219 const BoundNetLog& net_log)
191 : ClientMixin<interfaces::ProxyResolverRequestClient>( 220 : ClientMixin<interfaces::ProxyResolverRequestClient>(
192 resolver->host_resolver_, 221 resolver->host_resolver_,
193 resolver->error_observer_.get(), 222 resolver->error_observer_.get(),
194 resolver->net_log_, 223 resolver->net_log_,
195 net_log), 224 net_log),
196 resolver_(resolver), 225 resolver_(resolver),
197 url_(url), 226 url_(url),
198 results_(results), 227 results_(results),
199 callback_(callback), 228 callback_(callback),
200 binding_(this) { 229 binding_(this) {
201 resolver_->mojo_proxy_resolver_ptr_->GetProxyForUrl( 230 resolver_->mojo_proxy_resolver_ptr_->GetProxyForUrl(
202 mojo::String::From(url_), binding_.CreateInterfacePtrAndBind()); 231 mojo::String::From(url_), binding_.CreateInterfacePtrAndBind());
203 binding_.set_connection_error_handler(base::Bind( 232 binding_.set_connection_error_handler(base::Bind(
204 &ProxyResolverMojo::Job::OnConnectionError, base::Unretained(this))); 233 &ProxyResolverMojo::Job::OnConnectionError, base::Unretained(this)));
205 } 234 }
206 235
207 ProxyResolverMojo::Job::~Job() { 236 void ProxyResolverMojo::Job::PacScriptTerminated() {
208 DCHECK(thread_checker_.CalledOnValidThread()); 237 DCHECK(thread_checker_.CalledOnValidThread());
209 if (!callback_.is_null()) 238 if (!callback_.is_null())
210 callback_.Run(ERR_PAC_SCRIPT_TERMINATED); 239 callback_.Run(ERR_PAC_SCRIPT_TERMINATED);
240 callback_.Reset();
241 resolver_ = nullptr;
211 } 242 }
212 243
213 void ProxyResolverMojo::Job::Cancel() { 244 ProxyResolverMojo::Job::~Job() {}
214 DCHECK(thread_checker_.CalledOnValidThread()); 245
215 DCHECK(!callback_.is_null()); 246 bool ProxyResolverMojo::Job::IsCallback() {
eroman 2016/02/24 03:08:06 Is this used anywhere? I believe you can delete.
216 callback_.Reset(); 247 return !callback_.is_null();
217 } 248 }
218 249
219 LoadState ProxyResolverMojo::Job::GetLoadState() { 250 LoadState ProxyResolverMojo::Job::GetLoadState() {
220 return dns_request_in_progress() ? LOAD_STATE_RESOLVING_HOST_IN_PROXY_SCRIPT 251 return dns_request_in_progress() ? LOAD_STATE_RESOLVING_HOST_IN_PROXY_SCRIPT
221 : LOAD_STATE_RESOLVING_PROXY_FOR_URL; 252 : LOAD_STATE_RESOLVING_PROXY_FOR_URL;
222 } 253 }
223 254
255 ProxyResolverMojo* ProxyResolverMojo::Job::GetResolver() {
256 return resolver_;
257 };
258
224 void ProxyResolverMojo::Job::OnConnectionError() { 259 void ProxyResolverMojo::Job::OnConnectionError() {
225 DCHECK(thread_checker_.CalledOnValidThread()); 260 DCHECK(thread_checker_.CalledOnValidThread());
226 DVLOG(1) << "ProxyResolverMojo::Job::OnConnectionError"; 261 DVLOG(1) << "ProxyResolverMojo::Job::OnConnectionError";
227 resolver_->RemoveJob(this); 262 if (resolver_)
eroman 2016/02/24 03:08:06 Your refactor here looks correct (good job reading
263 resolver_->RemoveJob(this);
eroman 2016/02/24 03:08:06 Based on suggestion above, this would become:
228 } 264 }
229 265
230 void ProxyResolverMojo::Job::ReportResult( 266 void ProxyResolverMojo::Job::ReportResult(
231 int32_t error, 267 int32_t error,
232 mojo::Array<interfaces::ProxyServerPtr> proxy_servers) { 268 mojo::Array<interfaces::ProxyServerPtr> proxy_servers) {
233 DCHECK(thread_checker_.CalledOnValidThread()); 269 DCHECK(thread_checker_.CalledOnValidThread());
234 DVLOG(1) << "ProxyResolverMojo::Job::ReportResult: " << error; 270 DVLOG(1) << "ProxyResolverMojo::Job::ReportResult: " << error;
235 271
236 if (error == OK) { 272 if (error == OK) {
237 *results_ = proxy_servers.To<ProxyInfo>(); 273 *results_ = proxy_servers.To<ProxyInfo>();
238 DVLOG(1) << "Servers: " << results_->ToPacString(); 274 DVLOG(1) << "Servers: " << results_->ToPacString();
239 } 275 }
240 276
241 CompletionCallback callback = callback_; 277 CompletionCallback callback = callback_;
eroman 2016/02/24 03:08:06 Based on the suggestion above, the remainder of th
242 callback_.Reset(); 278 callback_.Reset();
243 resolver_->RemoveJob(this); 279 resolver_->RemoveJob(this);
244 callback.Run(error); 280 callback.Run(error);
245 } 281 }
246 282
247 ProxyResolverMojo::ProxyResolverMojo( 283 ProxyResolverMojo::ProxyResolverMojo(
248 interfaces::ProxyResolverPtr resolver_ptr, 284 interfaces::ProxyResolverPtr resolver_ptr,
249 HostResolver* host_resolver, 285 HostResolver* host_resolver,
250 scoped_ptr<base::ScopedClosureRunner> on_delete_callback_runner, 286 scoped_ptr<base::ScopedClosureRunner> on_delete_callback_runner,
251 scoped_ptr<ProxyResolverErrorObserver> error_observer, 287 scoped_ptr<ProxyResolverErrorObserver> error_observer,
(...skipping 16 matching lines...) Expand all
268 void ProxyResolverMojo::OnConnectionError() { 304 void ProxyResolverMojo::OnConnectionError() {
269 DCHECK(thread_checker_.CalledOnValidThread()); 305 DCHECK(thread_checker_.CalledOnValidThread());
270 DVLOG(1) << "ProxyResolverMojo::OnConnectionError"; 306 DVLOG(1) << "ProxyResolverMojo::OnConnectionError";
271 307
272 // Disconnect from the Mojo proxy resolver service. 308 // Disconnect from the Mojo proxy resolver service.
273 mojo_proxy_resolver_ptr_.reset(); 309 mojo_proxy_resolver_ptr_.reset();
274 } 310 }
275 311
276 void ProxyResolverMojo::RemoveJob(Job* job) { 312 void ProxyResolverMojo::RemoveJob(Job* job) {
277 DCHECK(thread_checker_.CalledOnValidThread()); 313 DCHECK(thread_checker_.CalledOnValidThread());
278 size_t num_erased = pending_jobs_.erase(job); 314 pending_jobs_.erase(job);
279 DCHECK(num_erased); 315 job->PacScriptTerminated();
280 delete job;
281 } 316 }
282 317
283 int ProxyResolverMojo::GetProxyForURL(const GURL& url, 318 int ProxyResolverMojo::GetProxyForURL(const GURL& url,
284 ProxyInfo* results, 319 ProxyInfo* results,
285 const CompletionCallback& callback, 320 const CompletionCallback& callback,
286 RequestHandle* request, 321 scoped_ptr<Request>* request,
287 const BoundNetLog& net_log) { 322 const BoundNetLog& net_log) {
288 DCHECK(thread_checker_.CalledOnValidThread()); 323 DCHECK(thread_checker_.CalledOnValidThread());
289 324
290 if (!mojo_proxy_resolver_ptr_) 325 if (!mojo_proxy_resolver_ptr_)
291 return ERR_PAC_SCRIPT_TERMINATED; 326 return ERR_PAC_SCRIPT_TERMINATED;
292 327
293 Job* job = new Job(this, url, results, callback, net_log); 328 scoped_ptr<Job> job(new Job(this, url, results, callback, net_log));
294 bool inserted = pending_jobs_.insert(job).second; 329 bool inserted = pending_jobs_.insert(job.get()).second;
295 DCHECK(inserted); 330 DCHECK(inserted);
296 *request = job; 331 request->reset(new RequestImpl(std::move(job)));
297 332
298 return ERR_IO_PENDING; 333 return ERR_IO_PENDING;
299 } 334 }
300 335
301 void ProxyResolverMojo::CancelRequest(RequestHandle request) {
302 DCHECK(thread_checker_.CalledOnValidThread());
303 Job* job = static_cast<Job*>(request);
304 DCHECK(job);
305 job->Cancel();
306 RemoveJob(job);
307 }
308 336
309 LoadState ProxyResolverMojo::GetLoadState(RequestHandle request) const {
310 Job* job = static_cast<Job*>(request);
311 CHECK_EQ(1u, pending_jobs_.count(job));
312 return job->GetLoadState();
313 }
314 337
315 } // namespace 338 } // namespace
316 339
317 // A Job to create a ProxyResolver instance. 340 // A Job to create a ProxyResolver instance.
318 // 341 //
319 // Note: a Job instance is not tied to a particular resolve request, and hence 342 // Note: a Job instance is not tied to a particular resolve request, and hence
320 // there is no per-request logging to be done (any netlog events are only sent 343 // there is no per-request logging to be done (any netlog events are only sent
321 // globally) so this always uses an empty BoundNetLog. 344 // globally) so this always uses an empty BoundNetLog.
322 class ProxyResolverFactoryMojo::Job 345 class ProxyResolverFactoryMojo::Job
323 : public ClientMixin<interfaces::ProxyResolverFactoryRequestClient>, 346 : public ClientMixin<interfaces::ProxyResolverFactoryRequestClient>,
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
401 return ERR_PAC_SCRIPT_FAILED; 424 return ERR_PAC_SCRIPT_FAILED;
402 } 425 }
403 request->reset(new Job(this, pac_script, resolver, callback, 426 request->reset(new Job(this, pac_script, resolver, callback,
404 error_observer_factory_.is_null() 427 error_observer_factory_.is_null()
405 ? nullptr 428 ? nullptr
406 : error_observer_factory_.Run())); 429 : error_observer_factory_.Run()));
407 return ERR_IO_PENDING; 430 return ERR_IO_PENDING;
408 } 431 }
409 432
410 } // namespace net 433 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698