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 <iterator> | 7 #include <iterator> |
| 8 | 8 |
| 9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 10 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
| (...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 267 last_match_was_search_(false), | 267 last_match_was_search_(false), |
| 268 omnibox_focus_state_(OMNIBOX_FOCUS_NONE), | 268 omnibox_focus_state_(OMNIBOX_FOCUS_NONE), |
| 269 omnibox_focus_change_reason_(OMNIBOX_FOCUS_CHANGE_EXPLICIT), | 269 omnibox_focus_change_reason_(OMNIBOX_FOCUS_CHANGE_EXPLICIT), |
| 270 omnibox_bounds_(-1, -1, 0, 0), | 270 omnibox_bounds_(-1, -1, 0, 0), |
| 271 allow_overlay_to_show_search_suggestions_(false) { | 271 allow_overlay_to_show_search_suggestions_(false) { |
| 272 | 272 |
| 273 // When the InstantController lives, the InstantService should live. | 273 // When the InstantController lives, the InstantService should live. |
| 274 // InstantService sets up profile-level facilities such as the ThemeSource for | 274 // InstantService sets up profile-level facilities such as the ThemeSource for |
| 275 // the NTP. | 275 // the NTP. |
| 276 // However, in some tests, browser_ may be null. | 276 // However, in some tests, browser_ may be null. |
| 277 if (browser_) | 277 if (browser_) { |
| 278 InstantServiceFactory::GetForProfile(browser_->profile()); | 278 InstantService* instant_service = GetInstantService(); |
| 279 instant_service->AddObserver(this); | |
| 280 } | |
| 279 } | 281 } |
| 280 | 282 |
| 281 InstantController::~InstantController() { | 283 InstantController::~InstantController() { |
| 284 if (browser_) { | |
| 285 InstantService* instant_service = GetInstantService(); | |
| 286 instant_service->RemoveObserver(this); | |
| 287 } | |
| 282 } | 288 } |
| 283 | 289 |
| 284 void InstantController::OnAutocompleteStart() { | 290 void InstantController::OnAutocompleteStart() { |
| 285 if (UseTabForSuggestions() && instant_tab_->supports_instant()) { | 291 if (UseTabForSuggestions() && instant_tab_->supports_instant()) { |
| 286 LOG_INSTANT_DEBUG_EVENT( | 292 LOG_INSTANT_DEBUG_EVENT( |
| 287 this, "OnAutocompleteStart: using InstantTab"); | 293 this, "OnAutocompleteStart: using InstantTab"); |
| 288 return; | 294 return; |
| 289 } | 295 } |
| 290 | 296 |
| 291 // Not using |instant_tab_|. Check if overlay is OK to use. | 297 // Not using |instant_tab_|. Check if overlay is OK to use. |
| (...skipping 497 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 789 ResetNTP(GetLocalInstantURL()); | 795 ResetNTP(GetLocalInstantURL()); |
| 790 } else if (IsContentsFrom(overlay(), contents)) { | 796 } else if (IsContentsFrom(overlay(), contents)) { |
| 791 LOG_INSTANT_DEBUG_EVENT(this, "InstantPageLoadFailed: overlay"); | 797 LOG_INSTANT_DEBUG_EVENT(this, "InstantPageLoadFailed: overlay"); |
| 792 bool is_local = overlay_->IsLocal(); | 798 bool is_local = overlay_->IsLocal(); |
| 793 DeletePageSoon(overlay_.Pass()); | 799 DeletePageSoon(overlay_.Pass()); |
| 794 if (!is_local) | 800 if (!is_local) |
| 795 ResetOverlay(GetLocalInstantURL()); | 801 ResetOverlay(GetLocalInstantURL()); |
| 796 } | 802 } |
| 797 } | 803 } |
| 798 | 804 |
| 805 void InstantController::ThemeInfoChanged( | |
| 806 const ThemeBackgroundInfo& theme_info) { | |
| 807 if (!extended_enabled()) | |
| 808 return; | |
| 809 | |
| 810 if (overlay_) | |
| 811 overlay_->SendThemeBackgroundInfo(theme_info); | |
| 812 | |
| 813 if (ntp_) | |
| 814 ntp_->SendThemeBackgroundInfo(theme_info); | |
| 815 | |
| 816 if (instant_tab_) | |
| 817 instant_tab_->SendThemeBackgroundInfo(theme_info); | |
| 818 } | |
| 819 | |
| 799 content::WebContents* InstantController::GetOverlayContents() const { | 820 content::WebContents* InstantController::GetOverlayContents() const { |
| 800 return overlay_ ? overlay_->contents() : NULL; | 821 return overlay_ ? overlay_->contents() : NULL; |
| 801 } | 822 } |
| 802 | 823 |
| 803 content::WebContents* InstantController::GetNTPContents() const { | 824 content::WebContents* InstantController::GetNTPContents() const { |
| 804 return ntp_ ? ntp_->contents() : NULL; | 825 return ntp_ ? ntp_->contents() : NULL; |
| 805 } | 826 } |
| 806 | 827 |
| 807 bool InstantController::IsOverlayingSearchResults() const { | 828 bool InstantController::IsOverlayingSearchResults() const { |
| 808 return model_.mode().is_search_suggestions() && IsFullHeight(model_) && | 829 return model_.mode().is_search_suggestions() && IsFullHeight(model_) && |
| (...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1077 | 1098 |
| 1078 // Preload the Instant NTP. | 1099 // Preload the Instant NTP. |
| 1079 ntp_.reset(); | 1100 ntp_.reset(); |
| 1080 if (extended_enabled() && preload_ntp_) | 1101 if (extended_enabled() && preload_ntp_) |
| 1081 ResetNTP(GetInstantURL()); | 1102 ResetNTP(GetInstantURL()); |
| 1082 | 1103 |
| 1083 if (instant_tab_) | 1104 if (instant_tab_) |
| 1084 instant_tab_->SetDisplayInstantResults(instant_enabled_); | 1105 instant_tab_->SetDisplayInstantResults(instant_enabled_); |
| 1085 } | 1106 } |
| 1086 | 1107 |
| 1087 void InstantController::ThemeChanged(const ThemeBackgroundInfo& theme_info) { | |
|
samarth
2013/06/19 22:37:15
Just undo this diff?
kmadhusu
2013/06/20 01:21:27
Done. I was trying to match the function declarati
| |
| 1088 if (!extended_enabled()) | |
| 1089 return; | |
| 1090 | |
| 1091 if (overlay_) | |
| 1092 overlay_->SendThemeBackgroundInfo(theme_info); | |
| 1093 if (ntp_) | |
| 1094 ntp_->SendThemeBackgroundInfo(theme_info); | |
| 1095 if (instant_tab_) | |
| 1096 instant_tab_->SendThemeBackgroundInfo(theme_info); | |
| 1097 } | |
| 1098 | |
| 1099 void InstantController::SwappedOverlayContents() { | 1108 void InstantController::SwappedOverlayContents() { |
| 1100 model_.SetOverlayContents(GetOverlayContents()); | 1109 model_.SetOverlayContents(GetOverlayContents()); |
| 1101 } | 1110 } |
| 1102 | 1111 |
| 1103 void InstantController::FocusedOverlayContents() { | 1112 void InstantController::FocusedOverlayContents() { |
| 1104 #if defined(USE_AURA) | 1113 #if defined(USE_AURA) |
| 1105 // On aura the omnibox only receives a focus lost if we initiate the focus | 1114 // On aura the omnibox only receives a focus lost if we initiate the focus |
| 1106 // change. This does that. | 1115 // change. This does that. |
| 1107 if (!model_.mode().is_default()) | 1116 if (!model_.mode().is_default()) |
| 1108 browser_->InstantOverlayFocused(); | 1117 browser_->InstantOverlayFocused(); |
| 1109 #endif | 1118 #endif |
| 1110 } | 1119 } |
| 1111 | 1120 |
| 1112 void InstantController::ReloadOverlayIfStale() { | 1121 void InstantController::ReloadOverlayIfStale() { |
| 1113 // The local overlay is never stale. | 1122 // The local overlay is never stale. |
| 1114 if (overlay_ && (overlay_->IsLocal() || !overlay_->is_stale())) | 1123 if (overlay_ && (overlay_->IsLocal() || !overlay_->is_stale())) |
| 1115 return; | 1124 return; |
| 1116 | 1125 |
| 1117 // If the overlay is showing or the omnibox has focus, don't refresh the | 1126 // If the overlay is showing or the omnibox has focus, don't refresh the |
| 1118 // overlay. It will get refreshed the next time the overlay is hidden or the | 1127 // overlay. It will get refreshed the next time the overlay is hidden or the |
| 1119 // omnibox loses focus. | 1128 // omnibox loses focus. |
| 1120 if (omnibox_focus_state_ == OMNIBOX_FOCUS_NONE && model_.mode().is_default()) | 1129 if (omnibox_focus_state_ == OMNIBOX_FOCUS_NONE && model_.mode().is_default()) |
| 1121 ResetOverlay(GetInstantURL()); | 1130 ResetOverlay(GetInstantURL()); |
| 1122 } | 1131 } |
| 1123 | 1132 |
| 1124 void InstantController::OverlayLoadCompletedMainFrame() { | 1133 void InstantController::OverlayLoadCompletedMainFrame() { |
| 1125 if (overlay_->supports_instant()) | 1134 if (overlay_->supports_instant()) |
| 1126 return; | 1135 return; |
| 1127 InstantService* instant_service = | 1136 InstantService* instant_service = GetInstantService(); |
| 1128 InstantServiceFactory::GetForProfile(browser_->profile()); | |
| 1129 content::WebContents* contents = overlay_->contents(); | 1137 content::WebContents* contents = overlay_->contents(); |
| 1130 DCHECK(contents); | 1138 DCHECK(contents); |
| 1131 if (instant_service->IsInstantProcess( | 1139 if (instant_service->IsInstantProcess( |
| 1132 contents->GetRenderProcessHost()->GetID())) { | 1140 contents->GetRenderProcessHost()->GetID())) { |
| 1133 return; | 1141 return; |
| 1134 } | 1142 } |
| 1135 InstantSupportDetermined(contents, false); | 1143 InstantSupportDetermined(contents, false); |
| 1136 } | 1144 } |
| 1137 | 1145 |
| 1138 void InstantController::LogDebugEvent(const std::string& info) const { | 1146 void InstantController::LogDebugEvent(const std::string& info) const { |
| 1139 DVLOG(1) << info; | 1147 DVLOG(1) << info; |
| 1140 | 1148 |
| 1141 debug_events_.push_front(std::make_pair( | 1149 debug_events_.push_front(std::make_pair( |
| 1142 base::Time::Now().ToInternalValue(), info)); | 1150 base::Time::Now().ToInternalValue(), info)); |
| 1143 static const size_t kMaxDebugEventSize = 2000; | 1151 static const size_t kMaxDebugEventSize = 2000; |
| 1144 if (debug_events_.size() > kMaxDebugEventSize) | 1152 if (debug_events_.size() > kMaxDebugEventSize) |
| 1145 debug_events_.pop_back(); | 1153 debug_events_.pop_back(); |
| 1146 } | 1154 } |
| 1147 | 1155 |
| 1148 void InstantController::ClearDebugEvents() { | 1156 void InstantController::ClearDebugEvents() { |
| 1149 debug_events_.clear(); | 1157 debug_events_.clear(); |
| 1150 } | 1158 } |
| 1151 | 1159 |
| 1152 void InstantController::UpdateMostVisitedItems() { | 1160 void InstantController::UpdateMostVisitedItems() { |
| 1153 InstantService* instant_service = | 1161 InstantService* instant_service = GetInstantService(); |
| 1154 InstantServiceFactory::GetForProfile(profile()); | |
| 1155 if (!instant_service) | 1162 if (!instant_service) |
| 1156 return; | 1163 return; |
| 1157 | 1164 |
| 1158 std::vector<InstantMostVisitedItem> items; | 1165 std::vector<InstantMostVisitedItem> items; |
| 1159 instant_service->GetCurrentMostVisitedItems(&items); | 1166 instant_service->GetCurrentMostVisitedItems(&items); |
| 1160 | 1167 |
| 1161 if (overlay_ && GetOverlayContents() && | 1168 if (overlay_ && GetOverlayContents() && |
| 1162 SearchTabHelper::FromWebContents(overlay_->contents())-> | 1169 SearchTabHelper::FromWebContents(overlay_->contents())-> |
| 1163 UpdateLastKnownMostVisitedItems(items)) { | 1170 UpdateLastKnownMostVisitedItems(items)) { |
| 1164 overlay_->SendMostVisitedItems(items); | 1171 overlay_->SendMostVisitedItems(items); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 1177 } | 1184 } |
| 1178 | 1185 |
| 1179 content::NotificationService::current()->Notify( | 1186 content::NotificationService::current()->Notify( |
| 1180 chrome::NOTIFICATION_INSTANT_SENT_MOST_VISITED_ITEMS, | 1187 chrome::NOTIFICATION_INSTANT_SENT_MOST_VISITED_ITEMS, |
| 1181 content::Source<InstantController>(this), | 1188 content::Source<InstantController>(this), |
| 1182 content::NotificationService::NoDetails()); | 1189 content::NotificationService::NoDetails()); |
| 1183 } | 1190 } |
| 1184 | 1191 |
| 1185 void InstantController::DeleteMostVisitedItem(const GURL& url) { | 1192 void InstantController::DeleteMostVisitedItem(const GURL& url) { |
| 1186 DCHECK(!url.is_empty()); | 1193 DCHECK(!url.is_empty()); |
| 1187 InstantService* instant_service = | 1194 InstantService* instant_service = GetInstantService(); |
| 1188 InstantServiceFactory::GetForProfile(profile()); | |
| 1189 if (!instant_service) | 1195 if (!instant_service) |
| 1190 return; | 1196 return; |
| 1191 | 1197 |
| 1192 instant_service->DeleteMostVisitedItem(url); | 1198 instant_service->DeleteMostVisitedItem(url); |
| 1193 } | 1199 } |
| 1194 | 1200 |
| 1195 void InstantController::UndoMostVisitedDeletion(const GURL& url) { | 1201 void InstantController::UndoMostVisitedDeletion(const GURL& url) { |
| 1196 DCHECK(!url.is_empty()); | 1202 DCHECK(!url.is_empty()); |
| 1197 InstantService* instant_service = | 1203 InstantService* instant_service = GetInstantService(); |
| 1198 InstantServiceFactory::GetForProfile(profile()); | |
| 1199 if (!instant_service) | 1204 if (!instant_service) |
| 1200 return; | 1205 return; |
| 1201 | 1206 |
| 1202 instant_service->UndoMostVisitedDeletion(url); | 1207 instant_service->UndoMostVisitedDeletion(url); |
| 1203 } | 1208 } |
| 1204 | 1209 |
| 1205 void InstantController::UndoAllMostVisitedDeletions() { | 1210 void InstantController::UndoAllMostVisitedDeletions() { |
| 1206 InstantService* instant_service = | 1211 InstantService* instant_service = GetInstantService(); |
| 1207 InstantServiceFactory::GetForProfile(profile()); | |
| 1208 if (!instant_service) | 1212 if (!instant_service) |
| 1209 return; | 1213 return; |
| 1210 | 1214 |
| 1211 instant_service->UndoAllMostVisitedDeletions(); | 1215 instant_service->UndoAllMostVisitedDeletions(); |
| 1212 } | 1216 } |
| 1213 | 1217 |
| 1214 Profile* InstantController::profile() const { | 1218 Profile* InstantController::profile() const { |
| 1215 return browser_->profile(); | 1219 return browser_->profile(); |
| 1216 } | 1220 } |
| 1217 | 1221 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 1228 } | 1232 } |
| 1229 | 1233 |
| 1230 // TODO(shishir): We assume that the WebContent's current RenderViewHost is the | 1234 // TODO(shishir): We assume that the WebContent's current RenderViewHost is the |
| 1231 // RenderViewHost being created which is not always true. Fix this. | 1235 // RenderViewHost being created which is not always true. Fix this. |
| 1232 void InstantController::InstantPageRenderViewCreated( | 1236 void InstantController::InstantPageRenderViewCreated( |
| 1233 const content::WebContents* contents) { | 1237 const content::WebContents* contents) { |
| 1234 if (!extended_enabled()) | 1238 if (!extended_enabled()) |
| 1235 return; | 1239 return; |
| 1236 | 1240 |
| 1237 // Update theme info so that the page picks it up. | 1241 // Update theme info so that the page picks it up. |
| 1238 browser_->UpdateThemeInfo(); | 1242 InstantService* instant_service = GetInstantService(); |
| 1243 if (instant_service) | |
| 1244 instant_service->UpdateThemeInfo(); | |
| 1239 | 1245 |
| 1240 // Ensure the searchbox API has the correct initial state. | 1246 // Ensure the searchbox API has the correct initial state. |
| 1241 if (IsContentsFrom(overlay(), contents)) { | 1247 if (IsContentsFrom(overlay(), contents)) { |
| 1242 overlay_->SetDisplayInstantResults(instant_enabled_); | 1248 overlay_->SetDisplayInstantResults(instant_enabled_); |
| 1243 overlay_->FocusChanged(omnibox_focus_state_, omnibox_focus_change_reason_); | 1249 overlay_->FocusChanged(omnibox_focus_state_, omnibox_focus_change_reason_); |
| 1244 overlay_->SetOmniboxBounds(omnibox_bounds_); | 1250 overlay_->SetOmniboxBounds(omnibox_bounds_); |
| 1245 overlay_->InitializeFonts(); | 1251 overlay_->InitializeFonts(); |
| 1246 } else if (IsContentsFrom(ntp(), contents)) { | 1252 } else if (IsContentsFrom(ntp(), contents)) { |
| 1247 ntp_->SetDisplayInstantResults(instant_enabled_); | 1253 ntp_->SetDisplayInstantResults(instant_enabled_); |
| 1248 ntp_->SetOmniboxBounds(omnibox_bounds_); | 1254 ntp_->SetOmniboxBounds(omnibox_bounds_); |
| (...skipping 416 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1665 | 1671 |
| 1666 // Hide the |overlay_| since we are now using |instant_tab_| instead. | 1672 // Hide the |overlay_| since we are now using |instant_tab_| instead. |
| 1667 HideOverlay(); | 1673 HideOverlay(); |
| 1668 } else { | 1674 } else { |
| 1669 instant_tab_.reset(); | 1675 instant_tab_.reset(); |
| 1670 } | 1676 } |
| 1671 } | 1677 } |
| 1672 | 1678 |
| 1673 void InstantController::UpdateInfoForInstantTab() { | 1679 void InstantController::UpdateInfoForInstantTab() { |
| 1674 if (instant_tab_) { | 1680 if (instant_tab_) { |
| 1675 browser_->UpdateThemeInfo(); | 1681 // Update theme details. |
| 1682 InstantService* instant_service = GetInstantService(); | |
| 1683 if (instant_service) | |
| 1684 instant_service->UpdateThemeInfo(); | |
| 1685 | |
| 1676 instant_tab_->SetDisplayInstantResults(instant_enabled_); | 1686 instant_tab_->SetDisplayInstantResults(instant_enabled_); |
| 1677 instant_tab_->SetOmniboxBounds(omnibox_bounds_); | 1687 instant_tab_->SetOmniboxBounds(omnibox_bounds_); |
| 1678 instant_tab_->InitializeFonts(); | 1688 instant_tab_->InitializeFonts(); |
| 1679 UpdateMostVisitedItems(); | 1689 UpdateMostVisitedItems(); |
| 1680 instant_tab_->FocusChanged(omnibox_focus_state_, | 1690 instant_tab_->FocusChanged(omnibox_focus_state_, |
| 1681 omnibox_focus_change_reason_); | 1691 omnibox_focus_change_reason_); |
| 1682 instant_tab_->SetInputInProgress(IsInputInProgress()); | 1692 instant_tab_->SetInputInProgress(IsInputInProgress()); |
| 1683 } | 1693 } |
| 1684 } | 1694 } |
| 1685 | 1695 |
| (...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1900 bool js_webkit_enabled = profile()->GetPrefs()->GetBoolean( | 1910 bool js_webkit_enabled = profile()->GetPrefs()->GetBoolean( |
| 1901 prefs::kWebKitJavascriptEnabled); | 1911 prefs::kWebKitJavascriptEnabled); |
| 1902 return js_content_enabled && js_webkit_enabled; | 1912 return js_content_enabled && js_webkit_enabled; |
| 1903 } | 1913 } |
| 1904 | 1914 |
| 1905 bool InstantController::InStartup() const { | 1915 bool InstantController::InStartup() const { |
| 1906 // TODO(shishir): This is not completely reliable. Find a better way to detect | 1916 // TODO(shishir): This is not completely reliable. Find a better way to detect |
| 1907 // startup time. | 1917 // startup time. |
| 1908 return !browser_->GetActiveWebContents(); | 1918 return !browser_->GetActiveWebContents(); |
| 1909 } | 1919 } |
| 1920 | |
| 1921 InstantService* InstantController::GetInstantService() const { | |
| 1922 return InstantServiceFactory::GetForProfile(profile()); | |
| 1923 } | |
| OLD | NEW |