| 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" |
| 11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/memory/singleton.h" | 12 #include "base/memory/singleton.h" |
| 13 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 13 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
| 14 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
| 15 #include "chrome/browser/history/history_service_factory.h" | 15 #include "chrome/browser/history/history_service_factory.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/browser/search/suggestions/image_decoder_impl.h" | 17 #include "chrome/browser/search/suggestions/image_decoder_impl.h" |
| 18 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 18 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 19 #include "chrome/browser/signin/signin_manager_factory.h" | 19 #include "chrome/browser/signin/signin_manager_factory.h" |
| 20 #include "chrome/browser/sync/profile_sync_service_factory.h" | 20 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 21 #include "chrome/browser/translate/language_model_factory.h" |
| 21 #include "chrome/common/channel_info.h" | 22 #include "chrome/common/channel_info.h" |
| 22 #include "components/bookmarks/browser/bookmark_model.h" | 23 #include "components/bookmarks/browser/bookmark_model.h" |
| 23 #include "components/browser_sync/profile_sync_service.h" | 24 #include "components/browser_sync/profile_sync_service.h" |
| 24 #include "components/image_fetcher/image_decoder.h" | 25 #include "components/image_fetcher/image_decoder.h" |
| 25 #include "components/image_fetcher/image_fetcher.h" | 26 #include "components/image_fetcher/image_fetcher.h" |
| 26 #include "components/image_fetcher/image_fetcher_impl.h" | 27 #include "components/image_fetcher/image_fetcher_impl.h" |
| 27 #include "components/keyed_service/content/browser_context_dependency_manager.h" | 28 #include "components/keyed_service/content/browser_context_dependency_manager.h" |
| 28 #include "components/keyed_service/core/service_access_type.h" | 29 #include "components/keyed_service/core/service_access_type.h" |
| 29 #include "components/ntp_snippets/bookmarks/bookmark_suggestions_provider.h" | 30 #include "components/ntp_snippets/bookmarks/bookmark_suggestions_provider.h" |
| 30 #include "components/ntp_snippets/category_factory.h" | 31 #include "components/ntp_snippets/category_factory.h" |
| 31 #include "components/ntp_snippets/content_suggestions_service.h" | 32 #include "components/ntp_snippets/content_suggestions_service.h" |
| 32 #include "components/ntp_snippets/features.h" | 33 #include "components/ntp_snippets/features.h" |
| 33 #include "components/ntp_snippets/ntp_snippets_constants.h" | 34 #include "components/ntp_snippets/ntp_snippets_constants.h" |
| 34 #include "components/ntp_snippets/remote/ntp_snippets_database.h" | 35 #include "components/ntp_snippets/remote/ntp_snippets_database.h" |
| 35 #include "components/ntp_snippets/remote/ntp_snippets_fetcher.h" | 36 #include "components/ntp_snippets/remote/ntp_snippets_fetcher.h" |
| 36 #include "components/ntp_snippets/remote/ntp_snippets_scheduler.h" | 37 #include "components/ntp_snippets/remote/ntp_snippets_scheduler.h" |
| 37 #include "components/ntp_snippets/remote/ntp_snippets_service.h" | 38 #include "components/ntp_snippets/remote/ntp_snippets_service.h" |
| 38 #include "components/ntp_snippets/remote/ntp_snippets_status_service.h" | 39 #include "components/ntp_snippets/remote/ntp_snippets_status_service.h" |
| 39 #include "components/ntp_snippets/sessions/foreign_sessions_suggestions_provider
.h" | 40 #include "components/ntp_snippets/sessions/foreign_sessions_suggestions_provider
.h" |
| 40 #include "components/ntp_snippets/sessions/tab_delegate_sync_adapter.h" | 41 #include "components/ntp_snippets/sessions/tab_delegate_sync_adapter.h" |
| 41 #include "components/prefs/pref_service.h" | 42 #include "components/prefs/pref_service.h" |
| 42 #include "components/safe_json/safe_json_parser.h" | 43 #include "components/safe_json/safe_json_parser.h" |
| 43 #include "components/signin/core/browser/profile_oauth2_token_service.h" | 44 #include "components/signin/core/browser/profile_oauth2_token_service.h" |
| 44 #include "components/signin/core/browser/signin_manager.h" | 45 #include "components/signin/core/browser/signin_manager.h" |
| 46 #include "components/translate/core/browser/language_model.h" |
| 45 #include "components/version_info/version_info.h" | 47 #include "components/version_info/version_info.h" |
| 46 #include "content/public/browser/browser_context.h" | 48 #include "content/public/browser/browser_context.h" |
| 47 #include "content/public/browser/browser_thread.h" | 49 #include "content/public/browser/browser_thread.h" |
| 48 #include "content/public/browser/storage_partition.h" | 50 #include "content/public/browser/storage_partition.h" |
| 49 #include "google_apis/google_api_keys.h" | 51 #include "google_apis/google_api_keys.h" |
| 50 #include "net/url_request/url_request_context_getter.h" | 52 #include "net/url_request/url_request_context_getter.h" |
| 51 | 53 |
| 52 #if defined(OS_ANDROID) | 54 #if defined(OS_ANDROID) |
| 53 #include "chrome/browser/android/chrome_feature_list.h" | 55 #include "chrome/browser/android/chrome_feature_list.h" |
| 54 #include "chrome/browser/android/ntp/ntp_snippets_launcher.h" | 56 #include "chrome/browser/android/ntp/ntp_snippets_launcher.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 74 using ntp_snippets::ContentSuggestionsService; | 76 using ntp_snippets::ContentSuggestionsService; |
| 75 using ntp_snippets::NTPSnippetsDatabase; | 77 using ntp_snippets::NTPSnippetsDatabase; |
| 76 using ntp_snippets::NTPSnippetsFetcher; | 78 using ntp_snippets::NTPSnippetsFetcher; |
| 77 using ntp_snippets::NTPSnippetsService; | 79 using ntp_snippets::NTPSnippetsService; |
| 78 using ntp_snippets::NTPSnippetsScheduler; | 80 using ntp_snippets::NTPSnippetsScheduler; |
| 79 using ntp_snippets::NTPSnippetsStatusService; | 81 using ntp_snippets::NTPSnippetsStatusService; |
| 80 using ntp_snippets::ForeignSessionsSuggestionsProvider; | 82 using ntp_snippets::ForeignSessionsSuggestionsProvider; |
| 81 using ntp_snippets::TabDelegateSyncAdapter; | 83 using ntp_snippets::TabDelegateSyncAdapter; |
| 82 using suggestions::ImageDecoderImpl; | 84 using suggestions::ImageDecoderImpl; |
| 83 using syncer::SyncService; | 85 using syncer::SyncService; |
| 86 using translate::LanguageModel; |
| 84 | 87 |
| 85 namespace { | 88 namespace { |
| 86 | 89 |
| 87 // Clear the tasks that can be scheduled by running services. | 90 // Clear the tasks that can be scheduled by running services. |
| 88 void ClearScheduledTasks() { | 91 void ClearScheduledTasks() { |
| 89 #if defined(OS_ANDROID) | 92 #if defined(OS_ANDROID) |
| 90 NTPSnippetsLauncher::Get()->Unschedule(); | 93 NTPSnippetsLauncher::Get()->Unschedule(); |
| 91 #endif // OS_ANDROID | 94 #endif // OS_ANDROID |
| 92 } | 95 } |
| 93 | 96 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 118 auto provider = base::MakeUnique<PhysicalWebPageSuggestionsProvider>( | 121 auto provider = base::MakeUnique<PhysicalWebPageSuggestionsProvider>( |
| 119 service, category_factory); | 122 service, category_factory); |
| 120 service->RegisterProvider(std::move(provider)); | 123 service->RegisterProvider(std::move(provider)); |
| 121 } | 124 } |
| 122 #endif // OS_ANDROID | 125 #endif // OS_ANDROID |
| 123 | 126 |
| 124 void RegisterArticleProvider(SigninManagerBase* signin_manager, | 127 void RegisterArticleProvider(SigninManagerBase* signin_manager, |
| 125 OAuth2TokenService* token_service, | 128 OAuth2TokenService* token_service, |
| 126 ContentSuggestionsService* service, | 129 ContentSuggestionsService* service, |
| 127 CategoryFactory* category_factory, | 130 CategoryFactory* category_factory, |
| 131 LanguageModel* language_model, |
| 128 PrefService* pref_service, | 132 PrefService* pref_service, |
| 129 Profile* profile) { | 133 Profile* profile) { |
| 130 scoped_refptr<net::URLRequestContextGetter> request_context = | 134 scoped_refptr<net::URLRequestContextGetter> request_context = |
| 131 content::BrowserContext::GetDefaultStoragePartition(profile) | 135 content::BrowserContext::GetDefaultStoragePartition(profile) |
| 132 ->GetURLRequestContext(); | 136 ->GetURLRequestContext(); |
| 133 | 137 |
| 134 NTPSnippetsScheduler* scheduler = nullptr; | 138 NTPSnippetsScheduler* scheduler = nullptr; |
| 135 #if defined(OS_ANDROID) | 139 #if defined(OS_ANDROID) |
| 136 scheduler = NTPSnippetsLauncher::Get(); | 140 scheduler = NTPSnippetsLauncher::Get(); |
| 137 #endif // OS_ANDROID | 141 #endif // OS_ANDROID |
| 138 base::FilePath database_dir( | 142 base::FilePath database_dir( |
| 139 profile->GetPath().Append(ntp_snippets::kDatabaseFolder)); | 143 profile->GetPath().Append(ntp_snippets::kDatabaseFolder)); |
| 140 scoped_refptr<base::SequencedTaskRunner> task_runner = | 144 scoped_refptr<base::SequencedTaskRunner> task_runner = |
| 141 BrowserThread::GetBlockingPool() | 145 BrowserThread::GetBlockingPool() |
| 142 ->GetSequencedTaskRunnerWithShutdownBehavior( | 146 ->GetSequencedTaskRunnerWithShutdownBehavior( |
| 143 base::SequencedWorkerPool::GetSequenceToken(), | 147 base::SequencedWorkerPool::GetSequenceToken(), |
| 144 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN); | 148 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN); |
| 145 bool is_stable_channel = | 149 bool is_stable_channel = |
| 146 chrome::GetChannel() == version_info::Channel::STABLE; | 150 chrome::GetChannel() == version_info::Channel::STABLE; |
| 147 auto provider = base::MakeUnique<NTPSnippetsService>( | 151 auto provider = base::MakeUnique<NTPSnippetsService>( |
| 148 service, service->category_factory(), pref_service, | 152 service, service->category_factory(), pref_service, |
| 149 g_browser_process->GetApplicationLocale(), service->user_classifier(), | 153 g_browser_process->GetApplicationLocale(), service->user_classifier(), |
| 150 scheduler, | 154 scheduler, base::MakeUnique<NTPSnippetsFetcher>( |
| 151 base::MakeUnique<NTPSnippetsFetcher>( | 155 signin_manager, token_service, request_context, |
| 152 signin_manager, token_service, request_context, pref_service, | 156 pref_service, category_factory, language_model, |
| 153 category_factory, base::Bind(&safe_json::SafeJsonParser::Parse), | 157 base::Bind(&safe_json::SafeJsonParser::Parse), |
| 154 is_stable_channel ? google_apis::GetAPIKey() | 158 is_stable_channel ? google_apis::GetAPIKey() |
| 155 : google_apis::GetNonStableAPIKey()), | 159 : google_apis::GetNonStableAPIKey()), |
| 156 base::MakeUnique<ImageFetcherImpl>(base::MakeUnique<ImageDecoderImpl>(), | 160 base::MakeUnique<ImageFetcherImpl>(base::MakeUnique<ImageDecoderImpl>(), |
| 157 request_context.get()), | 161 request_context.get()), |
| 158 base::MakeUnique<ImageDecoderImpl>(), | 162 base::MakeUnique<ImageDecoderImpl>(), |
| 159 base::MakeUnique<NTPSnippetsDatabase>(database_dir, task_runner), | 163 base::MakeUnique<NTPSnippetsDatabase>(database_dir, task_runner), |
| 160 base::MakeUnique<NTPSnippetsStatusService>(signin_manager, pref_service)); | 164 base::MakeUnique<NTPSnippetsStatusService>(signin_manager, pref_service)); |
| 161 service->set_ntp_snippets_service(provider.get()); | 165 service->set_ntp_snippets_service(provider.get()); |
| 162 service->RegisterProvider(std::move(provider)); | 166 service->RegisterProvider(std::move(provider)); |
| 163 } | 167 } |
| 164 | 168 |
| 165 void RegisterForeignSessionsProvider(SyncService* sync_service, | 169 void RegisterForeignSessionsProvider(SyncService* sync_service, |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 new OfflinePageProxy(offline_page_model)); | 246 new OfflinePageProxy(offline_page_model)); |
| 243 #endif // OS_ANDROID | 247 #endif // OS_ANDROID |
| 244 BookmarkModel* bookmark_model = | 248 BookmarkModel* bookmark_model = |
| 245 BookmarkModelFactory::GetForBrowserContext(profile); | 249 BookmarkModelFactory::GetForBrowserContext(profile); |
| 246 SigninManagerBase* signin_manager = | 250 SigninManagerBase* signin_manager = |
| 247 SigninManagerFactory::GetForProfile(profile); | 251 SigninManagerFactory::GetForProfile(profile); |
| 248 OAuth2TokenService* token_service = | 252 OAuth2TokenService* token_service = |
| 249 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); | 253 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); |
| 250 SyncService* sync_service = | 254 SyncService* sync_service = |
| 251 ProfileSyncServiceFactory::GetSyncServiceForBrowserContext(profile); | 255 ProfileSyncServiceFactory::GetSyncServiceForBrowserContext(profile); |
| 256 LanguageModel* language_model = |
| 257 LanguageModelFactory::GetInstance()->GetForBrowserContext(profile); |
| 252 | 258 |
| 253 #if defined(OS_ANDROID) | 259 #if defined(OS_ANDROID) |
| 254 if (base::FeatureList::IsEnabled( | 260 if (base::FeatureList::IsEnabled( |
| 255 ntp_snippets::kRecentOfflineTabSuggestionsFeature)) { | 261 ntp_snippets::kRecentOfflineTabSuggestionsFeature)) { |
| 256 RegisterRecentTabProvider(offline_page_proxy, service, category_factory, | 262 RegisterRecentTabProvider(offline_page_proxy, service, category_factory, |
| 257 pref_service); | 263 pref_service); |
| 258 } | 264 } |
| 259 #endif // OS_ANDROID | 265 #endif // OS_ANDROID |
| 260 | 266 |
| 261 // |bookmark_model| can be null in tests. | 267 // |bookmark_model| can be null in tests. |
| 262 if (base::FeatureList::IsEnabled(ntp_snippets::kBookmarkSuggestionsFeature) && | 268 if (base::FeatureList::IsEnabled(ntp_snippets::kBookmarkSuggestionsFeature) && |
| 263 bookmark_model) { | 269 bookmark_model) { |
| 264 RegisterBookmarkProvider(bookmark_model, service, category_factory, | 270 RegisterBookmarkProvider(bookmark_model, service, category_factory, |
| 265 pref_service); | 271 pref_service); |
| 266 } | 272 } |
| 267 | 273 |
| 268 #if defined(OS_ANDROID) | 274 #if defined(OS_ANDROID) |
| 269 if (base::FeatureList::IsEnabled( | 275 if (base::FeatureList::IsEnabled( |
| 270 ntp_snippets::kPhysicalWebPageSuggestionsFeature)) { | 276 ntp_snippets::kPhysicalWebPageSuggestionsFeature)) { |
| 271 RegisterPhysicalWebPageProvider(service, category_factory); | 277 RegisterPhysicalWebPageProvider(service, category_factory); |
| 272 } | 278 } |
| 273 #endif // OS_ANDROID | 279 #endif // OS_ANDROID |
| 274 | 280 |
| 275 if (base::FeatureList::IsEnabled(ntp_snippets::kArticleSuggestionsFeature)) { | 281 if (base::FeatureList::IsEnabled(ntp_snippets::kArticleSuggestionsFeature)) { |
| 276 RegisterArticleProvider(signin_manager, token_service, service, | 282 RegisterArticleProvider(signin_manager, token_service, service, |
| 277 category_factory, pref_service, profile); | 283 category_factory, language_model, pref_service, |
| 284 profile); |
| 278 } | 285 } |
| 279 | 286 |
| 280 if (base::FeatureList::IsEnabled( | 287 if (base::FeatureList::IsEnabled( |
| 281 ntp_snippets::kForeignSessionsSuggestionsFeature)) { | 288 ntp_snippets::kForeignSessionsSuggestionsFeature)) { |
| 282 RegisterForeignSessionsProvider(sync_service, service, category_factory, | 289 RegisterForeignSessionsProvider(sync_service, service, category_factory, |
| 283 pref_service); | 290 pref_service); |
| 284 } | 291 } |
| 285 | 292 |
| 286 return service; | 293 return service; |
| 287 } | 294 } |
| OLD | NEW |