OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/ntp_snippets/content_suggestions_service_factory.h" | 5 #include "chrome/browser/ntp_snippets/content_suggestions_service_factory.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/feature_list.h" | 9 #include "base/feature_list.h" |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
(...skipping 29 matching lines...) Expand all Loading... |
40 #include "components/ntp_snippets/ntp_snippets_constants.h" | 40 #include "components/ntp_snippets/ntp_snippets_constants.h" |
41 #include "components/ntp_snippets/remote/persistent_scheduler.h" | 41 #include "components/ntp_snippets/remote/persistent_scheduler.h" |
42 #include "components/ntp_snippets/remote/remote_suggestions_database.h" | 42 #include "components/ntp_snippets/remote/remote_suggestions_database.h" |
43 #include "components/ntp_snippets/remote/remote_suggestions_fetcher.h" | 43 #include "components/ntp_snippets/remote/remote_suggestions_fetcher.h" |
44 #include "components/ntp_snippets/remote/remote_suggestions_provider_impl.h" | 44 #include "components/ntp_snippets/remote/remote_suggestions_provider_impl.h" |
45 #include "components/ntp_snippets/remote/remote_suggestions_scheduler_impl.h" | 45 #include "components/ntp_snippets/remote/remote_suggestions_scheduler_impl.h" |
46 #include "components/ntp_snippets/remote/remote_suggestions_status_service.h" | 46 #include "components/ntp_snippets/remote/remote_suggestions_status_service.h" |
47 #include "components/ntp_snippets/sessions/foreign_sessions_suggestions_provider
.h" | 47 #include "components/ntp_snippets/sessions/foreign_sessions_suggestions_provider
.h" |
48 #include "components/ntp_snippets/sessions/tab_delegate_sync_adapter.h" | 48 #include "components/ntp_snippets/sessions/tab_delegate_sync_adapter.h" |
49 #include "components/ntp_snippets/user_classifier.h" | 49 #include "components/ntp_snippets/user_classifier.h" |
| 50 #include "components/offline_pages/features/features.h" |
50 #include "components/prefs/pref_service.h" | 51 #include "components/prefs/pref_service.h" |
51 #include "components/safe_json/safe_json_parser.h" | 52 #include "components/safe_json/safe_json_parser.h" |
52 #include "components/signin/core/browser/profile_oauth2_token_service.h" | 53 #include "components/signin/core/browser/profile_oauth2_token_service.h" |
53 #include "components/signin/core/browser/signin_manager.h" | 54 #include "components/signin/core/browser/signin_manager.h" |
54 #include "components/translate/core/browser/language_model.h" | 55 #include "components/translate/core/browser/language_model.h" |
55 #include "components/version_info/version_info.h" | 56 #include "components/version_info/version_info.h" |
56 #include "content/public/browser/browser_context.h" | 57 #include "content/public/browser/browser_context.h" |
57 #include "content/public/browser/browser_thread.h" | 58 #include "content/public/browser/browser_thread.h" |
58 #include "content/public/browser/storage_partition.h" | 59 #include "content/public/browser/storage_partition.h" |
59 #include "google_apis/google_api_keys.h" | 60 #include "google_apis/google_api_keys.h" |
60 #include "net/url_request/url_request_context_getter.h" | 61 #include "net/url_request/url_request_context_getter.h" |
61 | 62 |
62 #if defined(OS_ANDROID) | 63 #if defined(OS_ANDROID) |
63 #include "chrome/browser/android/chrome_feature_list.h" | 64 #include "chrome/browser/android/chrome_feature_list.h" |
64 #include "chrome/browser/android/ntp/ntp_snippets_launcher.h" | 65 #include "chrome/browser/android/ntp/ntp_snippets_launcher.h" |
65 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" | |
66 #include "chrome/browser/android/offline_pages/request_coordinator_factory.h" | |
67 #include "chrome/browser/download/download_core_service.h" | 66 #include "chrome/browser/download/download_core_service.h" |
68 #include "chrome/browser/download/download_core_service_factory.h" | 67 #include "chrome/browser/download/download_core_service_factory.h" |
69 #include "chrome/browser/download/download_history.h" | 68 #include "chrome/browser/download/download_history.h" |
70 #include "chrome/browser/ntp_snippets/download_suggestions_provider.h" | 69 #include "chrome/browser/ntp_snippets/download_suggestions_provider.h" |
| 70 #include "components/ntp_snippets/physical_web_pages/physical_web_page_suggestio
ns_provider.h" |
| 71 #include "components/physical_web/data_source/physical_web_data_source.h" |
| 72 #endif |
| 73 |
| 74 #if BUILDFLAG(ENABLE_OFFLINE_PAGES) |
| 75 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" |
| 76 #include "chrome/browser/android/offline_pages/request_coordinator_factory.h" |
| 77 #include "chrome/browser/offline_pages/prefetch/prefetch_service_factory.h" |
71 #include "components/ntp_snippets/offline_pages/recent_tab_suggestions_provider.
h" | 78 #include "components/ntp_snippets/offline_pages/recent_tab_suggestions_provider.
h" |
72 #include "components/ntp_snippets/physical_web_pages/physical_web_page_suggestio
ns_provider.h" | |
73 #include "components/offline_pages/content/suggested_articles_observer.h" | |
74 #include "components/offline_pages/core/background/request_coordinator.h" | 79 #include "components/offline_pages/core/background/request_coordinator.h" |
75 #include "components/offline_pages/core/offline_page_feature.h" | 80 #include "components/offline_pages/core/offline_page_feature.h" |
76 #include "components/offline_pages/core/offline_page_model.h" | 81 #include "components/offline_pages/core/offline_page_model.h" |
| 82 #include "components/offline_pages/core/prefetch/suggested_articles_observer.h" |
77 #include "components/offline_pages/core/recent_tabs/recent_tabs_ui_adapter_deleg
ate.h" | 83 #include "components/offline_pages/core/recent_tabs/recent_tabs_ui_adapter_deleg
ate.h" |
78 #include "components/physical_web/data_source/physical_web_data_source.h" | 84 #endif |
79 | |
80 using content::DownloadManager; | |
81 using ntp_snippets::PhysicalWebPageSuggestionsProvider; | |
82 using ntp_snippets::RecentTabSuggestionsProvider; | |
83 using offline_pages::OfflinePageModel; | |
84 using offline_pages::RequestCoordinator; | |
85 using offline_pages::OfflinePageModelFactory; | |
86 using offline_pages::RequestCoordinatorFactory; | |
87 using physical_web::PhysicalWebDataSource; | |
88 #endif // OS_ANDROID | |
89 | 85 |
90 using bookmarks::BookmarkModel; | 86 using bookmarks::BookmarkModel; |
91 using content::BrowserThread; | 87 using content::BrowserThread; |
92 using history::HistoryService; | 88 using history::HistoryService; |
93 using image_fetcher::ImageFetcherImpl; | 89 using image_fetcher::ImageFetcherImpl; |
94 using ntp_snippets::BookmarkSuggestionsProvider; | 90 using ntp_snippets::BookmarkSuggestionsProvider; |
95 using ntp_snippets::CategoryRanker; | 91 using ntp_snippets::CategoryRanker; |
96 using ntp_snippets::ContentSuggestionsService; | 92 using ntp_snippets::ContentSuggestionsService; |
97 using ntp_snippets::ForeignSessionsSuggestionsProvider; | 93 using ntp_snippets::ForeignSessionsSuggestionsProvider; |
98 using ntp_snippets::GetFetchEndpoint; | 94 using ntp_snippets::GetFetchEndpoint; |
99 using ntp_snippets::PersistentScheduler; | 95 using ntp_snippets::PersistentScheduler; |
100 using ntp_snippets::RemoteSuggestionsDatabase; | 96 using ntp_snippets::RemoteSuggestionsDatabase; |
101 using ntp_snippets::RemoteSuggestionsFetcher; | 97 using ntp_snippets::RemoteSuggestionsFetcher; |
102 using ntp_snippets::RemoteSuggestionsProviderImpl; | 98 using ntp_snippets::RemoteSuggestionsProviderImpl; |
103 using ntp_snippets::RemoteSuggestionsSchedulerImpl; | 99 using ntp_snippets::RemoteSuggestionsSchedulerImpl; |
104 using ntp_snippets::RemoteSuggestionsStatusService; | 100 using ntp_snippets::RemoteSuggestionsStatusService; |
105 using ntp_snippets::TabDelegateSyncAdapter; | 101 using ntp_snippets::TabDelegateSyncAdapter; |
106 using ntp_snippets::UserClassifier; | 102 using ntp_snippets::UserClassifier; |
107 using suggestions::ImageDecoderImpl; | 103 using suggestions::ImageDecoderImpl; |
108 using syncer::SyncService; | 104 using syncer::SyncService; |
109 using translate::LanguageModel; | 105 using translate::LanguageModel; |
110 | 106 |
| 107 #if defined(OS_ANDROID) |
| 108 using content::DownloadManager; |
| 109 using ntp_snippets::PhysicalWebPageSuggestionsProvider; |
| 110 using physical_web::PhysicalWebDataSource; |
| 111 #endif // OS_ANDROID |
| 112 |
| 113 #if BUILDFLAG(ENABLE_OFFLINE_PAGES) |
| 114 using ntp_snippets::RecentTabSuggestionsProvider; |
| 115 using offline_pages::OfflinePageModel; |
| 116 using offline_pages::RequestCoordinator; |
| 117 using offline_pages::OfflinePageModelFactory; |
| 118 using offline_pages::RequestCoordinatorFactory; |
| 119 #endif // BUILDFLAG(ENABLE_OFFLINE_PAGES) |
| 120 |
111 // For now, ContentSuggestionsService must only be instantiated on Android. | 121 // For now, ContentSuggestionsService must only be instantiated on Android. |
112 // See also crbug.com/688366. | 122 // See also crbug.com/688366. |
113 #if defined(OS_ANDROID) | 123 #if defined(OS_ANDROID) |
114 #define CONTENT_SUGGESTIONS_ENABLED 1 | 124 #define CONTENT_SUGGESTIONS_ENABLED 1 |
115 #else | 125 #else |
116 #define CONTENT_SUGGESTIONS_ENABLED 0 | 126 #define CONTENT_SUGGESTIONS_ENABLED 0 |
117 #endif // OS_ANDROID | 127 #endif // OS_ANDROID |
118 | 128 |
119 // The actual #if that does the work is below in BuildServiceInstanceFor. This | 129 // The actual #if that does the work is below in BuildServiceInstanceFor. This |
120 // one is just required to avoid "unused code" compiler errors. | 130 // one is just required to avoid "unused code" compiler errors. |
121 #if CONTENT_SUGGESTIONS_ENABLED | 131 #if CONTENT_SUGGESTIONS_ENABLED |
122 | 132 |
123 namespace { | 133 namespace { |
124 | 134 |
125 bool IsChromeHomeEnabled() { | 135 bool IsChromeHomeEnabled() { |
126 #if defined(OS_ANDROID) | 136 #if defined(OS_ANDROID) |
127 return base::FeatureList::IsEnabled(chrome::android::kChromeHomeFeature); | 137 return base::FeatureList::IsEnabled(chrome::android::kChromeHomeFeature); |
128 #else | 138 #else |
129 return false; | 139 return false; |
130 #endif | 140 #endif |
131 } | 141 } |
132 | 142 |
133 #if defined(OS_ANDROID) | 143 #if BUILDFLAG(ENABLE_OFFLINE_PAGES) |
134 | 144 |
135 bool IsRecentTabProviderEnabled() { | 145 bool IsRecentTabProviderEnabled() { |
136 return base::FeatureList::IsEnabled( | 146 return base::FeatureList::IsEnabled( |
137 ntp_snippets::kRecentOfflineTabSuggestionsFeature) && | 147 ntp_snippets::kRecentOfflineTabSuggestionsFeature) && |
138 base::FeatureList::IsEnabled( | 148 base::FeatureList::IsEnabled( |
139 offline_pages::kOffliningRecentPagesFeature); | 149 offline_pages::kOffliningRecentPagesFeature); |
140 } | 150 } |
141 | 151 |
142 void RegisterRecentTabProvider(OfflinePageModel* offline_page_model, | 152 void RegisterRecentTabProvider(OfflinePageModel* offline_page_model, |
143 RequestCoordinator* request_coordinator, | 153 RequestCoordinator* request_coordinator, |
144 ContentSuggestionsService* service, | 154 ContentSuggestionsService* service, |
145 PrefService* pref_service) { | 155 PrefService* pref_service) { |
146 offline_pages::DownloadUIAdapter* ui_adapter = offline_pages:: | 156 offline_pages::DownloadUIAdapter* ui_adapter = offline_pages:: |
147 RecentTabsUIAdapterDelegate::GetOrCreateRecentTabsUIAdapter( | 157 RecentTabsUIAdapterDelegate::GetOrCreateRecentTabsUIAdapter( |
148 offline_page_model, request_coordinator); | 158 offline_page_model, request_coordinator); |
149 auto provider = base::MakeUnique<RecentTabSuggestionsProvider>( | 159 auto provider = base::MakeUnique<RecentTabSuggestionsProvider>( |
150 service, ui_adapter, pref_service); | 160 service, ui_adapter, pref_service); |
151 service->RegisterProvider(std::move(provider)); | 161 service->RegisterProvider(std::move(provider)); |
152 } | 162 } |
153 | 163 |
| 164 #endif // BUILDFLAG(ENABLE_OFFLINE_PAGES) |
| 165 |
| 166 #if defined(OS_ANDROID) |
| 167 |
154 void RegisterDownloadsProvider(OfflinePageModel* offline_page_model, | 168 void RegisterDownloadsProvider(OfflinePageModel* offline_page_model, |
155 DownloadManager* download_manager, | 169 DownloadManager* download_manager, |
156 DownloadHistory* download_history, | 170 DownloadHistory* download_history, |
157 ContentSuggestionsService* service, | 171 ContentSuggestionsService* service, |
158 PrefService* pref_service) { | 172 PrefService* pref_service) { |
159 auto provider = base::MakeUnique<DownloadSuggestionsProvider>( | 173 auto provider = base::MakeUnique<DownloadSuggestionsProvider>( |
160 service, offline_page_model, download_manager, download_history, | 174 service, offline_page_model, download_manager, download_history, |
161 pref_service, base::MakeUnique<base::DefaultClock>()); | 175 pref_service, base::MakeUnique<base::DefaultClock>()); |
162 service->RegisterProvider(std::move(provider)); | 176 service->RegisterProvider(std::move(provider)); |
163 } | 177 } |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
278 GetInstance()->GetServiceForBrowserContext(profile, false)); | 292 GetInstance()->GetServiceForBrowserContext(profile, false)); |
279 } | 293 } |
280 | 294 |
281 ContentSuggestionsServiceFactory::ContentSuggestionsServiceFactory() | 295 ContentSuggestionsServiceFactory::ContentSuggestionsServiceFactory() |
282 : BrowserContextKeyedServiceFactory( | 296 : BrowserContextKeyedServiceFactory( |
283 "ContentSuggestionsService", | 297 "ContentSuggestionsService", |
284 BrowserContextDependencyManager::GetInstance()) { | 298 BrowserContextDependencyManager::GetInstance()) { |
285 DependsOn(BookmarkModelFactory::GetInstance()); | 299 DependsOn(BookmarkModelFactory::GetInstance()); |
286 DependsOn(HistoryServiceFactory::GetInstance()); | 300 DependsOn(HistoryServiceFactory::GetInstance()); |
287 DependsOn(LargeIconServiceFactory::GetInstance()); | 301 DependsOn(LargeIconServiceFactory::GetInstance()); |
288 #if defined(OS_ANDROID) | 302 #if BUILDFLAG(ENABLE_OFFLINE_PAGES) |
289 DependsOn(OfflinePageModelFactory::GetInstance()); | 303 DependsOn(OfflinePageModelFactory::GetInstance()); |
290 #endif // OS_ANDROID | 304 DependsOn(offline_pages::PrefetchServiceFactory::GetInstance()); |
| 305 #endif // BUILDFLAG(ENABLE_OFFLINE_PAGES) |
291 DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance()); | 306 DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance()); |
292 DependsOn(ProfileSyncServiceFactory::GetInstance()); | 307 DependsOn(ProfileSyncServiceFactory::GetInstance()); |
293 DependsOn(SigninManagerFactory::GetInstance()); | 308 DependsOn(SigninManagerFactory::GetInstance()); |
294 } | 309 } |
295 | 310 |
296 ContentSuggestionsServiceFactory::~ContentSuggestionsServiceFactory() = default; | 311 ContentSuggestionsServiceFactory::~ContentSuggestionsServiceFactory() = default; |
297 | 312 |
298 KeyedService* ContentSuggestionsServiceFactory::BuildServiceInstanceFor( | 313 KeyedService* ContentSuggestionsServiceFactory::BuildServiceInstanceFor( |
299 content::BrowserContext* context) const { | 314 content::BrowserContext* context) const { |
300 #if CONTENT_SUGGESTIONS_ENABLED | 315 #if CONTENT_SUGGESTIONS_ENABLED |
(...skipping 24 matching lines...) Expand all Loading... |
325 favicon::LargeIconService* large_icon_service = | 340 favicon::LargeIconService* large_icon_service = |
326 LargeIconServiceFactory::GetForBrowserContext(profile); | 341 LargeIconServiceFactory::GetForBrowserContext(profile); |
327 std::unique_ptr<CategoryRanker> category_ranker = | 342 std::unique_ptr<CategoryRanker> category_ranker = |
328 ntp_snippets::BuildSelectedCategoryRanker( | 343 ntp_snippets::BuildSelectedCategoryRanker( |
329 pref_service, base::MakeUnique<base::DefaultClock>()); | 344 pref_service, base::MakeUnique<base::DefaultClock>()); |
330 auto* service = new ContentSuggestionsService( | 345 auto* service = new ContentSuggestionsService( |
331 State::ENABLED, signin_manager, history_service, large_icon_service, | 346 State::ENABLED, signin_manager, history_service, large_icon_service, |
332 pref_service, std::move(category_ranker), std::move(user_classifier), | 347 pref_service, std::move(category_ranker), std::move(user_classifier), |
333 std::move(scheduler)); | 348 std::move(scheduler)); |
334 | 349 |
335 #if defined(OS_ANDROID) | 350 #if BUILDFLAG(ENABLE_OFFLINE_PAGES) |
336 OfflinePageModel* offline_page_model = | 351 OfflinePageModel* offline_page_model = |
337 OfflinePageModelFactory::GetForBrowserContext(profile); | 352 OfflinePageModelFactory::GetForBrowserContext(profile); |
338 if (IsRecentTabProviderEnabled()) { | 353 if (IsRecentTabProviderEnabled()) { |
339 RequestCoordinator* request_coordinator = | 354 RequestCoordinator* request_coordinator = |
340 RequestCoordinatorFactory::GetForBrowserContext(profile); | 355 RequestCoordinatorFactory::GetForBrowserContext(profile); |
341 RegisterRecentTabProvider(offline_page_model, request_coordinator, service, | 356 RegisterRecentTabProvider(offline_page_model, request_coordinator, service, |
342 pref_service); | 357 pref_service); |
343 } | 358 } |
344 | 359 |
| 360 offline_pages::PrefetchService* prefetch_service = |
| 361 offline_pages::PrefetchServiceFactory::GetForBrowserContext(profile); |
| 362 prefetch_service->ObserveContentSuggestionsService(service); |
| 363 #endif // BUILDFLAG(ENABLE_OFFLINE_PAGES) |
| 364 |
| 365 #if defined(OS_ANDROID) |
345 bool show_asset_downloads = | 366 bool show_asset_downloads = |
346 !IsChromeHomeEnabled() && | 367 !IsChromeHomeEnabled() && |
347 base::FeatureList::IsEnabled(features::kAssetDownloadSuggestionsFeature); | 368 base::FeatureList::IsEnabled(features::kAssetDownloadSuggestionsFeature); |
348 bool show_offline_page_downloads = | 369 bool show_offline_page_downloads = |
349 !IsChromeHomeEnabled() && | 370 !IsChromeHomeEnabled() && |
350 base::FeatureList::IsEnabled( | 371 base::FeatureList::IsEnabled( |
351 features::kOfflinePageDownloadSuggestionsFeature); | 372 features::kOfflinePageDownloadSuggestionsFeature); |
352 if (show_asset_downloads || show_offline_page_downloads) { | 373 if (show_asset_downloads || show_offline_page_downloads) { |
353 DownloadManager* download_manager = | 374 DownloadManager* download_manager = |
354 content::BrowserContext::GetDownloadManager(profile); | 375 content::BrowserContext::GetDownloadManager(profile); |
355 DownloadCoreService* download_core_service = | 376 DownloadCoreService* download_core_service = |
356 DownloadCoreServiceFactory::GetForBrowserContext(profile); | 377 DownloadCoreServiceFactory::GetForBrowserContext(profile); |
357 DownloadHistory* download_history = | 378 DownloadHistory* download_history = |
358 download_core_service->GetDownloadHistory(); | 379 download_core_service->GetDownloadHistory(); |
359 RegisterDownloadsProvider( | 380 RegisterDownloadsProvider( |
360 show_offline_page_downloads ? offline_page_model : nullptr, | 381 show_offline_page_downloads ? offline_page_model : nullptr, |
361 show_asset_downloads ? download_manager : nullptr, download_history, | 382 show_asset_downloads ? download_manager : nullptr, download_history, |
362 service, pref_service); | 383 service, pref_service); |
363 } | 384 } |
364 | |
365 offline_pages::SuggestedArticlesObserver::ObserveContentSuggestionsService( | |
366 profile, service); | |
367 #endif // OS_ANDROID | 385 #endif // OS_ANDROID |
368 | 386 |
369 // |bookmark_model| can be null in tests. | 387 // |bookmark_model| can be null in tests. |
370 BookmarkModel* bookmark_model = | 388 BookmarkModel* bookmark_model = |
371 BookmarkModelFactory::GetForBrowserContext(profile); | 389 BookmarkModelFactory::GetForBrowserContext(profile); |
372 if (base::FeatureList::IsEnabled(ntp_snippets::kBookmarkSuggestionsFeature) && | 390 if (base::FeatureList::IsEnabled(ntp_snippets::kBookmarkSuggestionsFeature) && |
373 bookmark_model && !IsChromeHomeEnabled()) { | 391 bookmark_model && !IsChromeHomeEnabled()) { |
374 RegisterBookmarkProvider(bookmark_model, service); | 392 RegisterBookmarkProvider(bookmark_model, service); |
375 } | 393 } |
376 | 394 |
(...skipping 22 matching lines...) Expand all Loading... |
399 ProfileSyncServiceFactory::GetSyncServiceForBrowserContext(profile); | 417 ProfileSyncServiceFactory::GetSyncServiceForBrowserContext(profile); |
400 RegisterForeignSessionsProvider(sync_service, service, pref_service); | 418 RegisterForeignSessionsProvider(sync_service, service, pref_service); |
401 } | 419 } |
402 | 420 |
403 return service; | 421 return service; |
404 | 422 |
405 #else | 423 #else |
406 return nullptr; | 424 return nullptr; |
407 #endif // CONTENT_SUGGESTIONS_ENABLED | 425 #endif // CONTENT_SUGGESTIONS_ENABLED |
408 } | 426 } |
OLD | NEW |