Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(280)

Side by Side Diff: chrome/browser/ui/search/instant_controller.cc

Issue 14660022: Move most visited item state info from InstantController to InstantService. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
229 active_entry->GetTransitionType(), 228 active_entry->GetTransitionType(),
230 false, 229 false,
231 std::string(), 230 std::string(),
232 contents->GetBrowserContext()); 231 contents->GetBrowserContext());
233 transient->SetExtraData(sessions::kSearchTermsKey, search_terms); 232 transient->SetExtraData(sessions::kSearchTermsKey, search_terms);
234 controller->SetTransientEntry(transient); 233 controller->SetTransientEntry(transient);
235 234
236 SearchTabHelper::FromWebContents(contents)->NavigationEntryUpdated(); 235 SearchTabHelper::FromWebContents(contents)->NavigationEntryUpdated();
237 } 236 }
238 237
239 bool GetURLForMostVisitedItemID(Profile* profile,
240 InstantRestrictedID most_visited_item_id,
241 GURL* url) {
242 InstantService* instant_service =
243 InstantServiceFactory::GetForProfile(profile);
244 if (!instant_service)
245 return false;
246
247 InstantMostVisitedItem item;
248 if (!instant_service->GetMostVisitedItemForID(most_visited_item_id, &item))
249 return false;
250
251 *url = item.url;
252 return true;
253 }
254
255 template <typename T> 238 template <typename T>
256 void DeletePageSoon(scoped_ptr<T> page) { 239 void DeletePageSoon(scoped_ptr<T> page) {
257 if (page->contents()) { 240 if (page->contents()) {
258 base::MessageLoop::current()->DeleteSoon( 241 base::MessageLoop::current()->DeleteSoon(
259 FROM_HERE, page->ReleaseContents().release()); 242 FROM_HERE, page->ReleaseContents().release());
260 } 243 }
261 244
262 base::MessageLoop::current()->DeleteSoon(FROM_HERE, page.release()); 245 base::MessageLoop::current()->DeleteSoon(FROM_HERE, page.release());
263 } 246 }
264 247
265 } // namespace 248 } // namespace
266 249
267 InstantController::InstantController(BrowserInstantController* browser, 250 InstantController::InstantController(BrowserInstantController* browser,
268 bool extended_enabled) 251 bool extended_enabled)
269 : browser_(browser), 252 : browser_(browser),
270 extended_enabled_(extended_enabled), 253 extended_enabled_(extended_enabled),
271 instant_enabled_(false), 254 instant_enabled_(false),
272 use_local_page_only_(true), 255 use_local_page_only_(true),
273 preload_ntp_(true), 256 preload_ntp_(true),
274 model_(this), 257 model_(this),
275 use_tab_for_suggestions_(false), 258 use_tab_for_suggestions_(false),
276 last_omnibox_text_has_inline_autocompletion_(false), 259 last_omnibox_text_has_inline_autocompletion_(false),
277 last_verbatim_(false), 260 last_verbatim_(false),
278 last_transition_type_(content::PAGE_TRANSITION_LINK), 261 last_transition_type_(content::PAGE_TRANSITION_LINK),
279 last_match_was_search_(false), 262 last_match_was_search_(false),
280 omnibox_focus_state_(OMNIBOX_FOCUS_NONE), 263 omnibox_focus_state_(OMNIBOX_FOCUS_NONE),
281 omnibox_focus_change_reason_(OMNIBOX_FOCUS_CHANGE_EXPLICIT), 264 omnibox_focus_change_reason_(OMNIBOX_FOCUS_CHANGE_EXPLICIT),
282 omnibox_bounds_(-1, -1, 0, 0), 265 omnibox_bounds_(-1, -1, 0, 0),
283 allow_overlay_to_show_search_suggestions_(false), 266 allow_overlay_to_show_search_suggestions_(false) {
284 weak_ptr_factory_(this) {
285 267
286 // When the InstantController lives, the InstantService should live. 268 // When the InstantController lives, the InstantService should live.
287 // InstantService sets up profile-level facilities such as the ThemeSource for 269 // InstantService sets up profile-level facilities such as the ThemeSource for
288 // the NTP. 270 // the NTP.
289 // However, in some tests, browser_ may be null. 271 // However, in some tests, browser_ may be null.
290 if (browser_) 272 if (browser_)
291 InstantServiceFactory::GetForProfile(browser_->profile()); 273 InstantServiceFactory::GetForProfile(browser_->profile());
292 } 274 }
293 275
294 InstantController::~InstantController() { 276 InstantController::~InstantController() {
(...skipping 820 matching lines...) Expand 10 before | Expand all | Expand 10 after
1115 base::Time::Now().ToInternalValue(), info)); 1097 base::Time::Now().ToInternalValue(), info));
1116 static const size_t kMaxDebugEventSize = 2000; 1098 static const size_t kMaxDebugEventSize = 2000;
1117 if (debug_events_.size() > kMaxDebugEventSize) 1099 if (debug_events_.size() > kMaxDebugEventSize)
1118 debug_events_.pop_back(); 1100 debug_events_.pop_back();
1119 } 1101 }
1120 1102
1121 void InstantController::ClearDebugEvents() { 1103 void InstantController::ClearDebugEvents() {
1122 debug_events_.clear(); 1104 debug_events_.clear();
1123 } 1105 }
1124 1106
1125 void InstantController::DeleteMostVisitedItem( 1107 void InstantController::UpdateMostVisitedItems() {
1126 InstantRestrictedID most_visited_item_id) { 1108 InstantService* instant_service =
1127 history::TopSites* top_sites = browser_->profile()->GetTopSites(); 1109 InstantServiceFactory::GetForProfile(profile());
1128 if (!top_sites) 1110 if (!instant_service)
1129 return; 1111 return;
1130 1112
1131 GURL url; 1113 std::vector<InstantMostVisitedItemIDPair> items;
1132 if (GetURLForMostVisitedItemID(browser_->profile(), 1114 instant_service->GetCurrentMostVisitedItems(&items);
1133 most_visited_item_id, &url)) 1115
1134 top_sites->AddBlacklistedURL(url); 1116 if (overlay_)
1117 overlay_->SendMostVisitedItems(items);
1118 if (ntp_)
1119 ntp_->SendMostVisitedItems(items);
1120 if (instant_tab_)
1121 instant_tab_->SendMostVisitedItems(items);
1122
1123 content::NotificationService::current()->Notify(
1124 chrome::NOTIFICATION_INSTANT_SENT_MOST_VISITED_ITEMS,
1125 content::Source<InstantController>(this),
1126 content::NotificationService::NoDetails());
1135 } 1127 }
1136 1128
1137 void InstantController::UndoMostVisitedDeletion( 1129 void InstantController::DeleteMostVisitedItem(const GURL& url) {
1138 InstantRestrictedID most_visited_item_id) { 1130 DCHECK(!url.is_empty());
1139 history::TopSites* top_sites = browser_->profile()->GetTopSites(); 1131 InstantService* instant_service =
1140 if (!top_sites) 1132 InstantServiceFactory::GetForProfile(profile());
1133 if (!instant_service)
1141 return; 1134 return;
1142 1135
1143 GURL url; 1136 instant_service->DeleteMostVisitedItem(url);
1144 if (GetURLForMostVisitedItemID(browser_->profile(), 1137 }
1145 most_visited_item_id, &url)) 1138
1146 top_sites->RemoveBlacklistedURL(url); 1139 void InstantController::UndoMostVisitedDeletion(const GURL& url) {
1140 DCHECK(!url.is_empty());
1141 InstantService* instant_service =
1142 InstantServiceFactory::GetForProfile(profile());
1143 if (!instant_service)
1144 return;
1145
1146 instant_service->UndoMostVisitedDeletion(url);
1147 } 1147 }
1148 1148
1149 void InstantController::UndoAllMostVisitedDeletions() { 1149 void InstantController::UndoAllMostVisitedDeletions() {
1150 history::TopSites* top_sites = browser_->profile()->GetTopSites(); 1150 InstantService* instant_service =
1151 if (!top_sites) 1151 InstantServiceFactory::GetForProfile(profile());
1152 if (!instant_service)
1152 return; 1153 return;
1153 1154
1154 top_sites->ClearBlacklistedURLs(); 1155 instant_service->UndoAllMostVisitedDeletions();
1155 } 1156 }
1156 1157
1157 Profile* InstantController::profile() const { 1158 Profile* InstantController::profile() const {
1158 return browser_->profile(); 1159 return browser_->profile();
1159 } 1160 }
1160 1161
1161 InstantOverlay* InstantController::overlay() const { 1162 InstantOverlay* InstantController::overlay() const {
1162 return overlay_.get(); 1163 return overlay_.get();
1163 } 1164 }
1164 1165
1165 InstantTab* InstantController::instant_tab() const { 1166 InstantTab* InstantController::instant_tab() const {
1166 return instant_tab_.get(); 1167 return instant_tab_.get();
1167 } 1168 }
1168 1169
1169 InstantNTP* InstantController::ntp() const { 1170 InstantNTP* InstantController::ntp() const {
1170 return ntp_.get(); 1171 return ntp_.get();
1171 } 1172 }
1172 1173
1173 void InstantController::Observe(int type,
1174 const content::NotificationSource& source,
1175 const content::NotificationDetails& details) {
1176 DCHECK_EQ(type, chrome::NOTIFICATION_TOP_SITES_CHANGED);
1177 RequestMostVisitedItems();
1178 }
1179
1180 // TODO(shishir): We assume that the WebContent's current RenderViewHost is the 1174 // TODO(shishir): We assume that the WebContent's current RenderViewHost is the
1181 // RenderViewHost being created which is not always true. Fix this. 1175 // RenderViewHost being created which is not always true. Fix this.
1182 void InstantController::InstantPageRenderViewCreated( 1176 void InstantController::InstantPageRenderViewCreated(
1183 const content::WebContents* contents) { 1177 const content::WebContents* contents) {
1184 if (!extended_enabled()) 1178 if (!extended_enabled())
1185 return; 1179 return;
1186 1180
1187 // Update theme info so that the page picks it up. 1181 // Update theme info so that the page picks it up.
1188 browser_->UpdateThemeInfo(); 1182 browser_->UpdateThemeInfo();
1189 1183
1190 // Ensure the searchbox API has the correct initial state. 1184 // Ensure the searchbox API has the correct initial state.
1191 if (IsContentsFrom(overlay(), contents)) { 1185 if (IsContentsFrom(overlay(), contents)) {
1192 overlay_->SetDisplayInstantResults(instant_enabled_); 1186 overlay_->SetDisplayInstantResults(instant_enabled_);
1193 overlay_->FocusChanged(omnibox_focus_state_, omnibox_focus_change_reason_); 1187 overlay_->FocusChanged(omnibox_focus_state_, omnibox_focus_change_reason_);
1194 overlay_->SetOmniboxBounds(omnibox_bounds_); 1188 overlay_->SetOmniboxBounds(omnibox_bounds_);
1195 overlay_->InitializeFonts(); 1189 overlay_->InitializeFonts();
1196 } else if (IsContentsFrom(ntp(), contents)) { 1190 } else if (IsContentsFrom(ntp(), contents)) {
1197 ntp_->SetDisplayInstantResults(instant_enabled_); 1191 ntp_->SetDisplayInstantResults(instant_enabled_);
1198 ntp_->SetOmniboxBounds(omnibox_bounds_); 1192 ntp_->SetOmniboxBounds(omnibox_bounds_);
1199 ntp_->InitializeFonts(); 1193 ntp_->InitializeFonts();
1200 } else { 1194 } else {
1201 NOTREACHED(); 1195 NOTREACHED();
1202 } 1196 }
1203 StartListeningToMostVisitedChanges(); 1197 UpdateMostVisitedItems();
1204 } 1198 }
1205 1199
1206 void InstantController::InstantSupportDetermined( 1200 void InstantController::InstantSupportDetermined(
1207 const content::WebContents* contents, 1201 const content::WebContents* contents,
1208 bool supports_instant) { 1202 bool supports_instant) {
1209 if (IsContentsFrom(instant_tab(), contents)) { 1203 if (IsContentsFrom(instant_tab(), contents)) {
1210 if (!supports_instant) 1204 if (!supports_instant)
1211 base::MessageLoop::current()->DeleteSoon(FROM_HERE, 1205 base::MessageLoop::current()->DeleteSoon(FROM_HERE,
1212 instant_tab_.release()); 1206 instant_tab_.release());
1213 1207
(...skipping 401 matching lines...) Expand 10 before | Expand all | Expand 10 after
1615 instant_tab_.reset(); 1609 instant_tab_.reset();
1616 } 1610 }
1617 } 1611 }
1618 1612
1619 void InstantController::UpdateInfoForInstantTab() { 1613 void InstantController::UpdateInfoForInstantTab() {
1620 if (instant_tab_) { 1614 if (instant_tab_) {
1621 browser_->UpdateThemeInfo(); 1615 browser_->UpdateThemeInfo();
1622 instant_tab_->SetDisplayInstantResults(instant_enabled_); 1616 instant_tab_->SetDisplayInstantResults(instant_enabled_);
1623 instant_tab_->SetOmniboxBounds(omnibox_bounds_); 1617 instant_tab_->SetOmniboxBounds(omnibox_bounds_);
1624 instant_tab_->InitializeFonts(); 1618 instant_tab_->InitializeFonts();
1625 StartListeningToMostVisitedChanges(); 1619 UpdateMostVisitedItemsInfo();
1626 instant_tab_->FocusChanged(omnibox_focus_state_, 1620 instant_tab_->FocusChanged(omnibox_focus_state_,
1627 omnibox_focus_change_reason_); 1621 omnibox_focus_change_reason_);
1628 } 1622 }
1629 } 1623 }
1630 1624
1631 void InstantController::HideOverlay() { 1625 void InstantController::HideOverlay() {
1632 HideInternal(); 1626 HideInternal();
1633 ReloadOverlayIfStale(); 1627 ReloadOverlayIfStale();
1634 } 1628 }
1635 1629
1636 void InstantController::HideInternal() { 1630 void InstantController::HideInternal() {
1637 LOG_INSTANT_DEBUG_EVENT(this, "Hide"); 1631 LOG_INSTANT_DEBUG_EVENT(this, "Hide");
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
1714 // There is no point in sanity-checking |intersection.y()| because the omnibox 1708 // There is no point in sanity-checking |intersection.y()| because the omnibox
1715 // can be placed anywhere vertically relative to the overlay (for example, in 1709 // can be placed anywhere vertically relative to the overlay (for example, in
1716 // Mac fullscreen mode, the omnibox is fully enclosed by the overlay bounds). 1710 // Mac fullscreen mode, the omnibox is fully enclosed by the overlay bounds).
1717 DCHECK_LE(0, intersection.x()); 1711 DCHECK_LE(0, intersection.x());
1718 DCHECK_LE(0, intersection.width()); 1712 DCHECK_LE(0, intersection.width());
1719 DCHECK_LE(0, intersection.height()); 1713 DCHECK_LE(0, intersection.height());
1720 1714
1721 overlay_->SetPopupBounds(intersection); 1715 overlay_->SetPopupBounds(intersection);
1722 } 1716 }
1723 1717
1724 void InstantController::StartListeningToMostVisitedChanges() {
1725 history::TopSites* top_sites = browser_->profile()->GetTopSites();
1726 if (top_sites) {
1727 if (!registrar_.IsRegistered(
1728 this, chrome::NOTIFICATION_TOP_SITES_CHANGED,
1729 content::Source<history::TopSites>(top_sites))) {
1730 // TopSites updates itself after a delay. This is especially noticable
1731 // when your profile is empty. Ask TopSites to update itself when we're
1732 // about to show the new tab page.
1733 top_sites->SyncWithHistory();
1734 1718
1735 RequestMostVisitedItems();
1736
1737 // Register for notification when TopSites changes.
1738 registrar_.Add(this, chrome::NOTIFICATION_TOP_SITES_CHANGED,
1739 content::Source<history::TopSites>(top_sites));
1740 } else {
1741 // We are already registered, so just get and send the most visited data.
1742 RequestMostVisitedItems();
1743 }
1744 }
1745 }
1746
1747 void InstantController::RequestMostVisitedItems() {
1748 history::TopSites* top_sites = browser_->profile()->GetTopSites();
1749 if (top_sites) {
1750 top_sites->GetMostVisitedURLs(
1751 base::Bind(&InstantController::OnMostVisitedItemsReceived,
1752 weak_ptr_factory_.GetWeakPtr()));
1753 }
1754 }
1755
1756 void InstantController::OnMostVisitedItemsReceived(
1757 const history::MostVisitedURLList& data) {
1758 InstantService* instant_service =
1759 InstantServiceFactory::GetForProfile(browser_->profile());
1760 if (!instant_service)
1761 return;
1762
1763 history::MostVisitedURLList reordered_data(data);
1764 history::TopSites::MaybeShuffle(&reordered_data);
1765
1766 std::vector<InstantMostVisitedItem> most_visited_items;
1767 for (size_t i = 0; i < reordered_data.size(); i++) {
1768 const history::MostVisitedURL& url = reordered_data[i];
1769 InstantMostVisitedItem item;
1770 item.url = url.url;
1771 item.title = url.title;
1772 most_visited_items.push_back(item);
1773 }
1774
1775 instant_service->AddMostVisitedItems(most_visited_items);
1776
1777 std::vector<InstantMostVisitedItemIDPair> items_with_ids;
1778 instant_service->GetCurrentMostVisitedItems(&items_with_ids);
1779 SendMostVisitedItems(items_with_ids);
1780 }
1781
1782 void InstantController::SendMostVisitedItems(
1783 const std::vector<InstantMostVisitedItemIDPair>& items) {
1784 if (overlay_)
1785 overlay_->SendMostVisitedItems(items);
1786 if (ntp_)
1787 ntp_->SendMostVisitedItems(items);
1788 if (instant_tab_)
1789 instant_tab_->SendMostVisitedItems(items);
1790 content::NotificationService::current()->Notify(
1791 chrome::NOTIFICATION_INSTANT_SENT_MOST_VISITED_ITEMS,
1792 content::Source<InstantController>(this),
1793 content::NotificationService::NoDetails());
1794 }
1795 1719
1796 bool InstantController::FixSuggestion(InstantSuggestion* suggestion) const { 1720 bool InstantController::FixSuggestion(InstantSuggestion* suggestion) const {
1797 // We only accept suggestions if the user has typed text. If the user is 1721 // We only accept suggestions if the user has typed text. If the user is
1798 // arrowing up/down (|last_user_text_| is empty), we reject suggestions. 1722 // arrowing up/down (|last_user_text_| is empty), we reject suggestions.
1799 if (last_user_text_.empty()) 1723 if (last_user_text_.empty())
1800 return false; 1724 return false;
1801 1725
1802 // If the page is trying to set inline autocompletion in verbatim mode, 1726 // If the page is trying to set inline autocompletion in verbatim mode,
1803 // instead try suggesting the exact omnibox text. This makes the omnibox 1727 // instead try suggesting the exact omnibox text. This makes the omnibox
1804 // interpret user text as an URL if possible while preventing unwanted 1728 // interpret user text as an URL if possible while preventing unwanted
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
1886 result->transition = match.transition; 1810 result->transition = match.transition;
1887 result->relevance = match.relevance; 1811 result->relevance = match.relevance;
1888 result->autocomplete_match_index = autocomplete_match_index; 1812 result->autocomplete_match_index = autocomplete_match_index;
1889 1813
1890 DVLOG(1) << " " << result->relevance << " " 1814 DVLOG(1) << " " << result->relevance << " "
1891 << UTF8ToUTF16(AutocompleteMatchType::ToString(result->type)) << " " 1815 << UTF8ToUTF16(AutocompleteMatchType::ToString(result->type)) << " "
1892 << result->provider << " " << result->destination_url << " '" 1816 << result->provider << " " << result->destination_url << " '"
1893 << result->description << "' '" << result->search_query << "' " 1817 << result->description << "' '" << result->search_query << "' "
1894 << result->transition << " " << result->autocomplete_match_index; 1818 << result->transition << " " << result->autocomplete_match_index;
1895 } 1819 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698