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

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

Issue 2245633002: ContentSuggestionsService providers registration refactoring. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@pw_provider
Patch Set: rebase Created 4 years, 4 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 | no next file » | 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 "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"
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 70
71 namespace { 71 namespace {
72 72
73 // Clear the tasks that can be scheduled by running services. 73 // Clear the tasks that can be scheduled by running services.
74 void ClearScheduledTasks() { 74 void ClearScheduledTasks() {
75 #if defined(OS_ANDROID) 75 #if defined(OS_ANDROID)
76 NTPSnippetsLauncher::Get()->Unschedule(); 76 NTPSnippetsLauncher::Get()->Unschedule();
77 #endif // OS_ANDROID 77 #endif // OS_ANDROID
78 } 78 }
79 79
80 void RegisterOfflinePageProvider(ContentSuggestionsService* service,
81 Profile* profile) {
82 bool recent_tabs_enabled = base::FeatureList::IsEnabled(
83 ntp_snippets::kRecentOfflineTabSuggestionsFeature);
84 bool downloads_enabled =
85 base::FeatureList::IsEnabled(ntp_snippets::kDownloadSuggestionsFeature);
86 bool download_manager_ui_enabled =
87 base::FeatureList::IsEnabled(chrome::android::kDownloadsUiFeature);
88 OfflinePageModel* offline_page_model =
89 OfflinePageModelFactory::GetForBrowserContext(profile);
90
91 std::unique_ptr<OfflinePageSuggestionsProvider>
92 offline_page_suggestions_provider =
93 base::MakeUnique<OfflinePageSuggestionsProvider>(
94 recent_tabs_enabled, downloads_enabled,
95 download_manager_ui_enabled, service, service->category_factory(),
96 offline_page_model, profile->GetPrefs());
97 service->RegisterProvider(std::move(offline_page_suggestions_provider));
98 }
99
100 void RegisterBookmarkProvider(ContentSuggestionsService* service,
101 Profile* profile) {
102 BookmarkModel* bookmark_model =
103 BookmarkModelFactory::GetForBrowserContext(profile);
104 std::unique_ptr<BookmarkSuggestionsProvider> bookmark_suggestions_provider =
105 base::MakeUnique<BookmarkSuggestionsProvider>(
106 service, service->category_factory(), bookmark_model);
107 service->RegisterProvider(std::move(bookmark_suggestions_provider));
108 }
109
110 void RegisterPhysicalWebPageProvider(ContentSuggestionsService* service) {
111 std::unique_ptr<PhysicalWebPageSuggestionsProvider>
112 physical_web_page_suggestions_provider =
113 base::MakeUnique<PhysicalWebPageSuggestionsProvider>(
114 service, service->category_factory());
115 service->RegisterProvider(std::move(physical_web_page_suggestions_provider));
116 }
117
118 void RegisterArticleProvider(ContentSuggestionsService* service,
119 Profile* profile) {
120 SigninManagerBase* signin_manager =
121 SigninManagerFactory::GetForProfile(profile);
dgn 2016/08/17 14:07:29 We need to specifically declare the dependency on
tschumann 2016/08/18 17:41:29 So you are concerned about keeping the DependsOn()
dgn 2016/08/19 09:25:14 Yes, I agree, this CL already helps a lot. What I
vitaliii 2016/08/25 13:44:18 Done.
122 OAuth2TokenService* token_service =
123 ProfileOAuth2TokenServiceFactory::GetForProfile(profile);
124 scoped_refptr<net::URLRequestContextGetter> request_context =
125 content::BrowserContext::GetDefaultStoragePartition(profile)
126 ->GetURLRequestContext();
127 SuggestionsService* suggestions_service =
128 SuggestionsServiceFactory::GetForProfile(profile);
129 NTPSnippetsScheduler* scheduler = nullptr;
130 #if defined(OS_ANDROID)
131 scheduler = NTPSnippetsLauncher::Get();
132 #endif // OS_ANDROID
133 base::FilePath database_dir(
134 profile->GetPath().Append(ntp_snippets::kDatabaseFolder));
135 scoped_refptr<base::SequencedTaskRunner> task_runner =
136 BrowserThread::GetBlockingPool()
137 ->GetSequencedTaskRunnerWithShutdownBehavior(
138 base::SequencedWorkerPool::GetSequenceToken(),
139 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN);
140 bool is_stable_channel =
141 chrome::GetChannel() == version_info::Channel::STABLE;
142 std::unique_ptr<NTPSnippetsService> ntp_snippets_service =
143 base::MakeUnique<NTPSnippetsService>(
144 service, service->category_factory(), profile->GetPrefs(),
145 suggestions_service, g_browser_process->GetApplicationLocale(),
146 scheduler,
147 base::MakeUnique<NTPSnippetsFetcher>(
148 signin_manager, token_service, request_context,
149 profile->GetPrefs(), service->category_factory(),
150 base::Bind(&safe_json::SafeJsonParser::Parse), is_stable_channel),
151 base::MakeUnique<ImageFetcherImpl>(
152 base::MakeUnique<ImageDecoderImpl>(), request_context.get()),
153 base::MakeUnique<ImageDecoderImpl>(),
154 base::MakeUnique<NTPSnippetsDatabase>(database_dir, task_runner),
155 base::MakeUnique<NTPSnippetsStatusService>(signin_manager,
156 profile->GetPrefs()));
157 service->set_ntp_snippets_service(ntp_snippets_service.get());
158 service->RegisterProvider(std::move(ntp_snippets_service));
159 }
160
80 } // namespace 161 } // namespace
81 162
82 // static 163 // static
83 ContentSuggestionsServiceFactory* 164 ContentSuggestionsServiceFactory*
84 ContentSuggestionsServiceFactory::GetInstance() { 165 ContentSuggestionsServiceFactory::GetInstance() {
85 return base::Singleton<ContentSuggestionsServiceFactory>::get(); 166 return base::Singleton<ContentSuggestionsServiceFactory>::get();
86 } 167 }
87 168
88 // static 169 // static
89 ContentSuggestionsService* ContentSuggestionsServiceFactory::GetForProfile( 170 ContentSuggestionsService* ContentSuggestionsServiceFactory::GetForProfile(
(...skipping 30 matching lines...) Expand all
120 : State::DISABLED; 201 : State::DISABLED;
121 ContentSuggestionsService* service = new ContentSuggestionsService(state); 202 ContentSuggestionsService* service = new ContentSuggestionsService(state);
122 if (state == State::DISABLED) { 203 if (state == State::DISABLED) {
123 // Since we won't initialise the services, they won't get a chance to 204 // Since we won't initialise the services, they won't get a chance to
124 // unschedule their tasks. We do it explicitly here instead. 205 // unschedule their tasks. We do it explicitly here instead.
125 ClearScheduledTasks(); 206 ClearScheduledTasks();
126 return service; 207 return service;
127 } 208 }
128 209
129 #if defined(OS_ANDROID) 210 #if defined(OS_ANDROID)
130 // Create the OfflinePageSuggestionsProvider. 211 if (base::FeatureList::IsEnabled(
131 bool recent_tabs_enabled = base::FeatureList::IsEnabled( 212 ntp_snippets::kRecentOfflineTabSuggestionsFeature) ||
132 ntp_snippets::kRecentOfflineTabSuggestionsFeature); 213 base::FeatureList::IsEnabled(ntp_snippets::kDownloadSuggestionsFeature)) {
133 bool downloads_enabled = 214 RegisterOfflinePageProvider(service, profile);
134 base::FeatureList::IsEnabled(ntp_snippets::kDownloadSuggestionsFeature);
135 bool download_manager_ui_enabled =
136 base::FeatureList::IsEnabled(chrome::android::kDownloadsUiFeature);
137 if (recent_tabs_enabled || downloads_enabled) {
138 OfflinePageModel* offline_page_model =
139 OfflinePageModelFactory::GetForBrowserContext(profile);
140
141 std::unique_ptr<OfflinePageSuggestionsProvider>
142 offline_page_suggestions_provider =
143 base::MakeUnique<OfflinePageSuggestionsProvider>(
144 recent_tabs_enabled, downloads_enabled,
145 download_manager_ui_enabled, service,
146 service->category_factory(), offline_page_model,
147 profile->GetPrefs());
148 service->RegisterProvider(std::move(offline_page_suggestions_provider));
149 } 215 }
150 #endif // OS_ANDROID 216 #endif // OS_ANDROID
151 217
152 // Create the BookmarkSuggestionsProvider. 218 if (base::FeatureList::IsEnabled(ntp_snippets::kBookmarkSuggestionsFeature)) {
153 if (base::FeatureList::IsEnabled( 219 RegisterBookmarkProvider(service, profile);
154 ntp_snippets::kBookmarkSuggestionsFeature)) {
155 BookmarkModel* bookmark_model =
156 BookmarkModelFactory::GetForBrowserContext(profile);
157 std::unique_ptr<BookmarkSuggestionsProvider> bookmark_suggestions_provider =
158 base::MakeUnique<BookmarkSuggestionsProvider>(
159 service, service->category_factory(), bookmark_model);
160 service->RegisterProvider(std::move(bookmark_suggestions_provider));
161 } 220 }
162 221
163 #if defined(OS_ANDROID) 222 #if defined(OS_ANDROID)
164 // Create the PhysicalWebPageSuggestionsProvider.
165 if (base::FeatureList::IsEnabled( 223 if (base::FeatureList::IsEnabled(
166 ntp_snippets::kPhysicalWebPageSuggestionsFeature)) { 224 ntp_snippets::kPhysicalWebPageSuggestionsFeature)) {
167 std::unique_ptr<PhysicalWebPageSuggestionsProvider> 225 RegisterPhysicalWebPageProvider(service);
168 physical_web_page_suggestions_provider =
169 base::MakeUnique<PhysicalWebPageSuggestionsProvider>(
170 service, service->category_factory());
171 service->RegisterProvider(
172 std::move(physical_web_page_suggestions_provider));
173 } 226 }
174 #endif // OS_ANDROID 227 #endif // OS_ANDROID
175 228
176 if (base::FeatureList::IsEnabled(ntp_snippets::kArticleSuggestionsFeature)) { 229 if (base::FeatureList::IsEnabled(ntp_snippets::kArticleSuggestionsFeature)) {
177 // Create the NTPSnippetsService (articles provider). 230 RegisterArticleProvider(service, profile);
178 SigninManagerBase* signin_manager =
179 SigninManagerFactory::GetForProfile(profile);
180 OAuth2TokenService* token_service =
181 ProfileOAuth2TokenServiceFactory::GetForProfile(profile);
182 scoped_refptr<net::URLRequestContextGetter> request_context =
183 content::BrowserContext::GetDefaultStoragePartition(context)
184 ->GetURLRequestContext();
185 SuggestionsService* suggestions_service =
186 SuggestionsServiceFactory::GetForProfile(profile);
187 NTPSnippetsScheduler* scheduler = nullptr;
188 #if defined(OS_ANDROID)
189 scheduler = NTPSnippetsLauncher::Get();
190 #endif // OS_ANDROID
191 base::FilePath database_dir(
192 profile->GetPath().Append(ntp_snippets::kDatabaseFolder));
193 scoped_refptr<base::SequencedTaskRunner> task_runner =
194 BrowserThread::GetBlockingPool()
195 ->GetSequencedTaskRunnerWithShutdownBehavior(
196 base::SequencedWorkerPool::GetSequenceToken(),
197 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN);
198 bool is_stable_channel =
199 chrome::GetChannel() == version_info::Channel::STABLE;
200 std::unique_ptr<NTPSnippetsService> ntp_snippets_service =
201 base::MakeUnique<NTPSnippetsService>(
202 service, service->category_factory(), profile->GetPrefs(),
203 suggestions_service, g_browser_process->GetApplicationLocale(),
204 scheduler, base::MakeUnique<NTPSnippetsFetcher>(
205 signin_manager, token_service, request_context,
206 profile->GetPrefs(), service->category_factory(),
207 base::Bind(&safe_json::SafeJsonParser::Parse),
208 is_stable_channel),
209 base::MakeUnique<ImageFetcherImpl>(
210 base::MakeUnique<ImageDecoderImpl>(), request_context.get()),
211 base::MakeUnique<ImageDecoderImpl>(),
212 base::MakeUnique<NTPSnippetsDatabase>(database_dir, task_runner),
213 base::MakeUnique<NTPSnippetsStatusService>(signin_manager,
214 profile->GetPrefs()));
215 service->set_ntp_snippets_service(ntp_snippets_service.get());
216 service->RegisterProvider(std::move(ntp_snippets_service));
217 } 231 }
218 232
219 return service; 233 return service;
220 } 234 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698