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/string_util.h" | 10 #include "base/string_util.h" |
11 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
13 #include "chrome/browser/autocomplete/autocomplete_provider.h" | 13 #include "chrome/browser/autocomplete/autocomplete_provider.h" |
14 #include "chrome/browser/autocomplete/autocomplete_result.h" | 14 #include "chrome/browser/autocomplete/autocomplete_result.h" |
15 #include "chrome/browser/autocomplete/search_provider.h" | 15 #include "chrome/browser/autocomplete/search_provider.h" |
16 #include "chrome/browser/history/history_service.h" | 16 #include "chrome/browser/history/history_service.h" |
17 #include "chrome/browser/history/history_service_factory.h" | 17 #include "chrome/browser/history/history_service_factory.h" |
18 #include "chrome/browser/history/history_tab_helper.h" | 18 #include "chrome/browser/history/history_tab_helper.h" |
19 #include "chrome/browser/history/top_sites.h" | |
20 #include "chrome/browser/platform_util.h" | 19 #include "chrome/browser/platform_util.h" |
21 #include "chrome/browser/search/instant_service.h" | 20 #include "chrome/browser/search/instant_service.h" |
22 #include "chrome/browser/search/instant_service_factory.h" | 21 #include "chrome/browser/search/instant_service_factory.h" |
23 #include "chrome/browser/search/search.h" | 22 #include "chrome/browser/search/search.h" |
24 #include "chrome/browser/search_engines/search_terms_data.h" | 23 #include "chrome/browser/search_engines/search_terms_data.h" |
25 #include "chrome/browser/search_engines/template_url_service.h" | 24 #include "chrome/browser/search_engines/template_url_service.h" |
26 #include "chrome/browser/search_engines/template_url_service_factory.h" | 25 #include "chrome/browser/search_engines/template_url_service_factory.h" |
27 #include "chrome/browser/ui/browser_instant_controller.h" | 26 #include "chrome/browser/ui/browser_instant_controller.h" |
28 #include "chrome/browser/ui/search/instant_ntp.h" | 27 #include "chrome/browser/ui/search/instant_ntp.h" |
29 #include "chrome/browser/ui/search/instant_overlay.h" | 28 #include "chrome/browser/ui/search/instant_overlay.h" |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 active_entry->GetTransitionType(), | 203 active_entry->GetTransitionType(), |
205 false, | 204 false, |
206 std::string(), | 205 std::string(), |
207 contents->GetBrowserContext()); | 206 contents->GetBrowserContext()); |
208 transient->SetExtraData(sessions::kSearchTermsKey, search_terms); | 207 transient->SetExtraData(sessions::kSearchTermsKey, search_terms); |
209 controller->SetTransientEntry(transient); | 208 controller->SetTransientEntry(transient); |
210 | 209 |
211 SearchTabHelper::FromWebContents(contents)->NavigationEntryUpdated(); | 210 SearchTabHelper::FromWebContents(contents)->NavigationEntryUpdated(); |
212 } | 211 } |
213 | 212 |
214 bool GetURLForMostVisitedItemID(Profile* profile, | |
215 InstantRestrictedID most_visited_item_id, | |
216 GURL* url) { | |
217 InstantService* instant_service = | |
218 InstantServiceFactory::GetForProfile(profile); | |
219 if (!instant_service) | |
220 return false; | |
221 | |
222 InstantMostVisitedItem item; | |
223 if (!instant_service->GetMostVisitedItemForID(most_visited_item_id, &item)) | |
224 return false; | |
225 | |
226 *url = item.url; | |
227 return true; | |
228 } | |
229 | |
230 template <typename T> | 213 template <typename T> |
231 void DeletePageSoon(scoped_ptr<T> page) { | 214 void DeletePageSoon(scoped_ptr<T> page) { |
232 if (page->contents()) | 215 if (page->contents()) |
233 page->ReleaseContents(); | 216 page->ReleaseContents(); |
234 | 217 |
235 base::MessageLoop::current()->DeleteSoon(FROM_HERE, page.release()); | 218 base::MessageLoop::current()->DeleteSoon(FROM_HERE, page.release()); |
236 } | 219 } |
237 | 220 |
238 } // namespace | 221 } // namespace |
239 | 222 |
240 InstantController::InstantController(BrowserInstantController* browser, | 223 InstantController::InstantController(BrowserInstantController* browser, |
241 bool extended_enabled) | 224 bool extended_enabled) |
242 : browser_(browser), | 225 : browser_(browser), |
243 extended_enabled_(extended_enabled), | 226 extended_enabled_(extended_enabled), |
244 instant_enabled_(false), | 227 instant_enabled_(false), |
245 use_local_page_only_(true), | 228 use_local_page_only_(true), |
246 preload_ntp_(true), | 229 preload_ntp_(true), |
247 model_(this), | 230 model_(this), |
248 use_tab_for_suggestions_(false), | 231 use_tab_for_suggestions_(false), |
249 last_omnibox_text_has_inline_autocompletion_(false), | 232 last_omnibox_text_has_inline_autocompletion_(false), |
250 last_verbatim_(false), | 233 last_verbatim_(false), |
251 last_transition_type_(content::PAGE_TRANSITION_LINK), | 234 last_transition_type_(content::PAGE_TRANSITION_LINK), |
252 last_match_was_search_(false), | 235 last_match_was_search_(false), |
253 omnibox_focus_state_(OMNIBOX_FOCUS_NONE), | 236 omnibox_focus_state_(OMNIBOX_FOCUS_NONE), |
254 omnibox_bounds_(-1, -1, 0, 0), | 237 omnibox_bounds_(-1, -1, 0, 0), |
255 allow_overlay_to_show_search_suggestions_(false), | 238 allow_overlay_to_show_search_suggestions_(false) { |
256 weak_ptr_factory_(this) { | |
257 | 239 |
258 // When the InstantController lives, the InstantService should live. | 240 // When the InstantController lives, the InstantService should live. |
259 // InstantService sets up profile-level facilities such as the ThemeSource for | 241 // InstantService sets up profile-level facilities such as the ThemeSource for |
260 // the NTP. | 242 // the NTP. |
261 InstantServiceFactory::GetForProfile(browser_->profile()); | 243 InstantServiceFactory::GetForProfile(browser_->profile()); |
262 } | 244 } |
263 | 245 |
264 InstantController::~InstantController() { | 246 InstantController::~InstantController() { |
265 } | 247 } |
266 | 248 |
(...skipping 822 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1089 base::Time::Now().ToInternalValue(), info)); | 1071 base::Time::Now().ToInternalValue(), info)); |
1090 static const size_t kMaxDebugEventSize = 2000; | 1072 static const size_t kMaxDebugEventSize = 2000; |
1091 if (debug_events_.size() > kMaxDebugEventSize) | 1073 if (debug_events_.size() > kMaxDebugEventSize) |
1092 debug_events_.pop_back(); | 1074 debug_events_.pop_back(); |
1093 } | 1075 } |
1094 | 1076 |
1095 void InstantController::ClearDebugEvents() { | 1077 void InstantController::ClearDebugEvents() { |
1096 debug_events_.clear(); | 1078 debug_events_.clear(); |
1097 } | 1079 } |
1098 | 1080 |
1099 void InstantController::DeleteMostVisitedItem( | 1081 void InstantController::UpdateMostVisitedItems() { |
1100 InstantRestrictedID most_visited_item_id) { | 1082 InstantService* instant_service = |
1101 history::TopSites* top_sites = browser_->profile()->GetTopSites(); | 1083 InstantServiceFactory::GetForProfile(profile()); |
1102 if (!top_sites) | 1084 if (!instant_service) |
1103 return; | 1085 return; |
1104 | 1086 |
1105 GURL url; | 1087 std::vector<InstantMostVisitedItemIDPair> items; |
1106 if (GetURLForMostVisitedItemID(browser_->profile(), | 1088 instant_service->GetCurrentMostVisitedItems(&items); |
1107 most_visited_item_id, &url)) | 1089 |
1108 top_sites->AddBlacklistedURL(url); | 1090 if (overlay_) |
| 1091 overlay_->SendMostVisitedItems(items); |
| 1092 if (ntp_) |
| 1093 ntp_->SendMostVisitedItems(items); |
| 1094 if (instant_tab_) |
| 1095 instant_tab_->SendMostVisitedItems(items); |
| 1096 |
| 1097 content::NotificationService::current()->Notify( |
| 1098 chrome::NOTIFICATION_INSTANT_SENT_MOST_VISITED_ITEMS, |
| 1099 content::Source<InstantController>(this), |
| 1100 content::NotificationService::NoDetails()); |
1109 } | 1101 } |
1110 | 1102 |
1111 void InstantController::UndoMostVisitedDeletion( | 1103 void InstantController::DeleteMostVisitedItem(const GURL& url) { |
1112 InstantRestrictedID most_visited_item_id) { | 1104 DCHECK(!url.is_empty()); |
1113 history::TopSites* top_sites = browser_->profile()->GetTopSites(); | 1105 InstantService* instant_service = |
1114 if (!top_sites) | 1106 InstantServiceFactory::GetForProfile(profile()); |
| 1107 if (!instant_service) |
1115 return; | 1108 return; |
1116 | 1109 |
1117 GURL url; | 1110 instant_service->DeleteMostVisitedItem(url); |
1118 if (GetURLForMostVisitedItemID(browser_->profile(), | 1111 } |
1119 most_visited_item_id, &url)) | 1112 |
1120 top_sites->RemoveBlacklistedURL(url); | 1113 void InstantController::UndoMostVisitedDeletion(const GURL& url) { |
| 1114 DCHECK(!url.is_empty()); |
| 1115 InstantService* instant_service = |
| 1116 InstantServiceFactory::GetForProfile(profile()); |
| 1117 if (!instant_service) |
| 1118 return; |
| 1119 |
| 1120 instant_service->UndoMostVisitedDeletion(url); |
1121 } | 1121 } |
1122 | 1122 |
1123 void InstantController::UndoAllMostVisitedDeletions() { | 1123 void InstantController::UndoAllMostVisitedDeletions() { |
1124 history::TopSites* top_sites = browser_->profile()->GetTopSites(); | 1124 InstantService* instant_service = |
1125 if (!top_sites) | 1125 InstantServiceFactory::GetForProfile(profile()); |
| 1126 if (!instant_service) |
1126 return; | 1127 return; |
1127 | 1128 |
1128 top_sites->ClearBlacklistedURLs(); | 1129 instant_service->UndoAllMostVisitedDeletions(); |
1129 } | 1130 } |
1130 | 1131 |
1131 Profile* InstantController::profile() const { | 1132 Profile* InstantController::profile() const { |
1132 return browser_->profile(); | 1133 return browser_->profile(); |
1133 } | 1134 } |
1134 | 1135 |
1135 InstantOverlay* InstantController::overlay() const { | 1136 InstantOverlay* InstantController::overlay() const { |
1136 return overlay_.get(); | 1137 return overlay_.get(); |
1137 } | 1138 } |
1138 | 1139 |
1139 InstantTab* InstantController::instant_tab() const { | 1140 InstantTab* InstantController::instant_tab() const { |
1140 return instant_tab_.get(); | 1141 return instant_tab_.get(); |
1141 } | 1142 } |
1142 | 1143 |
1143 InstantNTP* InstantController::ntp() const { | 1144 InstantNTP* InstantController::ntp() const { |
1144 return ntp_.get(); | 1145 return ntp_.get(); |
1145 } | 1146 } |
1146 | 1147 |
1147 void InstantController::Observe(int type, | |
1148 const content::NotificationSource& source, | |
1149 const content::NotificationDetails& details) { | |
1150 DCHECK_EQ(type, chrome::NOTIFICATION_TOP_SITES_CHANGED); | |
1151 RequestMostVisitedItems(); | |
1152 } | |
1153 | |
1154 // TODO(shishir): We assume that the WebContent's current RenderViewHost is the | 1148 // TODO(shishir): We assume that the WebContent's current RenderViewHost is the |
1155 // RenderViewHost being created which is not always true. Fix this. | 1149 // RenderViewHost being created which is not always true. Fix this. |
1156 void InstantController::InstantPageRenderViewCreated( | 1150 void InstantController::InstantPageRenderViewCreated( |
1157 const content::WebContents* contents) { | 1151 const content::WebContents* contents) { |
1158 if (!extended_enabled_) | 1152 if (!extended_enabled_) |
1159 return; | 1153 return; |
1160 | 1154 |
1161 // Update theme info so that the page picks it up. | 1155 // Update theme info so that the page picks it up. |
1162 browser_->UpdateThemeInfo(); | 1156 browser_->UpdateThemeInfo(); |
1163 | 1157 |
1164 // Ensure the searchbox API has the correct initial state. | 1158 // Ensure the searchbox API has the correct initial state. |
1165 if (IsContentsFrom(overlay(), contents)) { | 1159 if (IsContentsFrom(overlay(), contents)) { |
1166 overlay_->SetDisplayInstantResults(instant_enabled_); | 1160 overlay_->SetDisplayInstantResults(instant_enabled_); |
1167 overlay_->KeyCaptureChanged( | 1161 overlay_->KeyCaptureChanged( |
1168 omnibox_focus_state_ == OMNIBOX_FOCUS_INVISIBLE); | 1162 omnibox_focus_state_ == OMNIBOX_FOCUS_INVISIBLE); |
1169 overlay_->SetOmniboxBounds(omnibox_bounds_); | 1163 overlay_->SetOmniboxBounds(omnibox_bounds_); |
1170 overlay_->InitializeFonts(); | 1164 overlay_->InitializeFonts(); |
1171 } else if (IsContentsFrom(ntp(), contents)) { | 1165 } else if (IsContentsFrom(ntp(), contents)) { |
1172 ntp_->SetDisplayInstantResults(instant_enabled_); | 1166 ntp_->SetDisplayInstantResults(instant_enabled_); |
1173 ntp_->SetOmniboxBounds(omnibox_bounds_); | 1167 ntp_->SetOmniboxBounds(omnibox_bounds_); |
1174 ntp_->InitializeFonts(); | 1168 ntp_->InitializeFonts(); |
1175 } else { | 1169 } else { |
1176 NOTREACHED(); | 1170 NOTREACHED(); |
1177 } | 1171 } |
1178 StartListeningToMostVisitedChanges(); | 1172 UpdateMostVisitedItems(); |
1179 } | 1173 } |
1180 | 1174 |
1181 void InstantController::InstantSupportDetermined( | 1175 void InstantController::InstantSupportDetermined( |
1182 const content::WebContents* contents, | 1176 const content::WebContents* contents, |
1183 bool supports_instant) { | 1177 bool supports_instant) { |
1184 if (IsContentsFrom(instant_tab(), contents)) { | 1178 if (IsContentsFrom(instant_tab(), contents)) { |
1185 if (!supports_instant) | 1179 if (!supports_instant) |
1186 base::MessageLoop::current()->DeleteSoon(FROM_HERE, | 1180 base::MessageLoop::current()->DeleteSoon(FROM_HERE, |
1187 instant_tab_.release()); | 1181 instant_tab_.release()); |
1188 | 1182 |
(...skipping 375 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1564 instant_tab_.reset(); | 1558 instant_tab_.reset(); |
1565 } | 1559 } |
1566 } | 1560 } |
1567 | 1561 |
1568 void InstantController::UpdateInfoForInstantTab() { | 1562 void InstantController::UpdateInfoForInstantTab() { |
1569 if (instant_tab_) { | 1563 if (instant_tab_) { |
1570 browser_->UpdateThemeInfo(); | 1564 browser_->UpdateThemeInfo(); |
1571 instant_tab_->SetDisplayInstantResults(instant_enabled_); | 1565 instant_tab_->SetDisplayInstantResults(instant_enabled_); |
1572 instant_tab_->SetOmniboxBounds(omnibox_bounds_); | 1566 instant_tab_->SetOmniboxBounds(omnibox_bounds_); |
1573 instant_tab_->InitializeFonts(); | 1567 instant_tab_->InitializeFonts(); |
1574 StartListeningToMostVisitedChanges(); | 1568 UpdateMostVisitedItems(); |
1575 instant_tab_->KeyCaptureChanged( | 1569 instant_tab_->KeyCaptureChanged( |
1576 omnibox_focus_state_ == OMNIBOX_FOCUS_INVISIBLE); | 1570 omnibox_focus_state_ == OMNIBOX_FOCUS_INVISIBLE); |
1577 } | 1571 } |
1578 } | 1572 } |
1579 | 1573 |
1580 void InstantController::HideOverlay() { | 1574 void InstantController::HideOverlay() { |
1581 HideInternal(); | 1575 HideInternal(); |
1582 ReloadOverlayIfStale(); | 1576 ReloadOverlayIfStale(); |
1583 } | 1577 } |
1584 | 1578 |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1663 // There is no point in sanity-checking |intersection.y()| because the omnibox | 1657 // There is no point in sanity-checking |intersection.y()| because the omnibox |
1664 // can be placed anywhere vertically relative to the overlay (for example, in | 1658 // can be placed anywhere vertically relative to the overlay (for example, in |
1665 // Mac fullscreen mode, the omnibox is fully enclosed by the overlay bounds). | 1659 // Mac fullscreen mode, the omnibox is fully enclosed by the overlay bounds). |
1666 DCHECK_LE(0, intersection.x()); | 1660 DCHECK_LE(0, intersection.x()); |
1667 DCHECK_LE(0, intersection.width()); | 1661 DCHECK_LE(0, intersection.width()); |
1668 DCHECK_LE(0, intersection.height()); | 1662 DCHECK_LE(0, intersection.height()); |
1669 | 1663 |
1670 overlay_->SetPopupBounds(intersection); | 1664 overlay_->SetPopupBounds(intersection); |
1671 } | 1665 } |
1672 | 1666 |
1673 void InstantController::StartListeningToMostVisitedChanges() { | |
1674 history::TopSites* top_sites = browser_->profile()->GetTopSites(); | |
1675 if (top_sites) { | |
1676 if (!registrar_.IsRegistered( | |
1677 this, chrome::NOTIFICATION_TOP_SITES_CHANGED, | |
1678 content::Source<history::TopSites>(top_sites))) { | |
1679 // TopSites updates itself after a delay. This is especially noticable | |
1680 // when your profile is empty. Ask TopSites to update itself when we're | |
1681 // about to show the new tab page. | |
1682 top_sites->SyncWithHistory(); | |
1683 | 1667 |
1684 RequestMostVisitedItems(); | |
1685 | |
1686 // Register for notification when TopSites changes. | |
1687 registrar_.Add(this, chrome::NOTIFICATION_TOP_SITES_CHANGED, | |
1688 content::Source<history::TopSites>(top_sites)); | |
1689 } else { | |
1690 // We are already registered, so just get and send the most visited data. | |
1691 RequestMostVisitedItems(); | |
1692 } | |
1693 } | |
1694 } | |
1695 | |
1696 void InstantController::RequestMostVisitedItems() { | |
1697 history::TopSites* top_sites = browser_->profile()->GetTopSites(); | |
1698 if (top_sites) { | |
1699 top_sites->GetMostVisitedURLs( | |
1700 base::Bind(&InstantController::OnMostVisitedItemsReceived, | |
1701 weak_ptr_factory_.GetWeakPtr())); | |
1702 } | |
1703 } | |
1704 | |
1705 void InstantController::OnMostVisitedItemsReceived( | |
1706 const history::MostVisitedURLList& data) { | |
1707 InstantService* instant_service = | |
1708 InstantServiceFactory::GetForProfile(browser_->profile()); | |
1709 if (!instant_service) | |
1710 return; | |
1711 | |
1712 history::MostVisitedURLList reordered_data(data); | |
1713 history::TopSites::MaybeShuffle(&reordered_data); | |
1714 | |
1715 std::vector<InstantMostVisitedItem> most_visited_items; | |
1716 for (size_t i = 0; i < reordered_data.size(); i++) { | |
1717 const history::MostVisitedURL& url = reordered_data[i]; | |
1718 InstantMostVisitedItem item; | |
1719 item.url = url.url; | |
1720 item.title = url.title; | |
1721 most_visited_items.push_back(item); | |
1722 } | |
1723 | |
1724 instant_service->AddMostVisitedItems(most_visited_items); | |
1725 | |
1726 std::vector<InstantMostVisitedItemIDPair> items_with_ids; | |
1727 instant_service->GetCurrentMostVisitedItems(&items_with_ids); | |
1728 SendMostVisitedItems(items_with_ids); | |
1729 } | |
1730 | |
1731 void InstantController::SendMostVisitedItems( | |
1732 const std::vector<InstantMostVisitedItemIDPair>& items) { | |
1733 if (overlay_) | |
1734 overlay_->SendMostVisitedItems(items); | |
1735 if (ntp_) | |
1736 ntp_->SendMostVisitedItems(items); | |
1737 if (instant_tab_) | |
1738 instant_tab_->SendMostVisitedItems(items); | |
1739 content::NotificationService::current()->Notify( | |
1740 chrome::NOTIFICATION_INSTANT_SENT_MOST_VISITED_ITEMS, | |
1741 content::Source<InstantController>(this), | |
1742 content::NotificationService::NoDetails()); | |
1743 } | |
1744 | 1668 |
1745 bool InstantController::FixSuggestion(InstantSuggestion* suggestion) const { | 1669 bool InstantController::FixSuggestion(InstantSuggestion* suggestion) const { |
1746 // We only accept suggestions if the user has typed text. If the user is | 1670 // We only accept suggestions if the user has typed text. If the user is |
1747 // arrowing up/down (|last_user_text_| is empty), we reject suggestions. | 1671 // arrowing up/down (|last_user_text_| is empty), we reject suggestions. |
1748 if (last_user_text_.empty()) | 1672 if (last_user_text_.empty()) |
1749 return false; | 1673 return false; |
1750 | 1674 |
1751 // If the page is trying to set inline autocompletion in verbatim mode, | 1675 // If the page is trying to set inline autocompletion in verbatim mode, |
1752 // instead try suggesting the exact omnibox text. This makes the omnibox | 1676 // instead try suggesting the exact omnibox text. This makes the omnibox |
1753 // interpret user text as an URL if possible while preventing unwanted | 1677 // interpret user text as an URL if possible while preventing unwanted |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1834 | 1758 |
1835 result->transition = match.transition; | 1759 result->transition = match.transition; |
1836 result->relevance = match.relevance; | 1760 result->relevance = match.relevance; |
1837 result->autocomplete_match_index = autocomplete_match_index; | 1761 result->autocomplete_match_index = autocomplete_match_index; |
1838 | 1762 |
1839 DVLOG(1) << " " << result->relevance << " " << result->type << " " | 1763 DVLOG(1) << " " << result->relevance << " " << result->type << " " |
1840 << result->provider << " " << result->destination_url << " '" | 1764 << result->provider << " " << result->destination_url << " '" |
1841 << result->description << "' '" << result->search_query << "' " | 1765 << result->description << "' '" << result->search_query << "' " |
1842 << result->transition << " " << result->autocomplete_match_index; | 1766 << result->transition << " " << result->autocomplete_match_index; |
1843 } | 1767 } |
OLD | NEW |