OLD | NEW |
---|---|
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 Loading... | |
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 GURL local_fallback_url = chrome::GetLocalInstantURL(browser_->profile()); | |
samarth
2013/05/02 21:57:18
not used anymore
David Black
2013/05/02 23:30:01
Done.
| |
690 | |
691 if (IsContentsFrom(instant_tab(), contents)) { | |
692 std::string instant_url = GetInstantURL(); | |
samarth
2013/05/02 21:57:18
const std::string&
David Black
2013/05/02 23:30:01
Bloody c++ and its inability to have a basic strin
| |
693 if (instant_tab_->IsLocal() || contents->GetURL() != GURL(instant_url)) | |
694 return; | |
695 RedirectToLocalNTP(contents); | |
samarth
2013/05/02 21:57:18
Please add a comment explaining why we only redire
David Black
2013/05/02 23:30:01
You're talking about the url != url condition abov
| |
696 } | |
697 | |
698 if (IsContentsFrom(ntp(), contents)) { | |
699 if (ntp_->IsLocal()) | |
samarth
2013/05/02 21:57:18
I don't see how a local page would ever fail, but
David Black
2013/05/02 23:30:01
Done.
| |
700 return; | |
701 DeletePageSoon(ntp_.Pass()); | |
702 ResetNTP(GetLocalInstantURL()); | |
703 } | |
704 | |
705 if (IsContentsFrom(overlay(), contents)) { | |
706 if (overlay_->IsLocal()) | |
707 return; | |
708 DeletePageSoon(overlay_.Pass()); | |
709 ResetOverlay(GetLocalInstantURL()); | |
710 } | |
711 } | |
712 | |
680 content::WebContents* InstantController::GetOverlayContents() const { | 713 content::WebContents* InstantController::GetOverlayContents() const { |
681 return overlay_ ? overlay_->contents() : NULL; | 714 return overlay_ ? overlay_->contents() : NULL; |
682 } | 715 } |
683 | 716 |
684 bool InstantController::IsOverlayingSearchResults() const { | 717 bool InstantController::IsOverlayingSearchResults() const { |
685 return model_.mode().is_search_suggestions() && IsFullHeight(model_) && | 718 return model_.mode().is_search_suggestions() && IsFullHeight(model_) && |
686 (last_match_was_search_ || | 719 (last_match_was_search_ || |
687 last_suggestion_.behavior == INSTANT_COMPLETE_NEVER); | 720 last_suggestion_.behavior == INSTANT_COMPLETE_NEVER); |
688 } | 721 } |
689 | 722 |
(...skipping 404 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1094 bool supports_instant) { | 1127 bool supports_instant) { |
1095 if (IsContentsFrom(instant_tab(), contents)) { | 1128 if (IsContentsFrom(instant_tab(), contents)) { |
1096 if (!supports_instant) | 1129 if (!supports_instant) |
1097 MessageLoop::current()->DeleteSoon(FROM_HERE, instant_tab_.release()); | 1130 MessageLoop::current()->DeleteSoon(FROM_HERE, instant_tab_.release()); |
1098 | 1131 |
1099 content::NotificationService::current()->Notify( | 1132 content::NotificationService::current()->Notify( |
1100 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED, | 1133 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED, |
1101 content::Source<InstantController>(this), | 1134 content::Source<InstantController>(this), |
1102 content::NotificationService::NoDetails()); | 1135 content::NotificationService::NoDetails()); |
1103 } else if (IsContentsFrom(ntp(), contents)) { | 1136 } else if (IsContentsFrom(ntp(), contents)) { |
1104 if (!supports_instant) | 1137 if (!supports_instant) |
samarth
2013/05/02 21:57:18
I think it's a good idea to do the same thing here
David Black
2013/05/02 23:30:01
Done.
| |
1105 DeletePageSoon(ntp_.Pass()); | 1138 DeletePageSoon(ntp_.Pass()); |
1106 | 1139 |
1107 content::NotificationService::current()->Notify( | 1140 content::NotificationService::current()->Notify( |
1108 chrome::NOTIFICATION_INSTANT_NTP_SUPPORT_DETERMINED, | 1141 chrome::NOTIFICATION_INSTANT_NTP_SUPPORT_DETERMINED, |
1109 content::Source<InstantController>(this), | 1142 content::Source<InstantController>(this), |
1110 content::NotificationService::NoDetails()); | 1143 content::NotificationService::NoDetails()); |
1111 | 1144 |
1112 } else if (IsContentsFrom(overlay(), contents)) { | 1145 } else if (IsContentsFrom(overlay(), contents)) { |
1113 if (!supports_instant) { | 1146 if (!supports_instant) { |
1114 HideInternal(); | 1147 HideInternal(); |
(...skipping 15 matching lines...) Expand all Loading... | |
1130 } else if (IsContentsFrom(ntp(), contents)) { | 1163 } else if (IsContentsFrom(ntp(), contents)) { |
1131 DeletePageSoon(ntp_.Pass()); | 1164 DeletePageSoon(ntp_.Pass()); |
1132 } else { | 1165 } else { |
1133 NOTREACHED(); | 1166 NOTREACHED(); |
1134 } | 1167 } |
1135 } | 1168 } |
1136 | 1169 |
1137 void InstantController::InstantPageAboutToNavigateMainFrame( | 1170 void InstantController::InstantPageAboutToNavigateMainFrame( |
1138 const content::WebContents* contents, | 1171 const content::WebContents* contents, |
1139 const GURL& url) { | 1172 const GURL& url) { |
1140 DCHECK(IsContentsFrom(overlay(), contents)); | 1173 if (IsContentsFrom(overlay(), contents)) { |
1174 // If the page does not yet support Instant, we allow redirects and other | |
1175 // navigations to go through since the Instant URL can redirect - e.g. to | |
1176 // country specific pages. | |
1177 if (!overlay_->supports_instant()) | |
1178 return; | |
1141 | 1179 |
1142 // If the page does not yet support Instant, we allow redirects and other | 1180 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 | 1181 |
1148 GURL instant_url(overlay_->instant_url()); | 1182 // If we are navigating to the Instant URL, do nothing. |
1183 if (url == instant_url) | |
1184 return; | |
1149 | 1185 |
1150 // If we are navigating to the Instant URL, do nothing. | 1186 // Commit the navigation if either: |
1151 if (url == instant_url) | 1187 // - The page is in NTP mode (so it could only navigate on a user click) or |
1152 return; | 1188 // - The page is not in NTP mode and we are navigating to a URL with a |
1153 | 1189 // different host or path than the Instant URL. This enables the instant |
1154 // Commit the navigation if either: | 1190 // 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 | 1191 // 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 | 1192 if (model_.mode().is_ntp() || |
1157 // different host or path than the Instant URL. This enables the instant | 1193 (url.host() != instant_url.host() || |
1158 // page when it is showing search results to change the query parameters | 1194 url.path() != instant_url.path())) { |
1159 // and fragments of the URL without it navigating. | 1195 CommitIfPossible(INSTANT_COMMIT_NAVIGATED); |
1160 if (model_.mode().is_ntp() || | 1196 } |
1161 (url.host() != instant_url.host() || url.path() != instant_url.path())) { | 1197 } else if (IsContentsFrom(instant_tab(), contents)) { |
1162 CommitIfPossible(INSTANT_COMMIT_NAVIGATED); | 1198 // The Instant tab loaded. Send it the data it needs to display properly. |
samarth
2013/05/02 21:57:18
nit: The Instant tab navigated.
David Black
2013/05/02 23:30:01
AARG. THAT MAKES THE LINE 81 CHARS LONG.
Done.
| |
1199 UpdateInfoForInstantTab(); | |
1200 } else { | |
1201 NOTREACHED(); | |
1163 } | 1202 } |
1164 } | 1203 } |
1165 | 1204 |
1166 void InstantController::SetSuggestions( | 1205 void InstantController::SetSuggestions( |
1167 const content::WebContents* contents, | 1206 const content::WebContents* contents, |
1168 const std::vector<InstantSuggestion>& suggestions) { | 1207 const std::vector<InstantSuggestion>& suggestions) { |
1169 LOG_INSTANT_DEBUG_EVENT(this, "SetSuggestions"); | 1208 LOG_INSTANT_DEBUG_EVENT(this, "SetSuggestions"); |
1170 | 1209 |
1171 // Ignore if the message is from an unexpected source. | 1210 // Ignore if the message is from an unexpected source. |
1172 if (IsContentsFrom(ntp(), contents)) | 1211 if (IsContentsFrom(ntp(), contents)) |
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1440 | 1479 |
1441 void InstantController::ResetInstantTab() { | 1480 void InstantController::ResetInstantTab() { |
1442 // Do not wire up the InstantTab in Incognito, to prevent it from sending data | 1481 // Do not wire up the InstantTab in Incognito, to prevent it from sending data |
1443 // to the page. | 1482 // to the page. |
1444 if (!search_mode_.is_origin_default() && | 1483 if (!search_mode_.is_origin_default() && |
1445 !browser_->profile()->IsOffTheRecord()) { | 1484 !browser_->profile()->IsOffTheRecord()) { |
1446 content::WebContents* active_tab = browser_->GetActiveWebContents(); | 1485 content::WebContents* active_tab = browser_->GetActiveWebContents(); |
1447 if (!instant_tab_ || active_tab != instant_tab_->contents()) { | 1486 if (!instant_tab_ || active_tab != instant_tab_->contents()) { |
1448 instant_tab_.reset(new InstantTab(this)); | 1487 instant_tab_.reset(new InstantTab(this)); |
1449 instant_tab_->Init(active_tab); | 1488 instant_tab_->Init(active_tab); |
1450 // Update theme info for this tab. | 1489 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 } | 1490 } |
1459 | 1491 |
1460 // Hide the |overlay_| since we are now using |instant_tab_| instead. | 1492 // Hide the |overlay_| since we are now using |instant_tab_| instead. |
1461 HideOverlay(); | 1493 HideOverlay(); |
1462 } else { | 1494 } else { |
1463 instant_tab_.reset(); | 1495 instant_tab_.reset(); |
1464 } | 1496 } |
1465 } | 1497 } |
1466 | 1498 |
1499 void InstantController::UpdateInfoForInstantTab() { | |
1500 if (instant_tab_) { | |
1501 browser_->UpdateThemeInfo(); | |
1502 instant_tab_->SetDisplayInstantResults(instant_enabled_); | |
1503 instant_tab_->SetOmniboxBounds(omnibox_bounds_); | |
1504 instant_tab_->InitializeFonts(); | |
1505 StartListeningToMostVisitedChanges(); | |
1506 instant_tab_->KeyCaptureChanged( | |
1507 omnibox_focus_state_ == OMNIBOX_FOCUS_INVISIBLE); | |
1508 } | |
1509 } | |
1510 | |
1467 void InstantController::HideOverlay() { | 1511 void InstantController::HideOverlay() { |
1468 HideInternal(); | 1512 HideInternal(); |
1469 ReloadOverlayIfStale(); | 1513 ReloadOverlayIfStale(); |
1470 } | 1514 } |
1471 | 1515 |
1472 void InstantController::HideInternal() { | 1516 void InstantController::HideInternal() { |
1473 LOG_INSTANT_DEBUG_EVENT(this, "Hide"); | 1517 LOG_INSTANT_DEBUG_EVENT(this, "Hide"); |
1474 | 1518 |
1475 // If GetOverlayContents() returns NULL, either we're already in the desired | 1519 // 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 | 1520 // 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 Loading... | |
1680 } | 1724 } |
1681 } | 1725 } |
1682 | 1726 |
1683 return false; | 1727 return false; |
1684 } | 1728 } |
1685 | 1729 |
1686 bool InstantController::UsingLocalPage() const { | 1730 bool InstantController::UsingLocalPage() const { |
1687 return (instant_tab_ && instant_tab_->IsLocal()) || | 1731 return (instant_tab_ && instant_tab_->IsLocal()) || |
1688 (!instant_tab_ && overlay_ && overlay_->IsLocal()); | 1732 (!instant_tab_ && overlay_ && overlay_->IsLocal()); |
1689 } | 1733 } |
1734 | |
1735 void InstantController::RedirectToLocalNTP(content::WebContents* contents) { | |
1736 contents->GetController().LoadURL( | |
1737 chrome::GetLocalInstantURL(browser_->profile()), | |
1738 content::Referrer(), | |
1739 content::PAGE_TRANSITION_SERVER_REDIRECT, | |
1740 std::string()); // No extra headers. | |
1741 // TODO(dcblack): Remove extraneous history entry. | |
1742 } | |
OLD | NEW |