Chromium Code Reviews| 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 691 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 702 } | 702 } |
| 703 | 703 |
| 704 void InstantController::OmniboxNavigateToURL() { | 704 void InstantController::OmniboxNavigateToURL() { |
| 705 if (!extended_enabled_) | 705 if (!extended_enabled_) |
| 706 return; | 706 return; |
| 707 RecordNavigationHistogram(UsingLocalPage(), false); | 707 RecordNavigationHistogram(UsingLocalPage(), false); |
| 708 if (instant_tab_) | 708 if (instant_tab_) |
| 709 instant_tab_->Submit(string16()); | 709 instant_tab_->Submit(string16()); |
| 710 } | 710 } |
| 711 | 711 |
| 712 void InstantController::InstantPageLoadFailed(content::WebContents* contents) { | |
| 713 GURL local_fallback_url = chrome::GetLocalInstantURL(browser_->profile()); | |
| 714 | |
| 715 if (instant_tab_ && IsContentsFrom(instant_tab(), contents)) { | |
|
samarth
2013/05/02 03:12:56
IsContentsFrom already checks that the page is not
David Black
2013/05/02 05:02:49
Done.
| |
| 716 std::string instant_url; | |
| 717 bool has_online_url = GetInstantURL( | |
| 718 browser_->profile(), true, &instant_url); | |
| 719 if (instant_tab_->IsLocal() || !has_online_url || | |
| 720 contents->GetURL() != GURL(instant_url)) | |
| 721 return; | |
| 722 RedirectToLocalNTP(contents); | |
| 723 } | |
| 724 | |
| 725 if (ntp_ && IsContentsFrom(ntp(), contents)) { | |
| 726 if (ntp_->IsLocal()) | |
| 727 return; | |
| 728 MessageLoop::current()->DeleteSoon(FROM_HERE, ntp_.release()); | |
|
samarth
2013/05/02 03:12:56
It's best to also delete the contents held by ntp_
David Black
2013/05/02 05:02:49
Ok. That'll require a bunch of rebasing on my par
| |
| 729 ResetNTP(false, true); | |
| 730 } | |
| 731 | |
| 732 if (overlay_ && IsContentsFrom(overlay(), contents)) { | |
| 733 if (overlay_->IsLocal()) | |
| 734 return; | |
| 735 MessageLoop::current()->DeleteSoon(FROM_HERE, overlay_.release()); | |
| 736 CreateOverlay(local_fallback_url.spec(), contents); | |
| 737 } | |
| 738 } | |
| 739 | |
| 712 content::WebContents* InstantController::GetOverlayContents() const { | 740 content::WebContents* InstantController::GetOverlayContents() const { |
| 713 return overlay_ ? overlay_->contents() : NULL; | 741 return overlay_ ? overlay_->contents() : NULL; |
| 714 } | 742 } |
| 715 | 743 |
| 716 bool InstantController::IsOverlayingSearchResults() const { | 744 bool InstantController::IsOverlayingSearchResults() const { |
| 717 return model_.mode().is_search_suggestions() && IsFullHeight(model_) && | 745 return model_.mode().is_search_suggestions() && IsFullHeight(model_) && |
| 718 (last_match_was_search_ || | 746 (last_match_was_search_ || |
| 719 last_suggestion_.behavior == INSTANT_COMPLETE_NEVER); | 747 last_suggestion_.behavior == INSTANT_COMPLETE_NEVER); |
| 720 } | 748 } |
| 721 | 749 |
| (...skipping 427 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1149 BlacklistAndResetOverlay(); | 1177 BlacklistAndResetOverlay(); |
| 1150 else if (IsContentsFrom(ntp(), contents)) | 1178 else if (IsContentsFrom(ntp(), contents)) |
| 1151 BlacklistAndResetNTP(); | 1179 BlacklistAndResetNTP(); |
| 1152 else | 1180 else |
| 1153 NOTREACHED(); | 1181 NOTREACHED(); |
| 1154 } | 1182 } |
| 1155 | 1183 |
| 1156 void InstantController::InstantPageAboutToNavigateMainFrame( | 1184 void InstantController::InstantPageAboutToNavigateMainFrame( |
| 1157 const content::WebContents* contents, | 1185 const content::WebContents* contents, |
| 1158 const GURL& url) { | 1186 const GURL& url) { |
| 1159 DCHECK(IsContentsFrom(overlay(), contents)); | 1187 if (IsContentsFrom(overlay(), contents)) { |
| 1188 // If the page does not yet support Instant, we allow redirects and other | |
| 1189 // navigations to go through since the Instant URL can redirect - e.g. to | |
| 1190 // country specific pages. | |
| 1191 if (!overlay_->supports_instant()) | |
| 1192 return; | |
| 1160 | 1193 |
| 1161 // If the page does not yet support Instant, we allow redirects and other | 1194 GURL instant_url(overlay_->instant_url()); |
| 1162 // navigations to go through since the Instant URL can redirect - e.g. to | |
| 1163 // country specific pages. | |
| 1164 if (!overlay_->supports_instant()) | |
| 1165 return; | |
| 1166 | 1195 |
| 1167 GURL instant_url(overlay_->instant_url()); | 1196 // If we are navigating to the Instant URL, do nothing. |
| 1197 if (url == instant_url) | |
| 1198 return; | |
| 1168 | 1199 |
| 1169 // If we are navigating to the Instant URL, do nothing. | 1200 // Commit the navigation if either: |
| 1170 if (url == instant_url) | 1201 // - The page is in NTP mode (so it could only navigate on a user click) or |
| 1171 return; | 1202 // - The page is not in NTP mode and we are navigating to a URL with a |
| 1172 | 1203 // different host or path than the Instant URL. This enables the instant |
| 1173 // Commit the navigation if either: | 1204 // page when it is showing search results to change the query parameters |
| 1174 // - The page is in NTP mode (so it could only navigate on a user click) or | 1205 // and fragments of the URL without it navigating. |
| 1175 // - The page is not in NTP mode and we are navigating to a URL with a | 1206 if (model_.mode().is_ntp() || |
| 1176 // different host or path than the Instant URL. This enables the instant | 1207 (url.host() != instant_url.host() || |
| 1177 // page when it is showing search results to change the query parameters | 1208 url.path() != instant_url.path())) { |
| 1178 // and fragments of the URL without it navigating. | 1209 CommitIfPossible(INSTANT_COMMIT_NAVIGATED); |
| 1179 if (model_.mode().is_ntp() || | 1210 } |
| 1180 (url.host() != instant_url.host() || url.path() != instant_url.path())) { | 1211 } else if (IsContentsFrom(instant_tab(), contents)) { |
| 1181 CommitIfPossible(INSTANT_COMMIT_NAVIGATED); | 1212 // The Instant tab loaded. Send it the data it needs to display properly. |
| 1213 UpdateInfoForInstantTab(); | |
| 1214 } else { | |
| 1215 NOTREACHED(); | |
| 1182 } | 1216 } |
| 1183 } | 1217 } |
| 1184 | 1218 |
| 1185 void InstantController::SetSuggestions( | 1219 void InstantController::SetSuggestions( |
| 1186 const content::WebContents* contents, | 1220 const content::WebContents* contents, |
| 1187 const std::vector<InstantSuggestion>& suggestions) { | 1221 const std::vector<InstantSuggestion>& suggestions) { |
| 1188 LOG_INSTANT_DEBUG_EVENT(this, "SetSuggestions"); | 1222 LOG_INSTANT_DEBUG_EVENT(this, "SetSuggestions"); |
| 1189 | 1223 |
| 1190 // Ignore if the message is from an unexpected source. | 1224 // Ignore if the message is from an unexpected source. |
| 1191 if (IsContentsFrom(ntp(), contents)) | 1225 if (IsContentsFrom(ntp(), contents)) |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1389 | 1423 |
| 1390 void InstantController::ResetInstantTab() { | 1424 void InstantController::ResetInstantTab() { |
| 1391 // Do not wire up the InstantTab in Incognito, to prevent it from sending data | 1425 // Do not wire up the InstantTab in Incognito, to prevent it from sending data |
| 1392 // to the page. | 1426 // to the page. |
| 1393 if (!search_mode_.is_origin_default() && | 1427 if (!search_mode_.is_origin_default() && |
| 1394 !browser_->profile()->IsOffTheRecord()) { | 1428 !browser_->profile()->IsOffTheRecord()) { |
| 1395 content::WebContents* active_tab = browser_->GetActiveWebContents(); | 1429 content::WebContents* active_tab = browser_->GetActiveWebContents(); |
| 1396 if (!instant_tab_ || active_tab != instant_tab_->contents()) { | 1430 if (!instant_tab_ || active_tab != instant_tab_->contents()) { |
| 1397 instant_tab_.reset(new InstantTab(this)); | 1431 instant_tab_.reset(new InstantTab(this)); |
| 1398 instant_tab_->Init(active_tab); | 1432 instant_tab_->Init(active_tab); |
| 1399 // Update theme info for this tab. | 1433 UpdateInfoForInstantTab(); |
| 1400 browser_->UpdateThemeInfo(); | |
| 1401 instant_tab_->SetDisplayInstantResults(instant_enabled_); | |
| 1402 instant_tab_->SetOmniboxBounds(omnibox_bounds_); | |
| 1403 instant_tab_->InitializeFonts(); | |
| 1404 StartListeningToMostVisitedChanges(); | |
| 1405 instant_tab_->KeyCaptureChanged( | |
| 1406 omnibox_focus_state_ == OMNIBOX_FOCUS_INVISIBLE); | |
| 1407 } | 1434 } |
| 1408 | 1435 |
| 1409 // Hide the |overlay_| since we are now using |instant_tab_| instead. | 1436 // Hide the |overlay_| since we are now using |instant_tab_| instead. |
| 1410 HideOverlay(); | 1437 HideOverlay(); |
| 1411 } else { | 1438 } else { |
| 1412 instant_tab_.reset(); | 1439 instant_tab_.reset(); |
| 1413 } | 1440 } |
| 1414 } | 1441 } |
| 1415 | 1442 |
| 1443 void InstantController::UpdateInfoForInstantTab() { | |
| 1444 if (instant_tab_) { | |
| 1445 browser_->UpdateThemeInfo(); | |
| 1446 instant_tab_->SetDisplayInstantResults(instant_enabled_); | |
| 1447 instant_tab_->SetOmniboxBounds(omnibox_bounds_); | |
| 1448 instant_tab_->InitializeFonts(); | |
| 1449 StartListeningToMostVisitedChanges(); | |
| 1450 instant_tab_->KeyCaptureChanged( | |
| 1451 omnibox_focus_state_ == OMNIBOX_FOCUS_INVISIBLE); | |
| 1452 } | |
| 1453 } | |
| 1454 | |
| 1416 void InstantController::HideOverlay() { | 1455 void InstantController::HideOverlay() { |
| 1417 HideInternal(); | 1456 HideInternal(); |
| 1418 ReloadOverlayIfStale(); | 1457 ReloadOverlayIfStale(); |
| 1419 MaybeSwitchToRemoteOverlay(); | 1458 MaybeSwitchToRemoteOverlay(); |
| 1420 } | 1459 } |
| 1421 | 1460 |
| 1422 void InstantController::HideInternal() { | 1461 void InstantController::HideInternal() { |
| 1423 LOG_INSTANT_DEBUG_EVENT(this, "Hide"); | 1462 LOG_INSTANT_DEBUG_EVENT(this, "Hide"); |
| 1424 | 1463 |
| 1425 // If GetOverlayContents() returns NULL, either we're already in the desired | 1464 // If GetOverlayContents() returns NULL, either we're already in the desired |
| (...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1712 if (browser_->GetActiveWebContents()) | 1751 if (browser_->GetActiveWebContents()) |
| 1713 return true; | 1752 return true; |
| 1714 | 1753 |
| 1715 return chrome::IsAggressiveLocalNTPFallbackEnabled(); | 1754 return chrome::IsAggressiveLocalNTPFallbackEnabled(); |
| 1716 } | 1755 } |
| 1717 | 1756 |
| 1718 bool InstantController::UsingLocalPage() const { | 1757 bool InstantController::UsingLocalPage() const { |
| 1719 return (instant_tab_ && instant_tab_->IsLocal()) || | 1758 return (instant_tab_ && instant_tab_->IsLocal()) || |
| 1720 (!instant_tab_ && overlay_ && overlay_->IsLocal()); | 1759 (!instant_tab_ && overlay_ && overlay_->IsLocal()); |
| 1721 } | 1760 } |
| 1761 | |
| 1762 void InstantController::RedirectToLocalNTP(content::WebContents* contents) { | |
| 1763 contents->GetController().LoadURL( | |
| 1764 chrome::GetLocalInstantURL(browser_->profile()), | |
| 1765 content::Referrer(), | |
| 1766 content::PAGE_TRANSITION_SERVER_REDIRECT, | |
| 1767 std::string()); // No extra headers. | |
| 1768 // TODO(dcblack): Remove extraneous history entry. | |
| 1769 } | |
| OLD | NEW |