Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 <set> | |
|
Marc Treib
2016/06/28 11:29:10
not needed
Philipp Keck
2016/06/28 14:18:57
Done.
| |
| 12 #include <string> | |
| 13 #include <vector> | |
| 14 | |
|
Marc Treib
2016/06/28 11:29:10
#include "base/callback_forward.h"
Philipp Keck
2016/06/28 14:18:56
Done.
| |
| 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 class ContentSuggestionsServiceObserver; | |
| 27 | |
| 28 // Retrieves suggestions from a number of ContentSuggestionsProviders and serves | |
| 29 // them to the NTP. | |
|
Marc Treib
2016/06/28 11:29:09
I'd remove the reference to the NTP - that is not
Philipp Keck
2016/06/28 14:18:56
Done.
| |
| 30 class ContentSuggestionsService : public KeyedService, | |
| 31 public ContentSuggestionsProvider::Delegate { | |
| 32 public: | |
| 33 using ImageFetchedCallback = | |
| 34 base::Callback<void(const std::string& suggestion_id, const gfx::Image&)>; | |
| 35 | |
| 36 ContentSuggestionsService(bool enabled); | |
| 37 ~ContentSuggestionsService() override; | |
| 38 | |
| 39 // Inherited from KeyedService. | |
| 40 void Shutdown() override; | |
| 41 | |
| 42 // Available suggestions | |
| 43 const std::map<ContentSuggestionCategory, std::vector<ContentSuggestion>>& | |
|
Marc Treib
2016/06/28 11:29:09
Does this need to be exposed at all? If so, maybe
Philipp Keck
2016/06/28 14:18:56
You're right, but I'm not sure which solution is t
Marc Treib
2016/06/28 15:04:55
No, it would go over suggestions_by_category_ and
Philipp Keck
2016/06/30 09:35:35
Done. Changed OnSuggestionsChanged to not be calle
| |
| 44 suggestions() const { | |
| 45 return suggestions_; | |
| 46 } | |
| 47 const std::vector<ContentSuggestion>& suggestions( | |
|
Marc Treib
2016/06/28 11:29:10
Please give this a better name, something like "Ge
Philipp Keck
2016/06/28 14:18:56
Acknowledged.
Philipp Keck
2016/06/30 09:35:35
Done.
| |
| 48 ContentSuggestionCategory category) { | |
|
Marc Treib
2016/06/28 11:29:09
Should be const.
Philipp Keck
2016/06/28 14:18:56
Acknowledged.
| |
| 49 return suggestions_[category]; | |
| 50 } | |
| 51 | |
| 52 // Fetches the image for the suggestion with the given |suggestion_id| and | |
| 53 // runs the |callback|. If that suggestion doesn't exist or the fetch fails, | |
| 54 // the callback gets an empty image. | |
| 55 void FetchImage(const ContentSuggestionsProviderType provider_type, | |
| 56 const std::string& suggestion_id, | |
| 57 const ImageFetchedCallback& callback); | |
| 58 | |
| 59 // Removes all suggestions from all caches or internal stores in all | |
| 60 // providers. It does, however, not remove any suggestions from the provider's | |
| 61 // sources, so if their configuration hasn't changed, they should return the | |
| 62 // same results when they fetch the next time. In particular, calling this | |
| 63 // method will not mark any suggestions as discarded. | |
| 64 void ClearSuggestions(); | |
|
Marc Treib
2016/06/28 11:29:09
I think this is also used only for debugging purpo
Philipp Keck
2016/06/28 14:18:57
Done, renamed here and in the provider interface.
| |
| 65 | |
| 66 // Only for debugging use through the internals page. Some providers | |
| 67 // internally store a list of discarded suggestions to prevent them from | |
| 68 // reappearing. This function clears all such lists in all providers, making | |
| 69 // discarded suggestions reappear (only for certain providers). | |
| 70 void ClearDiscardedSuggestions(); | |
| 71 | |
| 72 // Discards the suggestion with the given |suggestion_id|, if it exists. | |
| 73 void Discard(const ContentSuggestionsProviderType provider_type, | |
|
Marc Treib
2016/06/28 11:29:09
No "const" if you pass by value.
Philipp Keck
2016/06/28 14:18:56
Done, fixed here and in FetchImage() above.
| |
| 74 const std::string& suggestion_id); | |
| 75 | |
| 76 // Observer accessors (outgoing observers, from this service towards UI) | |
|
Marc Treib
2016/06/28 11:29:09
I'd remove the part in parens - "outgoing" is obvi
Philipp Keck
2016/06/28 14:18:56
Done.
| |
| 77 void AddObserver(ContentSuggestionsServiceObserver* observer); | |
| 78 void RemoveObserver(ContentSuggestionsServiceObserver* observer); | |
| 79 | |
| 80 // Implementation of ContentSuggestionsProvider::Delegate (incoming data) | |
|
Marc Treib
2016/06/28 11:29:09
These can (and should) be private.
Also, nit: peri
Philipp Keck
2016/06/28 14:18:56
Aha ok? Done.
| |
| 81 void OnContentChanged(const ContentSuggestionsProvider& source, | |
| 82 ContentSuggestionCategory changed_category, | |
| 83 std::vector<ContentSuggestion> suggestions) override; | |
| 84 void OnProviderShutdown(const ContentSuggestionsProvider& source) override; | |
| 85 | |
| 86 // Registers a new ContentSuggestionsProvider. The service will call | |
| 87 // ContentSuggestionsProvider::setDelegate with a delegate object, through | |
|
Marc Treib
2016/06/28 11:29:10
I'd remove the "The service..." sentence, since it
Philipp Keck
2016/06/28 14:18:57
Done.
| |
| 88 // which the registered provider should report its new suggestions. This | |
| 89 // method will fail when a 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 // When |enabled_| is true the service will retrieve suggestions from the | |
| 100 // providers. | |
| 101 bool enabled_; | |
| 102 | |
| 103 // All current suggestions. | |
| 104 std::map<ContentSuggestionCategory, std::vector<ContentSuggestion>> | |
| 105 suggestions_; | |
|
Marc Treib
2016/06/28 11:29:09
suggestions_by_category_?
Philipp Keck
2016/06/28 14:18:56
Done.
| |
| 106 | |
| 107 std::map<ContentSuggestionsProviderType, ContentSuggestionsProvider*> | |
| 108 providers_; | |
| 109 | |
| 110 // The observers. | |
| 111 base::ObserverList<ContentSuggestionsServiceObserver> observers_; | |
| 112 | |
| 113 // Helper method to remove all suggestions of the given |provider_type| from | |
| 114 // the given |list|. Returns true if anything changed. | |
| 115 bool RemoveSuggestionsOfProvider( | |
| 116 std::vector<ContentSuggestion>* list, | |
| 117 ContentSuggestionsProviderType provider_type); | |
|
Marc Treib
2016/06/28 11:29:10
Private methods come before member variables. But
Philipp Keck
2016/06/28 14:18:56
Done.
| |
| 118 | |
| 119 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsService); | |
| 120 }; | |
| 121 | |
| 122 class ContentSuggestionsServiceObserver { | |
|
Marc Treib
2016/06/28 11:29:10
I'd make this an inner class of the service (just
Philipp Keck
2016/06/28 14:18:56
This implementation is analogous to NTPSnippetsSer
Marc Treib
2016/06/28 15:04:55
I don't know of any reasons for having it separate
Philipp Keck
2016/06/30 09:35:35
Yes, as soon as the internals page is able to get
| |
| 123 public: | |
| 124 // Fired every time the service loads a new set of data. This event is fired | |
| 125 // per |changed_category|. The new data is then available through | |
| 126 // |suggestions()[changed_category]|. | |
|
Marc Treib
2016/06/28 11:29:09
Please update to refer to the (proposed) GetSugges
Philipp Keck
2016/06/28 14:18:56
Acknowledged.
| |
| 127 virtual void OnSuggestionsChanged( | |
| 128 ContentSuggestionCategory changed_category) = 0; | |
| 129 // Sent when the service is shutting down. | |
| 130 virtual void ContentSuggestionsServiceShutdown() = 0; | |
| 131 | |
| 132 protected: | |
| 133 virtual ~ContentSuggestionsServiceObserver() {} | |
| 134 }; | |
| 135 | |
| 136 } // namespace ntp_snippets | |
| 137 | |
| 138 #endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_ | |
| OLD | NEW |