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

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

Issue 2532103002: Add support for components/ntp_tiles in InstantService (Closed)
Patch Set: . Created 4 years 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 <stddef.h> 7 #include <stddef.h>
8 8
9 #include "base/strings/string_util.h" 9 #include "base/bind.h"
10 #include "base/feature_list.h"
10 #include "build/build_config.h" 11 #include "build/build_config.h"
11 #include "chrome/browser/chrome_notification_types.h" 12 #include "chrome/browser/chrome_notification_types.h"
12 #include "chrome/browser/favicon/fallback_icon_service_factory.h" 13 #include "chrome/browser/favicon/fallback_icon_service_factory.h"
14 #include "chrome/browser/favicon/favicon_service_factory.h"
13 #include "chrome/browser/favicon/large_icon_service_factory.h" 15 #include "chrome/browser/favicon/large_icon_service_factory.h"
14 #include "chrome/browser/history/top_sites_factory.h" 16 #include "chrome/browser/history/top_sites_factory.h"
15 #include "chrome/browser/profiles/profile.h" 17 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/search/instant_io_context.h" 18 #include "chrome/browser/search/instant_io_context.h"
17 #include "chrome/browser/search/instant_service_observer.h" 19 #include "chrome/browser/search/instant_service_observer.h"
18 #include "chrome/browser/search/most_visited_iframe_source.h" 20 #include "chrome/browser/search/most_visited_iframe_source.h"
19 #include "chrome/browser/search/search.h" 21 #include "chrome/browser/search/search.h"
22 #include "chrome/browser/search/suggestions/image_decoder_impl.h"
23 #include "chrome/browser/search/suggestions/suggestions_service_factory.h"
20 #include "chrome/browser/search/thumbnail_source.h" 24 #include "chrome/browser/search/thumbnail_source.h"
21 #include "chrome/browser/search_engines/template_url_service_factory.h" 25 #include "chrome/browser/search_engines/template_url_service_factory.h"
22 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h" 26 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h"
23 #include "chrome/browser/thumbnails/thumbnail_list_source.h" 27 #include "chrome/browser/thumbnails/thumbnail_list_source.h"
24 #include "chrome/browser/ui/search/instant_search_prerenderer.h" 28 #include "chrome/browser/ui/search/instant_search_prerenderer.h"
25 #include "chrome/browser/ui/webui/fallback_icon_source.h" 29 #include "chrome/browser/ui/webui/fallback_icon_source.h"
26 #include "chrome/browser/ui/webui/favicon_source.h" 30 #include "chrome/browser/ui/webui/favicon_source.h"
27 #include "chrome/browser/ui/webui/large_icon_source.h" 31 #include "chrome/browser/ui/webui/large_icon_source.h"
28 #include "chrome/browser/ui/webui/theme_source.h" 32 #include "chrome/browser/ui/webui/theme_source.h"
29 #include "chrome/common/render_messages.h" 33 #include "chrome/common/render_messages.h"
30 #include "chrome/grit/theme_resources.h" 34 #include "chrome/grit/theme_resources.h"
31 #include "components/favicon/core/fallback_icon_service.h" 35 #include "components/favicon/core/fallback_icon_service.h"
32 #include "components/favicon/core/large_icon_service.h" 36 #include "components/favicon/core/large_icon_service.h"
33 #include "components/history/core/browser/top_sites.h" 37 #include "components/history/core/browser/top_sites.h"
38 #include "components/image_fetcher/image_fetcher_impl.h"
34 #include "components/keyed_service/core/service_access_type.h" 39 #include "components/keyed_service/core/service_access_type.h"
40 #include "components/ntp_tiles/icon_cacher.h"
35 #include "components/search/search.h" 41 #include "components/search/search.h"
36 #include "components/search_engines/template_url_service.h" 42 #include "components/search_engines/template_url_service.h"
37 #include "content/public/browser/browser_thread.h" 43 #include "content/public/browser/browser_thread.h"
38 #include "content/public/browser/child_process_security_policy.h"
39 #include "content/public/browser/notification_service.h" 44 #include "content/public/browser/notification_service.h"
40 #include "content/public/browser/notification_types.h" 45 #include "content/public/browser/notification_types.h"
41 #include "content/public/browser/render_process_host.h" 46 #include "content/public/browser/render_process_host.h"
42 #include "content/public/browser/url_data_source.h" 47 #include "content/public/browser/url_data_source.h"
43 #include "content/public/common/url_constants.h"
44 #include "extensions/common/constants.h"
45 #include "third_party/skia/include/core/SkColor.h" 48 #include "third_party/skia/include/core/SkColor.h"
46 #include "ui/gfx/color_utils.h" 49 #include "ui/gfx/color_utils.h"
47 #include "ui/gfx/image/image_skia.h" 50 #include "ui/gfx/image/image_skia.h"
48 #include "url/url_constants.h"
49 51
50 #if !defined(OS_ANDROID) 52 #if !defined(OS_ANDROID)
51 #include "chrome/browser/search/local_ntp_source.h" 53 #include "chrome/browser/search/local_ntp_source.h"
52 #endif 54 #endif
53 55
54 #if defined(ENABLE_THEMES) 56 #if defined(ENABLE_THEMES)
55 #include "chrome/browser/themes/theme_properties.h" 57 #include "chrome/browser/themes/theme_properties.h"
56 #include "chrome/browser/themes/theme_service.h" 58 #include "chrome/browser/themes/theme_service.h"
57 #include "chrome/browser/themes/theme_service_factory.h" 59 #include "chrome/browser/themes/theme_service_factory.h"
58 #endif // defined(ENABLE_THEMES) 60 #endif // defined(ENABLE_THEMES)
59 61
62 namespace {
63
64 const base::Feature kNtpTilesFeature{"NTPTilesInInstantService",
65 base::FEATURE_DISABLED_BY_DEFAULT};
66
67 } // namespace
68
60 InstantService::InstantService(Profile* profile) 69 InstantService::InstantService(Profile* profile)
61 : profile_(profile), 70 : profile_(profile),
62 template_url_service_(TemplateURLServiceFactory::GetForProfile(profile_)), 71 template_url_service_(TemplateURLServiceFactory::GetForProfile(profile_)),
63 weak_ptr_factory_(this) { 72 weak_ptr_factory_(this) {
64 // The initialization below depends on a typical set of browser threads. Skip 73 // The initialization below depends on a typical set of browser threads. Skip
65 // it if we are running in a unit test without the full suite. 74 // it if we are running in a unit test without the full suite.
66 if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) 75 if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI))
67 return; 76 return;
68 77
69 // This depends on the existence of the typical browser threads. Therefore it 78 // This depends on the existence of the typical browser threads. Therefore it
(...skipping 16 matching lines...) Expand all
86 95
87 ResetInstantSearchPrerendererIfNecessary(); 96 ResetInstantSearchPrerendererIfNecessary();
88 97
89 registrar_.Add(this, 98 registrar_.Add(this,
90 content::NOTIFICATION_RENDERER_PROCESS_CREATED, 99 content::NOTIFICATION_RENDERER_PROCESS_CREATED,
91 content::NotificationService::AllSources()); 100 content::NotificationService::AllSources());
92 registrar_.Add(this, 101 registrar_.Add(this,
93 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, 102 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
94 content::NotificationService::AllSources()); 103 content::NotificationService::AllSources());
95 104
96 scoped_refptr<history::TopSites> top_sites = 105 if (base::FeatureList::IsEnabled(kNtpTilesFeature)) {
97 TopSitesFactory::GetForProfile(profile_); 106 most_visited_sites_ = base::MakeUnique<ntp_tiles::MostVisitedSites>(
98 if (top_sites) { 107 profile_->GetPrefs(), TopSitesFactory::GetForProfile(profile_),
99 top_sites->AddObserver(this); 108 suggestions::SuggestionsServiceFactory::GetForProfile(profile_),
100 // Immediately query the TopSites state. 109 /*popular_sites=*/nullptr,
101 TopSitesChanged(top_sites.get(), 110 base::MakeUnique<ntp_tiles::IconCacher>(
102 history::TopSitesObserver::ChangeReason::MOST_VISITED); 111 FaviconServiceFactory::GetForProfile(
112 profile, ServiceAccessType::IMPLICIT_ACCESS),
113 base::MakeUnique<image_fetcher::ImageFetcherImpl>(
114 base::MakeUnique<suggestions::ImageDecoderImpl>(),
115 profile->GetRequestContext())),
116 /*supervisor=*/nullptr);
117 // TODO(treib): Add supervisor.
sfiera 2016/12/07 14:10:00 Another TODO: share this with Android in a factory
Marc Treib 2016/12/08 11:10:40 Done.
118
119 most_visited_sites_->SetMostVisitedURLsObserver(this, 8);
120 } else {
121 top_sites_ = TopSitesFactory::GetForProfile(profile_);
122 if (top_sites_) {
123 top_sites_->AddObserver(this);
124 // Immediately query the TopSites state.
125 TopSitesChanged(top_sites_.get(),
126 history::TopSitesObserver::ChangeReason::MOST_VISITED);
127 }
103 } 128 }
104 129
105 if (profile_ && profile_->GetResourceContext()) { 130 if (profile_ && profile_->GetResourceContext()) {
106 content::BrowserThread::PostTask( 131 content::BrowserThread::PostTask(
107 content::BrowserThread::IO, FROM_HERE, 132 content::BrowserThread::IO, FROM_HERE,
108 base::Bind(&InstantIOContext::SetUserDataOnIO, 133 base::Bind(&InstantIOContext::SetUserDataOnIO,
109 profile->GetResourceContext(), instant_io_context_)); 134 profile->GetResourceContext(), instant_io_context_));
110 } 135 }
111 136
112 // Set up the data sources that Instant uses on the NTP. 137 // Set up the data sources that Instant uses on the NTP.
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 188
164 void InstantService::AddObserver(InstantServiceObserver* observer) { 189 void InstantService::AddObserver(InstantServiceObserver* observer) {
165 observers_.AddObserver(observer); 190 observers_.AddObserver(observer);
166 } 191 }
167 192
168 void InstantService::RemoveObserver(InstantServiceObserver* observer) { 193 void InstantService::RemoveObserver(InstantServiceObserver* observer) {
169 observers_.RemoveObserver(observer); 194 observers_.RemoveObserver(observer);
170 } 195 }
171 196
172 void InstantService::OnNewTabPageOpened() { 197 void InstantService::OnNewTabPageOpened() {
173 scoped_refptr<history::TopSites> top_sites = 198 if (most_visited_sites_) {
174 TopSitesFactory::GetForProfile(profile_); 199 most_visited_sites_->Refresh();
175 if (top_sites) 200 } else if (top_sites_) {
176 top_sites->SyncWithHistory(); 201 top_sites_->SyncWithHistory();
202 }
177 } 203 }
178 204
179 void InstantService::DeleteMostVisitedItem(const GURL& url) { 205 void InstantService::DeleteMostVisitedItem(const GURL& url) {
180 scoped_refptr<history::TopSites> top_sites = 206 if (most_visited_sites_) {
181 TopSitesFactory::GetForProfile(profile_); 207 most_visited_sites_->AddOrRemoveBlacklistedUrl(url, true);
182 if (top_sites) 208 } else if (top_sites_) {
183 top_sites->AddBlacklistedURL(url); 209 top_sites_->AddBlacklistedURL(url);
210 }
184 } 211 }
185 212
186 void InstantService::UndoMostVisitedDeletion(const GURL& url) { 213 void InstantService::UndoMostVisitedDeletion(const GURL& url) {
187 scoped_refptr<history::TopSites> top_sites = 214 if (most_visited_sites_) {
188 TopSitesFactory::GetForProfile(profile_); 215 most_visited_sites_->AddOrRemoveBlacklistedUrl(url, false);
189 if (top_sites) 216 } else if (top_sites_) {
190 top_sites->RemoveBlacklistedURL(url); 217 top_sites_->RemoveBlacklistedURL(url);
218 }
191 } 219 }
192 220
193 void InstantService::UndoAllMostVisitedDeletions() { 221 void InstantService::UndoAllMostVisitedDeletions() {
194 scoped_refptr<history::TopSites> top_sites = 222 if (most_visited_sites_) {
195 TopSitesFactory::GetForProfile(profile_); 223 most_visited_sites_->ClearBlacklistedUrls();
196 if (top_sites) 224 } else if (top_sites_) {
197 top_sites->ClearBlacklistedURLs(); 225 top_sites_->ClearBlacklistedURLs();
226 }
198 } 227 }
199 228
200 void InstantService::UpdateThemeInfo() { 229 void InstantService::UpdateThemeInfo() {
201 #if defined(ENABLE_THEMES) 230 #if defined(ENABLE_THEMES)
202 // Update theme background info. 231 // Update theme background info.
203 // Initialize |theme_info| if necessary. 232 // Initialize |theme_info| if necessary.
204 if (!theme_info_) { 233 if (!theme_info_) {
205 OnThemeChanged(); 234 OnThemeChanged();
206 } else { 235 } else {
207 for (InstantServiceObserver& observer : observers_) 236 for (InstantServiceObserver& observer : observers_)
(...skipping 21 matching lines...) Expand all
229 void InstantService::Shutdown() { 258 void InstantService::Shutdown() {
230 process_ids_.clear(); 259 process_ids_.clear();
231 260
232 if (instant_io_context_.get()) { 261 if (instant_io_context_.get()) {
233 content::BrowserThread::PostTask( 262 content::BrowserThread::PostTask(
234 content::BrowserThread::IO, FROM_HERE, 263 content::BrowserThread::IO, FROM_HERE,
235 base::Bind(&InstantIOContext::ClearInstantProcessesOnIO, 264 base::Bind(&InstantIOContext::ClearInstantProcessesOnIO,
236 instant_io_context_)); 265 instant_io_context_));
237 } 266 }
238 267
239 scoped_refptr<history::TopSites> top_sites = 268 if (most_visited_sites_) {
240 TopSitesFactory::GetForProfile(profile_); 269 most_visited_sites_.reset();
241 if (top_sites) 270 } else if (top_sites_) {
242 top_sites->RemoveObserver(this); 271 top_sites_->RemoveObserver(this);
272 top_sites_ = nullptr;
273 }
243 274
244 instant_io_context_ = NULL; 275 instant_io_context_ = NULL;
245 } 276 }
246 277
247 void InstantService::Observe(int type, 278 void InstantService::Observe(int type,
248 const content::NotificationSource& source, 279 const content::NotificationSource& source,
249 const content::NotificationDetails& details) { 280 const content::NotificationDetails& details) {
250 switch (type) { 281 switch (type) {
251 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: 282 case content::NOTIFICATION_RENDERER_PROCESS_CREATED:
252 SendSearchURLsToRenderer( 283 SendSearchURLsToRenderer(
(...skipping 17 matching lines...) Expand all
270 process_ids_.erase(process_id); 301 process_ids_.erase(process_id);
271 302
272 if (instant_io_context_.get()) { 303 if (instant_io_context_.get()) {
273 content::BrowserThread::PostTask( 304 content::BrowserThread::PostTask(
274 content::BrowserThread::IO, FROM_HERE, 305 content::BrowserThread::IO, FROM_HERE,
275 base::Bind(&InstantIOContext::RemoveInstantProcessOnIO, 306 base::Bind(&InstantIOContext::RemoveInstantProcessOnIO,
276 instant_io_context_, process_id)); 307 instant_io_context_, process_id));
277 } 308 }
278 } 309 }
279 310
280 void InstantService::OnMostVisitedItemsReceived( 311 void InstantService::OnTopSitesReceived(
281 const history::MostVisitedURLList& data) { 312 const history::MostVisitedURLList& data) {
282 most_visited_items_.clear(); 313 most_visited_items_.clear();
283 for (const history::MostVisitedURL& mv_url : data) { 314 for (const history::MostVisitedURL& mv_url : data) {
284 InstantMostVisitedItem item; 315 InstantMostVisitedItem item;
285 item.url = mv_url.url; 316 item.url = mv_url.url;
286 item.title = mv_url.title; 317 item.title = mv_url.title;
287 item.is_server_side_suggestion = false; 318 item.source = ntp_tiles::NTPTileSource::TOP_SITES;
288 most_visited_items_.push_back(item); 319 most_visited_items_.push_back(item);
289 } 320 }
290 321
291 NotifyAboutMostVisitedItems(); 322 NotifyAboutMostVisitedItems();
292 } 323 }
293 324
325 void InstantService::OnMostVisitedURLsAvailable(
326 const ntp_tiles::NTPTilesVector& tiles) {
327 DCHECK(most_visited_sites_);
328 most_visited_items_.clear();
329 for (const ntp_tiles::NTPTile& tile : tiles) {
330 InstantMostVisitedItem item;
331 item.url = tile.url;
332 item.title = tile.title;
333 item.thumbnail = tile.thumbnail_url;
334 item.favicon = tile.favicon_url;
335 item.source = tile.source;
336 most_visited_items_.push_back(item);
337 }
338
339 NotifyAboutMostVisitedItems();
340 }
341
342 void InstantService::OnIconMadeAvailable(const GURL& site_url) {}
343
294 void InstantService::NotifyAboutMostVisitedItems() { 344 void InstantService::NotifyAboutMostVisitedItems() {
295 for (InstantServiceObserver& observer : observers_) 345 for (InstantServiceObserver& observer : observers_)
296 observer.MostVisitedItemsChanged(most_visited_items_); 346 observer.MostVisitedItemsChanged(most_visited_items_);
297 } 347 }
298 348
299 #if defined(ENABLE_THEMES) 349 #if defined(ENABLE_THEMES)
300 350
301 namespace { 351 namespace {
302 352
303 const int kSectionBorderAlphaTransparency = 80; 353 const int kSectionBorderAlphaTransparency = 80;
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
444 } 494 }
445 495
446 if (default_search_provider_changed || google_base_url_domain_changed) { 496 if (default_search_provider_changed || google_base_url_domain_changed) {
447 ResetInstantSearchPrerendererIfNecessary(); 497 ResetInstantSearchPrerendererIfNecessary();
448 for (InstantServiceObserver& observer : observers_) 498 for (InstantServiceObserver& observer : observers_)
449 observer.DefaultSearchProviderChanged(google_base_url_domain_changed); 499 observer.DefaultSearchProviderChanged(google_base_url_domain_changed);
450 } 500 }
451 } 501 }
452 502
453 void InstantService::TopSitesLoaded(history::TopSites* top_sites) { 503 void InstantService::TopSitesLoaded(history::TopSites* top_sites) {
504 DCHECK(!most_visited_sites_);
505 DCHECK_EQ(top_sites_.get(), top_sites);
454 } 506 }
455 507
456 void InstantService::TopSitesChanged(history::TopSites* top_sites, 508 void InstantService::TopSitesChanged(history::TopSites* top_sites,
457 ChangeReason change_reason) { 509 ChangeReason change_reason) {
510 DCHECK(!most_visited_sites_);
511 DCHECK_EQ(top_sites_.get(), top_sites);
458 // As forced urls already come from tiles, we can safely ignore those updates. 512 // As forced urls already come from tiles, we can safely ignore those updates.
459 if (change_reason == history::TopSitesObserver::ChangeReason::FORCED_URL) 513 if (change_reason == history::TopSitesObserver::ChangeReason::FORCED_URL)
460 return; 514 return;
461 top_sites->GetMostVisitedURLs( 515 top_sites_->GetMostVisitedURLs(base::Bind(&InstantService::OnTopSitesReceived,
462 base::Bind(&InstantService::OnMostVisitedItemsReceived, 516 weak_ptr_factory_.GetWeakPtr()),
463 weak_ptr_factory_.GetWeakPtr()), 517 false);
464 false);
465 } 518 }
466 519
467 void InstantService::ResetInstantSearchPrerendererIfNecessary() { 520 void InstantService::ResetInstantSearchPrerendererIfNecessary() {
468 if (!search::ShouldPrefetchSearchResults()) 521 if (!search::ShouldPrefetchSearchResults())
469 return; 522 return;
470 523
471 GURL url(search::GetSearchResultPrefetchBaseURL(profile_)); 524 GURL url(search::GetSearchResultPrefetchBaseURL(profile_));
472 if (!instant_prerenderer_ || instant_prerenderer_->prerender_url() != url) { 525 if (!instant_prerenderer_ || instant_prerenderer_->prerender_url() != url) {
473 instant_prerenderer_.reset( 526 instant_prerenderer_.reset(
474 url.is_valid() ? new InstantSearchPrerenderer(profile_, url) : nullptr); 527 url.is_valid() ? new InstantSearchPrerenderer(profile_, url) : nullptr);
475 } 528 }
476 } 529 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698