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

Side by Side Diff: components/ntp_snippets/ntp_snippets_service.h

Issue 2131943002: Change NTPSnippetsService to implement ContentSuggestionsProvider (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@neuerservice2
Patch Set: Only notify category status change if actually changed Created 4 years, 5 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
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 #ifndef COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ 5 #ifndef COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_
6 #define COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ 6 #define COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_
7 7
8 #include <stddef.h> 8 #include <stddef.h>
9 9
10 #include <memory> 10 #include <memory>
11 #include <set> 11 #include <set>
12 #include <string> 12 #include <string>
13 #include <vector> 13 #include <vector>
14 14
15 #include "base/callback_forward.h" 15 #include "base/callback_forward.h"
16 #include "base/gtest_prod_util.h" 16 #include "base/gtest_prod_util.h"
17 #include "base/macros.h" 17 #include "base/macros.h"
18 #include "base/observer_list.h" 18 #include "base/observer_list.h"
19 #include "base/timer/timer.h" 19 #include "base/timer/timer.h"
20 #include "components/image_fetcher/image_fetcher_delegate.h" 20 #include "components/image_fetcher/image_fetcher_delegate.h"
21 #include "components/keyed_service/core/keyed_service.h" 21 #include "components/keyed_service/core/keyed_service.h"
22 #include "components/ntp_snippets/content_suggestion.h"
23 #include "components/ntp_snippets/content_suggestions_provider.h"
22 #include "components/ntp_snippets/ntp_snippet.h" 24 #include "components/ntp_snippets/ntp_snippet.h"
23 #include "components/ntp_snippets/ntp_snippets_fetcher.h" 25 #include "components/ntp_snippets/ntp_snippets_fetcher.h"
24 #include "components/ntp_snippets/ntp_snippets_scheduler.h" 26 #include "components/ntp_snippets/ntp_snippets_scheduler.h"
25 #include "components/ntp_snippets/ntp_snippets_status_service.h" 27 #include "components/ntp_snippets/ntp_snippets_status_service.h"
26 #include "components/suggestions/suggestions_service.h" 28 #include "components/suggestions/suggestions_service.h"
27 #include "components/sync_driver/sync_service_observer.h" 29 #include "components/sync_driver/sync_service_observer.h"
28 30
29 class PrefRegistrySimple; 31 class PrefRegistrySimple;
30 class PrefService; 32 class PrefService;
31 class SigninManagerBase; 33 class SigninManagerBase;
(...skipping 20 matching lines...) Expand all
52 class SyncService; 54 class SyncService;
53 } 55 }
54 56
55 namespace ntp_snippets { 57 namespace ntp_snippets {
56 58
57 class NTPSnippetsDatabase; 59 class NTPSnippetsDatabase;
58 class NTPSnippetsServiceObserver; 60 class NTPSnippetsServiceObserver;
59 61
60 // Stores and vends fresh content data for the NTP. 62 // Stores and vends fresh content data for the NTP.
61 class NTPSnippetsService : public KeyedService, 63 class NTPSnippetsService : public KeyedService,
62 public image_fetcher::ImageFetcherDelegate { 64 public image_fetcher::ImageFetcherDelegate,
65 public ContentSuggestionsProvider {
63 public: 66 public:
64 using ImageFetchedCallback = 67 using ImageFetchedCallback =
65 base::Callback<void(const std::string& snippet_id, const gfx::Image&)>; 68 base::Callback<void(const std::string& suggestion_id, const gfx::Image&)>;
66 69
67 // |application_language_code| should be a ISO 639-1 compliant string, e.g. 70 // |application_language_code| should be a ISO 639-1 compliant string, e.g.
68 // 'en' or 'en-US'. Note that this code should only specify the language, not 71 // 'en' or 'en-US'. Note that this code should only specify the language, not
69 // the locale, so 'en_US' (English language with US locale) and 'en-GB_US' 72 // the locale, so 'en_US' (English language with US locale) and 'en-GB_US'
70 // (British English person in the US) are not language codes. 73 // (British English person in the US) are not language codes.
71 NTPSnippetsService(bool enabled, 74 NTPSnippetsService(bool enabled,
72 PrefService* pref_service, 75 PrefService* pref_service,
73 suggestions::SuggestionsService* suggestions_service, 76 suggestions::SuggestionsService* suggestions_service,
74 const std::string& application_language_code, 77 const std::string& application_language_code,
75 NTPSnippetsScheduler* scheduler, 78 NTPSnippetsScheduler* scheduler,
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 // Returns a reason why the service is disabled, or DisabledReason::NONE 122 // Returns a reason why the service is disabled, or DisabledReason::NONE
120 // if it's not. 123 // if it's not.
121 DisabledReason disabled_reason() const { 124 DisabledReason disabled_reason() const {
122 return snippets_status_service_->disabled_reason(); 125 return snippets_status_service_->disabled_reason();
123 } 126 }
124 127
125 // (Re)schedules the periodic fetching of snippets. This is necessary because 128 // (Re)schedules the periodic fetching of snippets. This is necessary because
126 // the schedule depends on the time of day. 129 // the schedule depends on the time of day.
127 void RescheduleFetching(); 130 void RescheduleFetching();
128 131
129 // Fetches the image for the snippet with the given |snippet_id| and runs the 132 // ContentSuggestionsProvider implementation
130 // |callback|. If that snippet doesn't exist or the fetch fails, the callback 133 // TODO(pke): At some point reorder the implementations in the .cc file
131 // gets a null image. 134 // accordingly.
132 void FetchSnippetImage(const std::string& snippet_id, 135 void SetObserver(Observer* observer) override;
Bernhard Bauer 2016/07/11 13:10:12 Can you add a comment about ownership of |observer
Philipp Keck 2016/07/11 13:25:01 You mean whether or not the implementation should
Marc Treib 2016/07/11 13:38:31 Pretty sure what Bernhard said was "Please add a c
Bernhard Bauer 2016/07/11 15:13:18 Yup, that. Marc knows me well ^^
Philipp Keck 2016/07/11 15:33:03 :-) Done.
133 const ImageFetchedCallback& callback); 136 ContentSuggestionsCategoryStatus GetCategoryStatus(
134 137 ContentSuggestionsCategory category) override;
135 // Deletes all currently stored snippets. 138 void DiscardSuggestion(const std::string& suggestion_id) override;
136 void ClearSnippets(); 139 void FetchSuggestionImage(const std::string& suggestion_id,
137 140 const ImageFetchedCallback& callback) override;
138 // Discards the snippet with the given |snippet_id|, if it exists. Returns 141 void ClearCachedSuggestionsForDebugging() override;
139 // true iff a snippet was discarded. 142 void ClearDiscardedSuggestionsForDebugging() override;
140 bool DiscardSnippet(const std::string& snippet_id);
141
142 // Clears the lists of snippets previously discarded by the user.
143 void ClearDiscardedSnippets();
144 143
145 // Returns the lists of suggestion hosts the snippets are restricted to. 144 // Returns the lists of suggestion hosts the snippets are restricted to.
146 std::set<std::string> GetSuggestionsHosts() const; 145 std::set<std::string> GetSuggestionsHosts() const;
147 146
148 // Observer accessors. 147 // Observer accessors.
149 void AddObserver(NTPSnippetsServiceObserver* observer); 148 void AddObserver(NTPSnippetsServiceObserver* observer);
150 void RemoveObserver(NTPSnippetsServiceObserver* observer); 149 void RemoveObserver(NTPSnippetsServiceObserver* observer);
151 150
152 // Returns the maximum number of snippets that will be shown at once. 151 // Returns the maximum number of snippets that will be shown at once.
153 static int GetMaxSnippetCountForTesting(); 152 static int GetMaxSnippetCountForTesting();
154 153
155 private: 154 private:
156 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceTest, HistorySyncStateChanges); 155 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceTest, HistorySyncStateChanges);
157 156
157 // TODO(pke): As soon as the DisabledReason is replaced with the new status,
158 // also remove the old State enum and replace it with
159 // ContentSuggestionsCategoryStatus and a similar status diagram.
158 // Possible state transitions: 160 // Possible state transitions:
159 // +------- NOT_INITED ------+ 161 // +------- NOT_INITED ------+
160 // | / \ | 162 // | / \ |
161 // | READY <--> DISABLED <-+ 163 // | READY <--> DISABLED <-+
162 // | \ / 164 // | \ /
163 // +-----> SHUT_DOWN 165 // +-----> SHUT_DOWN
164 enum class State { 166 enum class State {
165 // The service has just been created. Can change to states: 167 // The service has just been created. Can change to states:
166 // - DISABLED: if the constructor was called with |enabled == false| . In 168 // - DISABLED: if the constructor was called with |enabled == false| . In
167 // that case the service will stay disabled until it is shut 169 // that case the service will stay disabled until it is shut
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 233
232 void FetchSnippetImageFromNetwork(const std::string& snippet_id, 234 void FetchSnippetImageFromNetwork(const std::string& snippet_id,
233 const ImageFetchedCallback& callback); 235 const ImageFetchedCallback& callback);
234 236
235 // Triggers a state transition depending on the provided reason to be 237 // Triggers a state transition depending on the provided reason to be
236 // disabled (or lack thereof). This method is called when a change is detected 238 // disabled (or lack thereof). This method is called when a change is detected
237 // by |snippets_status_service_| 239 // by |snippets_status_service_|
238 void UpdateStateForStatus(DisabledReason disabled_reason); 240 void UpdateStateForStatus(DisabledReason disabled_reason);
239 241
240 // Verifies state transitions (see |State|'s documentation) and applies them. 242 // Verifies state transitions (see |State|'s documentation) and applies them.
241 // Does nothing if called with the current state. 243 // Also updates the provider status. Does nothing except updating the provider
242 void EnterState(State state); 244 // status if called with the current state.
245 void EnterState(State state, ContentSuggestionsCategoryStatus status);
243 246
244 // Enables the service and triggers a fetch if required. Do not call directly, 247 // Enables the service and triggers a fetch if required. Do not call directly,
245 // use |EnterState| instead. 248 // use |EnterState| instead.
246 void EnterStateEnabled(bool fetch_snippets); 249 void EnterStateEnabled(bool fetch_snippets);
247 250
248 // Disables the service. Do not call directly, use |EnterState| instead. 251 // Disables the service. Do not call directly, use |EnterState| instead.
249 void EnterStateDisabled(); 252 void EnterStateDisabled();
250 253
251 // Applies the effects of the transition to the SHUT_DOWN state. Do not call 254 // Applies the effects of the transition to the SHUT_DOWN state. Do not call
252 // directly, use |EnterState| instead. 255 // directly, use |EnterState| instead.
253 void EnterStateShutdown(); 256 void EnterStateShutdown();
254 257
255 void ClearDeprecatedPrefs(); 258 void ClearDeprecatedPrefs();
256 259
260 // Converts the cached snippets to article content suggestions and notifies
261 // the observers.
262 void NotifyNewSuggestions();
263
264 // Notifies the content suggestions observer about a change in the
265 // |category_status_|.
266 void NotifyCategoryStatusChanged();
267
257 State state_; 268 State state_;
258 269
270 ContentSuggestionsCategoryStatus category_status_;
271
259 PrefService* pref_service_; 272 PrefService* pref_service_;
260 273
261 suggestions::SuggestionsService* suggestions_service_; 274 suggestions::SuggestionsService* suggestions_service_;
262 275
263 // All current suggestions (i.e. not discarded ones). 276 // All current suggestions (i.e. not discarded ones).
264 NTPSnippet::PtrVector snippets_; 277 NTPSnippet::PtrVector snippets_;
265 278
266 // Suggestions that the user discarded. We keep these around until they expire 279 // Suggestions that the user discarded. We keep these around until they expire
267 // so we won't re-add them on the next fetch. 280 // so we won't re-add them on the next fetch.
268 NTPSnippet::PtrVector discarded_snippets_; 281 NTPSnippet::PtrVector discarded_snippets_;
269 282
270 // The ISO 639-1 code of the language used by the application. 283 // The ISO 639-1 code of the language used by the application.
271 const std::string application_language_code_; 284 const std::string application_language_code_;
272 285
273 // The observers. 286 // The observers.
287 // TODO(pke): This is an old kind of observers that will be removed.
274 base::ObserverList<NTPSnippetsServiceObserver> observers_; 288 base::ObserverList<NTPSnippetsServiceObserver> observers_;
275 289
290 // The observer to deliver data to.
291 Observer* observer_;
292
276 // Scheduler for fetching snippets. Not owned. 293 // Scheduler for fetching snippets. Not owned.
277 NTPSnippetsScheduler* scheduler_; 294 NTPSnippetsScheduler* scheduler_;
278 295
279 // The subscription to the SuggestionsService. When the suggestions change, 296 // The subscription to the SuggestionsService. When the suggestions change,
280 // SuggestionsService will call |OnSuggestionsChanged|, which triggers an 297 // SuggestionsService will call |OnSuggestionsChanged|, which triggers an
281 // update to the set of snippets. 298 // update to the set of snippets.
282 using SuggestionsSubscription = 299 using SuggestionsSubscription =
283 suggestions::SuggestionsService::ResponseCallbackList::Subscription; 300 suggestions::SuggestionsService::ResponseCallbackList::Subscription;
284 std::unique_ptr<SuggestionsSubscription> suggestions_service_subscription_; 301 std::unique_ptr<SuggestionsSubscription> suggestions_service_subscription_;
285 302
(...skipping 12 matching lines...) Expand all
298 // The service that provides events and data about the signin and sync state. 315 // The service that provides events and data about the signin and sync state.
299 std::unique_ptr<NTPSnippetsStatusService> snippets_status_service_; 316 std::unique_ptr<NTPSnippetsStatusService> snippets_status_service_;
300 317
301 // Set to true if FetchSnippets is called before the database has been loaded. 318 // Set to true if FetchSnippets is called before the database has been loaded.
302 // The fetch will be executed after the database load finishes. 319 // The fetch will be executed after the database load finishes.
303 bool fetch_after_load_; 320 bool fetch_after_load_;
304 321
305 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); 322 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService);
306 }; 323 };
307 324
325 // TODO(pke): Remove this when snippets internals don't access this service
326 // directly anymore.
308 class NTPSnippetsServiceObserver { 327 class NTPSnippetsServiceObserver {
309 public: 328 public:
310 // Sent every time the service loads a new set of data. 329 // Sent every time the service loads a new set of data.
311 virtual void NTPSnippetsServiceLoaded() = 0; 330 virtual void NTPSnippetsServiceLoaded() = 0;
312 331
313 // Sent when the service is shutting down. 332 // Sent when the service is shutting down.
314 virtual void NTPSnippetsServiceShutdown() = 0; 333 virtual void NTPSnippetsServiceShutdown() = 0;
315 334
316 // Sent when the state of the service is changing. Something changed in its 335 // Sent when the state of the service is changing. Something changed in its
317 // dependencies so it's notifying observers about incoming data changes. 336 // dependencies so it's notifying observers about incoming data changes.
318 // If the service might be enabled, DisabledReason::NONE will be provided. 337 // If the service might be enabled, DisabledReason::NONE will be provided.
319 virtual void NTPSnippetsServiceDisabledReasonChanged(DisabledReason) = 0; 338 virtual void NTPSnippetsServiceDisabledReasonChanged(DisabledReason) = 0;
320 339
321 protected: 340 protected:
322 virtual ~NTPSnippetsServiceObserver() {} 341 virtual ~NTPSnippetsServiceObserver() {}
323 }; 342 };
324 343
325 } // namespace ntp_snippets 344 } // namespace ntp_snippets
326 345
327 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ 346 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698