Chromium Code Reviews| 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 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 115 // Clears the lists of snippets previously discarded by the user. | 112 // Clears the lists of snippets previously discarded by the user. |
| 116 void ClearDiscardedSnippets(); | 113 void ClearDiscardedSnippets(); |
| 117 | 114 |
| 118 // Returns the lists of suggestion hosts the snippets are restricted to. | 115 // Returns the lists of suggestion hosts the snippets are restricted to. |
| 119 std::set<std::string> GetSuggestionsHosts() const; | 116 std::set<std::string> GetSuggestionsHosts() const; |
| 120 | 117 |
| 121 // Observer accessors. | 118 // Observer accessors. |
| 122 void AddObserver(NTPSnippetsServiceObserver* observer); | 119 void AddObserver(NTPSnippetsServiceObserver* observer); |
| 123 void RemoveObserver(NTPSnippetsServiceObserver* observer); | 120 void RemoveObserver(NTPSnippetsServiceObserver* observer); |
| 124 | 121 |
| 125 // Number of snippets available. | 122 // Available snippets. |
| 126 size_t size() const { return snippets_.size(); } | 123 const NTPSnippetStorage& snippets() const { return snippets_; } |
|
Marc Treib
2016/05/10 07:28:50
Not your doing, but the ordering is a bit weird he
mastiz
2016/05/10 07:35:16
Done, moved *after* FetchSnippetsFromHosts(), to s
| |
| 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 | 124 |
| 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(NTPSnippetStorage snippets, const std::string& status); | 130 void OnFetchFinished(NTPSnippetStorage snippets, const std::string& status); |
| 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. |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 212 // Sent when the service is shutting down. | 199 // Sent when the service is shutting down. |
| 213 virtual void NTPSnippetsServiceShutdown() = 0; | 200 virtual void NTPSnippetsServiceShutdown() = 0; |
| 214 | 201 |
| 215 protected: | 202 protected: |
| 216 virtual ~NTPSnippetsServiceObserver() {} | 203 virtual ~NTPSnippetsServiceObserver() {} |
| 217 }; | 204 }; |
| 218 | 205 |
| 219 } // namespace ntp_snippets | 206 } // namespace ntp_snippets |
| 220 | 207 |
| 221 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ | 208 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ |
| OLD | NEW |