Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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 } | |
| 119 | |
| 120 if (most_visited_sites_) { | |
|
sfiera
2016/11/29 10:24:03
base::MakeUnique<>() cannot return nullptr, can it
Marc Treib
2016/11/29 15:20:13
Done.
| |
| 121 most_visited_sites_->SetMostVisitedURLsObserver(this, 8); | |
| 122 } else { | |
| 123 scoped_refptr<history::TopSites> top_sites = | |
|
sfiera
2016/11/29 10:24:03
We repeat this call a lot below. Is there a reason
Marc Treib
2016/11/29 15:20:13
I don't see a reason not to keep a ref to the TopS
| |
| 124 TopSitesFactory::GetForProfile(profile_); | |
| 125 if (top_sites) { | |
| 126 top_sites->AddObserver(this); | |
| 127 // Immediately query the TopSites state. | |
| 128 TopSitesChanged(top_sites.get(), | |
| 129 history::TopSitesObserver::ChangeReason::MOST_VISITED); | |
| 130 } | |
| 103 } | 131 } |
| 104 | 132 |
| 105 if (profile_ && profile_->GetResourceContext()) { | 133 if (profile_ && profile_->GetResourceContext()) { |
| 106 content::BrowserThread::PostTask( | 134 content::BrowserThread::PostTask( |
| 107 content::BrowserThread::IO, FROM_HERE, | 135 content::BrowserThread::IO, FROM_HERE, |
| 108 base::Bind(&InstantIOContext::SetUserDataOnIO, | 136 base::Bind(&InstantIOContext::SetUserDataOnIO, |
| 109 profile->GetResourceContext(), instant_io_context_)); | 137 profile->GetResourceContext(), instant_io_context_)); |
| 110 } | 138 } |
| 111 | 139 |
| 112 // Set up the data sources that Instant uses on the NTP. | 140 // Set up the data sources that Instant uses on the NTP. |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 163 | 191 |
| 164 void InstantService::AddObserver(InstantServiceObserver* observer) { | 192 void InstantService::AddObserver(InstantServiceObserver* observer) { |
| 165 observers_.AddObserver(observer); | 193 observers_.AddObserver(observer); |
| 166 } | 194 } |
| 167 | 195 |
| 168 void InstantService::RemoveObserver(InstantServiceObserver* observer) { | 196 void InstantService::RemoveObserver(InstantServiceObserver* observer) { |
| 169 observers_.RemoveObserver(observer); | 197 observers_.RemoveObserver(observer); |
| 170 } | 198 } |
| 171 | 199 |
| 172 void InstantService::OnNewTabPageOpened() { | 200 void InstantService::OnNewTabPageOpened() { |
| 173 scoped_refptr<history::TopSites> top_sites = | 201 if (most_visited_sites_) { |
| 174 TopSitesFactory::GetForProfile(profile_); | 202 most_visited_sites_->Refresh(); |
| 175 if (top_sites) | 203 } else { |
| 176 top_sites->SyncWithHistory(); | 204 scoped_refptr<history::TopSites> top_sites = |
| 205 TopSitesFactory::GetForProfile(profile_); | |
| 206 if (top_sites) | |
| 207 top_sites->SyncWithHistory(); | |
| 208 } | |
| 177 } | 209 } |
| 178 | 210 |
| 179 void InstantService::DeleteMostVisitedItem(const GURL& url) { | 211 void InstantService::DeleteMostVisitedItem(const GURL& url) { |
| 180 scoped_refptr<history::TopSites> top_sites = | 212 if (most_visited_sites_) { |
| 181 TopSitesFactory::GetForProfile(profile_); | 213 most_visited_sites_->AddOrRemoveBlacklistedUrl(url, true); |
| 182 if (top_sites) | 214 } else { |
| 183 top_sites->AddBlacklistedURL(url); | 215 scoped_refptr<history::TopSites> top_sites = |
| 216 TopSitesFactory::GetForProfile(profile_); | |
| 217 if (top_sites) | |
| 218 top_sites->AddBlacklistedURL(url); | |
| 219 } | |
| 184 } | 220 } |
| 185 | 221 |
| 186 void InstantService::UndoMostVisitedDeletion(const GURL& url) { | 222 void InstantService::UndoMostVisitedDeletion(const GURL& url) { |
| 187 scoped_refptr<history::TopSites> top_sites = | 223 if (most_visited_sites_) { |
| 188 TopSitesFactory::GetForProfile(profile_); | 224 most_visited_sites_->AddOrRemoveBlacklistedUrl(url, false); |
| 189 if (top_sites) | 225 } else { |
| 190 top_sites->RemoveBlacklistedURL(url); | 226 scoped_refptr<history::TopSites> top_sites = |
| 227 TopSitesFactory::GetForProfile(profile_); | |
| 228 if (top_sites) | |
| 229 top_sites->RemoveBlacklistedURL(url); | |
| 230 } | |
| 191 } | 231 } |
| 192 | 232 |
| 193 void InstantService::UndoAllMostVisitedDeletions() { | 233 void InstantService::UndoAllMostVisitedDeletions() { |
| 194 scoped_refptr<history::TopSites> top_sites = | 234 if (most_visited_sites_) { |
| 195 TopSitesFactory::GetForProfile(profile_); | 235 most_visited_sites_->ClearBlacklistedUrls(); |
| 196 if (top_sites) | 236 } else { |
| 197 top_sites->ClearBlacklistedURLs(); | 237 scoped_refptr<history::TopSites> top_sites = |
| 238 TopSitesFactory::GetForProfile(profile_); | |
| 239 if (top_sites) | |
| 240 top_sites->ClearBlacklistedURLs(); | |
| 241 } | |
| 198 } | 242 } |
| 199 | 243 |
| 200 void InstantService::UpdateThemeInfo() { | 244 void InstantService::UpdateThemeInfo() { |
| 201 #if defined(ENABLE_THEMES) | 245 #if defined(ENABLE_THEMES) |
| 202 // Update theme background info. | 246 // Update theme background info. |
| 203 // Initialize |theme_info| if necessary. | 247 // Initialize |theme_info| if necessary. |
| 204 if (!theme_info_) { | 248 if (!theme_info_) { |
| 205 OnThemeChanged(); | 249 OnThemeChanged(); |
| 206 } else { | 250 } else { |
| 207 for (InstantServiceObserver& observer : observers_) | 251 for (InstantServiceObserver& observer : observers_) |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 229 void InstantService::Shutdown() { | 273 void InstantService::Shutdown() { |
| 230 process_ids_.clear(); | 274 process_ids_.clear(); |
| 231 | 275 |
| 232 if (instant_io_context_.get()) { | 276 if (instant_io_context_.get()) { |
| 233 content::BrowserThread::PostTask( | 277 content::BrowserThread::PostTask( |
| 234 content::BrowserThread::IO, FROM_HERE, | 278 content::BrowserThread::IO, FROM_HERE, |
| 235 base::Bind(&InstantIOContext::ClearInstantProcessesOnIO, | 279 base::Bind(&InstantIOContext::ClearInstantProcessesOnIO, |
| 236 instant_io_context_)); | 280 instant_io_context_)); |
| 237 } | 281 } |
| 238 | 282 |
| 239 scoped_refptr<history::TopSites> top_sites = | 283 if (most_visited_sites_) { |
| 240 TopSitesFactory::GetForProfile(profile_); | 284 most_visited_sites_.reset(); |
| 241 if (top_sites) | 285 } else { |
| 242 top_sites->RemoveObserver(this); | 286 scoped_refptr<history::TopSites> top_sites = |
| 287 TopSitesFactory::GetForProfile(profile_); | |
| 288 if (top_sites) | |
| 289 top_sites->RemoveObserver(this); | |
| 290 } | |
| 243 | 291 |
| 244 instant_io_context_ = NULL; | 292 instant_io_context_ = NULL; |
| 245 } | 293 } |
| 246 | 294 |
| 247 void InstantService::Observe(int type, | 295 void InstantService::Observe(int type, |
| 248 const content::NotificationSource& source, | 296 const content::NotificationSource& source, |
| 249 const content::NotificationDetails& details) { | 297 const content::NotificationDetails& details) { |
| 250 switch (type) { | 298 switch (type) { |
| 251 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: | 299 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: |
| 252 SendSearchURLsToRenderer( | 300 SendSearchURLsToRenderer( |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 284 InstantMostVisitedItem item; | 332 InstantMostVisitedItem item; |
| 285 item.url = mv_url.url; | 333 item.url = mv_url.url; |
| 286 item.title = mv_url.title; | 334 item.title = mv_url.title; |
| 287 item.is_server_side_suggestion = false; | 335 item.is_server_side_suggestion = false; |
| 288 most_visited_items_.push_back(item); | 336 most_visited_items_.push_back(item); |
| 289 } | 337 } |
| 290 | 338 |
| 291 NotifyAboutMostVisitedItems(); | 339 NotifyAboutMostVisitedItems(); |
| 292 } | 340 } |
| 293 | 341 |
| 342 void InstantService::OnMostVisitedURLsAvailable( | |
| 343 const ntp_tiles::NTPTilesVector& tiles) { | |
| 344 DCHECK(most_visited_sites_); | |
| 345 most_visited_items_.clear(); | |
| 346 for (const ntp_tiles::NTPTile& tile : tiles) { | |
| 347 InstantMostVisitedItem item; | |
| 348 item.url = tile.url; | |
| 349 item.title = tile.title; | |
| 350 item.thumbnail = tile.thumbnail_url; | |
| 351 item.favicon = tile.favicon_url; | |
| 352 item.is_server_side_suggestion = | |
| 353 tile.source == ntp_tiles::NTPTileSource::SUGGESTIONS_SERVICE; | |
| 354 most_visited_items_.push_back(item); | |
| 355 } | |
| 356 | |
| 357 NotifyAboutMostVisitedItems(); | |
| 358 } | |
| 359 | |
| 360 void InstantService::OnIconMadeAvailable(const GURL& site_url) {} | |
| 361 | |
| 294 void InstantService::NotifyAboutMostVisitedItems() { | 362 void InstantService::NotifyAboutMostVisitedItems() { |
| 295 for (InstantServiceObserver& observer : observers_) | 363 for (InstantServiceObserver& observer : observers_) |
| 296 observer.MostVisitedItemsChanged(most_visited_items_); | 364 observer.MostVisitedItemsChanged(most_visited_items_); |
| 297 } | 365 } |
| 298 | 366 |
| 299 #if defined(ENABLE_THEMES) | 367 #if defined(ENABLE_THEMES) |
| 300 | 368 |
| 301 namespace { | 369 namespace { |
| 302 | 370 |
| 303 const int kSectionBorderAlphaTransparency = 80; | 371 const int kSectionBorderAlphaTransparency = 80; |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 444 } | 512 } |
| 445 | 513 |
| 446 if (default_search_provider_changed || google_base_url_domain_changed) { | 514 if (default_search_provider_changed || google_base_url_domain_changed) { |
| 447 ResetInstantSearchPrerendererIfNecessary(); | 515 ResetInstantSearchPrerendererIfNecessary(); |
| 448 for (InstantServiceObserver& observer : observers_) | 516 for (InstantServiceObserver& observer : observers_) |
| 449 observer.DefaultSearchProviderChanged(google_base_url_domain_changed); | 517 observer.DefaultSearchProviderChanged(google_base_url_domain_changed); |
| 450 } | 518 } |
| 451 } | 519 } |
| 452 | 520 |
| 453 void InstantService::TopSitesLoaded(history::TopSites* top_sites) { | 521 void InstantService::TopSitesLoaded(history::TopSites* top_sites) { |
| 522 DCHECK(!most_visited_sites_); | |
| 454 } | 523 } |
| 455 | 524 |
| 456 void InstantService::TopSitesChanged(history::TopSites* top_sites, | 525 void InstantService::TopSitesChanged(history::TopSites* top_sites, |
| 457 ChangeReason change_reason) { | 526 ChangeReason change_reason) { |
| 527 DCHECK(!most_visited_sites_); | |
| 458 // As forced urls already come from tiles, we can safely ignore those updates. | 528 // As forced urls already come from tiles, we can safely ignore those updates. |
| 459 if (change_reason == history::TopSitesObserver::ChangeReason::FORCED_URL) | 529 if (change_reason == history::TopSitesObserver::ChangeReason::FORCED_URL) |
| 460 return; | 530 return; |
| 461 top_sites->GetMostVisitedURLs( | 531 top_sites->GetMostVisitedURLs( |
| 462 base::Bind(&InstantService::OnMostVisitedItemsReceived, | 532 base::Bind(&InstantService::OnMostVisitedItemsReceived, |
| 463 weak_ptr_factory_.GetWeakPtr()), | 533 weak_ptr_factory_.GetWeakPtr()), |
| 464 false); | 534 false); |
| 465 } | 535 } |
| 466 | 536 |
| 467 void InstantService::ResetInstantSearchPrerendererIfNecessary() { | 537 void InstantService::ResetInstantSearchPrerendererIfNecessary() { |
| 468 if (!search::ShouldPrefetchSearchResults()) | 538 if (!search::ShouldPrefetchSearchResults()) |
| 469 return; | 539 return; |
| 470 | 540 |
| 471 GURL url(search::GetSearchResultPrefetchBaseURL(profile_)); | 541 GURL url(search::GetSearchResultPrefetchBaseURL(profile_)); |
| 472 if (!instant_prerenderer_ || instant_prerenderer_->prerender_url() != url) { | 542 if (!instant_prerenderer_ || instant_prerenderer_->prerender_url() != url) { |
| 473 instant_prerenderer_.reset( | 543 instant_prerenderer_.reset( |
| 474 url.is_valid() ? new InstantSearchPrerenderer(profile_, url) : nullptr); | 544 url.is_valid() ? new InstantSearchPrerenderer(profile_, url) : nullptr); |
| 475 } | 545 } |
| 476 } | 546 } |
| OLD | NEW |