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

Side by Side Diff: chrome/browser/ntp_snippets/content_suggestions_service_factory.cc

Issue 2774663002: [Remote suggestions] Refactor the scheduler (Closed)
Patch Set: Marc's nits #2 Created 3 years, 8 months 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
« no previous file with comments | « no previous file | chrome/browser/prefs/browser_prefs.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 21 matching lines...) Expand all
32 #include "components/keyed_service/core/service_access_type.h" 32 #include "components/keyed_service/core/service_access_type.h"
33 #include "components/ntp_snippets/bookmarks/bookmark_suggestions_provider.h" 33 #include "components/ntp_snippets/bookmarks/bookmark_suggestions_provider.h"
34 #include "components/ntp_snippets/category_rankers/category_ranker.h" 34 #include "components/ntp_snippets/category_rankers/category_ranker.h"
35 #include "components/ntp_snippets/content_suggestions_service.h" 35 #include "components/ntp_snippets/content_suggestions_service.h"
36 #include "components/ntp_snippets/features.h" 36 #include "components/ntp_snippets/features.h"
37 #include "components/ntp_snippets/ntp_snippets_constants.h" 37 #include "components/ntp_snippets/ntp_snippets_constants.h"
38 #include "components/ntp_snippets/remote/persistent_scheduler.h" 38 #include "components/ntp_snippets/remote/persistent_scheduler.h"
39 #include "components/ntp_snippets/remote/remote_suggestions_database.h" 39 #include "components/ntp_snippets/remote/remote_suggestions_database.h"
40 #include "components/ntp_snippets/remote/remote_suggestions_fetcher.h" 40 #include "components/ntp_snippets/remote/remote_suggestions_fetcher.h"
41 #include "components/ntp_snippets/remote/remote_suggestions_provider_impl.h" 41 #include "components/ntp_snippets/remote/remote_suggestions_provider_impl.h"
42 #include "components/ntp_snippets/remote/remote_suggestions_scheduler_impl.h"
42 #include "components/ntp_snippets/remote/remote_suggestions_status_service.h" 43 #include "components/ntp_snippets/remote/remote_suggestions_status_service.h"
43 #include "components/ntp_snippets/remote/scheduling_remote_suggestions_provider. h"
44 #include "components/ntp_snippets/sessions/foreign_sessions_suggestions_provider .h" 44 #include "components/ntp_snippets/sessions/foreign_sessions_suggestions_provider .h"
45 #include "components/ntp_snippets/sessions/tab_delegate_sync_adapter.h" 45 #include "components/ntp_snippets/sessions/tab_delegate_sync_adapter.h"
46 #include "components/ntp_snippets/user_classifier.h"
46 #include "components/prefs/pref_service.h" 47 #include "components/prefs/pref_service.h"
47 #include "components/safe_json/safe_json_parser.h" 48 #include "components/safe_json/safe_json_parser.h"
48 #include "components/signin/core/browser/profile_oauth2_token_service.h" 49 #include "components/signin/core/browser/profile_oauth2_token_service.h"
49 #include "components/signin/core/browser/signin_manager.h" 50 #include "components/signin/core/browser/signin_manager.h"
50 #include "components/translate/core/browser/language_model.h" 51 #include "components/translate/core/browser/language_model.h"
51 #include "components/version_info/version_info.h" 52 #include "components/version_info/version_info.h"
52 #include "content/public/browser/browser_context.h" 53 #include "content/public/browser/browser_context.h"
53 #include "content/public/browser/browser_thread.h" 54 #include "content/public/browser/browser_thread.h"
54 #include "content/public/browser/storage_partition.h" 55 #include "content/public/browser/storage_partition.h"
55 #include "google_apis/google_api_keys.h" 56 #include "google_apis/google_api_keys.h"
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 using image_fetcher::ImageFetcherImpl; 89 using image_fetcher::ImageFetcherImpl;
89 using ntp_snippets::BookmarkSuggestionsProvider; 90 using ntp_snippets::BookmarkSuggestionsProvider;
90 using ntp_snippets::CategoryRanker; 91 using ntp_snippets::CategoryRanker;
91 using ntp_snippets::ContentSuggestionsService; 92 using ntp_snippets::ContentSuggestionsService;
92 using ntp_snippets::ForeignSessionsSuggestionsProvider; 93 using ntp_snippets::ForeignSessionsSuggestionsProvider;
93 using ntp_snippets::GetFetchEndpoint; 94 using ntp_snippets::GetFetchEndpoint;
94 using ntp_snippets::PersistentScheduler; 95 using ntp_snippets::PersistentScheduler;
95 using ntp_snippets::RemoteSuggestionsDatabase; 96 using ntp_snippets::RemoteSuggestionsDatabase;
96 using ntp_snippets::RemoteSuggestionsFetcher; 97 using ntp_snippets::RemoteSuggestionsFetcher;
97 using ntp_snippets::RemoteSuggestionsProviderImpl; 98 using ntp_snippets::RemoteSuggestionsProviderImpl;
99 using ntp_snippets::RemoteSuggestionsSchedulerImpl;
98 using ntp_snippets::RemoteSuggestionsStatusService; 100 using ntp_snippets::RemoteSuggestionsStatusService;
99 using ntp_snippets::SchedulingRemoteSuggestionsProvider;
100 using ntp_snippets::TabDelegateSyncAdapter; 101 using ntp_snippets::TabDelegateSyncAdapter;
102 using ntp_snippets::UserClassifier;
101 using suggestions::ImageDecoderImpl; 103 using suggestions::ImageDecoderImpl;
102 using syncer::SyncService; 104 using syncer::SyncService;
103 using translate::LanguageModel; 105 using translate::LanguageModel;
104 106
105 // For now, ContentSuggestionsService must only be instantiated on Android. 107 // For now, ContentSuggestionsService must only be instantiated on Android.
106 // See also crbug.com/688366. 108 // See also crbug.com/688366.
107 #if defined(OS_ANDROID) 109 #if defined(OS_ANDROID)
108 #define CONTENT_SUGGESTIONS_ENABLED 1 110 #define CONTENT_SUGGESTIONS_ENABLED 1
109 #else 111 #else
110 #define CONTENT_SUGGESTIONS_ENABLED 0 112 #define CONTENT_SUGGESTIONS_ENABLED 0
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 service, physical_web_data_source, pref_service); 176 service, physical_web_data_source, pref_service);
175 service->RegisterProvider(std::move(provider)); 177 service->RegisterProvider(std::move(provider));
176 } 178 }
177 179
178 #endif // OS_ANDROID 180 #endif // OS_ANDROID
179 181
180 void RegisterArticleProvider(SigninManagerBase* signin_manager, 182 void RegisterArticleProvider(SigninManagerBase* signin_manager,
181 OAuth2TokenService* token_service, 183 OAuth2TokenService* token_service,
182 ContentSuggestionsService* service, 184 ContentSuggestionsService* service,
183 LanguageModel* language_model, 185 LanguageModel* language_model,
186 UserClassifier* user_classifier,
184 PrefService* pref_service, 187 PrefService* pref_service,
185 Profile* profile) { 188 Profile* profile) {
186 scoped_refptr<net::URLRequestContextGetter> request_context = 189 scoped_refptr<net::URLRequestContextGetter> request_context =
187 content::BrowserContext::GetDefaultStoragePartition(profile) 190 content::BrowserContext::GetDefaultStoragePartition(profile)
188 ->GetURLRequestContext(); 191 ->GetURLRequestContext();
189 192
190 base::FilePath database_dir( 193 base::FilePath database_dir(
191 profile->GetPath().Append(ntp_snippets::kDatabaseFolder)); 194 profile->GetPath().Append(ntp_snippets::kDatabaseFolder));
192 scoped_refptr<base::SequencedTaskRunner> task_runner = 195 scoped_refptr<base::SequencedTaskRunner> task_runner =
193 BrowserThread::GetBlockingPool() 196 BrowserThread::GetBlockingPool()
194 ->GetSequencedTaskRunnerWithShutdownBehavior( 197 ->GetSequencedTaskRunnerWithShutdownBehavior(
195 base::SequencedWorkerPool::GetSequenceToken(), 198 base::SequencedWorkerPool::GetSequenceToken(),
196 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN); 199 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN);
197 std::string api_key; 200 std::string api_key;
198 // The API is private. If we don't have the official API key, don't even try. 201 // The API is private. If we don't have the official API key, don't even try.
199 if (google_apis::IsGoogleChromeAPIKeyUsed()) { 202 if (google_apis::IsGoogleChromeAPIKeyUsed()) {
200 bool is_stable_channel = 203 bool is_stable_channel =
201 chrome::GetChannel() == version_info::Channel::STABLE; 204 chrome::GetChannel() == version_info::Channel::STABLE;
202 api_key = is_stable_channel ? google_apis::GetAPIKey() 205 api_key = is_stable_channel ? google_apis::GetAPIKey()
203 : google_apis::GetNonStableAPIKey(); 206 : google_apis::GetNonStableAPIKey();
204 } 207 }
205 auto suggestions_fetcher = base::MakeUnique<RemoteSuggestionsFetcher>( 208 auto suggestions_fetcher = base::MakeUnique<RemoteSuggestionsFetcher>(
206 signin_manager, token_service, request_context, pref_service, 209 signin_manager, token_service, request_context, pref_service,
207 language_model, base::Bind(&safe_json::SafeJsonParser::Parse), 210 language_model, base::Bind(&safe_json::SafeJsonParser::Parse),
208 GetFetchEndpoint(chrome::GetChannel()), api_key, 211 GetFetchEndpoint(chrome::GetChannel()), api_key, user_classifier);
209 service->user_classifier());
210 auto provider = base::MakeUnique<RemoteSuggestionsProviderImpl>( 212 auto provider = base::MakeUnique<RemoteSuggestionsProviderImpl>(
211 service, pref_service, g_browser_process->GetApplicationLocale(), 213 service, pref_service, g_browser_process->GetApplicationLocale(),
212 service->category_ranker(), std::move(suggestions_fetcher), 214 service->category_ranker(), service->remote_suggestions_scheduler(),
215 std::move(suggestions_fetcher),
213 base::MakeUnique<ImageFetcherImpl>(base::MakeUnique<ImageDecoderImpl>(), 216 base::MakeUnique<ImageFetcherImpl>(base::MakeUnique<ImageDecoderImpl>(),
214 request_context.get()), 217 request_context.get()),
215 base::MakeUnique<RemoteSuggestionsDatabase>(database_dir, task_runner), 218 base::MakeUnique<RemoteSuggestionsDatabase>(database_dir, task_runner),
216 base::MakeUnique<RemoteSuggestionsStatusService>(signin_manager, 219 base::MakeUnique<RemoteSuggestionsStatusService>(signin_manager,
217 pref_service)); 220 pref_service));
218 221
219 PersistentScheduler* scheduler = nullptr; 222 service->remote_suggestions_scheduler()->SetProvider(provider.get());
220 #if defined(OS_ANDROID) 223 service->set_remote_suggestions_provider(provider.get());
221 scheduler = NTPSnippetsLauncher::Get(); 224 service->RegisterProvider(std::move(provider));
222 #endif // OS_ANDROID
223
224 RemoteSuggestionsProviderImpl* provider_raw = provider.get();
225 auto scheduling_provider =
226 base::MakeUnique<SchedulingRemoteSuggestionsProvider>(
227 service, std::move(provider), scheduler, service->user_classifier(),
228 pref_service, g_browser_process->local_state(),
229 base::MakeUnique<base::DefaultClock>());
230 provider_raw->SetRemoteSuggestionsScheduler(scheduling_provider.get());
231 service->set_remote_suggestions_provider(scheduling_provider.get());
232 service->set_remote_suggestions_scheduler(scheduling_provider.get());
233 service->RegisterProvider(std::move(scheduling_provider));
234 } 225 }
235 226
236 void RegisterForeignSessionsProvider(SyncService* sync_service, 227 void RegisterForeignSessionsProvider(SyncService* sync_service,
237 ContentSuggestionsService* service, 228 ContentSuggestionsService* service,
238 PrefService* pref_service) { 229 PrefService* pref_service) {
239 std::unique_ptr<TabDelegateSyncAdapter> sync_adapter = 230 std::unique_ptr<TabDelegateSyncAdapter> sync_adapter =
240 base::MakeUnique<TabDelegateSyncAdapter>(sync_service); 231 base::MakeUnique<TabDelegateSyncAdapter>(sync_service);
241 auto provider = base::MakeUnique<ForeignSessionsSuggestionsProvider>( 232 auto provider = base::MakeUnique<ForeignSessionsSuggestionsProvider>(
242 service, std::move(sync_adapter), pref_service); 233 service, std::move(sync_adapter), pref_service);
243 service->RegisterProvider(std::move(provider)); 234 service->RegisterProvider(std::move(provider));
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
283 274
284 ContentSuggestionsServiceFactory::~ContentSuggestionsServiceFactory() = default; 275 ContentSuggestionsServiceFactory::~ContentSuggestionsServiceFactory() = default;
285 276
286 KeyedService* ContentSuggestionsServiceFactory::BuildServiceInstanceFor( 277 KeyedService* ContentSuggestionsServiceFactory::BuildServiceInstanceFor(
287 content::BrowserContext* context) const { 278 content::BrowserContext* context) const {
288 #if CONTENT_SUGGESTIONS_ENABLED 279 #if CONTENT_SUGGESTIONS_ENABLED
289 280
290 using State = ContentSuggestionsService::State; 281 using State = ContentSuggestionsService::State;
291 Profile* profile = Profile::FromBrowserContext(context); 282 Profile* profile = Profile::FromBrowserContext(context);
292 DCHECK(!profile->IsOffTheRecord()); 283 DCHECK(!profile->IsOffTheRecord());
284 PrefService* pref_service = profile->GetPrefs();
285
286 auto user_classifier = base::MakeUnique<UserClassifier>(
287 pref_service, base::MakeUnique<base::DefaultClock>());
288 auto* user_classifier_raw = user_classifier.get();
289
290 // Create the RemoteSuggestionsScheduler.
291 PersistentScheduler* persistent_scheduler = nullptr;
292 #if defined(OS_ANDROID)
293 persistent_scheduler = NTPSnippetsLauncher::Get();
294 #endif // OS_ANDROID
295 auto scheduler = base::MakeUnique<RemoteSuggestionsSchedulerImpl>(
296 persistent_scheduler, user_classifier_raw, pref_service,
297 g_browser_process->local_state(), base::MakeUnique<base::DefaultClock>());
293 298
294 // Create the ContentSuggestionsService. 299 // Create the ContentSuggestionsService.
295 SigninManagerBase* signin_manager = 300 SigninManagerBase* signin_manager =
296 SigninManagerFactory::GetForProfile(profile); 301 SigninManagerFactory::GetForProfile(profile);
297 HistoryService* history_service = HistoryServiceFactory::GetForProfile( 302 HistoryService* history_service = HistoryServiceFactory::GetForProfile(
298 profile, ServiceAccessType::EXPLICIT_ACCESS); 303 profile, ServiceAccessType::EXPLICIT_ACCESS);
299 PrefService* pref_service = profile->GetPrefs();
300 std::unique_ptr<CategoryRanker> category_ranker = 304 std::unique_ptr<CategoryRanker> category_ranker =
301 ntp_snippets::BuildSelectedCategoryRanker( 305 ntp_snippets::BuildSelectedCategoryRanker(
302 pref_service, base::MakeUnique<base::DefaultClock>()); 306 pref_service, base::MakeUnique<base::DefaultClock>());
303 auto* service = new ContentSuggestionsService(State::ENABLED, signin_manager, 307 auto* service = new ContentSuggestionsService(
304 history_service, pref_service, 308 State::ENABLED, signin_manager, history_service, pref_service,
305 std::move(category_ranker)); 309 std::move(category_ranker), std::move(user_classifier),
310 std::move(scheduler));
306 311
307 #if defined(OS_ANDROID) 312 #if defined(OS_ANDROID)
308 OfflinePageModel* offline_page_model = 313 OfflinePageModel* offline_page_model =
309 OfflinePageModelFactory::GetForBrowserContext(profile); 314 OfflinePageModelFactory::GetForBrowserContext(profile);
310 RequestCoordinator* request_coordinator = 315 RequestCoordinator* request_coordinator =
311 RequestCoordinatorFactory::GetForBrowserContext(profile); 316 RequestCoordinatorFactory::GetForBrowserContext(profile);
312 DownloadManager* download_manager = 317 DownloadManager* download_manager =
313 content::BrowserContext::GetDownloadManager(profile); 318 content::BrowserContext::GetDownloadManager(profile);
314 DownloadService* download_service = 319 DownloadService* download_service =
315 DownloadServiceFactory::GetForBrowserContext(profile); 320 DownloadServiceFactory::GetForBrowserContext(profile);
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
352 357
353 #if defined(OS_ANDROID) 358 #if defined(OS_ANDROID)
354 if (IsPhysicalWebPageProviderEnabled()) { 359 if (IsPhysicalWebPageProviderEnabled()) {
355 RegisterPhysicalWebPageProvider(service, physical_web_data_source, 360 RegisterPhysicalWebPageProvider(service, physical_web_data_source,
356 pref_service); 361 pref_service);
357 } 362 }
358 #endif // OS_ANDROID 363 #endif // OS_ANDROID
359 364
360 if (base::FeatureList::IsEnabled(ntp_snippets::kArticleSuggestionsFeature)) { 365 if (base::FeatureList::IsEnabled(ntp_snippets::kArticleSuggestionsFeature)) {
361 RegisterArticleProvider(signin_manager, token_service, service, 366 RegisterArticleProvider(signin_manager, token_service, service,
362 language_model, pref_service, profile); 367 language_model, user_classifier_raw, pref_service,
368 profile);
363 } 369 }
364 370
365 if (base::FeatureList::IsEnabled( 371 if (base::FeatureList::IsEnabled(
366 ntp_snippets::kForeignSessionsSuggestionsFeature)) { 372 ntp_snippets::kForeignSessionsSuggestionsFeature)) {
367 RegisterForeignSessionsProvider(sync_service, service, pref_service); 373 RegisterForeignSessionsProvider(sync_service, service, pref_service);
368 } 374 }
369 375
370 return service; 376 return service;
371 377
372 #else 378 #else
373 return nullptr; 379 return nullptr;
374 #endif // CONTENT_SUGGESTIONS_ENABLED 380 #endif // CONTENT_SUGGESTIONS_ENABLED
375 } 381 }
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/prefs/browser_prefs.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698