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" |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 RegisterOfflinePageSuggestionsProvider(ContentSuggestionsService* service, | |
Marc Treib
2016/08/12 12:18:45
optional naming proposal: Should we remove "Sugges
vitaliii
2016/08/12 12:33:29
Done.
| |
81 Profile* profile) { | |
82 OfflinePageModel* offline_page_model = | |
83 OfflinePageModelFactory::GetForBrowserContext(profile); | |
84 | |
Marc Treib
2016/08/12 12:18:45
nit: I'd remove the empty line; the other Register
vitaliii
2016/08/12 12:33:29
Done.
| |
85 std::unique_ptr<OfflinePageSuggestionsProvider> | |
86 offline_page_suggestions_provider = | |
87 base::MakeUnique<OfflinePageSuggestionsProvider>( | |
88 service, service->category_factory(), offline_page_model); | |
89 service->RegisterProvider(std::move(offline_page_suggestions_provider)); | |
90 } | |
91 | |
92 void RegisterBookmarkSuggestionsProvider(ContentSuggestionsService* service, | |
93 Profile* profile) { | |
94 BookmarkModel* bookmark_model = | |
95 BookmarkModelFactory::GetForBrowserContext(profile); | |
96 std::unique_ptr<BookmarkSuggestionsProvider> bookmark_suggestions_provider = | |
97 base::MakeUnique<BookmarkSuggestionsProvider>( | |
98 service, service->category_factory(), bookmark_model); | |
99 service->RegisterProvider(std::move(bookmark_suggestions_provider)); | |
100 } | |
101 | |
102 void RegisterPhysicalWebSuggestionsProvider( | |
103 ContentSuggestionsService* service) { | |
104 std::unique_ptr<PhysicalWebPageSuggestionsProvider> | |
105 physical_web_page_suggestions_provider = | |
106 base::MakeUnique<PhysicalWebPageSuggestionsProvider>( | |
107 service, service->category_factory()); | |
108 service->RegisterProvider(std::move(physical_web_page_suggestions_provider)); | |
109 } | |
110 | |
111 void RegisterArticleSuggestionsProvider(ContentSuggestionsService* service, | |
112 Profile* profile, | |
113 content::BrowserContext* context) { | |
Marc Treib
2016/08/12 12:18:45
Profile and BrowserContext are actually the same,
vitaliii
2016/08/12 12:33:29
Done.
| |
114 SigninManagerBase* signin_manager = | |
115 SigninManagerFactory::GetForProfile(profile); | |
116 OAuth2TokenService* token_service = | |
117 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); | |
118 scoped_refptr<net::URLRequestContextGetter> request_context = | |
119 content::BrowserContext::GetDefaultStoragePartition(context) | |
120 ->GetURLRequestContext(); | |
121 SuggestionsService* suggestions_service = | |
122 SuggestionsServiceFactory::GetForProfile(profile); | |
123 NTPSnippetsScheduler* scheduler = nullptr; | |
124 #if defined(OS_ANDROID) | |
125 scheduler = NTPSnippetsLauncher::Get(); | |
126 #endif // OS_ANDROID | |
127 base::FilePath database_dir( | |
128 profile->GetPath().Append(ntp_snippets::kDatabaseFolder)); | |
129 scoped_refptr<base::SequencedTaskRunner> task_runner = | |
130 BrowserThread::GetBlockingPool() | |
131 ->GetSequencedTaskRunnerWithShutdownBehavior( | |
132 base::SequencedWorkerPool::GetSequenceToken(), | |
133 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN); | |
134 bool is_stable_channel = | |
135 chrome::GetChannel() == version_info::Channel::STABLE; | |
136 std::unique_ptr<NTPSnippetsService> ntp_snippets_service = | |
137 base::MakeUnique<NTPSnippetsService>( | |
138 service, service->category_factory(), profile->GetPrefs(), | |
139 suggestions_service, g_browser_process->GetApplicationLocale(), | |
140 scheduler, | |
141 base::MakeUnique<NTPSnippetsFetcher>( | |
142 signin_manager, token_service, request_context, | |
143 profile->GetPrefs(), | |
144 base::Bind(&safe_json::SafeJsonParser::Parse), is_stable_channel), | |
145 base::MakeUnique<ImageFetcherImpl>( | |
146 base::MakeUnique<ImageDecoderImpl>(), request_context.get()), | |
147 base::MakeUnique<ImageDecoderImpl>(), | |
148 base::MakeUnique<NTPSnippetsDatabase>(database_dir, task_runner), | |
149 base::MakeUnique<NTPSnippetsStatusService>(signin_manager, | |
150 profile->GetPrefs())); | |
151 service->set_ntp_snippets_service(ntp_snippets_service.get()); | |
152 service->RegisterProvider(std::move(ntp_snippets_service)); | |
153 } | |
154 | |
80 } // namespace | 155 } // namespace |
81 | 156 |
82 // static | 157 // static |
83 ContentSuggestionsServiceFactory* | 158 ContentSuggestionsServiceFactory* |
84 ContentSuggestionsServiceFactory::GetInstance() { | 159 ContentSuggestionsServiceFactory::GetInstance() { |
85 return base::Singleton<ContentSuggestionsServiceFactory>::get(); | 160 return base::Singleton<ContentSuggestionsServiceFactory>::get(); |
86 } | 161 } |
87 | 162 |
88 // static | 163 // static |
89 ContentSuggestionsService* ContentSuggestionsServiceFactory::GetForProfile( | 164 ContentSuggestionsService* ContentSuggestionsServiceFactory::GetForProfile( |
(...skipping 30 matching lines...) Expand all Loading... | |
120 : State::DISABLED; | 195 : State::DISABLED; |
121 ContentSuggestionsService* service = new ContentSuggestionsService(state); | 196 ContentSuggestionsService* service = new ContentSuggestionsService(state); |
122 if (state == State::DISABLED) { | 197 if (state == State::DISABLED) { |
123 // Since we won't initialise the services, they won't get a chance to | 198 // Since we won't initialise the services, they won't get a chance to |
124 // unschedule their tasks. We do it explicitly here instead. | 199 // unschedule their tasks. We do it explicitly here instead. |
125 ClearScheduledTasks(); | 200 ClearScheduledTasks(); |
126 return service; | 201 return service; |
127 } | 202 } |
128 | 203 |
129 #if defined(OS_ANDROID) | 204 #if defined(OS_ANDROID) |
130 // Create the OfflinePageSuggestionsProvider. | |
131 if (base::FeatureList::IsEnabled( | 205 if (base::FeatureList::IsEnabled( |
132 ntp_snippets::kOfflinePageSuggestionsFeature)) { | 206 ntp_snippets::kOfflinePageSuggestionsFeature)) { |
133 OfflinePageModel* offline_page_model = | 207 RegisterOfflinePageSuggestionsProvider(service, profile); |
134 OfflinePageModelFactory::GetForBrowserContext(profile); | |
135 | |
136 std::unique_ptr<OfflinePageSuggestionsProvider> | |
137 offline_page_suggestions_provider = | |
138 base::MakeUnique<OfflinePageSuggestionsProvider>( | |
139 service, service->category_factory(), offline_page_model); | |
140 service->RegisterProvider(std::move(offline_page_suggestions_provider)); | |
141 } | 208 } |
142 #endif // OS_ANDROID | 209 #endif // OS_ANDROID |
143 | 210 |
144 // Create the BookmarkSuggestionsProvider. | 211 if (base::FeatureList::IsEnabled(ntp_snippets::kBookmarkSuggestionsFeature)) { |
145 if (base::FeatureList::IsEnabled( | 212 RegisterBookmarkSuggestionsProvider(service, profile); |
146 ntp_snippets::kBookmarkSuggestionsFeature)) { | |
147 BookmarkModel* bookmark_model = | |
148 BookmarkModelFactory::GetForBrowserContext(profile); | |
149 std::unique_ptr<BookmarkSuggestionsProvider> bookmark_suggestions_provider = | |
150 base::MakeUnique<BookmarkSuggestionsProvider>( | |
151 service, service->category_factory(), bookmark_model); | |
152 service->RegisterProvider(std::move(bookmark_suggestions_provider)); | |
153 } | 213 } |
154 | 214 |
155 #if defined(OS_ANDROID) | 215 #if defined(OS_ANDROID) |
156 // Create the PhysicalWebPageSuggestionsProvider. | |
157 if (base::FeatureList::IsEnabled( | 216 if (base::FeatureList::IsEnabled( |
158 ntp_snippets::kPhysicalWebPageSuggestionsFeature)) { | 217 ntp_snippets::kPhysicalWebPageSuggestionsFeature)) { |
159 std::unique_ptr<PhysicalWebPageSuggestionsProvider> | 218 RegisterPhysicalWebSuggestionsProvider(service); |
160 physical_web_page_suggestions_provider = | |
161 base::MakeUnique<PhysicalWebPageSuggestionsProvider>( | |
162 service, service->category_factory()); | |
163 service->RegisterProvider( | |
164 std::move(physical_web_page_suggestions_provider)); | |
165 } | 219 } |
166 #endif // OS_ANDROID | 220 #endif // OS_ANDROID |
167 | 221 |
168 if (base::FeatureList::IsEnabled(ntp_snippets::kArticleSuggestionsFeature)) { | 222 if (base::FeatureList::IsEnabled(ntp_snippets::kArticleSuggestionsFeature)) { |
169 // Create the NTPSnippetsService (articles provider). | 223 RegisterArticleSuggestionsProvider(service, profile, context); |
170 SigninManagerBase* signin_manager = | |
171 SigninManagerFactory::GetForProfile(profile); | |
172 OAuth2TokenService* token_service = | |
173 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); | |
174 scoped_refptr<net::URLRequestContextGetter> request_context = | |
175 content::BrowserContext::GetDefaultStoragePartition(context) | |
176 ->GetURLRequestContext(); | |
177 SuggestionsService* suggestions_service = | |
178 SuggestionsServiceFactory::GetForProfile(profile); | |
179 NTPSnippetsScheduler* scheduler = nullptr; | |
180 #if defined(OS_ANDROID) | |
181 scheduler = NTPSnippetsLauncher::Get(); | |
182 #endif // OS_ANDROID | |
183 base::FilePath database_dir( | |
184 profile->GetPath().Append(ntp_snippets::kDatabaseFolder)); | |
185 scoped_refptr<base::SequencedTaskRunner> task_runner = | |
186 BrowserThread::GetBlockingPool() | |
187 ->GetSequencedTaskRunnerWithShutdownBehavior( | |
188 base::SequencedWorkerPool::GetSequenceToken(), | |
189 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN); | |
190 bool is_stable_channel = | |
191 chrome::GetChannel() == version_info::Channel::STABLE; | |
192 std::unique_ptr<NTPSnippetsService> ntp_snippets_service = | |
193 base::MakeUnique<NTPSnippetsService>( | |
194 service, service->category_factory(), profile->GetPrefs(), | |
195 suggestions_service, g_browser_process->GetApplicationLocale(), | |
196 scheduler, base::MakeUnique<NTPSnippetsFetcher>( | |
197 signin_manager, token_service, request_context, | |
198 profile->GetPrefs(), | |
199 base::Bind(&safe_json::SafeJsonParser::Parse), | |
200 is_stable_channel), | |
201 base::MakeUnique<ImageFetcherImpl>( | |
202 base::MakeUnique<ImageDecoderImpl>(), request_context.get()), | |
203 base::MakeUnique<ImageDecoderImpl>(), | |
204 base::MakeUnique<NTPSnippetsDatabase>(database_dir, task_runner), | |
205 base::MakeUnique<NTPSnippetsStatusService>(signin_manager, | |
206 profile->GetPrefs())); | |
207 service->set_ntp_snippets_service(ntp_snippets_service.get()); | |
208 service->RegisterProvider(std::move(ntp_snippets_service)); | |
209 } | 224 } |
210 | 225 |
211 return service; | 226 return service; |
212 } | 227 } |
OLD | NEW |