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

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: 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;
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();
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
247 246
248 // Disables the service. Do not call directly, use |EnterState| instead. 247 // Disables the service. Do not call directly, use |EnterState| instead.
249 void EnterStateDisabled(); 248 void EnterStateDisabled();
250 249
251 // Applies the effects of the transition to the SHUT_DOWN state. Do not call 250 // Applies the effects of the transition to the SHUT_DOWN state. Do not call
252 // directly, use |EnterState| instead. 251 // directly, use |EnterState| instead.
253 void EnterStateShutdown(); 252 void EnterStateShutdown();
254 253
255 void ClearDeprecatedPrefs(); 254 void ClearDeprecatedPrefs();
256 255
256 // Converts the cached snippets to article content suggestions and notifies
257 // the observers.
258 void NotifyNewSuggestions();
259
260 // Notifies the content suggestions observer about a change in the
261 // |category_status_|.
262 void NotifyCategoryStatusChanged();
263
257 State state_; 264 State state_;
258 265
266 ContentSuggestionsCategoryStatus category_status_ =
267 ContentSuggestionsCategoryStatus::LOADING;
Marc Treib 2016/07/08 13:26:02 Do we need both this and state_? Seems that it sho
Philipp Keck 2016/07/08 14:02:55 Yes, state_ and the "disabled reason" together are
Marc Treib 2016/07/08 15:17:23 Hm. We have fairly nice handling of state transiti
Philipp Keck 2016/07/08 16:17:01 The NTPSnippetsService's state diagram is quite ne
Marc Treib 2016/07/11 08:41:33 Alright. But I still don't like how category_statu
Philipp Keck 2016/07/11 09:41:49 Done.
268
259 PrefService* pref_service_; 269 PrefService* pref_service_;
260 270
261 suggestions::SuggestionsService* suggestions_service_; 271 suggestions::SuggestionsService* suggestions_service_;
262 272
263 // All current suggestions (i.e. not discarded ones). 273 // All current suggestions (i.e. not discarded ones).
264 NTPSnippet::PtrVector snippets_; 274 NTPSnippet::PtrVector snippets_;
265 275
266 // Suggestions that the user discarded. We keep these around until they expire 276 // Suggestions that the user discarded. We keep these around until they expire
267 // so we won't re-add them on the next fetch. 277 // so we won't re-add them on the next fetch.
268 NTPSnippet::PtrVector discarded_snippets_; 278 NTPSnippet::PtrVector discarded_snippets_;
269 279
270 // The ISO 639-1 code of the language used by the application. 280 // The ISO 639-1 code of the language used by the application.
271 const std::string application_language_code_; 281 const std::string application_language_code_;
272 282
273 // The observers. 283 // The observers.
284 // TODO(pke): This is an old kind of observers that will be removed.
274 base::ObserverList<NTPSnippetsServiceObserver> observers_; 285 base::ObserverList<NTPSnippetsServiceObserver> observers_;
275 286
287 // The observer to deliver data to.
288 Observer* observer_ = nullptr;
289
276 // Scheduler for fetching snippets. Not owned. 290 // Scheduler for fetching snippets. Not owned.
277 NTPSnippetsScheduler* scheduler_; 291 NTPSnippetsScheduler* scheduler_;
278 292
279 // The subscription to the SuggestionsService. When the suggestions change, 293 // The subscription to the SuggestionsService. When the suggestions change,
280 // SuggestionsService will call |OnSuggestionsChanged|, which triggers an 294 // SuggestionsService will call |OnSuggestionsChanged|, which triggers an
281 // update to the set of snippets. 295 // update to the set of snippets.
282 using SuggestionsSubscription = 296 using SuggestionsSubscription =
283 suggestions::SuggestionsService::ResponseCallbackList::Subscription; 297 suggestions::SuggestionsService::ResponseCallbackList::Subscription;
284 std::unique_ptr<SuggestionsSubscription> suggestions_service_subscription_; 298 std::unique_ptr<SuggestionsSubscription> suggestions_service_subscription_;
285 299
(...skipping 15 matching lines...) Expand all
301 // Set to true if FetchSnippets is called before the database has been loaded. 315 // Set to true if FetchSnippets is called before the database has been loaded.
302 // The fetch will be executed after the database load finishes. 316 // The fetch will be executed after the database load finishes.
303 bool fetch_after_load_; 317 bool fetch_after_load_;
304 318
305 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); 319 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService);
306 }; 320 };
307 321
308 class NTPSnippetsServiceObserver { 322 class NTPSnippetsServiceObserver {
309 public: 323 public:
310 // Sent every time the service loads a new set of data. 324 // Sent every time the service loads a new set of data.
325 // This is called in addition to the ContentSuggestionsProvider::Observer and
326 // should only be used by snippets-internals.
327 // TODO(pke): Remove this when snippets internals don't access this service
Marc Treib 2016/07/08 13:26:02 The whole class should be removed, right? Then I'd
Philipp Keck 2016/07/08 14:02:55 Done.
328 // directly anymore.
311 virtual void NTPSnippetsServiceLoaded() = 0; 329 virtual void NTPSnippetsServiceLoaded() = 0;
312 330
313 // Sent when the service is shutting down. 331 // Sent when the service is shutting down.
314 virtual void NTPSnippetsServiceShutdown() = 0; 332 virtual void NTPSnippetsServiceShutdown() = 0;
315 333
316 // Sent when the state of the service is changing. Something changed in its 334 // Sent when the state of the service is changing. Something changed in its
317 // dependencies so it's notifying observers about incoming data changes. 335 // dependencies so it's notifying observers about incoming data changes.
318 // If the service might be enabled, DisabledReason::NONE will be provided. 336 // If the service might be enabled, DisabledReason::NONE will be provided.
319 virtual void NTPSnippetsServiceDisabledReasonChanged(DisabledReason) = 0; 337 virtual void NTPSnippetsServiceDisabledReasonChanged(DisabledReason) = 0;
320 338
321 protected: 339 protected:
322 virtual ~NTPSnippetsServiceObserver() {} 340 virtual ~NTPSnippetsServiceObserver() {}
323 }; 341 };
324 342
325 } // namespace ntp_snippets 343 } // namespace ntp_snippets
326 344
327 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ 345 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698