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

Side by Side Diff: chrome/browser/ui/search/instant_controller.cc

Issue 14043009: Fall back to local page if online NTP fails to load. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix failing tests Created 7 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 | Annotate | Revision Log
OLDNEW
1 // Copyright 2012 The Chromium Authors. All rights reserved. 1 // Copyright 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 "chrome/browser/ui/search/instant_controller.h" 5 #include "chrome/browser/ui/search/instant_controller.h"
6 6
7 #include "base/metrics/histogram.h" 7 #include "base/metrics/histogram.h"
8 #include "base/string_util.h" 8 #include "base/string_util.h"
9 #include "base/stringprintf.h" 9 #include "base/stringprintf.h"
10 #include "base/utf_string_conversions.h" 10 #include "base/utf_string_conversions.h"
(...skipping 659 matching lines...) Expand 10 before | Expand all | Expand 10 after
670 } 670 }
671 671
672 void InstantController::OmniboxNavigateToURL() { 672 void InstantController::OmniboxNavigateToURL() {
673 if (!extended_enabled_) 673 if (!extended_enabled_)
674 return; 674 return;
675 RecordNavigationHistogram(UsingLocalPage(), false); 675 RecordNavigationHistogram(UsingLocalPage(), false);
676 if (instant_tab_) 676 if (instant_tab_)
677 instant_tab_->Submit(string16()); 677 instant_tab_->Submit(string16());
678 } 678 }
679 679
680 void InstantController::InstantPageLoadFailed(content::WebContents* contents) {
681 if (!chrome::ShouldPreferRemoteNTPOnStartup()) {
682 // We only need to fall back on errors if we're showing the online page
683 // at startup, as otherwise we fall back correctly when trying to show
684 // a page that hasn't yet indicated that it supports the InstantExtended
685 // API.
686 return;
687 }
688
689 if (IsContentsFrom(instant_tab(), contents)) {
690 // Verify we're not already on a local page and that the URL precisely
691 // equals the instant_url (minus the query params, as those will be filled
692 // in by template values). This check is necessary to make sure we don't
693 // inadvertently redirect to the local NTP if someone, say, reloads a SRP
694 // while offline, as a committed results page still counts as an instant
695 // url.
696 const GURL current_url = contents->GetURL();
697 if (instant_tab_->IsLocal() ||
698 !chrome::MatchesOriginAndPath(GURL(GetInstantURL()), current_url) ||
699 !current_url.ref().empty())
700 return;
701 RedirectToLocalNTP(contents);
702 }
703
704 if (IsContentsFrom(ntp(), contents)) {
samarth 2013/05/03 04:39:17 nit: use else if here and below.
David Black 2013/05/03 06:14:44 Done.
sreeram 2013/05/03 23:17:31 Not done?
David Black 2013/05/04 00:49:23 Done.
705 bool is_local = ntp_->IsLocal();
706 DeletePageSoon(ntp_.Pass());
707 if (!is_local)
708 ResetNTP(GetLocalInstantURL());
709 }
710
711 if (IsContentsFrom(overlay(), contents)) {
712 bool is_local = overlay_->IsLocal();
713 DeletePageSoon(overlay_.Pass());
714 if (!is_local)
715 ResetOverlay(GetLocalInstantURL());
716 }
717 }
718
680 content::WebContents* InstantController::GetOverlayContents() const { 719 content::WebContents* InstantController::GetOverlayContents() const {
681 return overlay_ ? overlay_->contents() : NULL; 720 return overlay_ ? overlay_->contents() : NULL;
682 } 721 }
683 722
684 bool InstantController::IsOverlayingSearchResults() const { 723 bool InstantController::IsOverlayingSearchResults() const {
685 return model_.mode().is_search_suggestions() && IsFullHeight(model_) && 724 return model_.mode().is_search_suggestions() && IsFullHeight(model_) &&
686 (last_match_was_search_ || 725 (last_match_was_search_ ||
687 last_suggestion_.behavior == INSTANT_COMPLETE_NEVER); 726 last_suggestion_.behavior == INSTANT_COMPLETE_NEVER);
688 } 727 }
689 728
(...skipping 404 matching lines...) Expand 10 before | Expand all | Expand 10 after
1094 bool supports_instant) { 1133 bool supports_instant) {
1095 if (IsContentsFrom(instant_tab(), contents)) { 1134 if (IsContentsFrom(instant_tab(), contents)) {
1096 if (!supports_instant) 1135 if (!supports_instant)
1097 MessageLoop::current()->DeleteSoon(FROM_HERE, instant_tab_.release()); 1136 MessageLoop::current()->DeleteSoon(FROM_HERE, instant_tab_.release());
1098 1137
1099 content::NotificationService::current()->Notify( 1138 content::NotificationService::current()->Notify(
1100 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED, 1139 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED,
1101 content::Source<InstantController>(this), 1140 content::Source<InstantController>(this),
1102 content::NotificationService::NoDetails()); 1141 content::NotificationService::NoDetails());
1103 } else if (IsContentsFrom(ntp(), contents)) { 1142 } else if (IsContentsFrom(ntp(), contents)) {
1104 if (!supports_instant) 1143 if (!supports_instant) {
1144 bool is_local = ntp_->IsLocal();
1105 DeletePageSoon(ntp_.Pass()); 1145 DeletePageSoon(ntp_.Pass());
1146 // Preload a local NTP in place of the broken online one.
1147 if (!is_local)
1148 ResetNTP(GetLocalInstantURL());
1149 }
1106 1150
1107 content::NotificationService::current()->Notify( 1151 content::NotificationService::current()->Notify(
1108 chrome::NOTIFICATION_INSTANT_NTP_SUPPORT_DETERMINED, 1152 chrome::NOTIFICATION_INSTANT_NTP_SUPPORT_DETERMINED,
1109 content::Source<InstantController>(this), 1153 content::Source<InstantController>(this),
1110 content::NotificationService::NoDetails()); 1154 content::NotificationService::NoDetails());
1111 1155
1112 } else if (IsContentsFrom(overlay(), contents)) { 1156 } else if (IsContentsFrom(overlay(), contents)) {
1113 if (!supports_instant) { 1157 if (!supports_instant) {
1114 HideInternal(); 1158 HideInternal();
1159 bool is_local = overlay_->IsLocal();
1115 DeletePageSoon(overlay_.Pass()); 1160 DeletePageSoon(overlay_.Pass());
1161 // Preload a local overlay in place of the broken online one.
1162 if (!is_local)
1163 ResetOverlay(GetLocalInstantURL());
1116 } 1164 }
1117 1165
1118 content::NotificationService::current()->Notify( 1166 content::NotificationService::current()->Notify(
1119 chrome::NOTIFICATION_INSTANT_OVERLAY_SUPPORT_DETERMINED, 1167 chrome::NOTIFICATION_INSTANT_OVERLAY_SUPPORT_DETERMINED,
1120 content::Source<InstantController>(this), 1168 content::Source<InstantController>(this),
1121 content::NotificationService::NoDetails()); 1169 content::NotificationService::NoDetails());
1122 } 1170 }
1123 } 1171 }
1124 1172
1125 void InstantController::InstantPageRenderViewGone( 1173 void InstantController::InstantPageRenderViewGone(
1126 const content::WebContents* contents) { 1174 const content::WebContents* contents) {
1127 if (IsContentsFrom(overlay(), contents)) { 1175 if (IsContentsFrom(overlay(), contents)) {
1128 HideInternal(); 1176 HideInternal();
1129 DeletePageSoon(overlay_.Pass()); 1177 DeletePageSoon(overlay_.Pass());
1130 } else if (IsContentsFrom(ntp(), contents)) { 1178 } else if (IsContentsFrom(ntp(), contents)) {
1131 DeletePageSoon(ntp_.Pass()); 1179 DeletePageSoon(ntp_.Pass());
1132 } else { 1180 } else {
1133 NOTREACHED(); 1181 NOTREACHED();
1134 } 1182 }
1135 } 1183 }
1136 1184
1137 void InstantController::InstantPageAboutToNavigateMainFrame( 1185 void InstantController::InstantPageAboutToNavigateMainFrame(
1138 const content::WebContents* contents, 1186 const content::WebContents* contents,
1139 const GURL& url) { 1187 const GURL& url) {
1140 DCHECK(IsContentsFrom(overlay(), contents)); 1188 if (IsContentsFrom(overlay(), contents)) {
1189 // If the page does not yet support Instant, we allow redirects and other
1190 // navigations to go through since the Instant URL can redirect - e.g. to
1191 // country specific pages.
1192 if (!overlay_->supports_instant())
1193 return;
1141 1194
1142 // If the page does not yet support Instant, we allow redirects and other 1195 GURL instant_url(overlay_->instant_url());
1143 // navigations to go through since the Instant URL can redirect - e.g. to
1144 // country specific pages.
1145 if (!overlay_->supports_instant())
1146 return;
1147 1196
1148 GURL instant_url(overlay_->instant_url()); 1197 // If we are navigating to the Instant URL, do nothing.
1198 if (url == instant_url)
1199 return;
1149 1200
1150 // If we are navigating to the Instant URL, do nothing. 1201 // Commit the navigation if either:
1151 if (url == instant_url) 1202 // - The page is in NTP mode (so it could only navigate on a user click) or
1152 return; 1203 // - The page is not in NTP mode and we are navigating to a URL with a
1153 1204 // different host or path than the Instant URL. This enables the instant
1154 // Commit the navigation if either: 1205 // page when it is showing search results to change the query parameters
1155 // - The page is in NTP mode (so it could only navigate on a user click) or 1206 // and fragments of the URL without it navigating.
1156 // - The page is not in NTP mode and we are navigating to a URL with a 1207 if (model_.mode().is_ntp() ||
1157 // different host or path than the Instant URL. This enables the instant 1208 (url.host() != instant_url.host() ||
1158 // page when it is showing search results to change the query parameters 1209 url.path() != instant_url.path())) {
1159 // and fragments of the URL without it navigating. 1210 CommitIfPossible(INSTANT_COMMIT_NAVIGATED);
1160 if (model_.mode().is_ntp() || 1211 }
1161 (url.host() != instant_url.host() || url.path() != instant_url.path())) { 1212 } else if (IsContentsFrom(instant_tab(), contents)) {
1162 CommitIfPossible(INSTANT_COMMIT_NAVIGATED); 1213 // The Instant tab navigated. Send it the data it needs to display
1214 // properly.
1215 UpdateInfoForInstantTab();
1216 } else {
1217 NOTREACHED();
1163 } 1218 }
1164 } 1219 }
1165 1220
1166 void InstantController::SetSuggestions( 1221 void InstantController::SetSuggestions(
1167 const content::WebContents* contents, 1222 const content::WebContents* contents,
1168 const std::vector<InstantSuggestion>& suggestions) { 1223 const std::vector<InstantSuggestion>& suggestions) {
1169 LOG_INSTANT_DEBUG_EVENT(this, "SetSuggestions"); 1224 LOG_INSTANT_DEBUG_EVENT(this, "SetSuggestions");
1170 1225
1171 // Ignore if the message is from an unexpected source. 1226 // Ignore if the message is from an unexpected source.
1172 if (IsContentsFrom(ntp(), contents)) 1227 if (IsContentsFrom(ntp(), contents))
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after
1440 1495
1441 void InstantController::ResetInstantTab() { 1496 void InstantController::ResetInstantTab() {
1442 // Do not wire up the InstantTab in Incognito, to prevent it from sending data 1497 // Do not wire up the InstantTab in Incognito, to prevent it from sending data
1443 // to the page. 1498 // to the page.
1444 if (!search_mode_.is_origin_default() && 1499 if (!search_mode_.is_origin_default() &&
1445 !browser_->profile()->IsOffTheRecord()) { 1500 !browser_->profile()->IsOffTheRecord()) {
1446 content::WebContents* active_tab = browser_->GetActiveWebContents(); 1501 content::WebContents* active_tab = browser_->GetActiveWebContents();
1447 if (!instant_tab_ || active_tab != instant_tab_->contents()) { 1502 if (!instant_tab_ || active_tab != instant_tab_->contents()) {
1448 instant_tab_.reset(new InstantTab(this)); 1503 instant_tab_.reset(new InstantTab(this));
1449 instant_tab_->Init(active_tab); 1504 instant_tab_->Init(active_tab);
1450 // Update theme info for this tab. 1505 UpdateInfoForInstantTab();
1451 browser_->UpdateThemeInfo();
1452 instant_tab_->SetDisplayInstantResults(instant_enabled_);
1453 instant_tab_->SetOmniboxBounds(omnibox_bounds_);
1454 instant_tab_->InitializeFonts();
1455 StartListeningToMostVisitedChanges();
1456 instant_tab_->KeyCaptureChanged(
1457 omnibox_focus_state_ == OMNIBOX_FOCUS_INVISIBLE);
1458 } 1506 }
1459 1507
1460 // Hide the |overlay_| since we are now using |instant_tab_| instead. 1508 // Hide the |overlay_| since we are now using |instant_tab_| instead.
1461 HideOverlay(); 1509 HideOverlay();
1462 } else { 1510 } else {
1463 instant_tab_.reset(); 1511 instant_tab_.reset();
1464 } 1512 }
1465 } 1513 }
1466 1514
1515 void InstantController::UpdateInfoForInstantTab() {
1516 if (instant_tab_) {
1517 browser_->UpdateThemeInfo();
1518 instant_tab_->SetDisplayInstantResults(instant_enabled_);
1519 instant_tab_->SetOmniboxBounds(omnibox_bounds_);
1520 instant_tab_->InitializeFonts();
1521 StartListeningToMostVisitedChanges();
1522 instant_tab_->KeyCaptureChanged(
1523 omnibox_focus_state_ == OMNIBOX_FOCUS_INVISIBLE);
1524 }
1525 }
1526
1467 void InstantController::HideOverlay() { 1527 void InstantController::HideOverlay() {
1468 HideInternal(); 1528 HideInternal();
1469 ReloadOverlayIfStale(); 1529 ReloadOverlayIfStale();
1470 } 1530 }
1471 1531
1472 void InstantController::HideInternal() { 1532 void InstantController::HideInternal() {
1473 LOG_INSTANT_DEBUG_EVENT(this, "Hide"); 1533 LOG_INSTANT_DEBUG_EVENT(this, "Hide");
1474 1534
1475 // If GetOverlayContents() returns NULL, either we're already in the desired 1535 // If GetOverlayContents() returns NULL, either we're already in the desired
1476 // MODE_DEFAULT state, or we're in the commit path. For the latter, don't 1536 // MODE_DEFAULT state, or we're in the commit path. For the latter, don't
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after
1680 } 1740 }
1681 } 1741 }
1682 1742
1683 return false; 1743 return false;
1684 } 1744 }
1685 1745
1686 bool InstantController::UsingLocalPage() const { 1746 bool InstantController::UsingLocalPage() const {
1687 return (instant_tab_ && instant_tab_->IsLocal()) || 1747 return (instant_tab_ && instant_tab_->IsLocal()) ||
1688 (!instant_tab_ && overlay_ && overlay_->IsLocal()); 1748 (!instant_tab_ && overlay_ && overlay_->IsLocal());
1689 } 1749 }
1750
1751 void InstantController::RedirectToLocalNTP(content::WebContents* contents) {
samarth 2013/05/03 05:23:11 Oh also, can you add LOG_INSTANT_DEBUG_EVENT loggi
David Black 2013/05/03 06:14:44 Good idea. Added it up in InstantPageLoadFailed.
1752 contents->GetController().LoadURL(
1753 chrome::GetLocalInstantURL(browser_->profile()),
1754 content::Referrer(),
1755 content::PAGE_TRANSITION_SERVER_REDIRECT,
1756 std::string()); // No extra headers.
1757 // TODO(dcblack): Remove extraneous history entry caused by 404s.
1758 // Note that the base case of a 204 being returned doesn't push a history
1759 // entry.
1760 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698