OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/renderer/net/net_error_helper_core.h" | 5 #include "chrome/renderer/net/net_error_helper_core.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | |
10 #include "base/callback.h" | |
11 #include "base/location.h" | |
9 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
10 #include "chrome/common/localized_error.h" | 13 #include "chrome/common/localized_error.h" |
11 #include "net/base/escape.h" | 14 #include "net/base/escape.h" |
12 #include "net/base/net_errors.h" | 15 #include "net/base/net_errors.h" |
13 #include "third_party/WebKit/public/platform/WebString.h" | 16 #include "third_party/WebKit/public/platform/WebString.h" |
14 #include "third_party/WebKit/public/platform/WebURLError.h" | 17 #include "third_party/WebKit/public/platform/WebURLError.h" |
15 #include "url/gurl.h" | 18 #include "url/gurl.h" |
16 | 19 |
17 namespace { | 20 namespace { |
18 | 21 |
22 base::TimeDelta GetAutoReloadTime(size_t reload_count) { | |
23 static const int kDelaysMs[] = { | |
24 0, 5000, 30000, 60000, 300000, 600000, 1800000 | |
25 }; | |
26 if (reload_count >= arraysize(kDelaysMs)) | |
27 reload_count = arraysize(kDelaysMs) - 1; | |
28 return base::TimeDelta::FromMilliseconds(kDelaysMs[reload_count]); | |
29 } | |
30 | |
19 // Returns whether |net_error| is a DNS-related error (and therefore whether | 31 // Returns whether |net_error| is a DNS-related error (and therefore whether |
20 // the tab helper should start a DNS probe after receiving it.) | 32 // the tab helper should start a DNS probe after receiving it.) |
21 bool IsDnsError(const blink::WebURLError& error) { | 33 bool IsDnsError(const blink::WebURLError& error) { |
22 return error.domain.utf8() == net::kErrorDomain && | 34 return error.domain.utf8() == net::kErrorDomain && |
23 (error.reason == net::ERR_NAME_NOT_RESOLVED || | 35 (error.reason == net::ERR_NAME_NOT_RESOLVED || |
24 error.reason == net::ERR_NAME_RESOLUTION_FAILED); | 36 error.reason == net::ERR_NAME_RESOLUTION_FAILED); |
25 } | 37 } |
26 | 38 |
39 bool IsReloadableError(const blink::WebURLError& error) { | |
40 return error.domain.utf8() == net::kErrorDomain && | |
41 error.reason != net::ERR_ABORTED; | |
42 } | |
43 | |
27 // If an alternate error page should be retrieved remotely for a main frame load | 44 // If an alternate error page should be retrieved remotely for a main frame load |
28 // that failed with |error|, returns true and sets |error_page_url| to the URL | 45 // that failed with |error|, returns true and sets |error_page_url| to the URL |
29 // of the remote error page. | 46 // of the remote error page. |
30 bool GetErrorPageURL(const blink::WebURLError& error, | 47 bool GetErrorPageURL(const blink::WebURLError& error, |
31 const GURL& alt_error_page_url, | 48 const GURL& alt_error_page_url, |
32 GURL* error_page_url) { | 49 GURL* error_page_url) { |
33 if (!alt_error_page_url.is_valid()) | 50 if (!alt_error_page_url.is_valid()) |
34 return false; | 51 return false; |
35 | 52 |
36 // Parameter to send to the error page indicating the error type. | 53 // Parameter to send to the error page indicating the error type. |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
109 // valid URL. Request will be issued when the error page finishes loading. | 126 // valid URL. Request will be issued when the error page finishes loading. |
110 // This is done on load complete to ensure that there are two complete loads | 127 // This is done on load complete to ensure that there are two complete loads |
111 // for tests to wait for. | 128 // for tests to wait for. |
112 GURL alternate_error_page_url; | 129 GURL alternate_error_page_url; |
113 | 130 |
114 // True if a page has completed loading, at which point it can receive | 131 // True if a page has completed loading, at which point it can receive |
115 // updates. | 132 // updates. |
116 bool is_finished_loading; | 133 bool is_finished_loading; |
117 }; | 134 }; |
118 | 135 |
119 NetErrorHelperCore::NetErrorHelperCore(Delegate* delegate) | 136 NetErrorHelperCore::NetErrorHelperCore(Delegate* delegate, |
137 scoped_ptr<MockableOneShotTimer> reload_timer) | |
120 : delegate_(delegate), | 138 : delegate_(delegate), |
121 last_probe_status_(chrome_common_net::DNS_PROBE_POSSIBLE) { | 139 last_probe_status_(chrome_common_net::DNS_PROBE_POSSIBLE), |
140 auto_reload_enabled_(false), | |
141 auto_reload_timer_(reload_timer.Pass()), | |
142 auto_reload_count_(0), | |
143 auto_reload_pending_(false), | |
144 online_(true) { | |
145 LOG(ERROR) << "NetErrorHelperCore: allocated"; | |
122 } | 146 } |
123 | 147 |
124 NetErrorHelperCore::~NetErrorHelperCore() { | 148 NetErrorHelperCore::~NetErrorHelperCore() { |
125 } | 149 } |
126 | 150 |
127 void NetErrorHelperCore::OnStop() { | 151 void NetErrorHelperCore::CancelPendingFetches() { |
128 // On stop, cancel loading the alternate error page, and prevent any pending | 152 // Cancel loading the alternate error page, and prevent any pending error page |
129 // error page load from starting a new error page load. Swapping in the error | 153 // load from starting a new error page load. Swapping in the error page when |
130 // page when it's finished loading could abort the navigation, otherwise. | 154 // it's finished loading could abort the navigation, otherwise. |
131 if (committed_error_page_info_) | 155 if (committed_error_page_info_) { |
132 committed_error_page_info_->alternate_error_page_url = GURL(); | 156 committed_error_page_info_->alternate_error_page_url = GURL(); |
157 } | |
133 if (pending_error_page_info_) | 158 if (pending_error_page_info_) |
134 pending_error_page_info_->alternate_error_page_url = GURL(); | 159 pending_error_page_info_->alternate_error_page_url = GURL(); |
135 delegate_->CancelFetchErrorPage(); | 160 delegate_->CancelFetchErrorPage(); |
161 auto_reload_timer_->Stop(); | |
162 } | |
163 | |
164 void NetErrorHelperCore::OnStop() { | |
165 CancelPendingFetches(); | |
166 if (auto_reload_pending_) { | |
167 HISTOGRAM_CUSTOM_ENUMERATION("Net.AutoReload.ErrorAtStop", | |
168 committed_error_page_info_->error.reason, | |
169 net::GetAllErrorCodesForUma()); | |
170 HISTOGRAM_COUNTS("Net.AutoReload.CountAtStop", | |
171 auto_reload_count_); | |
172 } | |
173 auto_reload_count_ = 0; | |
174 auto_reload_pending_ = false; | |
136 } | 175 } |
137 | 176 |
138 void NetErrorHelperCore::OnStartLoad(FrameType frame_type, PageType page_type) { | 177 void NetErrorHelperCore::OnStartLoad(FrameType frame_type, PageType page_type) { |
139 if (frame_type != MAIN_FRAME) | 178 if (frame_type != MAIN_FRAME) |
140 return; | 179 return; |
141 | 180 |
142 // If there's no pending error page information associated with the page load, | 181 // If there's no pending error page information associated with the page load, |
143 // or the new page is not an error page, then reset pending error page state. | 182 // or the new page is not an error page, then reset pending error page state. |
144 if (!pending_error_page_info_ || page_type != ERROR_PAGE) { | 183 if (!pending_error_page_info_ || page_type != ERROR_PAGE) { |
145 OnStop(); | 184 CancelPendingFetches(); |
146 } | 185 } |
147 } | 186 } |
148 | 187 |
149 void NetErrorHelperCore::OnCommitLoad(FrameType frame_type) { | 188 void NetErrorHelperCore::OnCommitLoad(FrameType frame_type) { |
150 if (frame_type != MAIN_FRAME) | 189 if (frame_type != MAIN_FRAME) |
151 return; | 190 return; |
152 | 191 |
192 if (committed_error_page_info_ && !pending_error_page_info_ && | |
193 auto_reload_pending_) { | |
194 int reason = committed_error_page_info_->error.reason; | |
195 HISTOGRAM_CUSTOM_ENUMERATION("Net.AutoReload.ErrorAtSuccess", | |
196 reason, | |
197 net::GetAllErrorCodesForUma()); | |
198 if (auto_reload_count_ == 1) | |
199 HISTOGRAM_CUSTOM_ENUMERATION("Net.AutoReload.ErrorAtFirstSuccess", | |
200 reason, | |
201 net::GetAllErrorCodesForUma()); | |
202 } | |
203 | |
153 committed_error_page_info_.reset(pending_error_page_info_.release()); | 204 committed_error_page_info_.reset(pending_error_page_info_.release()); |
154 } | 205 } |
155 | 206 |
156 void NetErrorHelperCore::OnFinishLoad(FrameType frame_type) { | 207 void NetErrorHelperCore::OnFinishLoad(FrameType frame_type) { |
157 if (frame_type != MAIN_FRAME || !committed_error_page_info_) | 208 if (frame_type != MAIN_FRAME) |
158 return; | 209 return; |
159 | 210 |
211 if (!committed_error_page_info_) { | |
212 if (auto_reload_pending_) | |
213 HISTOGRAM_COUNTS("Net.AutoReload.CountAtSuccess", auto_reload_count_); | |
Randy Smith (Not in Mondays)
2014/02/26 00:07:38
Suggestion: I'd be inclined to move this histogram
Elly Fong-Jones
2014/03/03 19:31:07
Done.
| |
214 auto_reload_count_ = 0; | |
215 auto_reload_pending_ = false; | |
216 return; | |
217 } | |
218 | |
160 committed_error_page_info_->is_finished_loading = true; | 219 committed_error_page_info_->is_finished_loading = true; |
161 | 220 |
162 if (committed_error_page_info_->alternate_error_page_url.is_valid()) { | 221 if (committed_error_page_info_->alternate_error_page_url.is_valid()) { |
163 // If there is another pending error page load, | 222 // If there is another pending error page load, |
164 // |replace_with_alternate_error_page| should have been set to false. | 223 // |replace_with_alternate_error_page| should have been set to false. |
165 DCHECK(!pending_error_page_info_); | 224 DCHECK(!pending_error_page_info_); |
166 DCHECK(!committed_error_page_info_->needs_dns_updates); | 225 DCHECK(!committed_error_page_info_->needs_dns_updates); |
167 GURL error_page_url; | 226 GURL error_page_url; |
168 delegate_->FetchErrorPage( | 227 delegate_->FetchErrorPage( |
169 committed_error_page_info_->alternate_error_page_url); | 228 committed_error_page_info_->alternate_error_page_url); |
229 } else if (auto_reload_enabled_) { | |
230 auto_reload_pending_ = true; | |
231 MaybeStartAutoReloadTimer(); | |
170 } | 232 } |
171 | 233 |
172 if (!committed_error_page_info_->needs_dns_updates || | 234 if (!committed_error_page_info_->needs_dns_updates || |
173 last_probe_status_ == chrome_common_net::DNS_PROBE_POSSIBLE) { | 235 last_probe_status_ == chrome_common_net::DNS_PROBE_POSSIBLE) { |
174 return; | 236 return; |
175 } | 237 } |
176 DVLOG(1) << "Error page finished loading; sending saved status."; | 238 DVLOG(1) << "Error page finished loading; sending saved status."; |
177 UpdateErrorPage(); | 239 UpdateErrorPage(); |
178 } | 240 } |
179 | 241 |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
309 } | 371 } |
310 | 372 |
311 blink::WebURLError updated_error; | 373 blink::WebURLError updated_error; |
312 updated_error.domain = blink::WebString::fromUTF8( | 374 updated_error.domain = blink::WebString::fromUTF8( |
313 chrome_common_net::kDnsProbeErrorDomain); | 375 chrome_common_net::kDnsProbeErrorDomain); |
314 updated_error.reason = last_probe_status_; | 376 updated_error.reason = last_probe_status_; |
315 updated_error.unreachableURL = error.unreachableURL; | 377 updated_error.unreachableURL = error.unreachableURL; |
316 | 378 |
317 return updated_error; | 379 return updated_error; |
318 } | 380 } |
381 | |
382 void NetErrorHelperCore::Reload() { | |
383 if (!committed_error_page_info_) { | |
384 return; | |
385 } | |
386 delegate_->ReloadPage(); | |
387 } | |
388 | |
389 bool NetErrorHelperCore::MaybeStartAutoReloadTimer() { | |
390 if (!committed_error_page_info_) | |
391 return false; | |
392 | |
393 if (!IsReloadableError(committed_error_page_info_->error)) | |
394 return false; | |
395 | |
396 if (committed_error_page_info_->was_failed_post) | |
397 return false; | |
398 | |
399 if (!online_) | |
400 return false; | |
401 | |
402 if (!auto_reload_pending_) | |
403 return false; | |
404 | |
405 StartAutoReloadTimer(); | |
406 return true; | |
407 } | |
408 | |
409 void NetErrorHelperCore::StartAutoReloadTimer() { | |
410 base::TimeDelta delay = GetAutoReloadTime(auto_reload_count_); | |
411 auto_reload_count_++; | |
412 auto_reload_timer_->Stop(); | |
413 auto_reload_timer_->Start(FROM_HERE, delay, | |
414 base::Bind(&NetErrorHelperCore::Reload, | |
415 base::Unretained(this))); | |
416 } | |
417 | |
418 // Handler for NetworkStateChanged notification from the browser process. If the | |
419 // network state changes to online, this method is responsible for starting the | |
420 // auto-reload process. | |
421 // | |
422 // Warning: if there are many tabs sitting at an error page, this handler will | |
423 // be run at the same time for each of their top-level renderframes, which can | |
424 // cause many requests to be started at the same time. There's no current | |
425 // protection against this kind of "reload storm". | |
426 // | |
427 // TODO(rdsmith): prevent the reload storm. | |
428 void NetErrorHelperCore::NetworkStateChanged(bool online) { | |
429 online_ = online; | |
430 if (auto_reload_timer_->IsRunning()) { | |
431 // If there's an existing timer running, stop it and reset the retry count. | |
432 auto_reload_timer_->Stop(); | |
433 auto_reload_count_ = 0; | |
434 } | |
435 | |
436 if (online) | |
437 // If we just got online, maybe start auto-reloading again. | |
438 MaybeStartAutoReloadTimer(); | |
mmenke
2014/02/25 22:09:52
Use braces for multi-line if blocks, or put the co
Elly Fong-Jones
2014/03/03 19:31:07
Done.
| |
439 } | |
440 | |
441 bool NetErrorHelperCore::ShouldSuppressErrorPage(const GURL& url) { | |
442 // Don't suppress errors if not auto-reloading. | |
443 if (!auto_reload_pending_) | |
444 return false; | |
445 | |
446 // If auto_reload_timer_ is still running, this error page isn't from an auto | |
447 // reload. | |
448 if (auto_reload_timer_->IsRunning()) | |
449 return false; | |
450 | |
451 // If there's no committed error page, this error page wasn't from an auto | |
452 // reload. | |
453 if (!committed_error_page_info_) | |
454 return false; | |
455 | |
456 GURL error_url = committed_error_page_info_->error.unreachableURL; | |
457 if (error_url != url) | |
458 return false; | |
459 | |
460 MaybeStartAutoReloadTimer(); | |
461 return true; | |
462 } | |
OLD | NEW |