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_ && GetOverlayContents() && |
| 811 SearchTabHelper::FromWebContents(overlay_->contents())-> |
| 812 UpdateLastKnownThemeBackgroundInfo(theme_info)) { |
| 813 overlay_->SendThemeBackgroundInfo(theme_info); |
| 814 } |
| 815 |
| 816 if (ntp_ && ntp_->contents() && |
| 817 SearchTabHelper::FromWebContents(ntp_->contents())-> |
| 818 UpdateLastKnownThemeBackgroundInfo(theme_info)) { |
| 819 ntp_->SendThemeBackgroundInfo(theme_info); |
| 820 } |
| 821 |
| 822 if (instant_tab_ && instant_tab_->contents() && |
| 823 SearchTabHelper::FromWebContents(instant_tab_->contents())-> |
| 824 UpdateLastKnownThemeBackgroundInfo(theme_info)) { |
| 825 instant_tab_->SendThemeBackgroundInfo(theme_info); |
| 826 } |
| 827 } |
| 828 |
799 content::WebContents* InstantController::GetOverlayContents() const { | 829 content::WebContents* InstantController::GetOverlayContents() const { |
800 return overlay_ ? overlay_->contents() : NULL; | 830 return overlay_ ? overlay_->contents() : NULL; |
801 } | 831 } |
802 | 832 |
803 content::WebContents* InstantController::GetNTPContents() const { | 833 content::WebContents* InstantController::GetNTPContents() const { |
804 return ntp_ ? ntp_->contents() : NULL; | 834 return ntp_ ? ntp_->contents() : NULL; |
805 } | 835 } |
806 | 836 |
807 bool InstantController::IsOverlayingSearchResults() const { | 837 bool InstantController::IsOverlayingSearchResults() const { |
808 return model_.mode().is_search_suggestions() && IsFullHeight(model_) && | 838 return model_.mode().is_search_suggestions() && IsFullHeight(model_) && |
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1077 | 1107 |
1078 // Preload the Instant NTP. | 1108 // Preload the Instant NTP. |
1079 ntp_.reset(); | 1109 ntp_.reset(); |
1080 if (extended_enabled() && preload_ntp_) | 1110 if (extended_enabled() && preload_ntp_) |
1081 ResetNTP(GetInstantURL()); | 1111 ResetNTP(GetInstantURL()); |
1082 | 1112 |
1083 if (instant_tab_) | 1113 if (instant_tab_) |
1084 instant_tab_->SetDisplayInstantResults(instant_enabled_); | 1114 instant_tab_->SetDisplayInstantResults(instant_enabled_); |
1085 } | 1115 } |
1086 | 1116 |
1087 void InstantController::ThemeChanged(const ThemeBackgroundInfo& theme_info) { | |
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() { | 1117 void InstantController::SwappedOverlayContents() { |
1100 model_.SetOverlayContents(GetOverlayContents()); | 1118 model_.SetOverlayContents(GetOverlayContents()); |
1101 } | 1119 } |
1102 | 1120 |
1103 void InstantController::FocusedOverlayContents() { | 1121 void InstantController::FocusedOverlayContents() { |
1104 #if defined(USE_AURA) | 1122 #if defined(USE_AURA) |
1105 // On aura the omnibox only receives a focus lost if we initiate the focus | 1123 // On aura the omnibox only receives a focus lost if we initiate the focus |
1106 // change. This does that. | 1124 // change. This does that. |
1107 if (!model_.mode().is_default()) | 1125 if (!model_.mode().is_default()) |
1108 browser_->InstantOverlayFocused(); | 1126 browser_->InstantOverlayFocused(); |
1109 #endif | 1127 #endif |
1110 } | 1128 } |
1111 | 1129 |
1112 void InstantController::ReloadOverlayIfStale() { | 1130 void InstantController::ReloadOverlayIfStale() { |
1113 // The local overlay is never stale. | 1131 // The local overlay is never stale. |
1114 if (overlay_ && (overlay_->IsLocal() || !overlay_->is_stale())) | 1132 if (overlay_ && (overlay_->IsLocal() || !overlay_->is_stale())) |
1115 return; | 1133 return; |
1116 | 1134 |
1117 // If the overlay is showing or the omnibox has focus, don't refresh the | 1135 // 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 | 1136 // overlay. It will get refreshed the next time the overlay is hidden or the |
1119 // omnibox loses focus. | 1137 // omnibox loses focus. |
1120 if (omnibox_focus_state_ == OMNIBOX_FOCUS_NONE && model_.mode().is_default()) | 1138 if (omnibox_focus_state_ == OMNIBOX_FOCUS_NONE && model_.mode().is_default()) |
1121 ResetOverlay(GetInstantURL()); | 1139 ResetOverlay(GetInstantURL()); |
1122 } | 1140 } |
1123 | 1141 |
1124 void InstantController::OverlayLoadCompletedMainFrame() { | 1142 void InstantController::OverlayLoadCompletedMainFrame() { |
1125 if (overlay_->supports_instant()) | 1143 if (overlay_->supports_instant()) |
1126 return; | 1144 return; |
1127 InstantService* instant_service = | 1145 InstantService* instant_service = GetInstantService(); |
1128 InstantServiceFactory::GetForProfile(browser_->profile()); | |
1129 content::WebContents* contents = overlay_->contents(); | 1146 content::WebContents* contents = overlay_->contents(); |
1130 DCHECK(contents); | 1147 DCHECK(contents); |
1131 if (instant_service->IsInstantProcess( | 1148 if (instant_service->IsInstantProcess( |
1132 contents->GetRenderProcessHost()->GetID())) { | 1149 contents->GetRenderProcessHost()->GetID())) { |
1133 return; | 1150 return; |
1134 } | 1151 } |
1135 InstantSupportDetermined(contents, false); | 1152 InstantSupportDetermined(contents, false); |
1136 } | 1153 } |
1137 | 1154 |
1138 void InstantController::LogDebugEvent(const std::string& info) const { | 1155 void InstantController::LogDebugEvent(const std::string& info) const { |
1139 DVLOG(1) << info; | 1156 DVLOG(1) << info; |
1140 | 1157 |
1141 debug_events_.push_front(std::make_pair( | 1158 debug_events_.push_front(std::make_pair( |
1142 base::Time::Now().ToInternalValue(), info)); | 1159 base::Time::Now().ToInternalValue(), info)); |
1143 static const size_t kMaxDebugEventSize = 2000; | 1160 static const size_t kMaxDebugEventSize = 2000; |
1144 if (debug_events_.size() > kMaxDebugEventSize) | 1161 if (debug_events_.size() > kMaxDebugEventSize) |
1145 debug_events_.pop_back(); | 1162 debug_events_.pop_back(); |
1146 } | 1163 } |
1147 | 1164 |
1148 void InstantController::ClearDebugEvents() { | 1165 void InstantController::ClearDebugEvents() { |
1149 debug_events_.clear(); | 1166 debug_events_.clear(); |
1150 } | 1167 } |
1151 | 1168 |
1152 void InstantController::UpdateMostVisitedItems() { | 1169 void InstantController::UpdateMostVisitedItems() { |
1153 InstantService* instant_service = | 1170 InstantService* instant_service = GetInstantService(); |
1154 InstantServiceFactory::GetForProfile(profile()); | |
1155 if (!instant_service) | 1171 if (!instant_service) |
1156 return; | 1172 return; |
1157 | 1173 |
1158 std::vector<InstantMostVisitedItem> items; | 1174 std::vector<InstantMostVisitedItem> items; |
1159 instant_service->GetCurrentMostVisitedItems(&items); | 1175 instant_service->GetCurrentMostVisitedItems(&items); |
1160 | 1176 |
1161 if (overlay_ && GetOverlayContents() && | 1177 if (overlay_ && GetOverlayContents() && |
1162 SearchTabHelper::FromWebContents(overlay_->contents())-> | 1178 SearchTabHelper::FromWebContents(overlay_->contents())-> |
1163 UpdateLastKnownMostVisitedItems(items)) { | 1179 UpdateLastKnownMostVisitedItems(items)) { |
1164 overlay_->SendMostVisitedItems(items); | 1180 overlay_->SendMostVisitedItems(items); |
(...skipping 12 matching lines...) Expand all Loading... |
1177 } | 1193 } |
1178 | 1194 |
1179 content::NotificationService::current()->Notify( | 1195 content::NotificationService::current()->Notify( |
1180 chrome::NOTIFICATION_INSTANT_SENT_MOST_VISITED_ITEMS, | 1196 chrome::NOTIFICATION_INSTANT_SENT_MOST_VISITED_ITEMS, |
1181 content::Source<InstantController>(this), | 1197 content::Source<InstantController>(this), |
1182 content::NotificationService::NoDetails()); | 1198 content::NotificationService::NoDetails()); |
1183 } | 1199 } |
1184 | 1200 |
1185 void InstantController::DeleteMostVisitedItem(const GURL& url) { | 1201 void InstantController::DeleteMostVisitedItem(const GURL& url) { |
1186 DCHECK(!url.is_empty()); | 1202 DCHECK(!url.is_empty()); |
1187 InstantService* instant_service = | 1203 InstantService* instant_service = GetInstantService(); |
1188 InstantServiceFactory::GetForProfile(profile()); | |
1189 if (!instant_service) | 1204 if (!instant_service) |
1190 return; | 1205 return; |
1191 | 1206 |
1192 instant_service->DeleteMostVisitedItem(url); | 1207 instant_service->DeleteMostVisitedItem(url); |
1193 } | 1208 } |
1194 | 1209 |
1195 void InstantController::UndoMostVisitedDeletion(const GURL& url) { | 1210 void InstantController::UndoMostVisitedDeletion(const GURL& url) { |
1196 DCHECK(!url.is_empty()); | 1211 DCHECK(!url.is_empty()); |
1197 InstantService* instant_service = | 1212 InstantService* instant_service = GetInstantService(); |
1198 InstantServiceFactory::GetForProfile(profile()); | |
1199 if (!instant_service) | 1213 if (!instant_service) |
1200 return; | 1214 return; |
1201 | 1215 |
1202 instant_service->UndoMostVisitedDeletion(url); | 1216 instant_service->UndoMostVisitedDeletion(url); |
1203 } | 1217 } |
1204 | 1218 |
1205 void InstantController::UndoAllMostVisitedDeletions() { | 1219 void InstantController::UndoAllMostVisitedDeletions() { |
1206 InstantService* instant_service = | 1220 InstantService* instant_service = GetInstantService(); |
1207 InstantServiceFactory::GetForProfile(profile()); | |
1208 if (!instant_service) | 1221 if (!instant_service) |
1209 return; | 1222 return; |
1210 | 1223 |
1211 instant_service->UndoAllMostVisitedDeletions(); | 1224 instant_service->UndoAllMostVisitedDeletions(); |
1212 } | 1225 } |
1213 | 1226 |
1214 Profile* InstantController::profile() const { | 1227 Profile* InstantController::profile() const { |
1215 return browser_->profile(); | 1228 return browser_->profile(); |
1216 } | 1229 } |
1217 | 1230 |
(...skipping 10 matching lines...) Expand all Loading... |
1228 } | 1241 } |
1229 | 1242 |
1230 // TODO(shishir): We assume that the WebContent's current RenderViewHost is the | 1243 // TODO(shishir): We assume that the WebContent's current RenderViewHost is the |
1231 // RenderViewHost being created which is not always true. Fix this. | 1244 // RenderViewHost being created which is not always true. Fix this. |
1232 void InstantController::InstantPageRenderViewCreated( | 1245 void InstantController::InstantPageRenderViewCreated( |
1233 const content::WebContents* contents) { | 1246 const content::WebContents* contents) { |
1234 if (!extended_enabled()) | 1247 if (!extended_enabled()) |
1235 return; | 1248 return; |
1236 | 1249 |
1237 // Update theme info so that the page picks it up. | 1250 // Update theme info so that the page picks it up. |
1238 browser_->UpdateThemeInfo(); | 1251 InstantService* instant_service = GetInstantService(); |
| 1252 if (instant_service) |
| 1253 instant_service->UpdateThemeInfo(); |
1239 | 1254 |
1240 // Ensure the searchbox API has the correct initial state. | 1255 // Ensure the searchbox API has the correct initial state. |
1241 if (IsContentsFrom(overlay(), contents)) { | 1256 if (IsContentsFrom(overlay(), contents)) { |
1242 overlay_->SetDisplayInstantResults(instant_enabled_); | 1257 overlay_->SetDisplayInstantResults(instant_enabled_); |
1243 overlay_->FocusChanged(omnibox_focus_state_, omnibox_focus_change_reason_); | 1258 overlay_->FocusChanged(omnibox_focus_state_, omnibox_focus_change_reason_); |
1244 overlay_->SetOmniboxBounds(omnibox_bounds_); | 1259 overlay_->SetOmniboxBounds(omnibox_bounds_); |
1245 overlay_->InitializeFonts(); | 1260 overlay_->InitializeFonts(); |
1246 } else if (IsContentsFrom(ntp(), contents)) { | 1261 } else if (IsContentsFrom(ntp(), contents)) { |
1247 ntp_->SetDisplayInstantResults(instant_enabled_); | 1262 ntp_->SetDisplayInstantResults(instant_enabled_); |
1248 ntp_->SetOmniboxBounds(omnibox_bounds_); | 1263 ntp_->SetOmniboxBounds(omnibox_bounds_); |
(...skipping 416 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1665 | 1680 |
1666 // Hide the |overlay_| since we are now using |instant_tab_| instead. | 1681 // Hide the |overlay_| since we are now using |instant_tab_| instead. |
1667 HideOverlay(); | 1682 HideOverlay(); |
1668 } else { | 1683 } else { |
1669 instant_tab_.reset(); | 1684 instant_tab_.reset(); |
1670 } | 1685 } |
1671 } | 1686 } |
1672 | 1687 |
1673 void InstantController::UpdateInfoForInstantTab() { | 1688 void InstantController::UpdateInfoForInstantTab() { |
1674 if (instant_tab_) { | 1689 if (instant_tab_) { |
1675 browser_->UpdateThemeInfo(); | 1690 // Update theme details. |
| 1691 InstantService* instant_service = GetInstantService(); |
| 1692 if (instant_service) |
| 1693 instant_service->UpdateThemeInfo(); |
| 1694 |
1676 instant_tab_->SetDisplayInstantResults(instant_enabled_); | 1695 instant_tab_->SetDisplayInstantResults(instant_enabled_); |
1677 instant_tab_->SetOmniboxBounds(omnibox_bounds_); | 1696 instant_tab_->SetOmniboxBounds(omnibox_bounds_); |
1678 instant_tab_->InitializeFonts(); | 1697 instant_tab_->InitializeFonts(); |
1679 UpdateMostVisitedItems(); | 1698 UpdateMostVisitedItems(); |
1680 instant_tab_->FocusChanged(omnibox_focus_state_, | 1699 instant_tab_->FocusChanged(omnibox_focus_state_, |
1681 omnibox_focus_change_reason_); | 1700 omnibox_focus_change_reason_); |
1682 instant_tab_->SetInputInProgress(IsInputInProgress()); | 1701 instant_tab_->SetInputInProgress(IsInputInProgress()); |
1683 } | 1702 } |
1684 } | 1703 } |
1685 | 1704 |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1900 bool js_webkit_enabled = profile()->GetPrefs()->GetBoolean( | 1919 bool js_webkit_enabled = profile()->GetPrefs()->GetBoolean( |
1901 prefs::kWebKitJavascriptEnabled); | 1920 prefs::kWebKitJavascriptEnabled); |
1902 return js_content_enabled && js_webkit_enabled; | 1921 return js_content_enabled && js_webkit_enabled; |
1903 } | 1922 } |
1904 | 1923 |
1905 bool InstantController::InStartup() const { | 1924 bool InstantController::InStartup() const { |
1906 // TODO(shishir): This is not completely reliable. Find a better way to detect | 1925 // TODO(shishir): This is not completely reliable. Find a better way to detect |
1907 // startup time. | 1926 // startup time. |
1908 return !browser_->GetActiveWebContents(); | 1927 return !browser_->GetActiveWebContents(); |
1909 } | 1928 } |
| 1929 |
| 1930 InstantService* InstantController::GetInstantService() const { |
| 1931 return InstantServiceFactory::GetForProfile(profile()); |
| 1932 } |
OLD | NEW |