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/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
(...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
262 last_match_was_search_(false), | 262 last_match_was_search_(false), |
263 omnibox_focus_state_(OMNIBOX_FOCUS_NONE), | 263 omnibox_focus_state_(OMNIBOX_FOCUS_NONE), |
264 omnibox_focus_change_reason_(OMNIBOX_FOCUS_CHANGE_EXPLICIT), | 264 omnibox_focus_change_reason_(OMNIBOX_FOCUS_CHANGE_EXPLICIT), |
265 omnibox_bounds_(-1, -1, 0, 0), | 265 omnibox_bounds_(-1, -1, 0, 0), |
266 allow_overlay_to_show_search_suggestions_(false) { | 266 allow_overlay_to_show_search_suggestions_(false) { |
267 | 267 |
268 // When the InstantController lives, the InstantService should live. | 268 // When the InstantController lives, the InstantService should live. |
269 // InstantService sets up profile-level facilities such as the ThemeSource for | 269 // InstantService sets up profile-level facilities such as the ThemeSource for |
270 // the NTP. | 270 // the NTP. |
271 // However, in some tests, browser_ may be null. | 271 // However, in some tests, browser_ may be null. |
272 if (browser_) | 272 if (browser_) { |
273 InstantServiceFactory::GetForProfile(browser_->profile()); | 273 InstantService* instant_service = GetInstantService(); |
274 instant_service->AddObserver(this); | |
275 } | |
274 } | 276 } |
275 | 277 |
276 InstantController::~InstantController() { | 278 InstantController::~InstantController() { |
279 if (browser_) { | |
280 InstantService* instant_service = GetInstantService(); | |
281 instant_service->RemoveObserver(this); | |
282 } | |
277 } | 283 } |
278 | 284 |
279 void InstantController::OnAutocompleteStart() { | 285 void InstantController::OnAutocompleteStart() { |
280 if (UseTabForSuggestions() && instant_tab_->supports_instant()) { | 286 if (UseTabForSuggestions() && instant_tab_->supports_instant()) { |
281 LOG_INSTANT_DEBUG_EVENT( | 287 LOG_INSTANT_DEBUG_EVENT( |
282 this, "OnAutocompleteStart: using InstantTab"); | 288 this, "OnAutocompleteStart: using InstantTab"); |
283 return; | 289 return; |
284 } | 290 } |
285 | 291 |
286 // Not using |instant_tab_|. Check if overlay is OK to use. | 292 // Not using |instant_tab_|. Check if overlay is OK to use. |
(...skipping 785 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1072 | 1078 |
1073 // Preload the Instant NTP. | 1079 // Preload the Instant NTP. |
1074 ntp_.reset(); | 1080 ntp_.reset(); |
1075 if (extended_enabled() && preload_ntp_) | 1081 if (extended_enabled() && preload_ntp_) |
1076 ResetNTP(GetInstantURL()); | 1082 ResetNTP(GetInstantURL()); |
1077 | 1083 |
1078 if (instant_tab_) | 1084 if (instant_tab_) |
1079 instant_tab_->SetDisplayInstantResults(instant_enabled_); | 1085 instant_tab_->SetDisplayInstantResults(instant_enabled_); |
1080 } | 1086 } |
1081 | 1087 |
1082 void InstantController::ThemeChanged(const ThemeBackgroundInfo& theme_info) { | 1088 void InstantController::ThemeInfoChanged( |
1089 const ThemeBackgroundInfo& theme_info) { | |
1083 if (!extended_enabled()) | 1090 if (!extended_enabled()) |
1084 return; | 1091 return; |
1085 | 1092 |
1086 if (overlay_) | 1093 if (overlay_ && GetOverlayContents() && |
1094 SearchTabHelper::FromWebContents(overlay_->contents())-> | |
1095 UpdateLastKnownThemeBackgroundInfo(theme_info)) { | |
1087 overlay_->SendThemeBackgroundInfo(theme_info); | 1096 overlay_->SendThemeBackgroundInfo(theme_info); |
1088 if (ntp_) | 1097 } |
1098 | |
1099 if (ntp_ && ntp_->contents() && | |
1100 SearchTabHelper::FromWebContents(ntp_->contents())-> | |
1101 UpdateLastKnownThemeBackgroundInfo(theme_info)) { | |
1089 ntp_->SendThemeBackgroundInfo(theme_info); | 1102 ntp_->SendThemeBackgroundInfo(theme_info); |
1090 if (instant_tab_) | 1103 } |
1104 | |
1105 if (instant_tab_ && instant_tab_->contents() && | |
1106 SearchTabHelper::FromWebContents(instant_tab_->contents())-> | |
1107 UpdateLastKnownThemeBackgroundInfo(theme_info)) { | |
1091 instant_tab_->SendThemeBackgroundInfo(theme_info); | 1108 instant_tab_->SendThemeBackgroundInfo(theme_info); |
1109 } | |
1092 } | 1110 } |
1093 | 1111 |
1094 void InstantController::SwappedOverlayContents() { | 1112 void InstantController::SwappedOverlayContents() { |
1095 model_.SetOverlayContents(GetOverlayContents()); | 1113 model_.SetOverlayContents(GetOverlayContents()); |
1096 } | 1114 } |
1097 | 1115 |
1098 void InstantController::FocusedOverlayContents() { | 1116 void InstantController::FocusedOverlayContents() { |
1099 #if defined(USE_AURA) | 1117 #if defined(USE_AURA) |
1100 // On aura the omnibox only receives a focus lost if we initiate the focus | 1118 // On aura the omnibox only receives a focus lost if we initiate the focus |
1101 // change. This does that. | 1119 // change. This does that. |
(...skipping 10 matching lines...) Expand all Loading... | |
1112 // If the overlay is showing or the omnibox has focus, don't refresh the | 1130 // If the overlay is showing or the omnibox has focus, don't refresh the |
1113 // overlay. It will get refreshed the next time the overlay is hidden or the | 1131 // overlay. It will get refreshed the next time the overlay is hidden or the |
1114 // omnibox loses focus. | 1132 // omnibox loses focus. |
1115 if (omnibox_focus_state_ == OMNIBOX_FOCUS_NONE && model_.mode().is_default()) | 1133 if (omnibox_focus_state_ == OMNIBOX_FOCUS_NONE && model_.mode().is_default()) |
1116 ResetOverlay(GetInstantURL()); | 1134 ResetOverlay(GetInstantURL()); |
1117 } | 1135 } |
1118 | 1136 |
1119 void InstantController::OverlayLoadCompletedMainFrame() { | 1137 void InstantController::OverlayLoadCompletedMainFrame() { |
1120 if (overlay_->supports_instant()) | 1138 if (overlay_->supports_instant()) |
1121 return; | 1139 return; |
1122 InstantService* instant_service = | 1140 InstantService* instant_service = GetInstantService(); |
1123 InstantServiceFactory::GetForProfile(browser_->profile()); | |
1124 content::WebContents* contents = overlay_->contents(); | 1141 content::WebContents* contents = overlay_->contents(); |
1125 DCHECK(contents); | 1142 DCHECK(contents); |
1126 if (instant_service->IsInstantProcess( | 1143 if (instant_service->IsInstantProcess( |
1127 contents->GetRenderProcessHost()->GetID())) { | 1144 contents->GetRenderProcessHost()->GetID())) { |
1128 return; | 1145 return; |
1129 } | 1146 } |
1130 InstantSupportDetermined(contents, false); | 1147 InstantSupportDetermined(contents, false); |
1131 } | 1148 } |
1132 | 1149 |
1133 void InstantController::LogDebugEvent(const std::string& info) const { | 1150 void InstantController::LogDebugEvent(const std::string& info) const { |
1134 DVLOG(1) << info; | 1151 DVLOG(1) << info; |
1135 | 1152 |
1136 debug_events_.push_front(std::make_pair( | 1153 debug_events_.push_front(std::make_pair( |
1137 base::Time::Now().ToInternalValue(), info)); | 1154 base::Time::Now().ToInternalValue(), info)); |
1138 static const size_t kMaxDebugEventSize = 2000; | 1155 static const size_t kMaxDebugEventSize = 2000; |
1139 if (debug_events_.size() > kMaxDebugEventSize) | 1156 if (debug_events_.size() > kMaxDebugEventSize) |
1140 debug_events_.pop_back(); | 1157 debug_events_.pop_back(); |
1141 } | 1158 } |
1142 | 1159 |
1143 void InstantController::ClearDebugEvents() { | 1160 void InstantController::ClearDebugEvents() { |
1144 debug_events_.clear(); | 1161 debug_events_.clear(); |
1145 } | 1162 } |
1146 | 1163 |
1147 void InstantController::UpdateMostVisitedItems() { | 1164 void InstantController::UpdateMostVisitedItems() { |
1148 InstantService* instant_service = | 1165 InstantService* instant_service = GetInstantService(); |
1149 InstantServiceFactory::GetForProfile(profile()); | |
1150 if (!instant_service) | 1166 if (!instant_service) |
1151 return; | 1167 return; |
1152 | 1168 |
1153 std::vector<InstantMostVisitedItem> items; | 1169 std::vector<InstantMostVisitedItem> items; |
1154 instant_service->GetCurrentMostVisitedItems(&items); | 1170 instant_service->GetCurrentMostVisitedItems(&items); |
1155 | 1171 |
1156 if (overlay_ && GetOverlayContents() && | 1172 if (overlay_ && GetOverlayContents() && |
1157 SearchTabHelper::FromWebContents(overlay_->contents())-> | 1173 SearchTabHelper::FromWebContents(overlay_->contents())-> |
1158 UpdateLastKnownMostVisitedItems(items)) { | 1174 UpdateLastKnownMostVisitedItems(items)) { |
1159 overlay_->SendMostVisitedItems(items); | 1175 overlay_->SendMostVisitedItems(items); |
(...skipping 10 matching lines...) Expand all Loading... | |
1170 UpdateLastKnownMostVisitedItems(items)) { | 1186 UpdateLastKnownMostVisitedItems(items)) { |
1171 instant_tab_->SendMostVisitedItems(items); | 1187 instant_tab_->SendMostVisitedItems(items); |
1172 } | 1188 } |
1173 | 1189 |
1174 content::NotificationService::current()->Notify( | 1190 content::NotificationService::current()->Notify( |
1175 chrome::NOTIFICATION_INSTANT_SENT_MOST_VISITED_ITEMS, | 1191 chrome::NOTIFICATION_INSTANT_SENT_MOST_VISITED_ITEMS, |
1176 content::Source<InstantController>(this), | 1192 content::Source<InstantController>(this), |
1177 content::NotificationService::NoDetails()); | 1193 content::NotificationService::NoDetails()); |
1178 } | 1194 } |
1179 | 1195 |
1196 | |
samarth
2013/06/19 05:34:54
Stray newline
kmadhusu
2013/06/19 17:43:30
Removed.
| |
1180 void InstantController::DeleteMostVisitedItem(const GURL& url) { | 1197 void InstantController::DeleteMostVisitedItem(const GURL& url) { |
1181 DCHECK(!url.is_empty()); | 1198 DCHECK(!url.is_empty()); |
1182 InstantService* instant_service = | 1199 InstantService* instant_service = GetInstantService(); |
1183 InstantServiceFactory::GetForProfile(profile()); | |
1184 if (!instant_service) | 1200 if (!instant_service) |
1185 return; | 1201 return; |
1186 | 1202 |
1187 instant_service->DeleteMostVisitedItem(url); | 1203 instant_service->DeleteMostVisitedItem(url); |
1188 } | 1204 } |
1189 | 1205 |
1190 void InstantController::UndoMostVisitedDeletion(const GURL& url) { | 1206 void InstantController::UndoMostVisitedDeletion(const GURL& url) { |
1191 DCHECK(!url.is_empty()); | 1207 DCHECK(!url.is_empty()); |
1192 InstantService* instant_service = | 1208 InstantService* instant_service = GetInstantService(); |
1193 InstantServiceFactory::GetForProfile(profile()); | |
1194 if (!instant_service) | 1209 if (!instant_service) |
1195 return; | 1210 return; |
1196 | 1211 |
1197 instant_service->UndoMostVisitedDeletion(url); | 1212 instant_service->UndoMostVisitedDeletion(url); |
1198 } | 1213 } |
1199 | 1214 |
1200 void InstantController::UndoAllMostVisitedDeletions() { | 1215 void InstantController::UndoAllMostVisitedDeletions() { |
1201 InstantService* instant_service = | 1216 InstantService* instant_service = GetInstantService(); |
1202 InstantServiceFactory::GetForProfile(profile()); | |
1203 if (!instant_service) | 1217 if (!instant_service) |
1204 return; | 1218 return; |
1205 | 1219 |
1206 instant_service->UndoAllMostVisitedDeletions(); | 1220 instant_service->UndoAllMostVisitedDeletions(); |
1207 } | 1221 } |
1208 | 1222 |
1209 Profile* InstantController::profile() const { | 1223 Profile* InstantController::profile() const { |
1210 return browser_->profile(); | 1224 return browser_->profile(); |
1211 } | 1225 } |
1212 | 1226 |
(...skipping 10 matching lines...) Expand all Loading... | |
1223 } | 1237 } |
1224 | 1238 |
1225 // TODO(shishir): We assume that the WebContent's current RenderViewHost is the | 1239 // TODO(shishir): We assume that the WebContent's current RenderViewHost is the |
1226 // RenderViewHost being created which is not always true. Fix this. | 1240 // RenderViewHost being created which is not always true. Fix this. |
1227 void InstantController::InstantPageRenderViewCreated( | 1241 void InstantController::InstantPageRenderViewCreated( |
1228 const content::WebContents* contents) { | 1242 const content::WebContents* contents) { |
1229 if (!extended_enabled()) | 1243 if (!extended_enabled()) |
1230 return; | 1244 return; |
1231 | 1245 |
1232 // Update theme info so that the page picks it up. | 1246 // Update theme info so that the page picks it up. |
1233 browser_->UpdateThemeInfo(); | 1247 InstantService* instant_service = GetInstantService(); |
1248 if (instant_service) | |
1249 instant_service->UpdateThemeInfo(); | |
1234 | 1250 |
1235 // Ensure the searchbox API has the correct initial state. | 1251 // Ensure the searchbox API has the correct initial state. |
1236 if (IsContentsFrom(overlay(), contents)) { | 1252 if (IsContentsFrom(overlay(), contents)) { |
1237 overlay_->SetDisplayInstantResults(instant_enabled_); | 1253 overlay_->SetDisplayInstantResults(instant_enabled_); |
1238 overlay_->FocusChanged(omnibox_focus_state_, omnibox_focus_change_reason_); | 1254 overlay_->FocusChanged(omnibox_focus_state_, omnibox_focus_change_reason_); |
1239 overlay_->SetOmniboxBounds(omnibox_bounds_); | 1255 overlay_->SetOmniboxBounds(omnibox_bounds_); |
1240 overlay_->InitializeFonts(); | 1256 overlay_->InitializeFonts(); |
1241 } else if (IsContentsFrom(ntp(), contents)) { | 1257 } else if (IsContentsFrom(ntp(), contents)) { |
1242 ntp_->SetDisplayInstantResults(instant_enabled_); | 1258 ntp_->SetDisplayInstantResults(instant_enabled_); |
1243 ntp_->SetOmniboxBounds(omnibox_bounds_); | 1259 ntp_->SetOmniboxBounds(omnibox_bounds_); |
(...skipping 412 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1656 | 1672 |
1657 // Hide the |overlay_| since we are now using |instant_tab_| instead. | 1673 // Hide the |overlay_| since we are now using |instant_tab_| instead. |
1658 HideOverlay(); | 1674 HideOverlay(); |
1659 } else { | 1675 } else { |
1660 instant_tab_.reset(); | 1676 instant_tab_.reset(); |
1661 } | 1677 } |
1662 } | 1678 } |
1663 | 1679 |
1664 void InstantController::UpdateInfoForInstantTab() { | 1680 void InstantController::UpdateInfoForInstantTab() { |
1665 if (instant_tab_) { | 1681 if (instant_tab_) { |
1666 browser_->UpdateThemeInfo(); | 1682 // Update theme details. |
1683 InstantService* instant_service = GetInstantService(); | |
1684 if (instant_service) | |
1685 instant_service->UpdateThemeInfo(); | |
1686 | |
1667 instant_tab_->SetDisplayInstantResults(instant_enabled_); | 1687 instant_tab_->SetDisplayInstantResults(instant_enabled_); |
1668 instant_tab_->SetOmniboxBounds(omnibox_bounds_); | 1688 instant_tab_->SetOmniboxBounds(omnibox_bounds_); |
1669 instant_tab_->InitializeFonts(); | 1689 instant_tab_->InitializeFonts(); |
1670 UpdateMostVisitedItems(); | 1690 UpdateMostVisitedItems(); |
1671 instant_tab_->FocusChanged(omnibox_focus_state_, | 1691 instant_tab_->FocusChanged(omnibox_focus_state_, |
1672 omnibox_focus_change_reason_); | 1692 omnibox_focus_change_reason_); |
1673 instant_tab_->SetInputInProgress(IsInputInProgress()); | 1693 instant_tab_->SetInputInProgress(IsInputInProgress()); |
1674 } | 1694 } |
1675 } | 1695 } |
1676 | 1696 |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1867 result->transition = match.transition; | 1887 result->transition = match.transition; |
1868 result->relevance = match.relevance; | 1888 result->relevance = match.relevance; |
1869 result->autocomplete_match_index = autocomplete_match_index; | 1889 result->autocomplete_match_index = autocomplete_match_index; |
1870 | 1890 |
1871 DVLOG(1) << " " << result->relevance << " " | 1891 DVLOG(1) << " " << result->relevance << " " |
1872 << UTF8ToUTF16(AutocompleteMatchType::ToString(result->type)) << " " | 1892 << UTF8ToUTF16(AutocompleteMatchType::ToString(result->type)) << " " |
1873 << result->provider << " " << result->destination_url << " '" | 1893 << result->provider << " " << result->destination_url << " '" |
1874 << result->description << "' '" << result->search_query << "' " | 1894 << result->description << "' '" << result->search_query << "' " |
1875 << result->transition << " " << result->autocomplete_match_index; | 1895 << result->transition << " " << result->autocomplete_match_index; |
1876 } | 1896 } |
1897 | |
1898 InstantService* InstantController::GetInstantService() const { | |
samarth
2013/06/19 05:34:54
Eh, seeing this, it's arguable whether this functi
kmadhusu
2013/06/19 17:43:30
Let's keep it.
| |
1899 return InstantServiceFactory::GetForProfile(profile()); | |
1900 } | |
OLD | NEW |