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

Side by Side Diff: chrome/renderer/net/net_error_helper_core.cc

Issue 136203009: Support auto-reload on errors. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@lkgr
Patch Set: Clean up nits Created 6 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 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
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 auto_reload_count_ = 0;
166 auto_reload_pending_ = false;
136 } 167 }
137 168
138 void NetErrorHelperCore::OnStartLoad(FrameType frame_type, PageType page_type) { 169 void NetErrorHelperCore::OnStartLoad(FrameType frame_type, PageType page_type) {
139 if (frame_type != MAIN_FRAME) 170 if (frame_type != MAIN_FRAME)
140 return; 171 return;
141 172
142 // If there's no pending error page information associated with the page load, 173 // 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. 174 // 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) { 175 if (!pending_error_page_info_ || page_type != ERROR_PAGE) {
145 OnStop(); 176 CancelPendingFetches();
146 } 177 }
147 } 178 }
148 179
149 void NetErrorHelperCore::OnCommitLoad(FrameType frame_type) { 180 void NetErrorHelperCore::OnCommitLoad(FrameType frame_type) {
150 if (frame_type != MAIN_FRAME) 181 if (frame_type != MAIN_FRAME)
151 return; 182 return;
152 183
153 committed_error_page_info_.reset(pending_error_page_info_.release()); 184 committed_error_page_info_.reset(pending_error_page_info_.release());
154 } 185 }
155 186
156 void NetErrorHelperCore::OnFinishLoad(FrameType frame_type) { 187 void NetErrorHelperCore::OnFinishLoad(FrameType frame_type) {
157 if (frame_type != MAIN_FRAME || !committed_error_page_info_) 188 if (frame_type != MAIN_FRAME)
158 return; 189 return;
159 190
191 if (!committed_error_page_info_) {
192 auto_reload_count_ = 0;
193 auto_reload_pending_ = false;
194 return;
195 }
196
160 committed_error_page_info_->is_finished_loading = true; 197 committed_error_page_info_->is_finished_loading = true;
161 198
162 if (committed_error_page_info_->alternate_error_page_url.is_valid()) { 199 if (committed_error_page_info_->alternate_error_page_url.is_valid()) {
163 // If there is another pending error page load, 200 // If there is another pending error page load,
164 // |replace_with_alternate_error_page| should have been set to false. 201 // |replace_with_alternate_error_page| should have been set to false.
165 DCHECK(!pending_error_page_info_); 202 DCHECK(!pending_error_page_info_);
166 DCHECK(!committed_error_page_info_->needs_dns_updates); 203 DCHECK(!committed_error_page_info_->needs_dns_updates);
167 GURL error_page_url; 204 GURL error_page_url;
168 delegate_->FetchErrorPage( 205 delegate_->FetchErrorPage(
169 committed_error_page_info_->alternate_error_page_url); 206 committed_error_page_info_->alternate_error_page_url);
207 } else if (auto_reload_enabled_) {
208 auto_reload_pending_ = true;
209 MaybeStartAutoReloadTimer();
170 } 210 }
171 211
172 if (!committed_error_page_info_->needs_dns_updates || 212 if (!committed_error_page_info_->needs_dns_updates ||
173 last_probe_status_ == chrome_common_net::DNS_PROBE_POSSIBLE) { 213 last_probe_status_ == chrome_common_net::DNS_PROBE_POSSIBLE) {
174 return; 214 return;
175 } 215 }
176 DVLOG(1) << "Error page finished loading; sending saved status."; 216 DVLOG(1) << "Error page finished loading; sending saved status.";
177 UpdateErrorPage(); 217 UpdateErrorPage();
178 } 218 }
179 219
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
309 } 349 }
310 350
311 blink::WebURLError updated_error; 351 blink::WebURLError updated_error;
312 updated_error.domain = blink::WebString::fromUTF8( 352 updated_error.domain = blink::WebString::fromUTF8(
313 chrome_common_net::kDnsProbeErrorDomain); 353 chrome_common_net::kDnsProbeErrorDomain);
314 updated_error.reason = last_probe_status_; 354 updated_error.reason = last_probe_status_;
315 updated_error.unreachableURL = error.unreachableURL; 355 updated_error.unreachableURL = error.unreachableURL;
316 356
317 return updated_error; 357 return updated_error;
318 } 358 }
359
360 void NetErrorHelperCore::Reload() {
361 if (!committed_error_page_info_) {
362 return;
363 }
364 delegate_->ReloadPage();
365 }
366
367 bool NetErrorHelperCore::MaybeStartAutoReloadTimer() {
368 if (!committed_error_page_info_)
369 return false;
370
371 if (!IsReloadableError(committed_error_page_info_->error))
372 return false;
373
374 if (committed_error_page_info_->was_failed_post)
375 return false;
376
377 if (!online_)
378 return false;
379
380 if (!auto_reload_pending_)
381 return false;
382
383 StartAutoReloadTimer();
384 return true;
385 }
386
387 void NetErrorHelperCore::StartAutoReloadTimer() {
388 base::TimeDelta delay = GetAutoReloadTime(auto_reload_count_);
389 auto_reload_count_++;
390 auto_reload_timer_->Stop();
391 auto_reload_timer_->Start(FROM_HERE, delay,
392 base::Bind(&NetErrorHelperCore::Reload,
393 base::Unretained(this)));
394 }
395
396 // Handler for NetworkStateChanged notification from the browser process. If the
397 // network state changes to online, this method is responsible for starting the
398 // auto-reload process.
399 //
400 // Warning: if there are many tabs sitting at an error page, this handler will
401 // be run at the same time for each of their top-level renderframes, which can
402 // cause many requests to be started at the same time. There's no current
403 // protection against this kind of "reload storm".
404 //
405 // TODO(rdsmith): prevent the reload storm.
406 void NetErrorHelperCore::NetworkStateChanged(bool online) {
407 online_ = online;
408 if (auto_reload_timer_->IsRunning()) {
409 // If there's an existing timer running, stop it and reset the retry count.
410 auto_reload_timer_->Stop();
411 auto_reload_count_ = 0;
412 }
413
414 if (online)
415 // If we just got online, maybe start auto-reloading again.
416 MaybeStartAutoReloadTimer();
417 }
418
419 bool NetErrorHelperCore::ShouldSuppressErrorPage(const GURL& url) {
420 // Don't suppress errors if not auto-reloading.
421 if (!auto_reload_pending_)
422 return false;
423
424 // If auto_reload_timer_ is still running, this error page isn't from an auto
425 // reload.
426 if (auto_reload_timer_->IsRunning())
427 return false;
428
429 // If there's no committed error page, this error page wasn't from an auto
430 // reload.
431 if (!committed_error_page_info_)
432 return false;
433
434 GURL error_url = committed_error_page_info_->error.unreachableURL;
435 if (error_url != url)
436 return false;
437
438 MaybeStartAutoReloadTimer();
439 return true;
440 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698