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

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

Issue 2532103002: Add support for components/ntp_tiles in InstantService (Closed)
Patch Set: rebase 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.
118 // TODO(sfiera): Share this with Android in a factory.
119
120 most_visited_sites_->SetMostVisitedURLsObserver(this, 8);
121 } else {
122 top_sites_ = TopSitesFactory::GetForProfile(profile_);
123 if (top_sites_) {
124 top_sites_->AddObserver(this);
125 // Immediately query the TopSites state.
126 TopSitesChanged(top_sites_.get(),
127 history::TopSitesObserver::ChangeReason::MOST_VISITED);
128 }
103 } 129 }
104 130
105 if (profile_ && profile_->GetResourceContext()) { 131 if (profile_ && profile_->GetResourceContext()) {
106 content::BrowserThread::PostTask( 132 content::BrowserThread::PostTask(
107 content::BrowserThread::IO, FROM_HERE, 133 content::BrowserThread::IO, FROM_HERE,
108 base::Bind(&InstantIOContext::SetUserDataOnIO, 134 base::Bind(&InstantIOContext::SetUserDataOnIO,
109 profile->GetResourceContext(), instant_io_context_)); 135 profile->GetResourceContext(), instant_io_context_));
110 } 136 }
111 137
112 // Set up the data sources that Instant uses on the NTP. 138 // Set up the data sources that Instant uses on the NTP.
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 189
164 void InstantService::AddObserver(InstantServiceObserver* observer) { 190 void InstantService::AddObserver(InstantServiceObserver* observer) {
165 observers_.AddObserver(observer); 191 observers_.AddObserver(observer);
166 } 192 }
167 193
168 void InstantService::RemoveObserver(InstantServiceObserver* observer) { 194 void InstantService::RemoveObserver(InstantServiceObserver* observer) {
169 observers_.RemoveObserver(observer); 195 observers_.RemoveObserver(observer);
170 } 196 }
171 197
172 void InstantService::OnNewTabPageOpened() { 198 void InstantService::OnNewTabPageOpened() {
173 scoped_refptr<history::TopSites> top_sites = 199 if (most_visited_sites_) {
174 TopSitesFactory::GetForProfile(profile_); 200 most_visited_sites_->Refresh();
175 if (top_sites) 201 } else if (top_sites_) {
176 top_sites->SyncWithHistory(); 202 top_sites_->SyncWithHistory();
203 }
177 } 204 }
178 205
179 void InstantService::DeleteMostVisitedItem(const GURL& url) { 206 void InstantService::DeleteMostVisitedItem(const GURL& url) {
180 scoped_refptr<history::TopSites> top_sites = 207 if (most_visited_sites_) {
181 TopSitesFactory::GetForProfile(profile_); 208 most_visited_sites_->AddOrRemoveBlacklistedUrl(url, true);
182 if (top_sites) 209 } else if (top_sites_) {
183 top_sites->AddBlacklistedURL(url); 210 top_sites_->AddBlacklistedURL(url);
211 }
184 } 212 }
185 213
186 void InstantService::UndoMostVisitedDeletion(const GURL& url) { 214 void InstantService::UndoMostVisitedDeletion(const GURL& url) {
187 scoped_refptr<history::TopSites> top_sites = 215 if (most_visited_sites_) {
188 TopSitesFactory::GetForProfile(profile_); 216 most_visited_sites_->AddOrRemoveBlacklistedUrl(url, false);
189 if (top_sites) 217 } else if (top_sites_) {
190 top_sites->RemoveBlacklistedURL(url); 218 top_sites_->RemoveBlacklistedURL(url);
219 }
191 } 220 }
192 221
193 void InstantService::UndoAllMostVisitedDeletions() { 222 void InstantService::UndoAllMostVisitedDeletions() {
194 scoped_refptr<history::TopSites> top_sites = 223 if (most_visited_sites_) {
195 TopSitesFactory::GetForProfile(profile_); 224 most_visited_sites_->ClearBlacklistedUrls();
196 if (top_sites) 225 } else if (top_sites_) {
197 top_sites->ClearBlacklistedURLs(); 226 top_sites_->ClearBlacklistedURLs();
227 }
198 } 228 }
199 229
200 void InstantService::UpdateThemeInfo() { 230 void InstantService::UpdateThemeInfo() {
201 #if defined(ENABLE_THEMES) 231 #if defined(ENABLE_THEMES)
202 // Update theme background info. 232 // Update theme background info.
203 // Initialize |theme_info| if necessary. 233 // Initialize |theme_info| if necessary.
204 if (!theme_info_) { 234 if (!theme_info_) {
205 OnThemeChanged(); 235 OnThemeChanged();
206 } else { 236 } else {
207 for (InstantServiceObserver& observer : observers_) 237 for (InstantServiceObserver& observer : observers_)
(...skipping 21 matching lines...) Expand all
229 void InstantService::Shutdown() { 259 void InstantService::Shutdown() {
230 process_ids_.clear(); 260 process_ids_.clear();
231 261
232 if (instant_io_context_.get()) { 262 if (instant_io_context_.get()) {
233 content::BrowserThread::PostTask( 263 content::BrowserThread::PostTask(
234 content::BrowserThread::IO, FROM_HERE, 264 content::BrowserThread::IO, FROM_HERE,
235 base::Bind(&InstantIOContext::ClearInstantProcessesOnIO, 265 base::Bind(&InstantIOContext::ClearInstantProcessesOnIO,
236 instant_io_context_)); 266 instant_io_context_));
237 } 267 }
238 268
239 scoped_refptr<history::TopSites> top_sites = 269 if (most_visited_sites_) {
240 TopSitesFactory::GetForProfile(profile_); 270 most_visited_sites_.reset();
241 if (top_sites) 271 } else if (top_sites_) {
242 top_sites->RemoveObserver(this); 272 top_sites_->RemoveObserver(this);
273 top_sites_ = nullptr;
274 }
243 275
244 instant_io_context_ = NULL; 276 instant_io_context_ = NULL;
245 } 277 }
246 278
247 void InstantService::Observe(int type, 279 void InstantService::Observe(int type,
248 const content::NotificationSource& source, 280 const content::NotificationSource& source,
249 const content::NotificationDetails& details) { 281 const content::NotificationDetails& details) {
250 switch (type) { 282 switch (type) {
251 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: 283 case content::NOTIFICATION_RENDERER_PROCESS_CREATED:
252 SendSearchURLsToRenderer( 284 SendSearchURLsToRenderer(
(...skipping 17 matching lines...) Expand all
270 process_ids_.erase(process_id); 302 process_ids_.erase(process_id);
271 303
272 if (instant_io_context_.get()) { 304 if (instant_io_context_.get()) {
273 content::BrowserThread::PostTask( 305 content::BrowserThread::PostTask(
274 content::BrowserThread::IO, FROM_HERE, 306 content::BrowserThread::IO, FROM_HERE,
275 base::Bind(&InstantIOContext::RemoveInstantProcessOnIO, 307 base::Bind(&InstantIOContext::RemoveInstantProcessOnIO,
276 instant_io_context_, process_id)); 308 instant_io_context_, process_id));
277 } 309 }
278 } 310 }
279 311
280 void InstantService::OnMostVisitedItemsReceived( 312 void InstantService::OnTopSitesReceived(
281 const history::MostVisitedURLList& data) { 313 const history::MostVisitedURLList& data) {
282 most_visited_items_.clear(); 314 most_visited_items_.clear();
283 for (const history::MostVisitedURL& mv_url : data) { 315 for (const history::MostVisitedURL& mv_url : data) {
284 InstantMostVisitedItem item; 316 InstantMostVisitedItem item;
285 item.url = mv_url.url; 317 item.url = mv_url.url;
286 item.title = mv_url.title; 318 item.title = mv_url.title;
287 item.is_server_side_suggestion = false; 319 item.source = ntp_tiles::NTPTileSource::TOP_SITES;
288 most_visited_items_.push_back(item); 320 most_visited_items_.push_back(item);
289 } 321 }
290 322
291 NotifyAboutMostVisitedItems(); 323 NotifyAboutMostVisitedItems();
292 } 324 }
293 325
326 void InstantService::OnMostVisitedURLsAvailable(
327 const ntp_tiles::NTPTilesVector& tiles) {
328 DCHECK(most_visited_sites_);
329 most_visited_items_.clear();
330 for (const ntp_tiles::NTPTile& tile : tiles) {
331 InstantMostVisitedItem item;
332 item.url = tile.url;
333 item.title = tile.title;
334 item.thumbnail = tile.thumbnail_url;
335 item.favicon = tile.favicon_url;
336 item.source = tile.source;
337 most_visited_items_.push_back(item);
338 }
339
340 NotifyAboutMostVisitedItems();
341 }
342
343 void InstantService::OnIconMadeAvailable(const GURL& site_url) {}
344
294 void InstantService::NotifyAboutMostVisitedItems() { 345 void InstantService::NotifyAboutMostVisitedItems() {
295 for (InstantServiceObserver& observer : observers_) 346 for (InstantServiceObserver& observer : observers_)
296 observer.MostVisitedItemsChanged(most_visited_items_); 347 observer.MostVisitedItemsChanged(most_visited_items_);
297 } 348 }
298 349
299 #if defined(ENABLE_THEMES) 350 #if defined(ENABLE_THEMES)
300 351
301 namespace { 352 namespace {
302 353
303 const int kSectionBorderAlphaTransparency = 80; 354 const int kSectionBorderAlphaTransparency = 80;
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
444 } 495 }
445 496
446 if (default_search_provider_changed || google_base_url_domain_changed) { 497 if (default_search_provider_changed || google_base_url_domain_changed) {
447 ResetInstantSearchPrerendererIfNecessary(); 498 ResetInstantSearchPrerendererIfNecessary();
448 for (InstantServiceObserver& observer : observers_) 499 for (InstantServiceObserver& observer : observers_)
449 observer.DefaultSearchProviderChanged(google_base_url_domain_changed); 500 observer.DefaultSearchProviderChanged(google_base_url_domain_changed);
450 } 501 }
451 } 502 }
452 503
453 void InstantService::TopSitesLoaded(history::TopSites* top_sites) { 504 void InstantService::TopSitesLoaded(history::TopSites* top_sites) {
505 DCHECK(!most_visited_sites_);
506 DCHECK_EQ(top_sites_.get(), top_sites);
454 } 507 }
455 508
456 void InstantService::TopSitesChanged(history::TopSites* top_sites, 509 void InstantService::TopSitesChanged(history::TopSites* top_sites,
457 ChangeReason change_reason) { 510 ChangeReason change_reason) {
511 DCHECK(!most_visited_sites_);
512 DCHECK_EQ(top_sites_.get(), top_sites);
458 // As forced urls already come from tiles, we can safely ignore those updates. 513 // As forced urls already come from tiles, we can safely ignore those updates.
459 if (change_reason == history::TopSitesObserver::ChangeReason::FORCED_URL) 514 if (change_reason == history::TopSitesObserver::ChangeReason::FORCED_URL)
460 return; 515 return;
461 top_sites->GetMostVisitedURLs( 516 top_sites_->GetMostVisitedURLs(base::Bind(&InstantService::OnTopSitesReceived,
462 base::Bind(&InstantService::OnMostVisitedItemsReceived, 517 weak_ptr_factory_.GetWeakPtr()),
463 weak_ptr_factory_.GetWeakPtr()), 518 false);
464 false);
465 } 519 }
466 520
467 void InstantService::ResetInstantSearchPrerendererIfNecessary() { 521 void InstantService::ResetInstantSearchPrerendererIfNecessary() {
468 if (!search::ShouldPrefetchSearchResults()) 522 if (!search::ShouldPrefetchSearchResults())
469 return; 523 return;
470 524
471 GURL url(search::GetSearchResultPrefetchBaseURL(profile_)); 525 GURL url(search::GetSearchResultPrefetchBaseURL(profile_));
472 if (!instant_prerenderer_ || instant_prerenderer_->prerender_url() != url) { 526 if (!instant_prerenderer_ || instant_prerenderer_->prerender_url() != url) {
473 instant_prerenderer_.reset( 527 instant_prerenderer_.reset(
474 url.is_valid() ? new InstantSearchPrerenderer(profile_, url) : nullptr); 528 url.is_valid() ? new InstantSearchPrerenderer(profile_, url) : nullptr);
475 } 529 }
476 } 530 }
OLDNEW
« no previous file with comments | « chrome/browser/search/instant_service.h ('k') | chrome/browser/search/instant_service_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698