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

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
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/utf_string_conversions.h"
7 #include "chrome/browser/chrome_notification_types.h" 9 #include "chrome/browser/chrome_notification_types.h"
8 #include "chrome/browser/favicon/fallback_icon_service_factory.h" 10 #include "chrome/browser/favicon/fallback_icon_service_factory.h"
9 #include "chrome/browser/favicon/large_icon_service_factory.h" 11 #include "chrome/browser/favicon/large_icon_service_factory.h"
10 #include "chrome/browser/history/top_sites_factory.h" 12 #include "chrome/browser/history/top_sites_factory.h"
11 #include "chrome/browser/profiles/profile.h" 13 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/search/instant_io_context.h" 14 #include "chrome/browser/search/instant_io_context.h"
13 #include "chrome/browser/search/instant_service_observer.h" 15 #include "chrome/browser/search/instant_service_observer.h"
14 #include "chrome/browser/search/most_visited_iframe_source.h" 16 #include "chrome/browser/search/most_visited_iframe_source.h"
15 #include "chrome/browser/search/search.h" 17 #include "chrome/browser/search/search.h"
18 #include "chrome/browser/search/suggestions/suggestions_service_factory.h"
16 #include "chrome/browser/search/suggestions/suggestions_source.h" 19 #include "chrome/browser/search/suggestions/suggestions_source.h"
17 #include "chrome/browser/search/thumbnail_source.h" 20 #include "chrome/browser/search/thumbnail_source.h"
18 #include "chrome/browser/search_engines/template_url_service_factory.h" 21 #include "chrome/browser/search_engines/template_url_service_factory.h"
19 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h" 22 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h"
20 #include "chrome/browser/thumbnails/thumbnail_list_source.h" 23 #include "chrome/browser/thumbnails/thumbnail_list_source.h"
21 #include "chrome/browser/ui/search/instant_search_prerenderer.h" 24 #include "chrome/browser/ui/search/instant_search_prerenderer.h"
22 #include "chrome/browser/ui/webui/fallback_icon_source.h" 25 #include "chrome/browser/ui/webui/fallback_icon_source.h"
23 #include "chrome/browser/ui/webui/favicon_source.h" 26 #include "chrome/browser/ui/webui/favicon_source.h"
24 #include "chrome/browser/ui/webui/large_icon_source.h" 27 #include "chrome/browser/ui/webui/large_icon_source.h"
25 #include "chrome/browser/ui/webui/theme_source.h" 28 #include "chrome/browser/ui/webui/theme_source.h"
26 #include "chrome/common/render_messages.h" 29 #include "chrome/common/render_messages.h"
27 #include "components/favicon/core/fallback_icon_service.h" 30 #include "components/favicon/core/fallback_icon_service.h"
28 #include "components/favicon/core/large_icon_service.h" 31 #include "components/favicon/core/large_icon_service.h"
29 #include "components/history/core/browser/top_sites.h" 32 #include "components/history/core/browser/top_sites.h"
30 #include "components/keyed_service/core/service_access_type.h" 33 #include "components/keyed_service/core/service_access_type.h"
31 #include "components/search_engines/template_url_service.h" 34 #include "components/search_engines/template_url_service.h"
35 #include "components/suggestions/proto/suggestions.pb.h"
36 #include "components/suggestions/suggestions_service.h"
32 #include "content/public/browser/browser_thread.h" 37 #include "content/public/browser/browser_thread.h"
33 #include "content/public/browser/notification_service.h" 38 #include "content/public/browser/notification_service.h"
34 #include "content/public/browser/notification_types.h" 39 #include "content/public/browser/notification_types.h"
35 #include "content/public/browser/render_process_host.h" 40 #include "content/public/browser/render_process_host.h"
36 #include "content/public/browser/url_data_source.h" 41 #include "content/public/browser/url_data_source.h"
37 #include "grit/theme_resources.h" 42 #include "grit/theme_resources.h"
38 #include "third_party/skia/include/core/SkColor.h" 43 #include "third_party/skia/include/core/SkColor.h"
39 #include "ui/gfx/color_utils.h" 44 #include "ui/gfx/color_utils.h"
40 #include "ui/gfx/image/image_skia.h" 45 #include "ui/gfx/image/image_skia.h"
41 46
42 #if !defined(OS_ANDROID) 47 #if !defined(OS_ANDROID)
43 #include "chrome/browser/search/local_ntp_source.h" 48 #include "chrome/browser/search/local_ntp_source.h"
44 #endif 49 #endif
45 50
46 #if defined(ENABLE_THEMES) 51 #if defined(ENABLE_THEMES)
47 #include "chrome/browser/themes/theme_properties.h" 52 #include "chrome/browser/themes/theme_properties.h"
48 #include "chrome/browser/themes/theme_service.h" 53 #include "chrome/browser/themes/theme_service.h"
49 #include "chrome/browser/themes/theme_service_factory.h" 54 #include "chrome/browser/themes/theme_service_factory.h"
50 #endif // defined(ENABLE_THEMES) 55 #endif // defined(ENABLE_THEMES)
51 56
52 InstantService::InstantService(Profile* profile) 57 InstantService::InstantService(Profile* profile)
53 : profile_(profile), 58 : profile_(profile),
54 template_url_service_(TemplateURLServiceFactory::GetForProfile(profile_)), 59 template_url_service_(TemplateURLServiceFactory::GetForProfile(profile_)),
55 omnibox_start_margin_(chrome::kDisableStartMargin), 60 omnibox_start_margin_(chrome::kDisableStartMargin),
61 suggestions_service_(NULL),
56 weak_ptr_factory_(this) { 62 weak_ptr_factory_(this) {
57 // The initialization below depends on a typical set of browser threads. Skip 63 // The initialization below depends on a typical set of browser threads. Skip
58 // it if we are running in a unit test without the full suite. 64 // it if we are running in a unit test without the full suite.
59 if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) 65 if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI))
60 return; 66 return;
61 67
62 // This depends on the existence of the typical browser threads. Therefore it 68 // This depends on the existence of the typical browser threads. Therefore it
63 // is only instantiated here (after the check for a UI thread above). 69 // is only instantiated here (after the check for a UI thread above).
64 instant_io_context_ = new InstantIOContext(); 70 instant_io_context_ = new InstantIOContext();
65 71
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 LargeIconServiceFactory::GetForBrowserContext(profile_); 129 LargeIconServiceFactory::GetForBrowserContext(profile_);
124 content::URLDataSource::Add( 130 content::URLDataSource::Add(
125 profile_, new FallbackIconSource(fallback_icon_service)); 131 profile_, new FallbackIconSource(fallback_icon_service));
126 content::URLDataSource::Add( 132 content::URLDataSource::Add(
127 profile_, new FaviconSource(profile_, FaviconSource::FAVICON)); 133 profile_, new FaviconSource(profile_, FaviconSource::FAVICON));
128 content::URLDataSource::Add( 134 content::URLDataSource::Add(
129 profile_, new LargeIconSource(fallback_icon_service, large_icon_service)); 135 profile_, new LargeIconSource(fallback_icon_service, large_icon_service));
130 content::URLDataSource::Add(profile_, new MostVisitedIframeSource()); 136 content::URLDataSource::Add(profile_, new MostVisitedIframeSource());
131 content::URLDataSource::Add( 137 content::URLDataSource::Add(
132 profile_, new suggestions::SuggestionsSource(profile_)); 138 profile_, new suggestions::SuggestionsSource(profile_));
139
140 if (base::StartsWith(
kmadhusu 2015/07/30 21:57:02 Define a helper function in the anonymous namespac
fserb 2015/07/31 17:08:17 Done.
141 base::FieldTrialList::FindFullName("LocalNTPSuggestionsService"),
142 "Enabled", base::CompareCase::INSENSITIVE_ASCII)) {
143 suggestions_service_ =
144 suggestions::SuggestionsServiceFactory::GetForProfile(profile_);
145 }
146
147 if (suggestions_service_) {
148 suggestions_service_->FetchSuggestionsData(
149 suggestions::INITIALIZED_ENABLED_HISTORY,
150 base::Bind(&InstantService::OnSuggestionsAvailable,
151 weak_ptr_factory_.GetWeakPtr()));
152 }
133 } 153 }
134 154
135 InstantService::~InstantService() { 155 InstantService::~InstantService() {
136 if (template_url_service_) 156 if (template_url_service_)
137 template_url_service_->RemoveObserver(this); 157 template_url_service_->RemoveObserver(this);
138 } 158 }
139 159
140 void InstantService::AddInstantProcess(int process_id) { 160 void InstantService::AddInstantProcess(int process_id) {
141 process_ids_.insert(process_id); 161 process_ids_.insert(process_id);
142 162
(...skipping 17 matching lines...) Expand all
160 observers_.RemoveObserver(observer); 180 observers_.RemoveObserver(observer);
161 } 181 }
162 182
163 void InstantService::DeleteMostVisitedItem(const GURL& url) { 183 void InstantService::DeleteMostVisitedItem(const GURL& url) {
164 scoped_refptr<history::TopSites> top_sites = 184 scoped_refptr<history::TopSites> top_sites =
165 TopSitesFactory::GetForProfile(profile_); 185 TopSitesFactory::GetForProfile(profile_);
166 if (!top_sites) 186 if (!top_sites)
167 return; 187 return;
168 188
169 top_sites->AddBlacklistedURL(url); 189 top_sites->AddBlacklistedURL(url);
190 if (suggestions_service_) {
191 suggestions_service_->BlacklistURL(
192 url, base::Bind(&InstantService::OnSuggestionsAvailable,
193 weak_ptr_factory_.GetWeakPtr()),
194 base::Closure());
195 }
170 } 196 }
171 197
172 void InstantService::UndoMostVisitedDeletion(const GURL& url) { 198 void InstantService::UndoMostVisitedDeletion(const GURL& url) {
173 scoped_refptr<history::TopSites> top_sites = 199 scoped_refptr<history::TopSites> top_sites =
174 TopSitesFactory::GetForProfile(profile_); 200 TopSitesFactory::GetForProfile(profile_);
175 if (!top_sites) 201 if (!top_sites)
176 return; 202 return;
177 203
178 top_sites->RemoveBlacklistedURL(url); 204 top_sites->RemoveBlacklistedURL(url);
205 if (suggestions_service_) {
206 suggestions_service_->UndoBlacklistURL(
207 url, base::Bind(&InstantService::OnSuggestionsAvailable,
208 weak_ptr_factory_.GetWeakPtr()),
209 base::Closure());
210 }
179 } 211 }
180 212
181 void InstantService::UndoAllMostVisitedDeletions() { 213 void InstantService::UndoAllMostVisitedDeletions() {
182 scoped_refptr<history::TopSites> top_sites = 214 scoped_refptr<history::TopSites> top_sites =
183 TopSitesFactory::GetForProfile(profile_); 215 TopSitesFactory::GetForProfile(profile_);
184 if (!top_sites) 216 if (!top_sites)
185 return; 217 return;
186 218
187 top_sites->ClearBlacklistedURLs(); 219 top_sites->ClearBlacklistedURLs();
220 if (suggestions_service_) {
221 suggestions_service_->ClearBlacklist(
222 base::Bind(&InstantService::OnSuggestionsAvailable,
223 weak_ptr_factory_.GetWeakPtr()));
224 }
188 } 225 }
189 226
190 void InstantService::UpdateThemeInfo() { 227 void InstantService::UpdateThemeInfo() {
191 #if defined(ENABLE_THEMES) 228 #if defined(ENABLE_THEMES)
192 // Update theme background info. 229 // Update theme background info.
193 // Initialize |theme_info| if necessary. 230 // Initialize |theme_info| if necessary.
194 if (!theme_info_) 231 if (!theme_info_)
195 OnThemeChanged(ThemeServiceFactory::GetForProfile(profile_)); 232 OnThemeChanged(ThemeServiceFactory::GetForProfile(profile_));
196 else 233 else
197 OnThemeChanged(NULL); 234 OnThemeChanged(NULL);
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
258 process_ids_.erase(process_id); 295 process_ids_.erase(process_id);
259 296
260 if (instant_io_context_.get()) { 297 if (instant_io_context_.get()) {
261 content::BrowserThread::PostTask( 298 content::BrowserThread::PostTask(
262 content::BrowserThread::IO, FROM_HERE, 299 content::BrowserThread::IO, FROM_HERE,
263 base::Bind(&InstantIOContext::RemoveInstantProcessOnIO, 300 base::Bind(&InstantIOContext::RemoveInstantProcessOnIO,
264 instant_io_context_, process_id)); 301 instant_io_context_, process_id));
265 } 302 }
266 } 303 }
267 304
305 void InstantService::OnSuggestionsAvailable(
306 const suggestions::SuggestionsProfile& profile) {
307 std::vector<InstantMostVisitedItem> new_suggestions_items;
308 for (int i = 0; i < profile.suggestions_size(); ++i) {
309 const suggestions::ChromeSuggestion& suggestion = profile.suggestions(i);
310
311 InstantMostVisitedItem item;
312 item.url = GURL(suggestion.url());
313 item.title = base::UTF8ToUTF16(suggestion.title());
314 item.thumbnail = GURL(suggestion.thumbnail());
315 item.favicon = GURL(suggestion.favicon_url());
316 new_suggestions_items.push_back(item);
317 }
318 suggestions_items_ = new_suggestions_items;
319 NotifyAboutMostVisitedItems();
320 }
321
268 void InstantService::OnMostVisitedItemsReceived( 322 void InstantService::OnMostVisitedItemsReceived(
269 const history::MostVisitedURLList& data) { 323 const history::MostVisitedURLList& data) {
270 history::MostVisitedURLList reordered_data(data); 324 history::MostVisitedURLList reordered_data(data);
271 std::vector<InstantMostVisitedItem> new_most_visited_items; 325 std::vector<InstantMostVisitedItem> new_most_visited_items;
272 for (size_t i = 0; i < reordered_data.size(); i++) { 326 for (size_t i = 0; i < reordered_data.size(); i++) {
273 const history::MostVisitedURL& url = reordered_data[i]; 327 const history::MostVisitedURL& url = reordered_data[i];
274 InstantMostVisitedItem item; 328 InstantMostVisitedItem item;
275 item.url = url.url; 329 item.url = url.url;
276 item.title = url.title; 330 item.title = url.title;
277 new_most_visited_items.push_back(item); 331 new_most_visited_items.push_back(item);
278 } 332 }
279 333
280 most_visited_items_ = new_most_visited_items; 334 most_visited_items_ = new_most_visited_items;
281 NotifyAboutMostVisitedItems(); 335 NotifyAboutMostVisitedItems();
282 } 336 }
283 337
284 void InstantService::NotifyAboutMostVisitedItems() { 338 void InstantService::NotifyAboutMostVisitedItems() {
285 FOR_EACH_OBSERVER(InstantServiceObserver, observers_, 339 if (suggestions_service_ && !suggestions_items_.empty()) {
286 MostVisitedItemsChanged(most_visited_items_)); 340 FOR_EACH_OBSERVER(InstantServiceObserver, observers_,
341 MostVisitedItemsChanged(suggestions_items_));
342 } else {
343 FOR_EACH_OBSERVER(InstantServiceObserver, observers_,
344 MostVisitedItemsChanged(most_visited_items_));
345 }
287 } 346 }
288 347
289 #if defined(ENABLE_THEMES) 348 #if defined(ENABLE_THEMES)
290 349
291 namespace { 350 namespace {
292 351
293 const int kSectionBorderAlphaTransparency = 80; 352 const int kSectionBorderAlphaTransparency = 80;
294 353
295 // Converts SkColor to RGBAColor 354 // Converts SkColor to RGBAColor
296 RGBAColor SkColorToRGBAColor(const SkColor& sKColor) { 355 RGBAColor SkColorToRGBAColor(const SkColor& sKColor) {
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
461 } 520 }
462 521
463 void InstantService::ResetInstantSearchPrerenderer() { 522 void InstantService::ResetInstantSearchPrerenderer() {
464 if (!chrome::ShouldPrefetchSearchResults()) 523 if (!chrome::ShouldPrefetchSearchResults())
465 return; 524 return;
466 525
467 GURL url(chrome::GetSearchResultPrefetchBaseURL(profile_)); 526 GURL url(chrome::GetSearchResultPrefetchBaseURL(profile_));
468 instant_prerenderer_.reset( 527 instant_prerenderer_.reset(
469 url.is_valid() ? new InstantSearchPrerenderer(profile_, url) : NULL); 528 url.is_valid() ? new InstantSearchPrerenderer(profile_, url) : NULL);
470 } 529 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698