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 #if defined(OS_ANDROID) |
| 86 void RegisterOfflinePageProvider(OfflinePageModel* offline_page_model, |
| 87 ContentSuggestionsService* service, |
| 88 CategoryFactory* category_factory, |
| 89 PrefService* pref_service) { |
| 90 bool recent_tabs_enabled = base::FeatureList::IsEnabled( |
| 91 ntp_snippets::kRecentOfflineTabSuggestionsFeature); |
| 92 bool downloads_enabled = |
| 93 base::FeatureList::IsEnabled(ntp_snippets::kDownloadSuggestionsFeature); |
| 94 bool download_manager_ui_enabled = |
| 95 base::FeatureList::IsEnabled(chrome::android::kDownloadsUiFeature); |
| 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 #endif // OS_ANDROID |
| 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 #if defined(OS_ANDROID) |
| 117 void RegisterPhysicalWebPageProvider(ContentSuggestionsService* service, |
| 118 CategoryFactory* category_factory) { |
| 119 std::unique_ptr<PhysicalWebPageSuggestionsProvider> |
| 120 physical_web_page_suggestions_provider = |
| 121 base::MakeUnique<PhysicalWebPageSuggestionsProvider>( |
| 122 service, category_factory); |
| 123 service->RegisterProvider(std::move(physical_web_page_suggestions_provider)); |
| 124 } |
| 125 #endif // OS_ANDROID |
| 126 |
| 127 void RegisterArticleProvider(SigninManagerBase* signin_manager, |
| 128 OAuth2TokenService* token_service, |
| 129 HistoryService* history_service, |
| 130 SuggestionsService* suggestions_service, |
| 131 ContentSuggestionsService* service, |
| 132 CategoryFactory* category_factory, |
| 133 PrefService* pref_service, |
| 134 Profile* profile) { |
| 135 scoped_refptr<net::URLRequestContextGetter> request_context = |
| 136 content::BrowserContext::GetDefaultStoragePartition(profile) |
| 137 ->GetURLRequestContext(); |
| 138 |
| 139 NTPSnippetsScheduler* scheduler = nullptr; |
| 140 #if defined(OS_ANDROID) |
| 141 scheduler = NTPSnippetsLauncher::Get(); |
| 142 #endif // OS_ANDROID |
| 143 base::FilePath database_dir( |
| 144 profile->GetPath().Append(ntp_snippets::kDatabaseFolder)); |
| 145 scoped_refptr<base::SequencedTaskRunner> task_runner = |
| 146 BrowserThread::GetBlockingPool() |
| 147 ->GetSequencedTaskRunnerWithShutdownBehavior( |
| 148 base::SequencedWorkerPool::GetSequenceToken(), |
| 149 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN); |
| 150 bool is_stable_channel = |
| 151 chrome::GetChannel() == version_info::Channel::STABLE; |
| 152 std::unique_ptr<NTPSnippetsService> ntp_snippets_service = |
| 153 base::MakeUnique<NTPSnippetsService>( |
| 154 service, service->category_factory(), pref_service, history_service, |
| 155 suggestions_service, g_browser_process->GetApplicationLocale(), |
| 156 scheduler, |
| 157 base::MakeUnique<NTPSnippetsFetcher>( |
| 158 signin_manager, token_service, request_context, pref_service, |
| 159 category_factory, base::Bind(&safe_json::SafeJsonParser::Parse), |
| 160 is_stable_channel), |
| 161 base::MakeUnique<ImageFetcherImpl>( |
| 162 base::MakeUnique<ImageDecoderImpl>(), request_context.get()), |
| 163 base::MakeUnique<ImageDecoderImpl>(), |
| 164 base::MakeUnique<NTPSnippetsDatabase>(database_dir, task_runner), |
| 165 base::MakeUnique<NTPSnippetsStatusService>(signin_manager, |
| 166 pref_service)); |
| 167 service->set_ntp_snippets_service(ntp_snippets_service.get()); |
| 168 service->RegisterProvider(std::move(ntp_snippets_service)); |
| 169 } |
| 170 |
83 } // namespace | 171 } // namespace |
84 | 172 |
85 // static | 173 // static |
86 ContentSuggestionsServiceFactory* | 174 ContentSuggestionsServiceFactory* |
87 ContentSuggestionsServiceFactory::GetInstance() { | 175 ContentSuggestionsServiceFactory::GetInstance() { |
88 return base::Singleton<ContentSuggestionsServiceFactory>::get(); | 176 return base::Singleton<ContentSuggestionsServiceFactory>::get(); |
89 } | 177 } |
90 | 178 |
91 // static | 179 // static |
92 ContentSuggestionsService* ContentSuggestionsServiceFactory::GetForProfile( | 180 ContentSuggestionsService* ContentSuggestionsServiceFactory::GetForProfile( |
(...skipping 30 matching lines...) Expand all Loading... |
123 ? State::ENABLED | 211 ? State::ENABLED |
124 : State::DISABLED; | 212 : State::DISABLED; |
125 ContentSuggestionsService* service = new ContentSuggestionsService(state); | 213 ContentSuggestionsService* service = new ContentSuggestionsService(state); |
126 if (state == State::DISABLED) { | 214 if (state == State::DISABLED) { |
127 // Since we won't initialise the services, they won't get a chance to | 215 // Since we won't initialise the services, they won't get a chance to |
128 // unschedule their tasks. We do it explicitly here instead. | 216 // unschedule their tasks. We do it explicitly here instead. |
129 ClearScheduledTasks(); | 217 ClearScheduledTasks(); |
130 return service; | 218 return service; |
131 } | 219 } |
132 | 220 |
| 221 CategoryFactory* category_factory = service->category_factory(); |
| 222 PrefService* pref_service = profile->GetPrefs(); |
133 #if defined(OS_ANDROID) | 223 #if defined(OS_ANDROID) |
134 // Create the OfflinePageSuggestionsProvider. | 224 OfflinePageModel* offline_page_model = |
| 225 OfflinePageModelFactory::GetForBrowserContext(profile); |
| 226 #endif // OS_ANDROID |
| 227 BookmarkModel* bookmark_model = |
| 228 BookmarkModelFactory::GetForBrowserContext(profile); |
| 229 SigninManagerBase* signin_manager = |
| 230 SigninManagerFactory::GetForProfile(profile); |
| 231 OAuth2TokenService* token_service = |
| 232 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); |
| 233 HistoryService* history_service = HistoryServiceFactory::GetForProfile( |
| 234 profile, ServiceAccessType::EXPLICIT_ACCESS); |
| 235 SuggestionsService* suggestions_service = |
| 236 SuggestionsServiceFactory::GetForProfile(profile); |
| 237 |
| 238 #if defined(OS_ANDROID) |
135 bool recent_tabs_enabled = base::FeatureList::IsEnabled( | 239 bool recent_tabs_enabled = base::FeatureList::IsEnabled( |
136 ntp_snippets::kRecentOfflineTabSuggestionsFeature); | 240 ntp_snippets::kRecentOfflineTabSuggestionsFeature); |
137 bool downloads_enabled = | 241 bool downloads_enabled = |
138 base::FeatureList::IsEnabled(ntp_snippets::kDownloadSuggestionsFeature); | 242 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) { | 243 if (recent_tabs_enabled || downloads_enabled) { |
142 OfflinePageModel* offline_page_model = | 244 RegisterOfflinePageProvider(offline_page_model, service, category_factory, |
143 OfflinePageModelFactory::GetForBrowserContext(profile); | 245 pref_service); |
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 } | 246 } |
154 #endif // OS_ANDROID | 247 #endif // OS_ANDROID |
155 | 248 |
156 // Create the BookmarkSuggestionsProvider. | 249 if (base::FeatureList::IsEnabled(ntp_snippets::kBookmarkSuggestionsFeature)) { |
157 if (base::FeatureList::IsEnabled( | 250 RegisterBookmarkProvider(bookmark_model, service, category_factory, |
158 ntp_snippets::kBookmarkSuggestionsFeature)) { | 251 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 } | 252 } |
167 | 253 |
168 #if defined(OS_ANDROID) | 254 #if defined(OS_ANDROID) |
169 // Create the PhysicalWebPageSuggestionsProvider. | |
170 if (base::FeatureList::IsEnabled( | 255 if (base::FeatureList::IsEnabled( |
171 ntp_snippets::kPhysicalWebPageSuggestionsFeature)) { | 256 ntp_snippets::kPhysicalWebPageSuggestionsFeature)) { |
172 std::unique_ptr<PhysicalWebPageSuggestionsProvider> | 257 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 } | 258 } |
179 #endif // OS_ANDROID | 259 #endif // OS_ANDROID |
180 | 260 |
181 if (base::FeatureList::IsEnabled(ntp_snippets::kArticleSuggestionsFeature)) { | 261 if (base::FeatureList::IsEnabled(ntp_snippets::kArticleSuggestionsFeature)) { |
182 // Create the NTPSnippetsService (articles provider). | 262 RegisterArticleProvider(signin_manager, token_service, history_service, |
183 SigninManagerBase* signin_manager = | 263 suggestions_service, service, category_factory, |
184 SigninManagerFactory::GetForProfile(profile); | 264 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 } | 265 } |
226 | 266 |
227 return service; | 267 return service; |
228 } | 268 } |
OLD | NEW |