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) { |
122 } | 145 } |
123 | 146 |
124 NetErrorHelperCore::~NetErrorHelperCore() { | 147 NetErrorHelperCore::~NetErrorHelperCore() { |
125 } | 148 } |
126 | 149 |
127 void NetErrorHelperCore::OnStop() { | 150 void NetErrorHelperCore::CancelPendingFetches() { |
128 // On stop, cancel loading the alternate error page, and prevent any pending | 151 // 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 | 152 // 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. | 153 // it's finished loading could abort the navigation, otherwise. |
131 if (committed_error_page_info_) | 154 if (committed_error_page_info_) { |
132 committed_error_page_info_->alternate_error_page_url = GURL(); | 155 committed_error_page_info_->alternate_error_page_url = GURL(); |
| 156 } |
133 if (pending_error_page_info_) | 157 if (pending_error_page_info_) |
134 pending_error_page_info_->alternate_error_page_url = GURL(); | 158 pending_error_page_info_->alternate_error_page_url = GURL(); |
135 delegate_->CancelFetchErrorPage(); | 159 delegate_->CancelFetchErrorPage(); |
| 160 auto_reload_timer_->Stop(); |
| 161 } |
| 162 |
| 163 void NetErrorHelperCore::OnStop() { |
| 164 CancelPendingFetches(); |
| 165 if (auto_reload_pending_) { |
| 166 HISTOGRAM_CUSTOM_ENUMERATION("Net.AutoReload.ErrorAtStop", |
| 167 committed_error_page_info_->error.reason, |
| 168 net::GetAllErrorCodesForUma()); |
| 169 HISTOGRAM_COUNTS("Net.AutoReload.CountAtStop", |
| 170 auto_reload_count_); |
| 171 } |
| 172 auto_reload_count_ = 0; |
| 173 auto_reload_pending_ = false; |
136 } | 174 } |
137 | 175 |
138 void NetErrorHelperCore::OnStartLoad(FrameType frame_type, PageType page_type) { | 176 void NetErrorHelperCore::OnStartLoad(FrameType frame_type, PageType page_type) { |
139 if (frame_type != MAIN_FRAME) | 177 if (frame_type != MAIN_FRAME) |
140 return; | 178 return; |
141 | 179 |
142 // If there's no pending error page information associated with the page load, | 180 // 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. | 181 // 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) { | 182 if (!pending_error_page_info_ || page_type != ERROR_PAGE) { |
145 OnStop(); | 183 CancelPendingFetches(); |
146 } | 184 } |
147 } | 185 } |
148 | 186 |
149 void NetErrorHelperCore::OnCommitLoad(FrameType frame_type) { | 187 void NetErrorHelperCore::OnCommitLoad(FrameType frame_type) { |
150 if (frame_type != MAIN_FRAME) | 188 if (frame_type != MAIN_FRAME) |
151 return; | 189 return; |
152 | 190 |
| 191 if (committed_error_page_info_ && !pending_error_page_info_ && |
| 192 auto_reload_pending_) { |
| 193 int reason = committed_error_page_info_->error.reason; |
| 194 HISTOGRAM_CUSTOM_ENUMERATION("Net.AutoReload.ErrorAtSuccess", |
| 195 reason, |
| 196 net::GetAllErrorCodesForUma()); |
| 197 if (auto_reload_count_ == 1) |
| 198 HISTOGRAM_CUSTOM_ENUMERATION("Net.AutoReload.ErrorAtFirstSuccess", |
| 199 reason, |
| 200 net::GetAllErrorCodesForUma()); |
| 201 } |
| 202 |
153 committed_error_page_info_.reset(pending_error_page_info_.release()); | 203 committed_error_page_info_.reset(pending_error_page_info_.release()); |
154 } | 204 } |
155 | 205 |
156 void NetErrorHelperCore::OnFinishLoad(FrameType frame_type) { | 206 void NetErrorHelperCore::OnFinishLoad(FrameType frame_type) { |
157 if (frame_type != MAIN_FRAME || !committed_error_page_info_) | 207 if (frame_type != MAIN_FRAME) |
158 return; | 208 return; |
159 | 209 |
| 210 if (!committed_error_page_info_) { |
| 211 if (auto_reload_pending_) |
| 212 HISTOGRAM_COUNTS("Net.AutoReload.CountAtSuccess", auto_reload_count_); |
| 213 auto_reload_count_ = 0; |
| 214 auto_reload_pending_ = false; |
| 215 return; |
| 216 } |
| 217 |
160 committed_error_page_info_->is_finished_loading = true; | 218 committed_error_page_info_->is_finished_loading = true; |
161 | 219 |
162 if (committed_error_page_info_->alternate_error_page_url.is_valid()) { | 220 if (committed_error_page_info_->alternate_error_page_url.is_valid()) { |
163 // If there is another pending error page load, | 221 // If there is another pending error page load, |
164 // |replace_with_alternate_error_page| should have been set to false. | 222 // |replace_with_alternate_error_page| should have been set to false. |
165 DCHECK(!pending_error_page_info_); | 223 DCHECK(!pending_error_page_info_); |
166 DCHECK(!committed_error_page_info_->needs_dns_updates); | 224 DCHECK(!committed_error_page_info_->needs_dns_updates); |
167 GURL error_page_url; | 225 GURL error_page_url; |
168 delegate_->FetchErrorPage( | 226 delegate_->FetchErrorPage( |
169 committed_error_page_info_->alternate_error_page_url); | 227 committed_error_page_info_->alternate_error_page_url); |
| 228 } else if (auto_reload_enabled_) { |
| 229 auto_reload_pending_ = true; |
| 230 MaybeStartAutoReloadTimer(); |
170 } | 231 } |
171 | 232 |
172 if (!committed_error_page_info_->needs_dns_updates || | 233 if (!committed_error_page_info_->needs_dns_updates || |
173 last_probe_status_ == chrome_common_net::DNS_PROBE_POSSIBLE) { | 234 last_probe_status_ == chrome_common_net::DNS_PROBE_POSSIBLE) { |
174 return; | 235 return; |
175 } | 236 } |
176 DVLOG(1) << "Error page finished loading; sending saved status."; | 237 DVLOG(1) << "Error page finished loading; sending saved status."; |
177 UpdateErrorPage(); | 238 UpdateErrorPage(); |
178 } | 239 } |
179 | 240 |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 } | 370 } |
310 | 371 |
311 blink::WebURLError updated_error; | 372 blink::WebURLError updated_error; |
312 updated_error.domain = blink::WebString::fromUTF8( | 373 updated_error.domain = blink::WebString::fromUTF8( |
313 chrome_common_net::kDnsProbeErrorDomain); | 374 chrome_common_net::kDnsProbeErrorDomain); |
314 updated_error.reason = last_probe_status_; | 375 updated_error.reason = last_probe_status_; |
315 updated_error.unreachableURL = error.unreachableURL; | 376 updated_error.unreachableURL = error.unreachableURL; |
316 | 377 |
317 return updated_error; | 378 return updated_error; |
318 } | 379 } |
| 380 |
| 381 void NetErrorHelperCore::Reload() { |
| 382 if (!committed_error_page_info_) { |
| 383 return; |
| 384 } |
| 385 delegate_->ReloadPage(); |
| 386 } |
| 387 |
| 388 bool NetErrorHelperCore::MaybeStartAutoReloadTimer() { |
| 389 if (!committed_error_page_info_) |
| 390 return false; |
| 391 |
| 392 if (!IsReloadableError(committed_error_page_info_->error)) |
| 393 return false; |
| 394 |
| 395 if (committed_error_page_info_->was_failed_post) |
| 396 return false; |
| 397 |
| 398 if (!online_) |
| 399 return false; |
| 400 |
| 401 if (!auto_reload_pending_) |
| 402 return false; |
| 403 |
| 404 StartAutoReloadTimer(); |
| 405 return true; |
| 406 } |
| 407 |
| 408 void NetErrorHelperCore::StartAutoReloadTimer() { |
| 409 base::TimeDelta delay = GetAutoReloadTime(auto_reload_count_); |
| 410 auto_reload_count_++; |
| 411 auto_reload_timer_->Stop(); |
| 412 auto_reload_timer_->Start(FROM_HERE, delay, |
| 413 base::Bind(&NetErrorHelperCore::Reload, |
| 414 base::Unretained(this))); |
| 415 } |
| 416 |
| 417 // Handler for NetworkStateChanged notification from the browser process. If the |
| 418 // network state changes to online, this method is responsible for starting the |
| 419 // auto-reload process. |
| 420 // |
| 421 // Warning: if there are many tabs sitting at an error page, this handler will |
| 422 // be run at the same time for each of their top-level renderframes, which can |
| 423 // cause many requests to be started at the same time. There's no current |
| 424 // protection against this kind of "reload storm". |
| 425 // |
| 426 // TODO(rdsmith): prevent the reload storm. |
| 427 void NetErrorHelperCore::NetworkStateChanged(bool online) { |
| 428 online_ = online; |
| 429 if (auto_reload_timer_->IsRunning()) { |
| 430 // If there's an existing timer running, stop it and reset the retry count. |
| 431 auto_reload_timer_->Stop(); |
| 432 auto_reload_count_ = 0; |
| 433 } |
| 434 |
| 435 if (online) |
| 436 // If we just got online, maybe start auto-reloading again. |
| 437 MaybeStartAutoReloadTimer(); |
| 438 } |
| 439 |
| 440 bool NetErrorHelperCore::ShouldSuppressErrorPage(const GURL& url) { |
| 441 // Don't suppress errors if not auto-reloading. |
| 442 if (!auto_reload_pending_) |
| 443 return false; |
| 444 |
| 445 // If auto_reload_timer_ is still running, this error page isn't from an auto |
| 446 // reload. |
| 447 if (auto_reload_timer_->IsRunning()) |
| 448 return false; |
| 449 |
| 450 // If there's no committed error page, this error page wasn't from an auto |
| 451 // reload. |
| 452 if (!committed_error_page_info_) |
| 453 return false; |
| 454 |
| 455 GURL error_url = committed_error_page_info_->error.unreachableURL; |
| 456 if (error_url != url) |
| 457 return false; |
| 458 |
| 459 MaybeStartAutoReloadTimer(); |
| 460 return true; |
| 461 } |
OLD | NEW |