| OLD | NEW |
| 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/gtest_prod_util.h" | 15 #include "base/gtest_prod_util.h" |
| 16 #include "base/macros.h" | 16 #include "base/macros.h" |
| 17 #include "base/observer_list.h" | 17 #include "base/observer_list.h" |
| 18 #include "base/sequenced_task_runner.h" | 18 #include "base/sequenced_task_runner.h" |
| 19 #include "base/timer/timer.h" | 19 #include "base/timer/timer.h" |
| 20 #include "components/keyed_service/core/keyed_service.h" | 20 #include "components/keyed_service/core/keyed_service.h" |
| 21 #include "components/ntp_snippets/inner_iterator.h" | |
| 22 #include "components/ntp_snippets/ntp_snippet.h" | 21 #include "components/ntp_snippets/ntp_snippet.h" |
| 23 #include "components/ntp_snippets/ntp_snippets_fetcher.h" | 22 #include "components/ntp_snippets/ntp_snippets_fetcher.h" |
| 24 #include "components/ntp_snippets/ntp_snippets_scheduler.h" | 23 #include "components/ntp_snippets/ntp_snippets_scheduler.h" |
| 25 #include "components/suggestions/suggestions_service.h" | 24 #include "components/suggestions/suggestions_service.h" |
| 26 | 25 |
| 27 class PrefRegistrySimple; | 26 class PrefRegistrySimple; |
| 28 class PrefService; | 27 class PrefService; |
| 29 class SkBitmap; | 28 class SkBitmap; |
| 30 | 29 |
| 31 namespace base { | 30 namespace base { |
| 32 class Value; | 31 class Value; |
| 33 } | 32 } |
| 34 | 33 |
| 35 namespace image_fetcher { | 34 namespace image_fetcher { |
| 36 class ImageFetcher; | 35 class ImageFetcher; |
| 37 } | 36 } |
| 38 | 37 |
| 39 namespace suggestions { | 38 namespace suggestions { |
| 40 class SuggestionsProfile; | 39 class SuggestionsProfile; |
| 41 } | 40 } |
| 42 | 41 |
| 43 namespace ntp_snippets { | 42 namespace ntp_snippets { |
| 44 | 43 |
| 45 class NTPSnippetsServiceObserver; | 44 class NTPSnippetsServiceObserver; |
| 46 | 45 |
| 47 // Stores and vends fresh content data for the NTP. | 46 // Stores and vends fresh content data for the NTP. |
| 48 class NTPSnippetsService : public KeyedService { | 47 class NTPSnippetsService : public KeyedService { |
| 49 public: | 48 public: |
| 50 using NTPSnippetStorage = NTPSnippet::PtrVector; | 49 using NTPSnippetStorage = NTPSnippet::PtrVector; |
| 51 using const_iterator = | |
| 52 InnerIterator<NTPSnippetStorage::const_iterator, const NTPSnippet>; | |
| 53 | 50 |
| 54 using ImageFetchedCallback = | 51 using ImageFetchedCallback = |
| 55 base::Callback<void(const GURL&, const SkBitmap*)>; | 52 base::Callback<void(const GURL&, const SkBitmap*)>; |
| 56 | 53 |
| 57 // |application_language_code| should be a ISO 639-1 compliant string, e.g. | 54 // |application_language_code| should be a ISO 639-1 compliant string, e.g. |
| 58 // 'en' or 'en-US'. Note that this code should only specify the language, not | 55 // 'en' or 'en-US'. Note that this code should only specify the language, not |
| 59 // the locale, so 'en_US' (English language with US locale) and 'en-GB_US' | 56 // the locale, so 'en_US' (English language with US locale) and 'en-GB_US' |
| 60 // (British English person in the US) are not language codes. | 57 // (British English person in the US) are not language codes. |
| 61 NTPSnippetsService( | 58 NTPSnippetsService( |
| 62 PrefService* pref_service, | 59 PrefService* pref_service, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 74 | 71 |
| 75 // Inherited from KeyedService. | 72 // Inherited from KeyedService. |
| 76 void Shutdown() override; | 73 void Shutdown() override; |
| 77 | 74 |
| 78 // Fetches snippets from the server and adds them to the current ones. | 75 // Fetches snippets from the server and adds them to the current ones. |
| 79 void FetchSnippets(); | 76 void FetchSnippets(); |
| 80 // Fetches snippets from the server for specified hosts (overriding | 77 // Fetches snippets from the server for specified hosts (overriding |
| 81 // suggestions from the suggestion service) and adds them to the current ones. | 78 // suggestions from the suggestion service) and adds them to the current ones. |
| 82 void FetchSnippetsFromHosts(const std::set<std::string>& hosts); | 79 void FetchSnippetsFromHosts(const std::set<std::string>& hosts); |
| 83 | 80 |
| 81 // Available snippets. |
| 82 const NTPSnippetStorage& snippets() const { return snippets_; } |
| 83 |
| 84 // Returns the list of snippets previously discarded by the user (that are |
| 85 // not expired yet). |
| 86 const NTPSnippetStorage& discarded_snippets() const { |
| 87 return discarded_snippets_; |
| 88 } |
| 89 |
| 84 // Returns the last status message from the snippets fetcher. | 90 // Returns the last status message from the snippets fetcher. |
| 85 const std::string& last_status() const { | 91 const std::string& last_status() const { |
| 86 return snippets_fetcher_->last_status(); | 92 return snippets_fetcher_->last_status(); |
| 87 } | 93 } |
| 88 | 94 |
| 89 // Returns the last json from the snippets fetcher. | 95 // Returns the last json from the snippets fetcher. |
| 90 const std::string& last_json() const { | 96 const std::string& last_json() const { |
| 91 return snippets_fetcher_->last_json(); | 97 return snippets_fetcher_->last_json(); |
| 92 } | 98 } |
| 93 | 99 |
| 94 // (Re)schedules the periodic fetching of snippets. This is necessary because | 100 // (Re)schedules the periodic fetching of snippets. This is necessary because |
| 95 // the schedule depends on the time of day. | 101 // the schedule depends on the time of day. |
| 96 void RescheduleFetching(); | 102 void RescheduleFetching(); |
| 97 | 103 |
| 98 // Fetches the image for the given URL and notifies the observer afterwards. | 104 // Fetches the image for the given URL and notifies the observer afterwards. |
| 99 void FetchSnippetImage(const GURL& snippet_url, | 105 void FetchSnippetImage(const GURL& snippet_url, |
| 100 const ImageFetchedCallback& callback); | 106 const ImageFetchedCallback& callback); |
| 101 | 107 |
| 102 // Deletes all currently stored snippets. | 108 // Deletes all currently stored snippets. |
| 103 void ClearSnippets(); | 109 void ClearSnippets(); |
| 104 | 110 |
| 105 // Discards the snippet with the given |url|, if it exists. Returns true iff | 111 // Discards the snippet with the given |url|, if it exists. Returns true iff |
| 106 // a snippet was discarded. | 112 // a snippet was discarded. |
| 107 bool DiscardSnippet(const GURL& url); | 113 bool DiscardSnippet(const GURL& url); |
| 108 | 114 |
| 109 // Returns the list of snippets previously discarded by the user (that are | |
| 110 // not expired yet). | |
| 111 const NTPSnippetStorage& discarded_snippets() const { | |
| 112 return discarded_snippets_; | |
| 113 } | |
| 114 | |
| 115 // Clears the lists of snippets previously discarded by the user. | 115 // Clears the lists of snippets previously discarded by the user. |
| 116 void ClearDiscardedSnippets(); | 116 void ClearDiscardedSnippets(); |
| 117 | 117 |
| 118 // Returns the lists of suggestion hosts the snippets are restricted to. | 118 // Returns the lists of suggestion hosts the snippets are restricted to. |
| 119 std::set<std::string> GetSuggestionsHosts() const; | 119 std::set<std::string> GetSuggestionsHosts() const; |
| 120 | 120 |
| 121 // Observer accessors. | 121 // Observer accessors. |
| 122 void AddObserver(NTPSnippetsServiceObserver* observer); | 122 void AddObserver(NTPSnippetsServiceObserver* observer); |
| 123 void RemoveObserver(NTPSnippetsServiceObserver* observer); | 123 void RemoveObserver(NTPSnippetsServiceObserver* observer); |
| 124 | 124 |
| 125 // Number of snippets available. | |
| 126 size_t size() const { return snippets_.size(); } | |
| 127 | |
| 128 // The snippets can be iterated upon only via a const_iterator. Recommended | |
| 129 // way to iterate is as follows: | |
| 130 // | |
| 131 // NTPSnippetsService* service; // Assume is set. | |
| 132 // for (auto& snippet : *service) { | |
| 133 // // |snippet| here is a const object. | |
| 134 // } | |
| 135 const_iterator begin() const { return const_iterator(snippets_.begin()); } | |
| 136 const_iterator end() const { return const_iterator(snippets_.end()); } | |
| 137 | |
| 138 // Returns the maximum number of snippets that will be shown at once. | 125 // Returns the maximum number of snippets that will be shown at once. |
| 139 static int GetMaxSnippetCountForTesting(); | 126 static int GetMaxSnippetCountForTesting(); |
| 140 | 127 |
| 141 private: | 128 private: |
| 142 void OnSuggestionsChanged(const suggestions::SuggestionsProfile& suggestions); | 129 void OnSuggestionsChanged(const suggestions::SuggestionsProfile& suggestions); |
| 143 void OnFetchFinished(NTPSnippetsFetcher::OptionalSnippets snippets); | 130 void OnFetchFinished(NTPSnippetsFetcher::OptionalSnippets snippets); |
| 144 | 131 |
| 145 // Expects a top-level dictionary containing a "recos" list, each element of | 132 // Expects a top-level dictionary containing a "recos" list, each element of |
| 146 // which will be parsed as a snippet. | 133 // which will be parsed as a snippet. |
| 147 bool LoadFromFetchedValue(const base::Value& value); | 134 bool LoadFromFetchedValue(const base::Value& value); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 // Sent when the service is shutting down. | 197 // Sent when the service is shutting down. |
| 211 virtual void NTPSnippetsServiceShutdown() = 0; | 198 virtual void NTPSnippetsServiceShutdown() = 0; |
| 212 | 199 |
| 213 protected: | 200 protected: |
| 214 virtual ~NTPSnippetsServiceObserver() {} | 201 virtual ~NTPSnippetsServiceObserver() {} |
| 215 }; | 202 }; |
| 216 | 203 |
| 217 } // namespace ntp_snippets | 204 } // namespace ntp_snippets |
| 218 | 205 |
| 219 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ | 206 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ |
| OLD | NEW |