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

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: DISALLOW_COPY_AND_ASSIGN NetErrorHelper Created 6 years, 9 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"
9 #include "base/i18n/rtl.h" 11 #include "base/i18n/rtl.h"
10 #include "base/json/json_reader.h" 12 #include "base/json/json_reader.h"
11 #include "base/json/json_writer.h" 13 #include "base/json/json_writer.h"
14 #include "base/location.h"
12 #include "base/metrics/histogram.h" 15 #include "base/metrics/histogram.h"
13 #include "base/strings/string16.h" 16 #include "base/strings/string16.h"
14 #include "base/values.h" 17 #include "base/values.h"
15 #include "chrome/common/localized_error.h" 18 #include "chrome/common/localized_error.h"
16 #include "grit/generated_resources.h" 19 #include "grit/generated_resources.h"
17 #include "net/base/escape.h" 20 #include "net/base/escape.h"
18 #include "net/base/net_errors.h" 21 #include "net/base/net_errors.h"
19 #include "net/base/net_util.h" 22 #include "net/base/net_util.h"
20 #include "third_party/WebKit/public/platform/WebString.h" 23 #include "third_party/WebKit/public/platform/WebString.h"
21 #include "third_party/WebKit/public/platform/WebURLError.h" 24 #include "third_party/WebKit/public/platform/WebURLError.h"
(...skipping 16 matching lines...) Expand all
38 {IDS_ERRORPAGES_SUGGESTION_ALTERNATE_URL, "sitemap"}, 41 {IDS_ERRORPAGES_SUGGESTION_ALTERNATE_URL, "sitemap"},
39 {IDS_ERRORPAGES_SUGGESTION_ALTERNATE_URL, "pathParentFolder"}, 42 {IDS_ERRORPAGES_SUGGESTION_ALTERNATE_URL, "pathParentFolder"},
40 // "siteSearchQuery" is not yet supported. 43 // "siteSearchQuery" is not yet supported.
41 // TODO(mmenke): Figure out what format "siteSearchQuery" uses for its 44 // TODO(mmenke): Figure out what format "siteSearchQuery" uses for its
42 // suggestions. 45 // suggestions.
43 // "webSearchQuery" has special handling. 46 // "webSearchQuery" has special handling.
44 {IDS_ERRORPAGES_SUGGESTION_ALTERNATE_URL, "contentOverlap"}, 47 {IDS_ERRORPAGES_SUGGESTION_ALTERNATE_URL, "contentOverlap"},
45 {IDS_ERRORPAGES_SUGGESTION_CORRECTED_URL, "emphasizedUrlCorrection"}, 48 {IDS_ERRORPAGES_SUGGESTION_CORRECTED_URL, "emphasizedUrlCorrection"},
46 }; 49 };
47 50
51 base::TimeDelta GetAutoReloadTime(size_t reload_count) {
52 static const int kDelaysMs[] = {
53 0, 5000, 30000, 60000, 300000, 600000, 1800000
54 };
55 if (reload_count >= arraysize(kDelaysMs))
56 reload_count = arraysize(kDelaysMs) - 1;
57 return base::TimeDelta::FromMilliseconds(kDelaysMs[reload_count]);
58 }
59
48 // Returns whether |net_error| is a DNS-related error (and therefore whether 60 // Returns whether |net_error| is a DNS-related error (and therefore whether
49 // the tab helper should start a DNS probe after receiving it.) 61 // the tab helper should start a DNS probe after receiving it.)
50 bool IsDnsError(const blink::WebURLError& error) { 62 bool IsDnsError(const blink::WebURLError& error) {
51 return error.domain.utf8() == net::kErrorDomain && 63 return error.domain.utf8() == net::kErrorDomain &&
52 (error.reason == net::ERR_NAME_NOT_RESOLVED || 64 (error.reason == net::ERR_NAME_NOT_RESOLVED ||
53 error.reason == net::ERR_NAME_RESOLUTION_FAILED); 65 error.reason == net::ERR_NAME_RESOLUTION_FAILED);
54 } 66 }
55 67
56 GURL SanitizeURL(const GURL& url) { 68 GURL SanitizeURL(const GURL& url) {
57 GURL::Replacements remove_params; 69 GURL::Replacements remove_params;
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after
264 // Request body to use when requesting corrections from a web service. 276 // Request body to use when requesting corrections from a web service.
265 // TODO(mmenke): Investigate loading the error page at the same time as 277 // TODO(mmenke): Investigate loading the error page at the same time as
266 // the blank page is loading, to get rid of these. 278 // the blank page is loading, to get rid of these.
267 std::string navigation_correction_request_body; 279 std::string navigation_correction_request_body;
268 280
269 // True if a page has completed loading, at which point it can receive 281 // True if a page has completed loading, at which point it can receive
270 // updates. 282 // updates.
271 bool is_finished_loading; 283 bool is_finished_loading;
272 }; 284 };
273 285
286 bool NetErrorHelperCore::IsReloadableError(
287 const NetErrorHelperCore::ErrorPageInfo& info) {
288 return info.error.domain.utf8() == net::kErrorDomain &&
289 info.error.reason != net::ERR_ABORTED &&
290 !info.was_failed_post;
291 }
292
274 NetErrorHelperCore::NetErrorHelperCore(Delegate* delegate) 293 NetErrorHelperCore::NetErrorHelperCore(Delegate* delegate)
275 : delegate_(delegate), 294 : delegate_(delegate),
276 last_probe_status_(chrome_common_net::DNS_PROBE_POSSIBLE) { 295 last_probe_status_(chrome_common_net::DNS_PROBE_POSSIBLE),
296 auto_reload_enabled_(false),
297 auto_reload_timer_(new MockableOneShotTimer()),
298 online_(true),
mmenke 2014/03/13 14:39:13 Don't suppose we can get the correct initial value
Elly Fong-Jones 2014/03/13 17:56:19 NetworkChangeNotifier has this state, but it's onl
mmenke 2014/03/13 19:18:12 Ah, you're right...unless we're currently automati
299 auto_reload_count_(0),
300 can_auto_reload_page_(false) {
277 } 301 }
278 302
279 NetErrorHelperCore::~NetErrorHelperCore() { 303 NetErrorHelperCore::~NetErrorHelperCore() {
304 if (committed_error_page_info_ && can_auto_reload_page_) {
305 UMA_HISTOGRAM_CUSTOM_ENUMERATION("Net.AutoReload.ErrorAtStop",
306 -committed_error_page_info_->error.reason,
307 net::GetAllErrorCodesForUma());
308 UMA_HISTOGRAM_COUNTS("Net.AutoReload.CountAtStop", auto_reload_count_);
309 }
280 } 310 }
281 311
282 void NetErrorHelperCore::OnStop() { 312 void NetErrorHelperCore::CancelPendingFetches() {
283 // On stop, cancel loading navigation corrections, and prevent any 313 // Cancel loading the alternate error page, and prevent any pending error page
284 // pending error page load from starting to load corrections. Swapping in an 314 // load from starting a new error page load. Swapping in the error page when
285 // error page once corrections are received could interrupt a navigation, 315 // it's finished loading could abort the navigation, otherwise.
286 // otherwise. 316 if (committed_error_page_info_ && can_auto_reload_page_) {
317 UMA_HISTOGRAM_CUSTOM_ENUMERATION("Net.AutoReload.ErrorAtStop",
318 -committed_error_page_info_->error.reason,
319 net::GetAllErrorCodesForUma());
320 UMA_HISTOGRAM_COUNTS("Net.AutoReload.CountAtStop", auto_reload_count_);
321 }
287 if (committed_error_page_info_) { 322 if (committed_error_page_info_) {
288 committed_error_page_info_->navigation_correction_url = GURL(); 323 committed_error_page_info_->navigation_correction_url = GURL();
289 committed_error_page_info_->navigation_correction_request_body.clear(); 324 committed_error_page_info_->navigation_correction_request_body.clear();
290 } 325 }
291 if (pending_error_page_info_) { 326 if (pending_error_page_info_) {
292 pending_error_page_info_->navigation_correction_url = GURL(); 327 pending_error_page_info_->navigation_correction_url = GURL();
293 pending_error_page_info_->navigation_correction_request_body.clear(); 328 pending_error_page_info_->navigation_correction_request_body.clear();
294 } 329 }
295 delegate_->CancelFetchNavigationCorrections(); 330 delegate_->CancelFetchNavigationCorrections();
331 auto_reload_timer_->Stop();
332 can_auto_reload_page_ = false;
333 }
334
335 void NetErrorHelperCore::OnStop() {
336 CancelPendingFetches();
337 auto_reload_count_ = 0;
296 } 338 }
297 339
298 void NetErrorHelperCore::OnStartLoad(FrameType frame_type, PageType page_type) { 340 void NetErrorHelperCore::OnStartLoad(FrameType frame_type, PageType page_type) {
299 if (frame_type != MAIN_FRAME) 341 if (frame_type != MAIN_FRAME)
300 return; 342 return;
301 343
302 // If there's no pending error page information associated with the page load, 344 // If there's no pending error page information associated with the page load,
303 // or the new page is not an error page, then reset pending error page state. 345 // or the new page is not an error page, then reset pending error page state.
304 if (!pending_error_page_info_ || page_type != ERROR_PAGE) { 346 if (!pending_error_page_info_ || page_type != ERROR_PAGE) {
305 OnStop(); 347 CancelPendingFetches();
348 } else {
349 // If an error load is starting, the resulting error page is autoreloadable.
350 can_auto_reload_page_ = IsReloadableError(*pending_error_page_info_);
306 } 351 }
307 } 352 }
308 353
309 void NetErrorHelperCore::OnCommitLoad(FrameType frame_type) { 354 void NetErrorHelperCore::OnCommitLoad(FrameType frame_type) {
310 if (frame_type != MAIN_FRAME) 355 if (frame_type != MAIN_FRAME)
311 return; 356 return;
312 357
358 if (committed_error_page_info_ && !pending_error_page_info_ &&
359 can_auto_reload_page_) {
360 int reason = committed_error_page_info_->error.reason;
361 UMA_HISTOGRAM_CUSTOM_ENUMERATION("Net.AutoReload.ErrorAtSuccess",
362 -reason,
363 net::GetAllErrorCodesForUma());
364 UMA_HISTOGRAM_COUNTS("Net.AutoReload.CountAtSuccess", auto_reload_count_);
365 if (auto_reload_count_ == 1) {
366 UMA_HISTOGRAM_CUSTOM_ENUMERATION("Net.AutoReload.ErrorAtFirstSuccess",
367 -reason,
368 net::GetAllErrorCodesForUma());
369 }
370 }
371
313 committed_error_page_info_.reset(pending_error_page_info_.release()); 372 committed_error_page_info_.reset(pending_error_page_info_.release());
314 } 373 }
315 374
316 void NetErrorHelperCore::OnFinishLoad(FrameType frame_type) { 375 void NetErrorHelperCore::OnFinishLoad(FrameType frame_type) {
317 if (frame_type != MAIN_FRAME || !committed_error_page_info_) 376 if (frame_type != MAIN_FRAME)
318 return; 377 return;
319 378
379 if (!committed_error_page_info_) {
380 auto_reload_count_ = 0;
381 return;
382 }
383
320 committed_error_page_info_->is_finished_loading = true; 384 committed_error_page_info_->is_finished_loading = true;
321 385
322 if (committed_error_page_info_->navigation_correction_url.is_valid()) { 386 if (committed_error_page_info_->navigation_correction_url.is_valid()) {
323 // If there is another pending error page load, |fix_url| should have been 387 // If there is another pending error page load, |fix_url| should have been
324 // cleared. 388 // cleared.
325 DCHECK(!pending_error_page_info_); 389 DCHECK(!pending_error_page_info_);
326 DCHECK(!committed_error_page_info_->needs_dns_updates); 390 DCHECK(!committed_error_page_info_->needs_dns_updates);
327 delegate_->FetchNavigationCorrections( 391 delegate_->FetchNavigationCorrections(
328 committed_error_page_info_->navigation_correction_url, 392 committed_error_page_info_->navigation_correction_url,
329 committed_error_page_info_->navigation_correction_request_body); 393 committed_error_page_info_->navigation_correction_request_body);
394 } else if (auto_reload_enabled_ &&
395 IsReloadableError(*committed_error_page_info_)) {
396 MaybeStartAutoReloadTimer();
330 } 397 }
331 398
332 if (!committed_error_page_info_->needs_dns_updates || 399 if (!committed_error_page_info_->needs_dns_updates ||
333 last_probe_status_ == chrome_common_net::DNS_PROBE_POSSIBLE) { 400 last_probe_status_ == chrome_common_net::DNS_PROBE_POSSIBLE) {
334 return; 401 return;
335 } 402 }
336 DVLOG(1) << "Error page finished loading; sending saved status."; 403 DVLOG(1) << "Error page finished loading; sending saved status.";
337 UpdateErrorPage(); 404 UpdateErrorPage();
338 } 405 }
339 406
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
491 558
492 blink::WebURLError updated_error; 559 blink::WebURLError updated_error;
493 updated_error.domain = blink::WebString::fromUTF8( 560 updated_error.domain = blink::WebString::fromUTF8(
494 chrome_common_net::kDnsProbeErrorDomain); 561 chrome_common_net::kDnsProbeErrorDomain);
495 updated_error.reason = last_probe_status_; 562 updated_error.reason = last_probe_status_;
496 updated_error.unreachableURL = error.unreachableURL; 563 updated_error.unreachableURL = error.unreachableURL;
497 updated_error.staleCopyInCache = error.staleCopyInCache; 564 updated_error.staleCopyInCache = error.staleCopyInCache;
498 565
499 return updated_error; 566 return updated_error;
500 } 567 }
568
569 void NetErrorHelperCore::Reload() {
570 if (!committed_error_page_info_) {
571 return;
572 }
573 delegate_->ReloadPage();
574 }
575
576 bool NetErrorHelperCore::MaybeStartAutoReloadTimer() {
577 if (!committed_error_page_info_ ||
578 !committed_error_page_info_->is_finished_loading ||
579 !can_auto_reload_page_ ||
580 pending_error_page_info_) {
mmenke 2014/03/13 14:39:13 Can any of these not be true, except in the offlin
Elly Fong-Jones 2014/03/13 17:56:19 Yes: as called in ShouldSuppressErrorPage(), is_fi
581 return false;
582 }
583
584 DCHECK(IsReloadableError(*committed_error_page_info_));
585
586 if (!online_)
587 return false;
588
589 StartAutoReloadTimer();
590 return true;
591 }
592
593 void NetErrorHelperCore::StartAutoReloadTimer() {
594 DCHECK(committed_error_page_info_);
595 DCHECK(can_auto_reload_page_);
596 base::TimeDelta delay = GetAutoReloadTime(auto_reload_count_);
597 auto_reload_count_++;
598 auto_reload_timer_->Stop();
599 auto_reload_timer_->Start(FROM_HERE, delay,
600 base::Bind(&NetErrorHelperCore::Reload,
601 base::Unretained(this)));
602 }
603
604 void NetErrorHelperCore::NetworkStateChanged(bool online) {
605 online_ = online;
606 if (auto_reload_timer_->IsRunning()) {
607 DCHECK(committed_error_page_info_);
608 // If there's an existing timer running, stop it and reset the retry count.
609 auto_reload_timer_->Stop();
610 auto_reload_count_ = 0;
611 }
612
613 // If the network state changed to online, maybe start auto-reloading again.
614 if (online)
615 MaybeStartAutoReloadTimer();
616 }
617
618 bool NetErrorHelperCore::ShouldSuppressErrorPage(FrameType frame_type,
619 const GURL& url) {
620 // Don't suppress child frame errors.
621 if (frame_type != MAIN_FRAME)
622 return false;
623
624 // If |auto_reload_timer_| is still running, this error page isn't from an
625 // auto reload.
626 if (auto_reload_timer_->IsRunning())
627 return false;
628
629 // If there's no committed error page, this error page wasn't from an auto
630 // reload.
631 if (!committed_error_page_info_ || !can_auto_reload_page_)
632 return false;
633
634 GURL error_url = committed_error_page_info_->error.unreachableURL;
635 // TODO(ellyjones): also plumb the error code down to CCRC and check that
636 if (error_url != url)
637 return false;
638
639 // The first iteration of the timer is started by OnFinishLoad calling
640 // MaybeStartAutoReloadTimer, but since error pages for subsequent loads are
641 // suppressed in this function, subsequent iterations of the timer have to be
642 // started here.
643 MaybeStartAutoReloadTimer();
644 return true;
645 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698