Chromium Code Reviews| 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 "base/feature_list.h" | 7 #include "base/feature_list.h" |
| 8 #include "base/files/file_path.h" | |
| 9 #include "base/memory/ptr_util.h" | |
| 8 #include "base/memory/singleton.h" | 10 #include "base/memory/singleton.h" |
| 11 #include "chrome/browser/browser_process.h" | |
| 9 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/browser/search/suggestions/image_decoder_impl.h" | |
| 14 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" | |
| 15 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | |
| 16 #include "chrome/browser/signin/signin_manager_factory.h" | |
| 17 #include "chrome/common/channel_info.h" | |
| 10 #include "chrome/common/pref_names.h" | 18 #include "chrome/common/pref_names.h" |
| 19 #include "components/image_fetcher/image_decoder.h" | |
| 20 #include "components/image_fetcher/image_fetcher.h" | |
| 21 #include "components/image_fetcher/image_fetcher_impl.h" | |
| 11 #include "components/keyed_service/content/browser_context_dependency_manager.h" | 22 #include "components/keyed_service/content/browser_context_dependency_manager.h" |
| 12 #include "components/ntp_snippets/content_suggestions_service.h" | 23 #include "components/ntp_snippets/content_suggestions_service.h" |
| 24 #include "components/ntp_snippets/ntp_snippets_constants.h" | |
| 25 #include "components/ntp_snippets/ntp_snippets_database.h" | |
| 26 #include "components/ntp_snippets/ntp_snippets_fetcher.h" | |
| 27 #include "components/ntp_snippets/ntp_snippets_scheduler.h" | |
| 28 #include "components/ntp_snippets/ntp_snippets_service.h" | |
| 29 #include "components/ntp_snippets/ntp_snippets_status_service.h" | |
| 13 #include "components/prefs/pref_service.h" | 30 #include "components/prefs/pref_service.h" |
| 31 #include "components/safe_json/safe_json_parser.h" | |
| 32 #include "components/signin/core/browser/profile_oauth2_token_service.h" | |
| 33 #include "components/signin/core/browser/signin_manager.h" | |
| 34 #include "components/version_info/version_info.h" | |
| 14 #include "content/public/browser/browser_context.h" | 35 #include "content/public/browser/browser_context.h" |
| 36 #include "content/public/browser/browser_thread.h" | |
| 37 #include "content/public/browser/storage_partition.h" | |
| 38 #include "net/url_request/url_request_context_getter.h" | |
| 15 | 39 |
| 16 #if defined(OS_ANDROID) | 40 #if defined(OS_ANDROID) |
| 17 #include "chrome/browser/android/chrome_feature_list.h" | 41 #include "chrome/browser/android/chrome_feature_list.h" |
| 42 #include "chrome/browser/android/ntp/ntp_snippets_launcher.h" | |
| 43 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" | |
| 44 #include "components/ntp_snippets/offline_pages/offline_page_suggestions_provide r.h" | |
| 45 #include "components/offline_pages/offline_page_model.h" | |
| 18 #endif // OS_ANDROID | 46 #endif // OS_ANDROID |
| 19 | 47 |
| 48 using content::BrowserThread; | |
| 49 using image_fetcher::ImageFetcherImpl; | |
| 50 using suggestions::ImageDecoderImpl; | |
|
Marc Treib
2016/08/03 11:53:23
nit: keep these alphabetical
Philipp Keck
2016/08/03 12:28:24
Done.
| |
| 51 using suggestions::SuggestionsService; | |
| 52 using suggestions::SuggestionsServiceFactory; | |
| 53 using ntp_snippets::ContentSuggestionsService; | |
| 54 using ntp_snippets::NTPSnippetsService; | |
| 55 using ntp_snippets::OfflinePageSuggestionsProvider; | |
| 56 using offline_pages::OfflinePageModel; | |
| 57 using offline_pages::OfflinePageModelFactory; | |
| 58 | |
| 20 // static | 59 // static |
| 21 ContentSuggestionsServiceFactory* | 60 ContentSuggestionsServiceFactory* |
| 22 ContentSuggestionsServiceFactory::GetInstance() { | 61 ContentSuggestionsServiceFactory::GetInstance() { |
| 23 return base::Singleton<ContentSuggestionsServiceFactory>::get(); | 62 return base::Singleton<ContentSuggestionsServiceFactory>::get(); |
| 24 } | 63 } |
| 25 | 64 |
| 26 // static | 65 // static |
| 27 ntp_snippets::ContentSuggestionsService* | 66 ContentSuggestionsService* ContentSuggestionsServiceFactory::GetForProfile( |
| 28 ContentSuggestionsServiceFactory::GetForProfile(Profile* profile) { | 67 Profile* profile) { |
| 29 DCHECK(!profile->IsOffTheRecord()); | 68 DCHECK(!profile->IsOffTheRecord()); |
| 30 return static_cast<ntp_snippets::ContentSuggestionsService*>( | 69 return static_cast<ContentSuggestionsService*>( |
| 31 GetInstance()->GetServiceForBrowserContext(profile, true)); | 70 GetInstance()->GetServiceForBrowserContext(profile, true)); |
| 32 } | 71 } |
| 33 | 72 |
| 34 ContentSuggestionsServiceFactory::ContentSuggestionsServiceFactory() | 73 ContentSuggestionsServiceFactory::ContentSuggestionsServiceFactory() |
| 35 : BrowserContextKeyedServiceFactory( | 74 : BrowserContextKeyedServiceFactory( |
| 36 "ContentSuggestionsService", | 75 "ContentSuggestionsService", |
| 37 BrowserContextDependencyManager::GetInstance()) {} | 76 BrowserContextDependencyManager::GetInstance()) {} |
| 38 | 77 |
| 39 ContentSuggestionsServiceFactory::~ContentSuggestionsServiceFactory() {} | 78 ContentSuggestionsServiceFactory::~ContentSuggestionsServiceFactory() {} |
| 40 | 79 |
| 41 KeyedService* ContentSuggestionsServiceFactory::BuildServiceInstanceFor( | 80 KeyedService* ContentSuggestionsServiceFactory::BuildServiceInstanceFor( |
| 42 content::BrowserContext* context) const { | 81 content::BrowserContext* context) const { |
| 43 using State = ntp_snippets::ContentSuggestionsService::State; | 82 using State = ContentSuggestionsService::State; |
| 83 Profile* profile = Profile::FromBrowserContext(context); | |
| 44 | 84 |
| 45 // TODO(mvanouwerkerk): Move the enable logic into the service once we start | 85 // Create the ContentSuggestionsService. |
| 46 // observing pref changes. | |
| 47 State enabled = State::DISABLED; | 86 State enabled = State::DISABLED; |
| 48 #if defined(OS_ANDROID) | 87 #if defined(OS_ANDROID) |
| 49 // TODO(pke): Split that feature into suggestions overall and article | |
| 50 // suggestions in particular. | |
| 51 if (base::FeatureList::IsEnabled(chrome::android::kNTPSnippetsFeature)) | 88 if (base::FeatureList::IsEnabled(chrome::android::kNTPSnippetsFeature)) |
| 52 enabled = State::ENABLED; | 89 enabled = State::ENABLED; |
| 53 #endif // OS_ANDROID | 90 #endif // OS_ANDROID |
| 91 ContentSuggestionsService* service = new ContentSuggestionsService(enabled); | |
| 92 if (enabled == State::DISABLED) | |
| 93 return service; | |
| 54 | 94 |
| 55 return new ntp_snippets::ContentSuggestionsService(enabled); | 95 // Create the OfflinePageSuggestionsProvider. |
| 96 #if defined(OS_ANDROID) | |
| 97 if (base::FeatureList::IsEnabled( | |
| 98 chrome::android::kNTPOfflinePageSuggestionsFeature)) { | |
| 99 OfflinePageModel* offline_page_model = | |
| 100 OfflinePageModelFactory::GetForBrowserContext(profile); | |
| 101 | |
| 102 std::unique_ptr<OfflinePageSuggestionsProvider> | |
| 103 offline_page_suggestions_provider = | |
| 104 base::MakeUnique<OfflinePageSuggestionsProvider>( | |
| 105 service, service->category_factory(), offline_page_model); | |
| 106 service->RegisterProvider(std::move(offline_page_suggestions_provider)); | |
| 107 } | |
| 108 #endif // OS_ANDROID | |
| 109 | |
| 110 // Create the NTPSnippetsService (articles provider). | |
| 111 SigninManagerBase* signin_manager = | |
| 112 SigninManagerFactory::GetForProfile(profile); | |
| 113 OAuth2TokenService* token_service = | |
| 114 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); | |
| 115 scoped_refptr<net::URLRequestContextGetter> request_context = | |
| 116 content::BrowserContext::GetDefaultStoragePartition(context) | |
| 117 ->GetURLRequestContext(); | |
| 118 SuggestionsService* suggestions_service = | |
| 119 SuggestionsServiceFactory::GetForProfile(profile); | |
| 120 ntp_snippets::NTPSnippetsScheduler* scheduler = nullptr; | |
| 121 #if defined(OS_ANDROID) | |
| 122 scheduler = NTPSnippetsLauncher::Get(); | |
| 123 #endif // OS_ANDROID | |
| 124 base::FilePath database_dir( | |
| 125 profile->GetPath().Append(ntp_snippets::kDatabaseFolder)); | |
| 126 scoped_refptr<base::SequencedTaskRunner> task_runner = | |
| 127 BrowserThread::GetBlockingPool() | |
| 128 ->GetSequencedTaskRunnerWithShutdownBehavior( | |
| 129 base::SequencedWorkerPool::GetSequenceToken(), | |
| 130 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN); | |
| 131 std::unique_ptr<NTPSnippetsService> ntp_snippets_service = | |
| 132 base::MakeUnique<NTPSnippetsService>( | |
| 133 service, service->category_factory(), profile->GetPrefs(), | |
| 134 suggestions_service, g_browser_process->GetApplicationLocale(), | |
| 135 scheduler, base::MakeUnique<ntp_snippets::NTPSnippetsFetcher>( | |
| 136 signin_manager, token_service, request_context, | |
| 137 profile->GetPrefs(), | |
| 138 base::Bind(&safe_json::SafeJsonParser::Parse), | |
| 139 chrome::GetChannel() == version_info::Channel::STABLE), | |
| 140 base::MakeUnique<ImageFetcherImpl>( | |
| 141 base::MakeUnique<ImageDecoderImpl>(), request_context.get()), | |
| 142 base::MakeUnique<ImageDecoderImpl>(), | |
| 143 base::MakeUnique<ntp_snippets::NTPSnippetsDatabase>(database_dir, | |
| 144 task_runner), | |
| 145 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.
| |
| 146 signin_manager, profile->GetPrefs())); | |
| 147 service->set_ntp_snippets_service(ntp_snippets_service.get()); | |
| 148 service->RegisterProvider(std::move(ntp_snippets_service)); | |
| 149 | |
| 150 return service; | |
| 56 } | 151 } |
| OLD | NEW |