Chromium Code Reviews| Index: chrome/browser/ntp_snippets/content_suggestions_service_factory.cc |
| diff --git a/chrome/browser/ntp_snippets/content_suggestions_service_factory.cc b/chrome/browser/ntp_snippets/content_suggestions_service_factory.cc |
| index 6e16f96b0f7445ceb0ba854d009f72c761aa8c4b..bc4b8c9af9e93a8ac3f76d5b552bc58f7dee32a9 100644 |
| --- a/chrome/browser/ntp_snippets/content_suggestions_service_factory.cc |
| +++ b/chrome/browser/ntp_snippets/content_suggestions_service_factory.cc |
| @@ -5,18 +5,57 @@ |
| #include "chrome/browser/ntp_snippets/content_suggestions_service_factory.h" |
| #include "base/feature_list.h" |
| +#include "base/files/file_path.h" |
| +#include "base/memory/ptr_util.h" |
| #include "base/memory/singleton.h" |
| +#include "chrome/browser/browser_process.h" |
| #include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/search/suggestions/image_decoder_impl.h" |
| +#include "chrome/browser/search/suggestions/suggestions_service_factory.h" |
| +#include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| +#include "chrome/browser/signin/signin_manager_factory.h" |
| +#include "chrome/common/channel_info.h" |
| #include "chrome/common/pref_names.h" |
| +#include "components/image_fetcher/image_decoder.h" |
| +#include "components/image_fetcher/image_fetcher.h" |
| +#include "components/image_fetcher/image_fetcher_impl.h" |
| #include "components/keyed_service/content/browser_context_dependency_manager.h" |
| #include "components/ntp_snippets/content_suggestions_service.h" |
| +#include "components/ntp_snippets/ntp_snippets_constants.h" |
| +#include "components/ntp_snippets/ntp_snippets_database.h" |
| +#include "components/ntp_snippets/ntp_snippets_fetcher.h" |
| +#include "components/ntp_snippets/ntp_snippets_scheduler.h" |
| +#include "components/ntp_snippets/ntp_snippets_service.h" |
| +#include "components/ntp_snippets/ntp_snippets_status_service.h" |
| #include "components/prefs/pref_service.h" |
| +#include "components/safe_json/safe_json_parser.h" |
| +#include "components/signin/core/browser/profile_oauth2_token_service.h" |
| +#include "components/signin/core/browser/signin_manager.h" |
| +#include "components/version_info/version_info.h" |
| #include "content/public/browser/browser_context.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/browser/storage_partition.h" |
| +#include "net/url_request/url_request_context_getter.h" |
| #if defined(OS_ANDROID) |
| #include "chrome/browser/android/chrome_feature_list.h" |
| +#include "chrome/browser/android/ntp/ntp_snippets_launcher.h" |
| +#include "chrome/browser/android/offline_pages/offline_page_model_factory.h" |
| +#include "components/ntp_snippets/offline_pages/offline_page_suggestions_provider.h" |
| +#include "components/offline_pages/offline_page_model.h" |
| #endif // OS_ANDROID |
| +using content::BrowserThread; |
| +using image_fetcher::ImageFetcherImpl; |
| +using suggestions::ImageDecoderImpl; |
|
Marc Treib
2016/08/03 11:53:23
nit: keep these alphabetical
Philipp Keck
2016/08/03 12:28:24
Done.
|
| +using suggestions::SuggestionsService; |
| +using suggestions::SuggestionsServiceFactory; |
| +using ntp_snippets::ContentSuggestionsService; |
| +using ntp_snippets::NTPSnippetsService; |
| +using ntp_snippets::OfflinePageSuggestionsProvider; |
| +using offline_pages::OfflinePageModel; |
| +using offline_pages::OfflinePageModelFactory; |
| + |
| // static |
| ContentSuggestionsServiceFactory* |
| ContentSuggestionsServiceFactory::GetInstance() { |
| @@ -24,10 +63,10 @@ ContentSuggestionsServiceFactory::GetInstance() { |
| } |
| // static |
| -ntp_snippets::ContentSuggestionsService* |
| -ContentSuggestionsServiceFactory::GetForProfile(Profile* profile) { |
| +ContentSuggestionsService* ContentSuggestionsServiceFactory::GetForProfile( |
| + Profile* profile) { |
| DCHECK(!profile->IsOffTheRecord()); |
| - return static_cast<ntp_snippets::ContentSuggestionsService*>( |
| + return static_cast<ContentSuggestionsService*>( |
| GetInstance()->GetServiceForBrowserContext(profile, true)); |
| } |
| @@ -40,17 +79,73 @@ ContentSuggestionsServiceFactory::~ContentSuggestionsServiceFactory() {} |
| KeyedService* ContentSuggestionsServiceFactory::BuildServiceInstanceFor( |
| content::BrowserContext* context) const { |
| - using State = ntp_snippets::ContentSuggestionsService::State; |
| + using State = ContentSuggestionsService::State; |
| + Profile* profile = Profile::FromBrowserContext(context); |
| - // TODO(mvanouwerkerk): Move the enable logic into the service once we start |
| - // observing pref changes. |
| + // Create the ContentSuggestionsService. |
| State enabled = State::DISABLED; |
| #if defined(OS_ANDROID) |
| - // TODO(pke): Split that feature into suggestions overall and article |
| - // suggestions in particular. |
| if (base::FeatureList::IsEnabled(chrome::android::kNTPSnippetsFeature)) |
| enabled = State::ENABLED; |
| #endif // OS_ANDROID |
| + ContentSuggestionsService* service = new ContentSuggestionsService(enabled); |
| + if (enabled == State::DISABLED) |
| + return service; |
| + |
| +// Create the OfflinePageSuggestionsProvider. |
| +#if defined(OS_ANDROID) |
| + if (base::FeatureList::IsEnabled( |
| + chrome::android::kNTPOfflinePageSuggestionsFeature)) { |
| + OfflinePageModel* offline_page_model = |
| + OfflinePageModelFactory::GetForBrowserContext(profile); |
| + |
| + std::unique_ptr<OfflinePageSuggestionsProvider> |
| + offline_page_suggestions_provider = |
| + base::MakeUnique<OfflinePageSuggestionsProvider>( |
| + service, service->category_factory(), offline_page_model); |
| + service->RegisterProvider(std::move(offline_page_suggestions_provider)); |
| + } |
| +#endif // OS_ANDROID |
| + |
| + // Create the NTPSnippetsService (articles provider). |
| + SigninManagerBase* signin_manager = |
| + SigninManagerFactory::GetForProfile(profile); |
| + OAuth2TokenService* token_service = |
| + ProfileOAuth2TokenServiceFactory::GetForProfile(profile); |
| + scoped_refptr<net::URLRequestContextGetter> request_context = |
| + content::BrowserContext::GetDefaultStoragePartition(context) |
| + ->GetURLRequestContext(); |
| + SuggestionsService* suggestions_service = |
| + SuggestionsServiceFactory::GetForProfile(profile); |
| + ntp_snippets::NTPSnippetsScheduler* scheduler = nullptr; |
| +#if defined(OS_ANDROID) |
| + scheduler = NTPSnippetsLauncher::Get(); |
| +#endif // OS_ANDROID |
| + base::FilePath database_dir( |
| + profile->GetPath().Append(ntp_snippets::kDatabaseFolder)); |
| + scoped_refptr<base::SequencedTaskRunner> task_runner = |
| + BrowserThread::GetBlockingPool() |
| + ->GetSequencedTaskRunnerWithShutdownBehavior( |
| + base::SequencedWorkerPool::GetSequenceToken(), |
| + base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN); |
| + std::unique_ptr<NTPSnippetsService> ntp_snippets_service = |
| + base::MakeUnique<NTPSnippetsService>( |
| + service, service->category_factory(), profile->GetPrefs(), |
| + suggestions_service, g_browser_process->GetApplicationLocale(), |
| + scheduler, base::MakeUnique<ntp_snippets::NTPSnippetsFetcher>( |
| + signin_manager, token_service, request_context, |
| + profile->GetPrefs(), |
| + base::Bind(&safe_json::SafeJsonParser::Parse), |
| + chrome::GetChannel() == version_info::Channel::STABLE), |
| + base::MakeUnique<ImageFetcherImpl>( |
| + base::MakeUnique<ImageDecoderImpl>(), request_context.get()), |
| + base::MakeUnique<ImageDecoderImpl>(), |
| + base::MakeUnique<ntp_snippets::NTPSnippetsDatabase>(database_dir, |
| + task_runner), |
| + base::MakeUnique<ntp_snippets::NTPSnippetsStatusService>( |
|
Marc Treib
2016/08/03 11:53:23
nit: You already have a bunch of using declaration
Philipp Keck
2016/08/03 12:28:24
Done, and a couple more from ntp_snippets.
|
| + signin_manager, profile->GetPrefs())); |
| + service->set_ntp_snippets_service(ntp_snippets_service.get()); |
| + service->RegisterProvider(std::move(ntp_snippets_service)); |
| - return new ntp_snippets::ContentSuggestionsService(enabled); |
| + return service; |
| } |