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

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

Issue 2845643003: Allow ProxyService to share URLRequestContext with everything else. (Closed)
Patch Set: Fix test Created 3 years, 7 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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_script_decider.h" 5 #include "net/proxy/proxy_script_decider.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/bind_helpers.h" 10 #include "base/bind_helpers.h"
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 DhcpProxyScriptFetcher* dhcp_proxy_script_fetcher, 85 DhcpProxyScriptFetcher* dhcp_proxy_script_fetcher,
86 NetLog* net_log) 86 NetLog* net_log)
87 : proxy_script_fetcher_(proxy_script_fetcher), 87 : proxy_script_fetcher_(proxy_script_fetcher),
88 dhcp_proxy_script_fetcher_(dhcp_proxy_script_fetcher), 88 dhcp_proxy_script_fetcher_(dhcp_proxy_script_fetcher),
89 current_pac_source_index_(0u), 89 current_pac_source_index_(0u),
90 pac_mandatory_(false), 90 pac_mandatory_(false),
91 next_state_(STATE_NONE), 91 next_state_(STATE_NONE),
92 net_log_(NetLogWithSource::Make(net_log, 92 net_log_(NetLogWithSource::Make(net_log,
93 NetLogSourceType::PROXY_SCRIPT_DECIDER)), 93 NetLogSourceType::PROXY_SCRIPT_DECIDER)),
94 fetch_pac_bytes_(false), 94 fetch_pac_bytes_(false),
95 quick_check_enabled_(true), 95 quick_check_enabled_(true) {}
96 host_resolver_(nullptr) {
97 if (proxy_script_fetcher &&
98 proxy_script_fetcher->GetRequestContext() &&
99 proxy_script_fetcher->GetRequestContext()->host_resolver()) {
100 host_resolver_ = proxy_script_fetcher->GetRequestContext()->host_resolver();
101 }
102 }
103 96
104 ProxyScriptDecider::~ProxyScriptDecider() { 97 ProxyScriptDecider::~ProxyScriptDecider() {
105 if (next_state_ != STATE_NONE) 98 if (next_state_ != STATE_NONE)
106 Cancel(); 99 Cancel();
107 } 100 }
108 101
109 int ProxyScriptDecider::Start( 102 int ProxyScriptDecider::Start(
110 const ProxyConfig& config, const base::TimeDelta wait_delay, 103 const ProxyConfig& config, const base::TimeDelta wait_delay,
111 bool fetch_pac_bytes, const CompletionCallback& callback) { 104 bool fetch_pac_bytes, const CompletionCallback& callback) {
112 DCHECK_EQ(STATE_NONE, next_state_); 105 DCHECK_EQ(STATE_NONE, next_state_);
(...skipping 19 matching lines...) Expand all
132 125
133 int rv = DoLoop(OK); 126 int rv = DoLoop(OK);
134 if (rv == ERR_IO_PENDING) 127 if (rv == ERR_IO_PENDING)
135 callback_ = callback; 128 callback_ = callback;
136 else 129 else
137 DidComplete(); 130 DidComplete();
138 131
139 return rv; 132 return rv;
140 } 133 }
141 134
135 void ProxyScriptDecider::OnShutdown() {
136 // Don't do anything if idle.
137 if (next_state_ == STATE_NONE)
138 return;
139
140 CompletionCallback callback = std::move(callback_);
141
142 // Just cancel any pending work.
143 Cancel();
144
145 if (callback)
146 callback.Run(ERR_CONTEXT_SHUT_DOWN);
147 }
148
142 const ProxyConfig& ProxyScriptDecider::effective_config() const { 149 const ProxyConfig& ProxyScriptDecider::effective_config() const {
143 DCHECK_EQ(STATE_NONE, next_state_); 150 DCHECK_EQ(STATE_NONE, next_state_);
144 return effective_config_; 151 return effective_config_;
145 } 152 }
146 153
147 const scoped_refptr<ProxyResolverScriptData>& 154 const scoped_refptr<ProxyResolverScriptData>&
148 ProxyScriptDecider::script_data() const { 155 ProxyScriptDecider::script_data() const {
149 DCHECK_EQ(STATE_NONE, next_state_); 156 DCHECK_EQ(STATE_NONE, next_state_);
150 return script_data_; 157 return script_data_;
151 } 158 }
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
248 } 255 }
249 if (quick_check_enabled_ && current_pac_source().type == PacSource::WPAD_DNS) 256 if (quick_check_enabled_ && current_pac_source().type == PacSource::WPAD_DNS)
250 next_state_ = STATE_QUICK_CHECK; 257 next_state_ = STATE_QUICK_CHECK;
251 else 258 else
252 next_state_ = GetStartState(); 259 next_state_ = GetStartState();
253 return OK; 260 return OK;
254 } 261 }
255 262
256 int ProxyScriptDecider::DoQuickCheck() { 263 int ProxyScriptDecider::DoQuickCheck() {
257 DCHECK(quick_check_enabled_); 264 DCHECK(quick_check_enabled_);
258 if (host_resolver_ == nullptr) { 265 if (!proxy_script_fetcher_ || !proxy_script_fetcher_->GetRequestContext() ||
266 !proxy_script_fetcher_->GetRequestContext()->host_resolver()) {
259 // If we have no resolver, skip QuickCheck altogether. 267 // If we have no resolver, skip QuickCheck altogether.
260 next_state_ = GetStartState(); 268 next_state_ = GetStartState();
261 return OK; 269 return OK;
262 } 270 }
263 271
264 quick_check_start_time_ = base::Time::Now(); 272 quick_check_start_time_ = base::Time::Now();
265 std::string host = current_pac_source().url.host(); 273 std::string host = current_pac_source().url.host();
266 HostResolver::RequestInfo reqinfo(HostPortPair(host, 80)); 274 HostResolver::RequestInfo reqinfo(HostPortPair(host, 80));
267 reqinfo.set_host_resolver_flags(HOST_RESOLVER_SYSTEM_ONLY); 275 reqinfo.set_host_resolver_flags(HOST_RESOLVER_SYSTEM_ONLY);
268 CompletionCallback callback = base::Bind( 276 CompletionCallback callback = base::Bind(
269 &ProxyScriptDecider::OnIOCompletion, 277 &ProxyScriptDecider::OnIOCompletion,
270 base::Unretained(this)); 278 base::Unretained(this));
271 279
272 next_state_ = STATE_QUICK_CHECK_COMPLETE; 280 next_state_ = STATE_QUICK_CHECK_COMPLETE;
273 quick_check_timer_.Start(FROM_HERE, 281 quick_check_timer_.Start(FROM_HERE,
274 base::TimeDelta::FromMilliseconds( 282 base::TimeDelta::FromMilliseconds(
275 kQuickCheckDelayMs), 283 kQuickCheckDelayMs),
276 base::Bind(callback, ERR_NAME_NOT_RESOLVED)); 284 base::Bind(callback, ERR_NAME_NOT_RESOLVED));
277 285
286 HostResolver* host_resolver =
287 proxy_script_fetcher_->GetRequestContext()->host_resolver();
288
278 // We use HIGHEST here because proxy decision blocks doing any other requests. 289 // We use HIGHEST here because proxy decision blocks doing any other requests.
279 return host_resolver_->Resolve(reqinfo, HIGHEST, &wpad_addresses_, callback, 290 return host_resolver->Resolve(reqinfo, HIGHEST, &wpad_addresses_, callback,
280 &request_, net_log_); 291 &request_, net_log_);
281 } 292 }
282 293
283 int ProxyScriptDecider::DoQuickCheckComplete(int result) { 294 int ProxyScriptDecider::DoQuickCheckComplete(int result) {
284 DCHECK(quick_check_enabled_); 295 DCHECK(quick_check_enabled_);
285 base::TimeDelta delta = base::Time::Now() - quick_check_start_time_; 296 base::TimeDelta delta = base::Time::Now() - quick_check_start_time_;
286 if (result == OK) 297 if (result == OK)
287 UMA_HISTOGRAM_TIMES("Net.WpadQuickCheckSuccess", delta); 298 UMA_HISTOGRAM_TIMES("Net.WpadQuickCheckSuccess", delta);
288 else 299 else
289 UMA_HISTOGRAM_TIMES("Net.WpadQuickCheckFailure", delta); 300 UMA_HISTOGRAM_TIMES("Net.WpadQuickCheckFailure", delta);
290 request_.reset(); 301 request_.reset();
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
458 void ProxyScriptDecider::DidComplete() { 469 void ProxyScriptDecider::DidComplete() {
459 net_log_.EndEvent(NetLogEventType::PROXY_SCRIPT_DECIDER); 470 net_log_.EndEvent(NetLogEventType::PROXY_SCRIPT_DECIDER);
460 } 471 }
461 472
462 void ProxyScriptDecider::Cancel() { 473 void ProxyScriptDecider::Cancel() {
463 DCHECK_NE(STATE_NONE, next_state_); 474 DCHECK_NE(STATE_NONE, next_state_);
464 475
465 net_log_.AddEvent(NetLogEventType::CANCELLED); 476 net_log_.AddEvent(NetLogEventType::CANCELLED);
466 477
467 switch (next_state_) { 478 switch (next_state_) {
479 case STATE_QUICK_CHECK_COMPLETE:
480 request_.reset();
mmenke 2017/05/01 16:52:14 This could just be put in OnShutdown(), but not ha
481 break;
468 case STATE_WAIT_COMPLETE: 482 case STATE_WAIT_COMPLETE:
469 wait_timer_.Stop(); 483 wait_timer_.Stop();
470 break; 484 break;
471 case STATE_FETCH_PAC_SCRIPT_COMPLETE: 485 case STATE_FETCH_PAC_SCRIPT_COMPLETE:
472 proxy_script_fetcher_->Cancel(); 486 proxy_script_fetcher_->Cancel();
473 break; 487 break;
474 default: 488 default:
475 break; 489 break;
476 } 490 }
477 491
492 next_state_ = STATE_NONE;
493
478 // This is safe to call in any state. 494 // This is safe to call in any state.
479 if (dhcp_proxy_script_fetcher_) 495 if (dhcp_proxy_script_fetcher_)
480 dhcp_proxy_script_fetcher_->Cancel(); 496 dhcp_proxy_script_fetcher_->Cancel();
481 497
498 DCHECK(!request_);
499
482 DidComplete(); 500 DidComplete();
483 } 501 }
484 502
485 } // namespace net 503 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698