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

Side by Side Diff: chrome/browser/search/instant_service.cc

Issue 1260113002: Adds an experiment that enabled SuggestionsService suggestions (MostLikely) on LocalNTP (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 4 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
« no previous file with comments | « chrome/browser/search/instant_service.h ('k') | chrome/common/instant_types.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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/search/instant_service.h" 5 #include "chrome/browser/search/instant_service.h"
6 6
7 #include "base/metrics/field_trial.h"
8 #include "base/strings/string_util.h"
9 #include "base/strings/utf_string_conversions.h"
7 #include "chrome/browser/chrome_notification_types.h" 10 #include "chrome/browser/chrome_notification_types.h"
8 #include "chrome/browser/favicon/fallback_icon_service_factory.h" 11 #include "chrome/browser/favicon/fallback_icon_service_factory.h"
9 #include "chrome/browser/favicon/large_icon_service_factory.h" 12 #include "chrome/browser/favicon/large_icon_service_factory.h"
10 #include "chrome/browser/history/top_sites_factory.h" 13 #include "chrome/browser/history/top_sites_factory.h"
11 #include "chrome/browser/profiles/profile.h" 14 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/search/instant_io_context.h" 15 #include "chrome/browser/search/instant_io_context.h"
13 #include "chrome/browser/search/instant_service_observer.h" 16 #include "chrome/browser/search/instant_service_observer.h"
14 #include "chrome/browser/search/most_visited_iframe_source.h" 17 #include "chrome/browser/search/most_visited_iframe_source.h"
15 #include "chrome/browser/search/search.h" 18 #include "chrome/browser/search/search.h"
19 #include "chrome/browser/search/suggestions/suggestions_service_factory.h"
16 #include "chrome/browser/search/suggestions/suggestions_source.h" 20 #include "chrome/browser/search/suggestions/suggestions_source.h"
17 #include "chrome/browser/search/thumbnail_source.h" 21 #include "chrome/browser/search/thumbnail_source.h"
18 #include "chrome/browser/search_engines/template_url_service_factory.h" 22 #include "chrome/browser/search_engines/template_url_service_factory.h"
19 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h" 23 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h"
20 #include "chrome/browser/thumbnails/thumbnail_list_source.h" 24 #include "chrome/browser/thumbnails/thumbnail_list_source.h"
21 #include "chrome/browser/ui/search/instant_search_prerenderer.h" 25 #include "chrome/browser/ui/search/instant_search_prerenderer.h"
22 #include "chrome/browser/ui/webui/fallback_icon_source.h" 26 #include "chrome/browser/ui/webui/fallback_icon_source.h"
23 #include "chrome/browser/ui/webui/favicon_source.h" 27 #include "chrome/browser/ui/webui/favicon_source.h"
24 #include "chrome/browser/ui/webui/large_icon_source.h" 28 #include "chrome/browser/ui/webui/large_icon_source.h"
25 #include "chrome/browser/ui/webui/theme_source.h" 29 #include "chrome/browser/ui/webui/theme_source.h"
(...skipping 17 matching lines...) Expand all
43 #if !defined(OS_ANDROID) 47 #if !defined(OS_ANDROID)
44 #include "chrome/browser/search/local_ntp_source.h" 48 #include "chrome/browser/search/local_ntp_source.h"
45 #endif 49 #endif
46 50
47 #if defined(ENABLE_THEMES) 51 #if defined(ENABLE_THEMES)
48 #include "chrome/browser/themes/theme_properties.h" 52 #include "chrome/browser/themes/theme_properties.h"
49 #include "chrome/browser/themes/theme_service.h" 53 #include "chrome/browser/themes/theme_service.h"
50 #include "chrome/browser/themes/theme_service_factory.h" 54 #include "chrome/browser/themes/theme_service_factory.h"
51 #endif // defined(ENABLE_THEMES) 55 #endif // defined(ENABLE_THEMES)
52 56
57 namespace {
58
59 const char kLocalNTPSuggestionService[] = "LocalNTPSuggestionsService";
60 const char kLocalNTPSuggestionServiceEnabled[] = "Enabled";
61
62 bool IsLocalNTPSuggestionServiceEnabled() {
63 return base::StartsWith(
64 base::FieldTrialList::FindFullName(kLocalNTPSuggestionService),
65 kLocalNTPSuggestionServiceEnabled, base::CompareCase::INSENSITIVE_ASCII);
66 }
67
68 } // namespace
69
53 InstantService::InstantService(Profile* profile) 70 InstantService::InstantService(Profile* profile)
54 : profile_(profile), 71 : profile_(profile),
55 template_url_service_(TemplateURLServiceFactory::GetForProfile(profile_)), 72 template_url_service_(TemplateURLServiceFactory::GetForProfile(profile_)),
56 omnibox_start_margin_(search::kDisableStartMargin), 73 omnibox_start_margin_(search::kDisableStartMargin),
74 suggestions_service_(NULL),
57 weak_ptr_factory_(this) { 75 weak_ptr_factory_(this) {
58 // The initialization below depends on a typical set of browser threads. Skip 76 // The initialization below depends on a typical set of browser threads. Skip
59 // it if we are running in a unit test without the full suite. 77 // it if we are running in a unit test without the full suite.
60 if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) 78 if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI))
61 return; 79 return;
62 80
63 // This depends on the existence of the typical browser threads. Therefore it 81 // This depends on the existence of the typical browser threads. Therefore it
64 // is only instantiated here (after the check for a UI thread above). 82 // is only instantiated here (after the check for a UI thread above).
65 instant_io_context_ = new InstantIOContext(); 83 instant_io_context_ = new InstantIOContext();
66 84
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 LargeIconServiceFactory::GetForBrowserContext(profile_); 142 LargeIconServiceFactory::GetForBrowserContext(profile_);
125 content::URLDataSource::Add( 143 content::URLDataSource::Add(
126 profile_, new FallbackIconSource(fallback_icon_service)); 144 profile_, new FallbackIconSource(fallback_icon_service));
127 content::URLDataSource::Add( 145 content::URLDataSource::Add(
128 profile_, new FaviconSource(profile_, FaviconSource::FAVICON)); 146 profile_, new FaviconSource(profile_, FaviconSource::FAVICON));
129 content::URLDataSource::Add( 147 content::URLDataSource::Add(
130 profile_, new LargeIconSource(fallback_icon_service, large_icon_service)); 148 profile_, new LargeIconSource(fallback_icon_service, large_icon_service));
131 content::URLDataSource::Add(profile_, new MostVisitedIframeSource()); 149 content::URLDataSource::Add(profile_, new MostVisitedIframeSource());
132 content::URLDataSource::Add( 150 content::URLDataSource::Add(
133 profile_, new suggestions::SuggestionsSource(profile_)); 151 profile_, new suggestions::SuggestionsSource(profile_));
152
153 if (IsLocalNTPSuggestionServiceEnabled()) {
154 suggestions_service_ =
155 suggestions::SuggestionsServiceFactory::GetForProfile(profile_);
156 }
157
158 if (suggestions_service_) {
159 suggestions_service_->FetchSuggestionsData(
160 suggestions::INITIALIZED_ENABLED_HISTORY,
161 base::Bind(&InstantService::OnSuggestionsAvailable,
162 weak_ptr_factory_.GetWeakPtr()));
163 }
134 } 164 }
135 165
136 InstantService::~InstantService() { 166 InstantService::~InstantService() {
137 if (template_url_service_) 167 if (template_url_service_)
138 template_url_service_->RemoveObserver(this); 168 template_url_service_->RemoveObserver(this);
139 } 169 }
140 170
141 void InstantService::AddInstantProcess(int process_id) { 171 void InstantService::AddInstantProcess(int process_id) {
142 process_ids_.insert(process_id); 172 process_ids_.insert(process_id);
143 173
(...skipping 13 matching lines...) Expand all
157 observers_.AddObserver(observer); 187 observers_.AddObserver(observer);
158 } 188 }
159 189
160 void InstantService::RemoveObserver(InstantServiceObserver* observer) { 190 void InstantService::RemoveObserver(InstantServiceObserver* observer) {
161 observers_.RemoveObserver(observer); 191 observers_.RemoveObserver(observer);
162 } 192 }
163 193
164 void InstantService::DeleteMostVisitedItem(const GURL& url) { 194 void InstantService::DeleteMostVisitedItem(const GURL& url) {
165 scoped_refptr<history::TopSites> top_sites = 195 scoped_refptr<history::TopSites> top_sites =
166 TopSitesFactory::GetForProfile(profile_); 196 TopSitesFactory::GetForProfile(profile_);
167 if (!top_sites) 197 if (top_sites)
168 return; 198 top_sites->AddBlacklistedURL(url);
169 199
170 top_sites->AddBlacklistedURL(url); 200 if (suggestions_service_) {
201 suggestions_service_->BlacklistURL(
202 url, base::Bind(&InstantService::OnSuggestionsAvailable,
203 weak_ptr_factory_.GetWeakPtr()),
204 base::Closure());
205 }
171 } 206 }
172 207
173 void InstantService::UndoMostVisitedDeletion(const GURL& url) { 208 void InstantService::UndoMostVisitedDeletion(const GURL& url) {
174 scoped_refptr<history::TopSites> top_sites = 209 scoped_refptr<history::TopSites> top_sites =
175 TopSitesFactory::GetForProfile(profile_); 210 TopSitesFactory::GetForProfile(profile_);
176 if (!top_sites) 211 if (top_sites)
177 return; 212 top_sites->RemoveBlacklistedURL(url);
178 213
179 top_sites->RemoveBlacklistedURL(url); 214 if (suggestions_service_) {
215 suggestions_service_->UndoBlacklistURL(
216 url, base::Bind(&InstantService::OnSuggestionsAvailable,
217 weak_ptr_factory_.GetWeakPtr()),
218 base::Closure());
219 }
180 } 220 }
181 221
182 void InstantService::UndoAllMostVisitedDeletions() { 222 void InstantService::UndoAllMostVisitedDeletions() {
183 scoped_refptr<history::TopSites> top_sites = 223 scoped_refptr<history::TopSites> top_sites =
184 TopSitesFactory::GetForProfile(profile_); 224 TopSitesFactory::GetForProfile(profile_);
185 if (!top_sites) 225 if (top_sites)
186 return; 226 top_sites->ClearBlacklistedURLs();
187 227
188 top_sites->ClearBlacklistedURLs(); 228 if (suggestions_service_) {
229 suggestions_service_->ClearBlacklist(
230 base::Bind(&InstantService::OnSuggestionsAvailable,
231 weak_ptr_factory_.GetWeakPtr()));
232 }
189 } 233 }
190 234
191 void InstantService::UpdateThemeInfo() { 235 void InstantService::UpdateThemeInfo() {
192 #if defined(ENABLE_THEMES) 236 #if defined(ENABLE_THEMES)
193 // Update theme background info. 237 // Update theme background info.
194 // Initialize |theme_info| if necessary. 238 // Initialize |theme_info| if necessary.
195 if (!theme_info_) 239 if (!theme_info_)
196 OnThemeChanged(ThemeServiceFactory::GetForProfile(profile_)); 240 OnThemeChanged(ThemeServiceFactory::GetForProfile(profile_));
197 else 241 else
198 OnThemeChanged(NULL); 242 OnThemeChanged(NULL);
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
259 process_ids_.erase(process_id); 303 process_ids_.erase(process_id);
260 304
261 if (instant_io_context_.get()) { 305 if (instant_io_context_.get()) {
262 content::BrowserThread::PostTask( 306 content::BrowserThread::PostTask(
263 content::BrowserThread::IO, FROM_HERE, 307 content::BrowserThread::IO, FROM_HERE,
264 base::Bind(&InstantIOContext::RemoveInstantProcessOnIO, 308 base::Bind(&InstantIOContext::RemoveInstantProcessOnIO,
265 instant_io_context_, process_id)); 309 instant_io_context_, process_id));
266 } 310 }
267 } 311 }
268 312
313 void InstantService::OnSuggestionsAvailable(
314 const suggestions::SuggestionsProfile& profile) {
315 std::vector<InstantMostVisitedItem> new_suggestions_items;
316 for (int i = 0; i < profile.suggestions_size(); ++i) {
317 const suggestions::ChromeSuggestion& suggestion = profile.suggestions(i);
318
319 InstantMostVisitedItem item;
320 item.url = GURL(suggestion.url());
321 item.title = base::UTF8ToUTF16(suggestion.title());
322 if (suggestion.has_thumbnail()) {
323 item.thumbnail = GURL(suggestion.thumbnail());
324 }
325 if (suggestion.has_favicon_url()) {
326 item.favicon = GURL(suggestion.favicon_url());
327 }
328 new_suggestions_items.push_back(item);
329 }
330 suggestions_items_ = new_suggestions_items;
331 NotifyAboutMostVisitedItems();
332 }
333
269 void InstantService::OnMostVisitedItemsReceived( 334 void InstantService::OnMostVisitedItemsReceived(
270 const history::MostVisitedURLList& data) { 335 const history::MostVisitedURLList& data) {
271 history::MostVisitedURLList reordered_data(data); 336 history::MostVisitedURLList reordered_data(data);
272 std::vector<InstantMostVisitedItem> new_most_visited_items; 337 std::vector<InstantMostVisitedItem> new_most_visited_items;
273 for (size_t i = 0; i < reordered_data.size(); i++) { 338 for (size_t i = 0; i < reordered_data.size(); i++) {
274 const history::MostVisitedURL& url = reordered_data[i]; 339 const history::MostVisitedURL& url = reordered_data[i];
275 InstantMostVisitedItem item; 340 InstantMostVisitedItem item;
276 item.url = url.url; 341 item.url = url.url;
277 item.title = url.title; 342 item.title = url.title;
278 new_most_visited_items.push_back(item); 343 new_most_visited_items.push_back(item);
279 } 344 }
280 345
281 most_visited_items_ = new_most_visited_items; 346 most_visited_items_ = new_most_visited_items;
282 NotifyAboutMostVisitedItems(); 347 NotifyAboutMostVisitedItems();
283 } 348 }
284 349
285 void InstantService::NotifyAboutMostVisitedItems() { 350 void InstantService::NotifyAboutMostVisitedItems() {
286 FOR_EACH_OBSERVER(InstantServiceObserver, observers_, 351 if (suggestions_service_ && !suggestions_items_.empty()) {
287 MostVisitedItemsChanged(most_visited_items_)); 352 FOR_EACH_OBSERVER(InstantServiceObserver, observers_,
353 MostVisitedItemsChanged(suggestions_items_));
354 } else {
355 FOR_EACH_OBSERVER(InstantServiceObserver, observers_,
356 MostVisitedItemsChanged(most_visited_items_));
357 }
288 } 358 }
289 359
290 #if defined(ENABLE_THEMES) 360 #if defined(ENABLE_THEMES)
291 361
292 namespace { 362 namespace {
293 363
294 const int kSectionBorderAlphaTransparency = 80; 364 const int kSectionBorderAlphaTransparency = 80;
295 365
296 // Converts SkColor to RGBAColor 366 // Converts SkColor to RGBAColor
297 RGBAColor SkColorToRGBAColor(const SkColor& sKColor) { 367 RGBAColor SkColorToRGBAColor(const SkColor& sKColor) {
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
462 } 532 }
463 533
464 void InstantService::ResetInstantSearchPrerenderer() { 534 void InstantService::ResetInstantSearchPrerenderer() {
465 if (!search::ShouldPrefetchSearchResults()) 535 if (!search::ShouldPrefetchSearchResults())
466 return; 536 return;
467 537
468 GURL url(search::GetSearchResultPrefetchBaseURL(profile_)); 538 GURL url(search::GetSearchResultPrefetchBaseURL(profile_));
469 instant_prerenderer_.reset( 539 instant_prerenderer_.reset(
470 url.is_valid() ? new InstantSearchPrerenderer(profile_, url) : NULL); 540 url.is_valid() ? new InstantSearchPrerenderer(profile_, url) : NULL);
471 } 541 }
OLDNEW
« no previous file with comments | « chrome/browser/search/instant_service.h ('k') | chrome/common/instant_types.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698