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" | 8 #include "base/files/file_path.h" |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/memory/singleton.h" | 10 #include "base/memory/singleton.h" |
| 11 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 11 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
| 12 #include "chrome/browser/browser_process.h" | 12 #include "chrome/browser/browser_process.h" |
| 13 #include "chrome/browser/history/history_service_factory.h" | 13 #include "chrome/browser/history/history_service_factory.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/browser/search/suggestions/image_decoder_impl.h" | 15 #include "chrome/browser/search/suggestions/image_decoder_impl.h" |
| 16 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" | 16 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" |
| 17 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 17 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 18 #include "chrome/browser/signin/signin_manager_factory.h" | 18 #include "chrome/browser/signin/signin_manager_factory.h" |
| 19 #include "chrome/common/channel_info.h" | 19 #include "chrome/common/channel_info.h" |
| 20 #include "components/bookmarks/browser/bookmark_model.h" | 20 #include "components/bookmarks/browser/bookmark_model.h" |
| 21 #include "components/image_fetcher/image_decoder.h" | 21 #include "components/image_fetcher/image_decoder.h" |
| 22 #include "components/image_fetcher/image_fetcher.h" | 22 #include "components/image_fetcher/image_fetcher.h" |
| 23 #include "components/image_fetcher/image_fetcher_impl.h" | 23 #include "components/image_fetcher/image_fetcher_impl.h" |
| 24 #include "components/keyed_service/content/browser_context_dependency_manager.h" | 24 #include "components/keyed_service/content/browser_context_dependency_manager.h" |
| 25 #include "components/keyed_service/core/service_access_type.h" | 25 #include "components/keyed_service/core/service_access_type.h" |
| 26 #include "components/ntp_snippets/bookmarks/bookmark_suggestions_provider.h" | 26 #include "components/ntp_snippets/bookmarks/bookmark_suggestions_provider.h" |
| 27 #include "components/ntp_snippets/category_factory.h" | |
| 27 #include "components/ntp_snippets/content_suggestions_service.h" | 28 #include "components/ntp_snippets/content_suggestions_service.h" |
| 28 #include "components/ntp_snippets/features.h" | 29 #include "components/ntp_snippets/features.h" |
| 29 #include "components/ntp_snippets/ntp_snippets_constants.h" | 30 #include "components/ntp_snippets/ntp_snippets_constants.h" |
| 30 #include "components/ntp_snippets/ntp_snippets_database.h" | 31 #include "components/ntp_snippets/ntp_snippets_database.h" |
| 31 #include "components/ntp_snippets/ntp_snippets_fetcher.h" | 32 #include "components/ntp_snippets/ntp_snippets_fetcher.h" |
| 32 #include "components/ntp_snippets/ntp_snippets_scheduler.h" | 33 #include "components/ntp_snippets/ntp_snippets_scheduler.h" |
| 33 #include "components/ntp_snippets/ntp_snippets_service.h" | 34 #include "components/ntp_snippets/ntp_snippets_service.h" |
| 34 #include "components/ntp_snippets/ntp_snippets_status_service.h" | 35 #include "components/ntp_snippets/ntp_snippets_status_service.h" |
| 35 #include "components/prefs/pref_service.h" | 36 #include "components/prefs/pref_service.h" |
| 36 #include "components/safe_json/safe_json_parser.h" | 37 #include "components/safe_json/safe_json_parser.h" |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 54 using ntp_snippets::PhysicalWebPageSuggestionsProvider; | 55 using ntp_snippets::PhysicalWebPageSuggestionsProvider; |
| 55 using offline_pages::OfflinePageModel; | 56 using offline_pages::OfflinePageModel; |
| 56 using offline_pages::OfflinePageModelFactory; | 57 using offline_pages::OfflinePageModelFactory; |
| 57 #endif // OS_ANDROID | 58 #endif // OS_ANDROID |
| 58 | 59 |
| 59 using bookmarks::BookmarkModel; | 60 using bookmarks::BookmarkModel; |
| 60 using content::BrowserThread; | 61 using content::BrowserThread; |
| 61 using history::HistoryService; | 62 using history::HistoryService; |
| 62 using image_fetcher::ImageFetcherImpl; | 63 using image_fetcher::ImageFetcherImpl; |
| 63 using ntp_snippets::BookmarkSuggestionsProvider; | 64 using ntp_snippets::BookmarkSuggestionsProvider; |
| 65 using ntp_snippets::CategoryFactory; | |
| 64 using ntp_snippets::ContentSuggestionsService; | 66 using ntp_snippets::ContentSuggestionsService; |
| 65 using ntp_snippets::NTPSnippetsDatabase; | 67 using ntp_snippets::NTPSnippetsDatabase; |
| 66 using ntp_snippets::NTPSnippetsFetcher; | 68 using ntp_snippets::NTPSnippetsFetcher; |
| 67 using ntp_snippets::NTPSnippetsService; | 69 using ntp_snippets::NTPSnippetsService; |
| 68 using ntp_snippets::NTPSnippetsScheduler; | 70 using ntp_snippets::NTPSnippetsScheduler; |
| 69 using ntp_snippets::NTPSnippetsStatusService; | 71 using ntp_snippets::NTPSnippetsStatusService; |
| 70 using suggestions::ImageDecoderImpl; | 72 using suggestions::ImageDecoderImpl; |
| 71 using suggestions::SuggestionsService; | 73 using suggestions::SuggestionsService; |
| 72 using suggestions::SuggestionsServiceFactory; | 74 using suggestions::SuggestionsServiceFactory; |
| 73 | 75 |
| 74 namespace { | 76 namespace { |
| 75 | 77 |
| 76 // Clear the tasks that can be scheduled by running services. | 78 // Clear the tasks that can be scheduled by running services. |
| 77 void ClearScheduledTasks() { | 79 void ClearScheduledTasks() { |
| 78 #if defined(OS_ANDROID) | 80 #if defined(OS_ANDROID) |
| 79 NTPSnippetsLauncher::Get()->Unschedule(); | 81 NTPSnippetsLauncher::Get()->Unschedule(); |
| 80 #endif // OS_ANDROID | 82 #endif // OS_ANDROID |
| 81 } | 83 } |
| 82 | 84 |
| 85 void RegisterOfflinePageProvider(OfflinePageModel* offline_page_model, | |
| 86 ContentSuggestionsService* service, | |
| 87 CategoryFactory* category_factory, | |
| 88 PrefService* pref_service) { | |
| 89 bool recent_tabs_enabled = base::FeatureList::IsEnabled( | |
| 90 ntp_snippets::kRecentOfflineTabSuggestionsFeature); | |
| 91 bool downloads_enabled = | |
| 92 base::FeatureList::IsEnabled(ntp_snippets::kDownloadSuggestionsFeature); | |
| 93 bool download_manager_ui_enabled = | |
| 94 base::FeatureList::IsEnabled(chrome::android::kDownloadsUiFeature); | |
| 95 if (recent_tabs_enabled || downloads_enabled) { | |
|
Marc Treib
2016/08/25 13:51:54
Hm, so this method checks the features itself, but
vitaliii
2016/08/25 14:24:24
Done.
| |
| 96 std::unique_ptr<OfflinePageSuggestionsProvider> | |
| 97 offline_page_suggestions_provider = | |
| 98 base::MakeUnique<OfflinePageSuggestionsProvider>( | |
| 99 recent_tabs_enabled, downloads_enabled, | |
| 100 download_manager_ui_enabled, service, category_factory, | |
| 101 offline_page_model, pref_service); | |
| 102 service->RegisterProvider(std::move(offline_page_suggestions_provider)); | |
| 103 } | |
| 104 } | |
| 105 | |
| 106 void RegisterBookmarkProvider(BookmarkModel* bookmark_model, | |
| 107 ContentSuggestionsService* service, | |
| 108 CategoryFactory* category_factory, | |
| 109 PrefService* pref_service) { | |
| 110 std::unique_ptr<BookmarkSuggestionsProvider> bookmark_suggestions_provider = | |
| 111 base::MakeUnique<BookmarkSuggestionsProvider>( | |
| 112 service, category_factory, bookmark_model, pref_service); | |
| 113 service->RegisterProvider(std::move(bookmark_suggestions_provider)); | |
| 114 } | |
| 115 | |
| 116 void RegisterPhysicalWebPageProvider(ContentSuggestionsService* service, | |
| 117 CategoryFactory* category_factory) { | |
| 118 std::unique_ptr<PhysicalWebPageSuggestionsProvider> | |
| 119 physical_web_page_suggestions_provider = | |
| 120 base::MakeUnique<PhysicalWebPageSuggestionsProvider>( | |
| 121 service, category_factory); | |
| 122 service->RegisterProvider(std::move(physical_web_page_suggestions_provider)); | |
| 123 } | |
| 124 | |
| 125 void RegisterArticleProvider(SigninManagerBase* signin_manager, | |
| 126 OAuth2TokenService* token_service, | |
| 127 HistoryService* history_service, | |
| 128 SuggestionsService* suggestions_service, | |
| 129 ContentSuggestionsService* service, | |
| 130 CategoryFactory* category_factory, | |
| 131 PrefService* pref_service, | |
| 132 Profile* profile) { | |
| 133 scoped_refptr<net::URLRequestContextGetter> request_context = | |
| 134 content::BrowserContext::GetDefaultStoragePartition(profile) | |
| 135 ->GetURLRequestContext(); | |
| 136 | |
| 137 NTPSnippetsScheduler* scheduler = nullptr; | |
| 138 #if defined(OS_ANDROID) | |
| 139 scheduler = NTPSnippetsLauncher::Get(); | |
| 140 #endif // OS_ANDROID | |
| 141 base::FilePath database_dir( | |
| 142 profile->GetPath().Append(ntp_snippets::kDatabaseFolder)); | |
| 143 scoped_refptr<base::SequencedTaskRunner> task_runner = | |
| 144 BrowserThread::GetBlockingPool() | |
| 145 ->GetSequencedTaskRunnerWithShutdownBehavior( | |
| 146 base::SequencedWorkerPool::GetSequenceToken(), | |
| 147 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN); | |
| 148 bool is_stable_channel = | |
| 149 chrome::GetChannel() == version_info::Channel::STABLE; | |
| 150 std::unique_ptr<NTPSnippetsService> ntp_snippets_service = | |
| 151 base::MakeUnique<NTPSnippetsService>( | |
| 152 service, service->category_factory(), pref_service, history_service, | |
| 153 suggestions_service, g_browser_process->GetApplicationLocale(), | |
| 154 scheduler, | |
| 155 base::MakeUnique<NTPSnippetsFetcher>( | |
| 156 signin_manager, token_service, request_context, pref_service, | |
| 157 category_factory, base::Bind(&safe_json::SafeJsonParser::Parse), | |
| 158 is_stable_channel), | |
| 159 base::MakeUnique<ImageFetcherImpl>( | |
| 160 base::MakeUnique<ImageDecoderImpl>(), request_context.get()), | |
| 161 base::MakeUnique<ImageDecoderImpl>(), | |
| 162 base::MakeUnique<NTPSnippetsDatabase>(database_dir, task_runner), | |
| 163 base::MakeUnique<NTPSnippetsStatusService>(signin_manager, | |
| 164 pref_service)); | |
| 165 service->set_ntp_snippets_service(ntp_snippets_service.get()); | |
| 166 service->RegisterProvider(std::move(ntp_snippets_service)); | |
| 167 } | |
| 168 | |
| 83 } // namespace | 169 } // namespace |
| 84 | 170 |
| 85 // static | 171 // static |
| 86 ContentSuggestionsServiceFactory* | 172 ContentSuggestionsServiceFactory* |
| 87 ContentSuggestionsServiceFactory::GetInstance() { | 173 ContentSuggestionsServiceFactory::GetInstance() { |
| 88 return base::Singleton<ContentSuggestionsServiceFactory>::get(); | 174 return base::Singleton<ContentSuggestionsServiceFactory>::get(); |
| 89 } | 175 } |
| 90 | 176 |
| 91 // static | 177 // static |
| 92 ContentSuggestionsService* ContentSuggestionsServiceFactory::GetForProfile( | 178 ContentSuggestionsService* ContentSuggestionsServiceFactory::GetForProfile( |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 123 ? State::ENABLED | 209 ? State::ENABLED |
| 124 : State::DISABLED; | 210 : State::DISABLED; |
| 125 ContentSuggestionsService* service = new ContentSuggestionsService(state); | 211 ContentSuggestionsService* service = new ContentSuggestionsService(state); |
| 126 if (state == State::DISABLED) { | 212 if (state == State::DISABLED) { |
| 127 // Since we won't initialise the services, they won't get a chance to | 213 // Since we won't initialise the services, they won't get a chance to |
| 128 // unschedule their tasks. We do it explicitly here instead. | 214 // unschedule their tasks. We do it explicitly here instead. |
| 129 ClearScheduledTasks(); | 215 ClearScheduledTasks(); |
| 130 return service; | 216 return service; |
| 131 } | 217 } |
| 132 | 218 |
| 219 CategoryFactory* category_factory = service->category_factory(); | |
| 220 PrefService* pref_service = profile->GetPrefs(); | |
| 221 OfflinePageModel* offline_page_model = | |
| 222 OfflinePageModelFactory::GetForBrowserContext(profile); | |
| 223 BookmarkModel* bookmark_model = | |
| 224 BookmarkModelFactory::GetForBrowserContext(profile); | |
| 225 SigninManagerBase* signin_manager = | |
| 226 SigninManagerFactory::GetForProfile(profile); | |
| 227 OAuth2TokenService* token_service = | |
| 228 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); | |
| 229 HistoryService* history_service = HistoryServiceFactory::GetForProfile( | |
| 230 profile, ServiceAccessType::EXPLICIT_ACCESS); | |
| 231 SuggestionsService* suggestions_service = | |
| 232 SuggestionsServiceFactory::GetForProfile(profile); | |
| 233 | |
| 133 #if defined(OS_ANDROID) | 234 #if defined(OS_ANDROID) |
| 134 // Create the OfflinePageSuggestionsProvider. | 235 RegisterOfflinePageProvider(offline_page_model, service, category_factory, |
| 135 bool recent_tabs_enabled = base::FeatureList::IsEnabled( | 236 pref_service); |
| 136 ntp_snippets::kRecentOfflineTabSuggestionsFeature); | |
| 137 bool downloads_enabled = | |
| 138 base::FeatureList::IsEnabled(ntp_snippets::kDownloadSuggestionsFeature); | |
| 139 bool download_manager_ui_enabled = | |
| 140 base::FeatureList::IsEnabled(chrome::android::kDownloadsUiFeature); | |
| 141 if (recent_tabs_enabled || downloads_enabled) { | |
| 142 OfflinePageModel* offline_page_model = | |
| 143 OfflinePageModelFactory::GetForBrowserContext(profile); | |
| 144 | |
| 145 std::unique_ptr<OfflinePageSuggestionsProvider> | |
| 146 offline_page_suggestions_provider = | |
| 147 base::MakeUnique<OfflinePageSuggestionsProvider>( | |
| 148 recent_tabs_enabled, downloads_enabled, | |
| 149 download_manager_ui_enabled, service, | |
| 150 service->category_factory(), offline_page_model, | |
| 151 profile->GetPrefs()); | |
| 152 service->RegisterProvider(std::move(offline_page_suggestions_provider)); | |
| 153 } | |
| 154 #endif // OS_ANDROID | 237 #endif // OS_ANDROID |
| 155 | 238 |
| 156 // Create the BookmarkSuggestionsProvider. | 239 if (base::FeatureList::IsEnabled(ntp_snippets::kBookmarkSuggestionsFeature)) { |
| 157 if (base::FeatureList::IsEnabled( | 240 RegisterBookmarkProvider(bookmark_model, service, category_factory, |
| 158 ntp_snippets::kBookmarkSuggestionsFeature)) { | 241 pref_service); |
| 159 BookmarkModel* bookmark_model = | |
| 160 BookmarkModelFactory::GetForBrowserContext(profile); | |
| 161 std::unique_ptr<BookmarkSuggestionsProvider> bookmark_suggestions_provider = | |
| 162 base::MakeUnique<BookmarkSuggestionsProvider>( | |
| 163 service, service->category_factory(), bookmark_model, | |
| 164 profile->GetPrefs()); | |
| 165 service->RegisterProvider(std::move(bookmark_suggestions_provider)); | |
| 166 } | 242 } |
| 167 | 243 |
| 168 #if defined(OS_ANDROID) | 244 #if defined(OS_ANDROID) |
| 169 // Create the PhysicalWebPageSuggestionsProvider. | |
| 170 if (base::FeatureList::IsEnabled( | 245 if (base::FeatureList::IsEnabled( |
| 171 ntp_snippets::kPhysicalWebPageSuggestionsFeature)) { | 246 ntp_snippets::kPhysicalWebPageSuggestionsFeature)) { |
| 172 std::unique_ptr<PhysicalWebPageSuggestionsProvider> | 247 RegisterPhysicalWebPageProvider(service, category_factory); |
| 173 physical_web_page_suggestions_provider = | |
| 174 base::MakeUnique<PhysicalWebPageSuggestionsProvider>( | |
| 175 service, service->category_factory()); | |
| 176 service->RegisterProvider( | |
| 177 std::move(physical_web_page_suggestions_provider)); | |
| 178 } | 248 } |
| 179 #endif // OS_ANDROID | 249 #endif // OS_ANDROID |
| 180 | 250 |
| 181 if (base::FeatureList::IsEnabled(ntp_snippets::kArticleSuggestionsFeature)) { | 251 if (base::FeatureList::IsEnabled(ntp_snippets::kArticleSuggestionsFeature)) { |
| 182 // Create the NTPSnippetsService (articles provider). | 252 RegisterArticleProvider(signin_manager, token_service, history_service, |
| 183 SigninManagerBase* signin_manager = | 253 suggestions_service, service, category_factory, |
| 184 SigninManagerFactory::GetForProfile(profile); | 254 pref_service, profile); |
| 185 OAuth2TokenService* token_service = | |
| 186 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); | |
| 187 scoped_refptr<net::URLRequestContextGetter> request_context = | |
| 188 content::BrowserContext::GetDefaultStoragePartition(context) | |
| 189 ->GetURLRequestContext(); | |
| 190 HistoryService* history_service = HistoryServiceFactory::GetForProfile( | |
| 191 profile, ServiceAccessType::EXPLICIT_ACCESS); | |
| 192 SuggestionsService* suggestions_service = | |
| 193 SuggestionsServiceFactory::GetForProfile(profile); | |
| 194 NTPSnippetsScheduler* scheduler = nullptr; | |
| 195 #if defined(OS_ANDROID) | |
| 196 scheduler = NTPSnippetsLauncher::Get(); | |
| 197 #endif // OS_ANDROID | |
| 198 base::FilePath database_dir( | |
| 199 profile->GetPath().Append(ntp_snippets::kDatabaseFolder)); | |
| 200 scoped_refptr<base::SequencedTaskRunner> task_runner = | |
| 201 BrowserThread::GetBlockingPool() | |
| 202 ->GetSequencedTaskRunnerWithShutdownBehavior( | |
| 203 base::SequencedWorkerPool::GetSequenceToken(), | |
| 204 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN); | |
| 205 bool is_stable_channel = | |
| 206 chrome::GetChannel() == version_info::Channel::STABLE; | |
| 207 std::unique_ptr<NTPSnippetsService> ntp_snippets_service = | |
| 208 base::MakeUnique<NTPSnippetsService>( | |
| 209 service, service->category_factory(), profile->GetPrefs(), | |
| 210 history_service, suggestions_service, | |
| 211 g_browser_process->GetApplicationLocale(), | |
| 212 scheduler, base::MakeUnique<NTPSnippetsFetcher>( | |
| 213 signin_manager, token_service, request_context, | |
| 214 profile->GetPrefs(), service->category_factory(), | |
| 215 base::Bind(&safe_json::SafeJsonParser::Parse), | |
| 216 is_stable_channel), | |
| 217 base::MakeUnique<ImageFetcherImpl>( | |
| 218 base::MakeUnique<ImageDecoderImpl>(), request_context.get()), | |
| 219 base::MakeUnique<ImageDecoderImpl>(), | |
| 220 base::MakeUnique<NTPSnippetsDatabase>(database_dir, task_runner), | |
| 221 base::MakeUnique<NTPSnippetsStatusService>(signin_manager, | |
| 222 profile->GetPrefs())); | |
| 223 service->set_ntp_snippets_service(ntp_snippets_service.get()); | |
| 224 service->RegisterProvider(std::move(ntp_snippets_service)); | |
| 225 } | 255 } |
| 226 | 256 |
| 227 return service; | 257 return service; |
| 228 } | 258 } |
| OLD | NEW |