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

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

Issue 2102023002: Add ContentSuggestionsService (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Minor corrections 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #ifndef COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_
6 #define COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_
7
8 #include <stddef.h>
9
10 #include <map>
11 #include <string>
12 #include <vector>
13
14 #include "base/callback_forward.h"
15 #include "base/observer_list.h"
16 #include "components/keyed_service/core/keyed_service.h"
17 #include "components/ntp_snippets/content_suggestions_provider.h"
18 #include "components/ntp_snippets/content_suggestions_provider_type.h"
19
20 namespace gfx {
21 class Image;
22 }
23
24 namespace ntp_snippets {
25
26 // Retrieves suggestions from a number of ContentSuggestionsProviders and serves
27 // them grouped into categories.
Marc Treib 2016/06/30 10:36:54 Add a "not used yet" comment, similar to ContentSu
Philipp Keck 2016/06/30 17:14:08 Done.
28 class ContentSuggestionsService : public KeyedService,
29 public ContentSuggestionsProvider::Observer {
30 public:
31 using ImageFetchedCallback =
32 base::Callback<void(const std::string& suggestion_id, const gfx::Image&)>;
33
34 class Observer {
35 public:
36 // Fired every time the service loads a new set of data. The new data is
37 // then available through the getters of the service.
38 virtual void OnSuggestionsChanged() = 0;
tschumann 2016/06/30 10:55:19 The service interface is taking on a lot of roles.
Marc Treib 2016/06/30 11:34:23 Hm. I'm not convinced that's worth the extra compl
tschumann 2016/06/30 11:58:11 While that's true, I think it would be nice if the
Philipp Keck 2016/06/30 17:14:08 The observer here is the UI, really. The UI needs
Marc Treib 2016/07/01 09:15:32 +1. I see the point of Tim's suggestion, but I thi
Philipp Keck 2016/07/01 13:00:04 Acknowledged.
39
40 // Sent when the service is shutting down.
tschumann 2016/06/30 10:55:19 what are the post conditions after this function?
Philipp Keck 2016/06/30 17:14:09 Done.
41 virtual void ContentSuggestionsServiceShutdown() = 0;
42
43 protected:
44 virtual ~Observer() {}
45 };
46
47 ContentSuggestionsService(bool enabled);
48 ~ContentSuggestionsService() override;
49
50 // Inherited from KeyedService.
51 void Shutdown() override;
52
53 // Getters for available categories and suggestions in these categories
54 const std::vector<ContentSuggestionCategory>& GetCategories() const {
55 return categories_;
56 }
57 const std::vector<ContentSuggestion>& GetSuggestionsForCategory(
58 ContentSuggestionCategory category) const;
59
60 // Fetches the image for the suggestion with the given |suggestion_id| and
61 // runs the |callback|. If that suggestion doesn't exist or the fetch fails,
62 // the callback gets an empty image.
63 void FetchSuggestionImage(const std::string& suggestion_id,
64 const ImageFetchedCallback& callback);
65
66 // Only for debugging use through the internals page.
67 // Removes all suggestions from all caches or internal stores in all
68 // providers. It does, however, not remove any suggestions from the provider's
69 // sources, so if their configuration hasn't changed, they should return the
70 // same results when they fetch the next time. In particular, calling this
71 // method will not mark any suggestions as discarded.
72 void ClearCachedSuggestions();
73
74 // Only for debugging use through the internals page. Some providers
75 // internally store a list of discarded suggestions to prevent them from
76 // reappearing. This function clears all such lists in all providers, making
77 // discarded suggestions reappear (only for certain providers).
78 void ClearDiscardedSuggestions();
79
80 // Discards the suggestion with the given |suggestion_id|, if it exists.
81 // This won't necessarily trigger an update through the observers.
82 void DiscardSuggestion(const std::string& suggestion_id);
83
84 // Observer accessors.
85 void AddObserver(Observer* observer);
86 void RemoveObserver(Observer* observer);
87
88 // Registers a new ContentSuggestionsProvider. This method will fail if a
89 // provider of the same type is already registered.
90 void RegisterProvider(ContentSuggestionsProvider* provider);
91
92 // For use by the snippets-internals page only.
93 const std::map<ContentSuggestionsProviderType, ContentSuggestionsProvider*>&
94 providers() const {
95 return providers_;
96 }
97
98 private:
99 // Implementation of ContentSuggestionsProvider::Observer.
100 void OnSuggestionsChanged(
101 ContentSuggestionsProviderType provider_type,
102 ContentSuggestionCategory changed_category,
103 std::vector<ContentSuggestion> suggestions) override;
104
105 void OnProviderShutdown(
Marc Treib 2016/06/30 10:36:54 This is one of the cases where there usually isn't
Philipp Keck 2016/06/30 17:14:09 Yep, this empty line has been inserted because of
Marc Treib 2016/07/01 09:15:32 I'd argue that nobody who reads this header should
106 ContentSuggestionsProviderType provider_type) override;
107
108 // When |enabled_| is true the service will retrieve suggestions from the
109 // providers.
110 bool enabled_;
111
112 // All current suggestion categories, in order.
tschumann 2016/06/30 10:55:19 what's the order?
Philipp Keck 2016/06/30 17:14:09 That's not yet defined. It's a UX question, I gues
113 std::vector<ContentSuggestionCategory> categories_;
114 // All current suggestions grouped by category.
115 std::map<ContentSuggestionCategory, std::vector<ContentSuggestion>>
116 suggestions_by_category_;
Marc Treib 2016/06/30 10:36:54 Hm, maybe at this point just a vector of pairs wou
Philipp Keck 2016/06/30 17:14:09 Acknowledged.
117
118 std::map<ContentSuggestionsProviderType, ContentSuggestionsProvider*>
119 providers_;
120
121 // The observers.
Marc Treib 2016/06/30 10:36:54 This is kinda "add 1 to i" :D
tschumann 2016/06/30 10:55:19 you can drop this comment -- it's not adding any v
Philipp Keck 2016/06/30 17:14:09 Done.
122 base::ObserverList<Observer> observers_;
123
124 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsService);
125 };
126
127 } // namespace ntp_snippets
128
129 #endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698