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

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: '' Created 7 years, 7 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 174 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 820 matching lines...) Expand 10 before | Expand all | Expand 10 after
1087 base::Time::Now().ToInternalValue(), info)); 1069 base::Time::Now().ToInternalValue(), info));
1088 static const size_t kMaxDebugEventSize = 2000; 1070 static const size_t kMaxDebugEventSize = 2000;
1089 if (debug_events_.size() > kMaxDebugEventSize) 1071 if (debug_events_.size() > kMaxDebugEventSize)
1090 debug_events_.pop_back(); 1072 debug_events_.pop_back();
1091 } 1073 }
1092 1074
1093 void InstantController::ClearDebugEvents() { 1075 void InstantController::ClearDebugEvents() {
1094 debug_events_.clear(); 1076 debug_events_.clear();
1095 } 1077 }
1096 1078
1097 void InstantController::DeleteMostVisitedItem( 1079 void InstantController::DeleteMostVisitedItem(const GURL& item_url) {
1098 InstantRestrictedID most_visited_item_id) { 1080 DCHECK(!item_url.is_empty());
1099 history::TopSites* top_sites = browser_->profile()->GetTopSites(); 1081 InstantService* instant_service =
1100 if (!top_sites) 1082 InstantServiceFactory::GetForProfile(profile());
1083 if (!instant_service)
1101 return; 1084 return;
1102 1085
1103 GURL url; 1086 instant_service->DeleteMostVisitedItem(item_url);
1104 if (GetURLForMostVisitedItemID(browser_->profile(),
1105 most_visited_item_id, &url))
1106 top_sites->AddBlacklistedURL(url);
1107 } 1087 }
1108 1088
1109 void InstantController::UndoMostVisitedDeletion( 1089 void InstantController::UndoMostVisitedDeletion(
1110 InstantRestrictedID most_visited_item_id) { 1090 const GURL& most_visited_item_url) {
1111 history::TopSites* top_sites = browser_->profile()->GetTopSites(); 1091 DCHECK(!most_visited_item_url.is_empty());
1112 if (!top_sites) 1092 InstantService* instant_service =
1093 InstantServiceFactory::GetForProfile(profile());
1094 if (!instant_service)
1113 return; 1095 return;
1114 1096
1115 GURL url; 1097 instant_service->UndoMostVisitedDeletion(most_visited_item_url);
1116 if (GetURLForMostVisitedItemID(browser_->profile(),
1117 most_visited_item_id, &url))
1118 top_sites->RemoveBlacklistedURL(url);
1119 } 1098 }
1120 1099
1121 void InstantController::UndoAllMostVisitedDeletions() { 1100 void InstantController::UndoAllMostVisitedDeletions() {
1122 history::TopSites* top_sites = browser_->profile()->GetTopSites(); 1101 InstantService* instant_service =
1123 if (!top_sites) 1102 InstantServiceFactory::GetForProfile(profile());
1103 if (!instant_service)
1124 return; 1104 return;
1125 1105
1126 top_sites->ClearBlacklistedURLs(); 1106 instant_service->UndoAllMostVisitedDeletions();
1127 } 1107 }
1128 1108
1129 Profile* InstantController::profile() const { 1109 Profile* InstantController::profile() const {
1130 return browser_->profile(); 1110 return browser_->profile();
1131 } 1111 }
1132 1112
1133 InstantOverlay* InstantController::overlay() const { 1113 InstantOverlay* InstantController::overlay() const {
1134 return overlay_.get(); 1114 return overlay_.get();
1135 } 1115 }
1136 1116
1137 InstantTab* InstantController::instant_tab() const { 1117 InstantTab* InstantController::instant_tab() const {
1138 return instant_tab_.get(); 1118 return instant_tab_.get();
1139 } 1119 }
1140 1120
1141 InstantNTP* InstantController::ntp() const { 1121 InstantNTP* InstantController::ntp() const {
1142 return ntp_.get(); 1122 return ntp_.get();
1143 } 1123 }
1144 1124
1145 void InstantController::Observe(int type,
1146 const content::NotificationSource& source,
1147 const content::NotificationDetails& details) {
1148 DCHECK_EQ(type, chrome::NOTIFICATION_TOP_SITES_CHANGED);
1149 RequestMostVisitedItems();
1150 }
1151
1152 // TODO(shishir): We assume that the WebContent's current RenderViewHost is the 1125 // TODO(shishir): We assume that the WebContent's current RenderViewHost is the
1153 // RenderViewHost being created which is not always true. Fix this. 1126 // RenderViewHost being created which is not always true. Fix this.
1154 void InstantController::InstantPageRenderViewCreated( 1127 void InstantController::InstantPageRenderViewCreated(
1155 const content::WebContents* contents) { 1128 const content::WebContents* contents) {
1156 if (!extended_enabled_) 1129 if (!extended_enabled_)
1157 return; 1130 return;
1158 1131
1159 // Update theme info so that the page picks it up. 1132 // Update theme info so that the page picks it up.
1160 browser_->UpdateThemeInfo(); 1133 browser_->UpdateThemeInfo();
1161 1134
1162 // Ensure the searchbox API has the correct initial state. 1135 // Ensure the searchbox API has the correct initial state.
1163 if (IsContentsFrom(overlay(), contents)) { 1136 if (IsContentsFrom(overlay(), contents)) {
1164 overlay_->SetDisplayInstantResults(instant_enabled_); 1137 overlay_->SetDisplayInstantResults(instant_enabled_);
1165 overlay_->KeyCaptureChanged( 1138 overlay_->KeyCaptureChanged(
1166 omnibox_focus_state_ == OMNIBOX_FOCUS_INVISIBLE); 1139 omnibox_focus_state_ == OMNIBOX_FOCUS_INVISIBLE);
1167 overlay_->SetOmniboxBounds(omnibox_bounds_); 1140 overlay_->SetOmniboxBounds(omnibox_bounds_);
1168 overlay_->InitializeFonts(); 1141 overlay_->InitializeFonts();
1169 } else if (IsContentsFrom(ntp(), contents)) { 1142 } else if (IsContentsFrom(ntp(), contents)) {
1170 ntp_->SetDisplayInstantResults(instant_enabled_); 1143 ntp_->SetDisplayInstantResults(instant_enabled_);
1171 ntp_->SetOmniboxBounds(omnibox_bounds_); 1144 ntp_->SetOmniboxBounds(omnibox_bounds_);
1172 ntp_->InitializeFonts(); 1145 ntp_->InitializeFonts();
1173 } else { 1146 } else {
1174 NOTREACHED(); 1147 NOTREACHED();
1175 } 1148 }
1176 StartListeningToMostVisitedChanges(); 1149 UpdateMostVisitedItemsInfo();
1177 } 1150 }
1178 1151
1179 void InstantController::InstantSupportDetermined( 1152 void InstantController::InstantSupportDetermined(
1180 const content::WebContents* contents, 1153 const content::WebContents* contents,
1181 bool supports_instant) { 1154 bool supports_instant) {
1182 if (IsContentsFrom(instant_tab(), contents)) { 1155 if (IsContentsFrom(instant_tab(), contents)) {
1183 if (!supports_instant) 1156 if (!supports_instant)
1184 base::MessageLoop::current()->DeleteSoon(FROM_HERE, 1157 base::MessageLoop::current()->DeleteSoon(FROM_HERE,
1185 instant_tab_.release()); 1158 instant_tab_.release());
1186 1159
(...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after
1535 instant_tab_.reset(); 1508 instant_tab_.reset();
1536 } 1509 }
1537 } 1510 }
1538 1511
1539 void InstantController::UpdateInfoForInstantTab() { 1512 void InstantController::UpdateInfoForInstantTab() {
1540 if (instant_tab_) { 1513 if (instant_tab_) {
1541 browser_->UpdateThemeInfo(); 1514 browser_->UpdateThemeInfo();
1542 instant_tab_->SetDisplayInstantResults(instant_enabled_); 1515 instant_tab_->SetDisplayInstantResults(instant_enabled_);
1543 instant_tab_->SetOmniboxBounds(omnibox_bounds_); 1516 instant_tab_->SetOmniboxBounds(omnibox_bounds_);
1544 instant_tab_->InitializeFonts(); 1517 instant_tab_->InitializeFonts();
1545 StartListeningToMostVisitedChanges(); 1518 UpdateMostVisitedItemsInfo();
1546 instant_tab_->KeyCaptureChanged( 1519 instant_tab_->KeyCaptureChanged(
1547 omnibox_focus_state_ == OMNIBOX_FOCUS_INVISIBLE); 1520 omnibox_focus_state_ == OMNIBOX_FOCUS_INVISIBLE);
1548 } 1521 }
1549 } 1522 }
1550 1523
1551 void InstantController::HideOverlay() { 1524 void InstantController::HideOverlay() {
1552 HideInternal(); 1525 HideInternal();
1553 ReloadOverlayIfStale(); 1526 ReloadOverlayIfStale();
1554 } 1527 }
1555 1528
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
1640 // There is no point in sanity-checking |intersection.y()| because the omnibox 1613 // There is no point in sanity-checking |intersection.y()| because the omnibox
1641 // can be placed anywhere vertically relative to the overlay (for example, in 1614 // can be placed anywhere vertically relative to the overlay (for example, in
1642 // Mac fullscreen mode, the omnibox is fully enclosed by the overlay bounds). 1615 // Mac fullscreen mode, the omnibox is fully enclosed by the overlay bounds).
1643 DCHECK_LE(0, intersection.x()); 1616 DCHECK_LE(0, intersection.x());
1644 DCHECK_LE(0, intersection.width()); 1617 DCHECK_LE(0, intersection.width());
1645 DCHECK_LE(0, intersection.height()); 1618 DCHECK_LE(0, intersection.height());
1646 1619
1647 overlay_->SetPopupBounds(intersection); 1620 overlay_->SetPopupBounds(intersection);
1648 } 1621 }
1649 1622
1650 void InstantController::StartListeningToMostVisitedChanges() { 1623 void InstantController::UpdateMostVisitedItemsInfo() {
1651 history::TopSites* top_sites = browser_->profile()->GetTopSites();
1652 if (top_sites) {
1653 if (!registrar_.IsRegistered(
1654 this, chrome::NOTIFICATION_TOP_SITES_CHANGED,
1655 content::Source<history::TopSites>(top_sites))) {
1656 // TopSites updates itself after a delay. This is especially noticable
1657 // when your profile is empty. Ask TopSites to update itself when we're
1658 // about to show the new tab page.
1659 top_sites->SyncWithHistory();
1660
1661 RequestMostVisitedItems();
1662
1663 // Register for notification when TopSites changes.
1664 registrar_.Add(this, chrome::NOTIFICATION_TOP_SITES_CHANGED,
1665 content::Source<history::TopSites>(top_sites));
1666 } else {
1667 // We are already registered, so just get and send the most visited data.
1668 RequestMostVisitedItems();
1669 }
1670 }
1671 }
1672
1673 void InstantController::RequestMostVisitedItems() {
1674 history::TopSites* top_sites = browser_->profile()->GetTopSites();
1675 if (top_sites) {
1676 top_sites->GetMostVisitedURLs(
1677 base::Bind(&InstantController::OnMostVisitedItemsReceived,
1678 weak_ptr_factory_.GetWeakPtr()));
1679 }
1680 }
1681
1682 void InstantController::OnMostVisitedItemsReceived(
1683 const history::MostVisitedURLList& data) {
1684 InstantService* instant_service = 1624 InstantService* instant_service =
1685 InstantServiceFactory::GetForProfile(browser_->profile()); 1625 InstantServiceFactory::GetForProfile(profile());
1686 if (!instant_service) 1626 if (!instant_service)
1687 return; 1627 return;
1688 1628
1689 std::vector<InstantMostVisitedItem> most_visited_items;
1690 for (size_t i = 0; i < data.size(); i++) {
1691 const history::MostVisitedURL& url = data[i];
1692 InstantMostVisitedItem item;
1693 item.url = url.url;
1694 item.title = url.title;
1695 most_visited_items.push_back(item);
1696 }
1697
1698 instant_service->AddMostVisitedItems(most_visited_items);
1699
1700 std::vector<InstantMostVisitedItemIDPair> items_with_ids; 1629 std::vector<InstantMostVisitedItemIDPair> items_with_ids;
1701 instant_service->GetCurrentMostVisitedItems(&items_with_ids); 1630 instant_service->GetCurrentMostVisitedItems(&items_with_ids);
1702 SendMostVisitedItems(items_with_ids); 1631 SendMostVisitedItems(items_with_ids);
1703 } 1632 }
1704 1633
1705 void InstantController::SendMostVisitedItems( 1634 void InstantController::SendMostVisitedItems(
Jered 2013/05/21 20:18:21 Inline this in UpdateMostVisitedItems() and expose
kmadhusu 2013/05/23 18:26:22 Done.
1706 const std::vector<InstantMostVisitedItemIDPair>& items) { 1635 const std::vector<InstantMostVisitedItemIDPair>& items) {
1707 if (overlay_) 1636 if (overlay_)
1708 overlay_->SendMostVisitedItems(items); 1637 overlay_->SendMostVisitedItems(items);
1709 if (ntp_) 1638 if (ntp_)
1710 ntp_->SendMostVisitedItems(items); 1639 ntp_->SendMostVisitedItems(items);
1711 if (instant_tab_) 1640 if (instant_tab_)
1712 instant_tab_->SendMostVisitedItems(items); 1641 instant_tab_->SendMostVisitedItems(items);
1713 content::NotificationService::current()->Notify( 1642 content::NotificationService::current()->Notify(
1714 chrome::NOTIFICATION_INSTANT_SENT_MOST_VISITED_ITEMS, 1643 chrome::NOTIFICATION_INSTANT_SENT_MOST_VISITED_ITEMS,
1715 content::Source<InstantController>(this), 1644 content::Source<InstantController>(this),
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
1808 1737
1809 result->transition = match.transition; 1738 result->transition = match.transition;
1810 result->relevance = match.relevance; 1739 result->relevance = match.relevance;
1811 result->autocomplete_match_index = autocomplete_match_index; 1740 result->autocomplete_match_index = autocomplete_match_index;
1812 1741
1813 DVLOG(1) << " " << result->relevance << " " << result->type << " " 1742 DVLOG(1) << " " << result->relevance << " " << result->type << " "
1814 << result->provider << " " << result->destination_url << " '" 1743 << result->provider << " " << result->destination_url << " '"
1815 << result->description << "' '" << result->search_query << "' " 1744 << result->description << "' '" << result->search_query << "' "
1816 << result->transition << " " << result->autocomplete_match_index; 1745 << result->transition << " " << result->autocomplete_match_index;
1817 } 1746 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698