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

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
« no previous file with comments | « chrome/browser/ui/search/instant_controller.h ('k') | chrome/browser/ui/search/instant_page.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 823 matching lines...) Expand 10 before | Expand all | Expand 10 after
1118 base::Time::Now().ToInternalValue(), info)); 1100 base::Time::Now().ToInternalValue(), info));
1119 static const size_t kMaxDebugEventSize = 2000; 1101 static const size_t kMaxDebugEventSize = 2000;
1120 if (debug_events_.size() > kMaxDebugEventSize) 1102 if (debug_events_.size() > kMaxDebugEventSize)
1121 debug_events_.pop_back(); 1103 debug_events_.pop_back();
1122 } 1104 }
1123 1105
1124 void InstantController::ClearDebugEvents() { 1106 void InstantController::ClearDebugEvents() {
1125 debug_events_.clear(); 1107 debug_events_.clear();
1126 } 1108 }
1127 1109
1128 void InstantController::DeleteMostVisitedItem( 1110 void InstantController::UpdateMostVisitedItems() {
1129 InstantRestrictedID most_visited_item_id) { 1111 InstantService* instant_service =
1130 history::TopSites* top_sites = browser_->profile()->GetTopSites(); 1112 InstantServiceFactory::GetForProfile(profile());
1131 if (!top_sites) 1113 if (!instant_service)
1132 return; 1114 return;
1133 1115
1134 GURL url; 1116 std::vector<InstantMostVisitedItemIDPair> items;
1135 if (GetURLForMostVisitedItemID(browser_->profile(), 1117 instant_service->GetCurrentMostVisitedItems(&items);
1136 most_visited_item_id, &url)) 1118
1137 top_sites->AddBlacklistedURL(url); 1119 if (overlay_)
1120 overlay_->SendMostVisitedItems(items);
1121 if (ntp_)
1122 ntp_->SendMostVisitedItems(items);
1123 if (instant_tab_)
1124 instant_tab_->SendMostVisitedItems(items);
1125
1126 content::NotificationService::current()->Notify(
1127 chrome::NOTIFICATION_INSTANT_SENT_MOST_VISITED_ITEMS,
1128 content::Source<InstantController>(this),
1129 content::NotificationService::NoDetails());
1138 } 1130 }
1139 1131
1140 void InstantController::UndoMostVisitedDeletion( 1132 void InstantController::DeleteMostVisitedItem(const GURL& url) {
1141 InstantRestrictedID most_visited_item_id) { 1133 DCHECK(!url.is_empty());
1142 history::TopSites* top_sites = browser_->profile()->GetTopSites(); 1134 InstantService* instant_service =
1143 if (!top_sites) 1135 InstantServiceFactory::GetForProfile(profile());
1136 if (!instant_service)
1144 return; 1137 return;
1145 1138
1146 GURL url; 1139 instant_service->DeleteMostVisitedItem(url);
1147 if (GetURLForMostVisitedItemID(browser_->profile(), 1140 }
1148 most_visited_item_id, &url)) 1141
1149 top_sites->RemoveBlacklistedURL(url); 1142 void InstantController::UndoMostVisitedDeletion(const GURL& url) {
1143 DCHECK(!url.is_empty());
1144 InstantService* instant_service =
1145 InstantServiceFactory::GetForProfile(profile());
1146 if (!instant_service)
1147 return;
1148
1149 instant_service->UndoMostVisitedDeletion(url);
1150 } 1150 }
1151 1151
1152 void InstantController::UndoAllMostVisitedDeletions() { 1152 void InstantController::UndoAllMostVisitedDeletions() {
1153 history::TopSites* top_sites = browser_->profile()->GetTopSites(); 1153 InstantService* instant_service =
1154 if (!top_sites) 1154 InstantServiceFactory::GetForProfile(profile());
1155 if (!instant_service)
1155 return; 1156 return;
1156 1157
1157 top_sites->ClearBlacklistedURLs(); 1158 instant_service->UndoAllMostVisitedDeletions();
1158 } 1159 }
1159 1160
1160 Profile* InstantController::profile() const { 1161 Profile* InstantController::profile() const {
1161 return browser_->profile(); 1162 return browser_->profile();
1162 } 1163 }
1163 1164
1164 InstantOverlay* InstantController::overlay() const { 1165 InstantOverlay* InstantController::overlay() const {
1165 return overlay_.get(); 1166 return overlay_.get();
1166 } 1167 }
1167 1168
1168 InstantTab* InstantController::instant_tab() const { 1169 InstantTab* InstantController::instant_tab() const {
1169 return instant_tab_.get(); 1170 return instant_tab_.get();
1170 } 1171 }
1171 1172
1172 InstantNTP* InstantController::ntp() const { 1173 InstantNTP* InstantController::ntp() const {
1173 return ntp_.get(); 1174 return ntp_.get();
1174 } 1175 }
1175 1176
1176 void InstantController::Observe(int type,
1177 const content::NotificationSource& source,
1178 const content::NotificationDetails& details) {
1179 DCHECK_EQ(type, chrome::NOTIFICATION_TOP_SITES_CHANGED);
1180 RequestMostVisitedItems();
1181 }
1182
1183 // TODO(shishir): We assume that the WebContent's current RenderViewHost is the 1177 // TODO(shishir): We assume that the WebContent's current RenderViewHost is the
1184 // RenderViewHost being created which is not always true. Fix this. 1178 // RenderViewHost being created which is not always true. Fix this.
1185 void InstantController::InstantPageRenderViewCreated( 1179 void InstantController::InstantPageRenderViewCreated(
1186 const content::WebContents* contents) { 1180 const content::WebContents* contents) {
1187 if (!extended_enabled()) 1181 if (!extended_enabled())
1188 return; 1182 return;
1189 1183
1190 // Update theme info so that the page picks it up. 1184 // Update theme info so that the page picks it up.
1191 browser_->UpdateThemeInfo(); 1185 browser_->UpdateThemeInfo();
1192 1186
1193 // Ensure the searchbox API has the correct initial state. 1187 // Ensure the searchbox API has the correct initial state.
1194 if (IsContentsFrom(overlay(), contents)) { 1188 if (IsContentsFrom(overlay(), contents)) {
1195 overlay_->SetDisplayInstantResults(instant_enabled_); 1189 overlay_->SetDisplayInstantResults(instant_enabled_);
1196 overlay_->FocusChanged(omnibox_focus_state_, omnibox_focus_change_reason_); 1190 overlay_->FocusChanged(omnibox_focus_state_, omnibox_focus_change_reason_);
1197 overlay_->SetOmniboxBounds(omnibox_bounds_); 1191 overlay_->SetOmniboxBounds(omnibox_bounds_);
1198 overlay_->InitializeFonts(); 1192 overlay_->InitializeFonts();
1199 } else if (IsContentsFrom(ntp(), contents)) { 1193 } else if (IsContentsFrom(ntp(), contents)) {
1200 ntp_->SetDisplayInstantResults(instant_enabled_); 1194 ntp_->SetDisplayInstantResults(instant_enabled_);
1201 ntp_->SetOmniboxBounds(omnibox_bounds_); 1195 ntp_->SetOmniboxBounds(omnibox_bounds_);
1202 ntp_->InitializeFonts(); 1196 ntp_->InitializeFonts();
1203 } else { 1197 } else {
1204 NOTREACHED(); 1198 NOTREACHED();
1205 } 1199 }
1206 StartListeningToMostVisitedChanges(); 1200 UpdateMostVisitedItems();
1207 } 1201 }
1208 1202
1209 void InstantController::InstantSupportDetermined( 1203 void InstantController::InstantSupportDetermined(
1210 const content::WebContents* contents, 1204 const content::WebContents* contents,
1211 bool supports_instant) { 1205 bool supports_instant) {
1212 if (IsContentsFrom(instant_tab(), contents)) { 1206 if (IsContentsFrom(instant_tab(), contents)) {
1213 if (!supports_instant) 1207 if (!supports_instant)
1214 base::MessageLoop::current()->DeleteSoon(FROM_HERE, 1208 base::MessageLoop::current()->DeleteSoon(FROM_HERE,
1215 instant_tab_.release()); 1209 instant_tab_.release());
1216 1210
(...skipping 401 matching lines...) Expand 10 before | Expand all | Expand 10 after
1618 instant_tab_.reset(); 1612 instant_tab_.reset();
1619 } 1613 }
1620 } 1614 }
1621 1615
1622 void InstantController::UpdateInfoForInstantTab() { 1616 void InstantController::UpdateInfoForInstantTab() {
1623 if (instant_tab_) { 1617 if (instant_tab_) {
1624 browser_->UpdateThemeInfo(); 1618 browser_->UpdateThemeInfo();
1625 instant_tab_->SetDisplayInstantResults(instant_enabled_); 1619 instant_tab_->SetDisplayInstantResults(instant_enabled_);
1626 instant_tab_->SetOmniboxBounds(omnibox_bounds_); 1620 instant_tab_->SetOmniboxBounds(omnibox_bounds_);
1627 instant_tab_->InitializeFonts(); 1621 instant_tab_->InitializeFonts();
1628 StartListeningToMostVisitedChanges(); 1622 UpdateMostVisitedItems();
1629 instant_tab_->FocusChanged(omnibox_focus_state_, 1623 instant_tab_->FocusChanged(omnibox_focus_state_,
1630 omnibox_focus_change_reason_); 1624 omnibox_focus_change_reason_);
1631 } 1625 }
1632 } 1626 }
1633 1627
1634 void InstantController::HideOverlay() { 1628 void InstantController::HideOverlay() {
1635 HideInternal(); 1629 HideInternal();
1636 ReloadOverlayIfStale(); 1630 ReloadOverlayIfStale();
1637 } 1631 }
1638 1632
1639 void InstantController::HideInternal() { 1633 void InstantController::HideInternal() {
1640 LOG_INSTANT_DEBUG_EVENT(this, "Hide"); 1634 LOG_INSTANT_DEBUG_EVENT(this, "Hide");
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
1717 // There is no point in sanity-checking |intersection.y()| because the omnibox 1711 // There is no point in sanity-checking |intersection.y()| because the omnibox
1718 // can be placed anywhere vertically relative to the overlay (for example, in 1712 // can be placed anywhere vertically relative to the overlay (for example, in
1719 // Mac fullscreen mode, the omnibox is fully enclosed by the overlay bounds). 1713 // Mac fullscreen mode, the omnibox is fully enclosed by the overlay bounds).
1720 DCHECK_LE(0, intersection.x()); 1714 DCHECK_LE(0, intersection.x());
1721 DCHECK_LE(0, intersection.width()); 1715 DCHECK_LE(0, intersection.width());
1722 DCHECK_LE(0, intersection.height()); 1716 DCHECK_LE(0, intersection.height());
1723 1717
1724 overlay_->SetPopupBounds(intersection); 1718 overlay_->SetPopupBounds(intersection);
1725 } 1719 }
1726 1720
1727 void InstantController::StartListeningToMostVisitedChanges() {
1728 history::TopSites* top_sites = browser_->profile()->GetTopSites();
1729 if (top_sites) {
1730 if (!registrar_.IsRegistered(
1731 this, chrome::NOTIFICATION_TOP_SITES_CHANGED,
1732 content::Source<history::TopSites>(top_sites))) {
1733 // TopSites updates itself after a delay. This is especially noticable
1734 // when your profile is empty. Ask TopSites to update itself when we're
1735 // about to show the new tab page.
1736 top_sites->SyncWithHistory();
1737 1721
1738 RequestMostVisitedItems();
1739
1740 // Register for notification when TopSites changes.
1741 registrar_.Add(this, chrome::NOTIFICATION_TOP_SITES_CHANGED,
1742 content::Source<history::TopSites>(top_sites));
1743 } else {
1744 // We are already registered, so just get and send the most visited data.
1745 RequestMostVisitedItems();
1746 }
1747 }
1748 }
1749
1750 void InstantController::RequestMostVisitedItems() {
1751 history::TopSites* top_sites = browser_->profile()->GetTopSites();
1752 if (top_sites) {
1753 top_sites->GetMostVisitedURLs(
1754 base::Bind(&InstantController::OnMostVisitedItemsReceived,
1755 weak_ptr_factory_.GetWeakPtr()));
1756 }
1757 }
1758
1759 void InstantController::OnMostVisitedItemsReceived(
1760 const history::MostVisitedURLList& data) {
1761 InstantService* instant_service =
1762 InstantServiceFactory::GetForProfile(browser_->profile());
1763 if (!instant_service)
1764 return;
1765
1766 history::MostVisitedURLList reordered_data(data);
1767 history::TopSites::MaybeShuffle(&reordered_data);
1768
1769 std::vector<InstantMostVisitedItem> most_visited_items;
1770 for (size_t i = 0; i < reordered_data.size(); i++) {
1771 const history::MostVisitedURL& url = reordered_data[i];
1772 InstantMostVisitedItem item;
1773 item.url = url.url;
1774 item.title = url.title;
1775 most_visited_items.push_back(item);
1776 }
1777
1778 instant_service->AddMostVisitedItems(most_visited_items);
1779
1780 std::vector<InstantMostVisitedItemIDPair> items_with_ids;
1781 instant_service->GetCurrentMostVisitedItems(&items_with_ids);
1782 SendMostVisitedItems(items_with_ids);
1783 }
1784
1785 void InstantController::SendMostVisitedItems(
1786 const std::vector<InstantMostVisitedItemIDPair>& items) {
1787 if (overlay_)
1788 overlay_->SendMostVisitedItems(items);
1789 if (ntp_)
1790 ntp_->SendMostVisitedItems(items);
1791 if (instant_tab_)
1792 instant_tab_->SendMostVisitedItems(items);
1793 content::NotificationService::current()->Notify(
1794 chrome::NOTIFICATION_INSTANT_SENT_MOST_VISITED_ITEMS,
1795 content::Source<InstantController>(this),
1796 content::NotificationService::NoDetails());
1797 }
1798 1722
1799 bool InstantController::FixSuggestion(InstantSuggestion* suggestion) const { 1723 bool InstantController::FixSuggestion(InstantSuggestion* suggestion) const {
1800 // We only accept suggestions if the user has typed text. If the user is 1724 // We only accept suggestions if the user has typed text. If the user is
1801 // arrowing up/down (|last_user_text_| is empty), we reject suggestions. 1725 // arrowing up/down (|last_user_text_| is empty), we reject suggestions.
1802 if (last_user_text_.empty()) 1726 if (last_user_text_.empty())
1803 return false; 1727 return false;
1804 1728
1805 // If the page is trying to set inline autocompletion in verbatim mode, 1729 // If the page is trying to set inline autocompletion in verbatim mode,
1806 // instead try suggesting the exact omnibox text. This makes the omnibox 1730 // instead try suggesting the exact omnibox text. This makes the omnibox
1807 // interpret user text as an URL if possible while preventing unwanted 1731 // interpret user text as an URL if possible while preventing unwanted
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
1889 result->transition = match.transition; 1813 result->transition = match.transition;
1890 result->relevance = match.relevance; 1814 result->relevance = match.relevance;
1891 result->autocomplete_match_index = autocomplete_match_index; 1815 result->autocomplete_match_index = autocomplete_match_index;
1892 1816
1893 DVLOG(1) << " " << result->relevance << " " 1817 DVLOG(1) << " " << result->relevance << " "
1894 << UTF8ToUTF16(AutocompleteMatchType::ToString(result->type)) << " " 1818 << UTF8ToUTF16(AutocompleteMatchType::ToString(result->type)) << " "
1895 << result->provider << " " << result->destination_url << " '" 1819 << result->provider << " " << result->destination_url << " '"
1896 << result->description << "' '" << result->search_query << "' " 1820 << result->description << "' '" << result->search_query << "' "
1897 << result->transition << " " << result->autocomplete_match_index; 1821 << result->transition << " " << result->autocomplete_match_index;
1898 } 1822 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/search/instant_controller.h ('k') | chrome/browser/ui/search/instant_page.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698