| 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 <set> | 11 #include <set> |
| 11 #include <string> | 12 #include <string> |
| 12 #include <vector> | 13 #include <vector> |
| 13 | 14 |
| 14 #include "base/gtest_prod_util.h" | 15 #include "base/gtest_prod_util.h" |
| 15 #include "base/macros.h" | 16 #include "base/macros.h" |
| 16 #include "base/memory/weak_ptr.h" | 17 #include "base/memory/weak_ptr.h" |
| 17 #include "base/observer_list.h" | 18 #include "base/observer_list.h" |
| 18 #include "base/sequenced_task_runner.h" | 19 #include "base/sequenced_task_runner.h" |
| 19 #include "base/timer/timer.h" | 20 #include "base/timer/timer.h" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 36 class SuggestionsProfile; | 37 class SuggestionsProfile; |
| 37 } | 38 } |
| 38 | 39 |
| 39 namespace ntp_snippets { | 40 namespace ntp_snippets { |
| 40 | 41 |
| 41 class NTPSnippetsServiceObserver; | 42 class NTPSnippetsServiceObserver; |
| 42 | 43 |
| 43 // Stores and vends fresh content data for the NTP. | 44 // Stores and vends fresh content data for the NTP. |
| 44 class NTPSnippetsService : public KeyedService { | 45 class NTPSnippetsService : public KeyedService { |
| 45 public: | 46 public: |
| 46 using NTPSnippetStorage = std::vector<scoped_ptr<NTPSnippet>>; | 47 using NTPSnippetStorage = std::vector<std::unique_ptr<NTPSnippet>>; |
| 47 using const_iterator = | 48 using const_iterator = |
| 48 InnerIterator<NTPSnippetStorage::const_iterator, const NTPSnippet>; | 49 InnerIterator<NTPSnippetStorage::const_iterator, const NTPSnippet>; |
| 49 | 50 |
| 50 // Callbacks for JSON parsing. | 51 // Callbacks for JSON parsing. |
| 51 using SuccessCallback = base::Callback<void(scoped_ptr<base::Value>)>; | 52 using SuccessCallback = base::Callback<void(std::unique_ptr<base::Value>)>; |
| 52 using ErrorCallback = base::Callback<void(const std::string&)>; | 53 using ErrorCallback = base::Callback<void(const std::string&)>; |
| 53 using ParseJSONCallback = base::Callback< | 54 using ParseJSONCallback = base::Callback< |
| 54 void(const std::string&, const SuccessCallback&, const ErrorCallback&)>; | 55 void(const std::string&, const SuccessCallback&, const ErrorCallback&)>; |
| 55 | 56 |
| 56 // |application_language_code| should be a ISO 639-1 compliant string, e.g. | 57 // |application_language_code| should be a ISO 639-1 compliant string, e.g. |
| 57 // 'en' or 'en-US'. Note that this code should only specify the language, not | 58 // 'en' or 'en-US'. Note that this code should only specify the language, not |
| 58 // the locale, so 'en_US' (English language with US locale) and 'en-GB_US' | 59 // the locale, so 'en_US' (English language with US locale) and 'en-GB_US' |
| 59 // (British English person in the US) are not language codes. | 60 // (British English person in the US) are not language codes. |
| 60 NTPSnippetsService(PrefService* pref_service, | 61 NTPSnippetsService(PrefService* pref_service, |
| 61 suggestions::SuggestionsService* suggestions_service, | 62 suggestions::SuggestionsService* suggestions_service, |
| 62 scoped_refptr<base::SequencedTaskRunner> file_task_runner, | 63 scoped_refptr<base::SequencedTaskRunner> file_task_runner, |
| 63 const std::string& application_language_code, | 64 const std::string& application_language_code, |
| 64 NTPSnippetsScheduler* scheduler, | 65 NTPSnippetsScheduler* scheduler, |
| 65 scoped_ptr<NTPSnippetsFetcher> snippets_fetcher, | 66 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher, |
| 66 const ParseJSONCallback& parse_json_callback); | 67 const ParseJSONCallback& parse_json_callback); |
| 67 ~NTPSnippetsService() override; | 68 ~NTPSnippetsService() override; |
| 68 | 69 |
| 69 static void RegisterProfilePrefs(PrefRegistrySimple* registry); | 70 static void RegisterProfilePrefs(PrefRegistrySimple* registry); |
| 70 | 71 |
| 71 void Init(bool enabled); | 72 void Init(bool enabled); |
| 72 | 73 |
| 73 // Inherited from KeyedService. | 74 // Inherited from KeyedService. |
| 74 void Shutdown() override; | 75 void Shutdown() override; |
| 75 | 76 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 static int GetMaxSnippetCountForTesting(); | 129 static int GetMaxSnippetCountForTesting(); |
| 129 | 130 |
| 130 private: | 131 private: |
| 131 friend class NTPSnippetsServiceTest; | 132 friend class NTPSnippetsServiceTest; |
| 132 | 133 |
| 133 void OnSuggestionsChanged(const suggestions::SuggestionsProfile& suggestions); | 134 void OnSuggestionsChanged(const suggestions::SuggestionsProfile& suggestions); |
| 134 void OnSnippetsDownloaded(const std::string& snippets_json, | 135 void OnSnippetsDownloaded(const std::string& snippets_json, |
| 135 const std::string& status); | 136 const std::string& status); |
| 136 | 137 |
| 137 void OnJsonParsed(const std::string& snippets_json, | 138 void OnJsonParsed(const std::string& snippets_json, |
| 138 scoped_ptr<base::Value> parsed); | 139 std::unique_ptr<base::Value> parsed); |
| 139 void OnJsonError(const std::string& snippets_json, const std::string& error); | 140 void OnJsonError(const std::string& snippets_json, const std::string& error); |
| 140 | 141 |
| 141 // Expects a top-level dictionary containing a "recos" list, which will be | 142 // Expects a top-level dictionary containing a "recos" list, which will be |
| 142 // passed to LoadFromListValue(). | 143 // passed to LoadFromListValue(). |
| 143 bool LoadFromValue(const base::Value& value); | 144 bool LoadFromValue(const base::Value& value); |
| 144 | 145 |
| 145 // Expects a list of dictionaries each containing a "contentInfo" dictionary | 146 // Expects a list of dictionaries each containing a "contentInfo" dictionary |
| 146 // with keys matching the properties of a snippet (url, title, site_title, | 147 // with keys matching the properties of a snippet (url, title, site_title, |
| 147 // etc...). The URL is the only mandatory value. | 148 // etc...). The URL is the only mandatory value. |
| 148 bool LoadFromListValue(const base::ListValue& list); | 149 bool LoadFromListValue(const base::ListValue& list); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 base::ObserverList<NTPSnippetsServiceObserver> observers_; | 183 base::ObserverList<NTPSnippetsServiceObserver> observers_; |
| 183 | 184 |
| 184 // Scheduler for fetching snippets. Not owned. | 185 // Scheduler for fetching snippets. Not owned. |
| 185 NTPSnippetsScheduler* scheduler_; | 186 NTPSnippetsScheduler* scheduler_; |
| 186 | 187 |
| 187 // The subscription to the SuggestionsService. When the suggestions change, | 188 // The subscription to the SuggestionsService. When the suggestions change, |
| 188 // SuggestionsService will call |OnSuggestionsChanged|, which triggers an | 189 // SuggestionsService will call |OnSuggestionsChanged|, which triggers an |
| 189 // update to the set of snippets. | 190 // update to the set of snippets. |
| 190 using SuggestionsSubscription = | 191 using SuggestionsSubscription = |
| 191 suggestions::SuggestionsService::ResponseCallbackList::Subscription; | 192 suggestions::SuggestionsService::ResponseCallbackList::Subscription; |
| 192 scoped_ptr<SuggestionsSubscription> suggestions_service_subscription_; | 193 std::unique_ptr<SuggestionsSubscription> suggestions_service_subscription_; |
| 193 | 194 |
| 194 // The snippets fetcher. | 195 // The snippets fetcher. |
| 195 scoped_ptr<NTPSnippetsFetcher> snippets_fetcher_; | 196 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_; |
| 196 | 197 |
| 197 // The subscription to the snippets fetcher. | 198 // The subscription to the snippets fetcher. |
| 198 scoped_ptr<NTPSnippetsFetcher::SnippetsAvailableCallbackList::Subscription> | 199 std::unique_ptr< |
| 200 NTPSnippetsFetcher::SnippetsAvailableCallbackList::Subscription> |
| 199 snippets_fetcher_subscription_; | 201 snippets_fetcher_subscription_; |
| 200 | 202 |
| 201 std::string last_fetch_status_; | 203 std::string last_fetch_status_; |
| 202 | 204 |
| 203 // Timer that calls us back when the next snippet expires. | 205 // Timer that calls us back when the next snippet expires. |
| 204 base::OneShotTimer expiry_timer_; | 206 base::OneShotTimer expiry_timer_; |
| 205 | 207 |
| 206 ParseJSONCallback parse_json_callback_; | 208 ParseJSONCallback parse_json_callback_; |
| 207 | 209 |
| 208 base::WeakPtrFactory<NTPSnippetsService> weak_ptr_factory_; | 210 base::WeakPtrFactory<NTPSnippetsService> weak_ptr_factory_; |
| 209 | 211 |
| 210 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); | 212 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); |
| 211 }; | 213 }; |
| 212 | 214 |
| 213 class NTPSnippetsServiceObserver { | 215 class NTPSnippetsServiceObserver { |
| 214 public: | 216 public: |
| 215 // Sent every time the service loads a new set of data. | 217 // Sent every time the service loads a new set of data. |
| 216 virtual void NTPSnippetsServiceLoaded() = 0; | 218 virtual void NTPSnippetsServiceLoaded() = 0; |
| 217 // Sent when the service is shutting down. | 219 // Sent when the service is shutting down. |
| 218 virtual void NTPSnippetsServiceShutdown() = 0; | 220 virtual void NTPSnippetsServiceShutdown() = 0; |
| 219 | 221 |
| 220 protected: | 222 protected: |
| 221 virtual ~NTPSnippetsServiceObserver() {} | 223 virtual ~NTPSnippetsServiceObserver() {} |
| 222 }; | 224 }; |
| 223 | 225 |
| 224 } // namespace ntp_snippets | 226 } // namespace ntp_snippets |
| 225 | 227 |
| 226 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ | 228 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ |
| OLD | NEW |