| 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/scoped_observer.h" | 18 #include "base/scoped_observer.h" |
| 19 #include "base/sequenced_task_runner.h" | |
| 20 #include "base/timer/timer.h" | 19 #include "base/timer/timer.h" |
| 21 #include "components/keyed_service/core/keyed_service.h" | 20 #include "components/keyed_service/core/keyed_service.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 #include "components/sync_driver/sync_service_observer.h" | 25 #include "components/sync_driver/sync_service_observer.h" |
| 27 | 26 |
| 28 class PrefRegistrySimple; | 27 class PrefRegistrySimple; |
| 29 class PrefService; | 28 class PrefService; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 43 namespace suggestions { | 42 namespace suggestions { |
| 44 class SuggestionsProfile; | 43 class SuggestionsProfile; |
| 45 } | 44 } |
| 46 | 45 |
| 47 namespace sync_driver { | 46 namespace sync_driver { |
| 48 class SyncService; | 47 class SyncService; |
| 49 } | 48 } |
| 50 | 49 |
| 51 namespace ntp_snippets { | 50 namespace ntp_snippets { |
| 52 | 51 |
| 52 class NTPSnippetsDatabase; |
| 53 class NTPSnippetsServiceObserver; | 53 class NTPSnippetsServiceObserver; |
| 54 | 54 |
| 55 // Stores and vends fresh content data for the NTP. | 55 // Stores and vends fresh content data for the NTP. |
| 56 class NTPSnippetsService : public KeyedService, | 56 class NTPSnippetsService : public KeyedService, |
| 57 public sync_driver::SyncServiceObserver { | 57 public sync_driver::SyncServiceObserver { |
| 58 public: | 58 public: |
| 59 using ImageFetchedCallback = | 59 using ImageFetchedCallback = |
| 60 base::Callback<void(const std::string& snippet_id, const gfx::Image&)>; | 60 base::Callback<void(const std::string& snippet_id, const gfx::Image&)>; |
| 61 | 61 |
| 62 // |application_language_code| should be a ISO 639-1 compliant string, e.g. | 62 // |application_language_code| should be a ISO 639-1 compliant string, e.g. |
| 63 // 'en' or 'en-US'. Note that this code should only specify the language, not | 63 // 'en' or 'en-US'. Note that this code should only specify the language, not |
| 64 // the locale, so 'en_US' (English language with US locale) and 'en-GB_US' | 64 // the locale, so 'en_US' (English language with US locale) and 'en-GB_US' |
| 65 // (British English person in the US) are not language codes. | 65 // (British English person in the US) are not language codes. |
| 66 NTPSnippetsService( | 66 NTPSnippetsService(bool enabled, |
| 67 bool enabled, | 67 PrefService* pref_service, |
| 68 PrefService* pref_service, | 68 sync_driver::SyncService* sync_service, |
| 69 sync_driver::SyncService* sync_service, | 69 suggestions::SuggestionsService* suggestions_service, |
| 70 suggestions::SuggestionsService* suggestions_service, | 70 const std::string& application_language_code, |
| 71 scoped_refptr<base::SequencedTaskRunner> file_task_runner, | 71 NTPSnippetsScheduler* scheduler, |
| 72 const std::string& application_language_code, | 72 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher, |
| 73 NTPSnippetsScheduler* scheduler, | 73 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher, |
| 74 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher, | 74 std::unique_ptr<NTPSnippetsDatabase> database); |
| 75 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher); | |
| 76 ~NTPSnippetsService() override; | 75 ~NTPSnippetsService() override; |
| 77 | 76 |
| 78 static void RegisterProfilePrefs(PrefRegistrySimple* registry); | 77 static void RegisterProfilePrefs(PrefRegistrySimple* registry); |
| 79 | 78 |
| 80 // Inherited from KeyedService. | 79 // Inherited from KeyedService. |
| 81 void Shutdown() override; | 80 void Shutdown() override; |
| 82 | 81 |
| 82 // Returns whether the initial set of snippets has been loaded from the |
| 83 // database. While this is false, the list of snippets will be empty, and all |
| 84 // modifications to it (fetch, discard, etc) will be ignored. |
| 85 bool loaded() const { return state_ == State::LOADED; } |
| 86 |
| 83 // Fetches snippets from the server and adds them to the current ones. | 87 // Fetches snippets from the server and adds them to the current ones. |
| 84 void FetchSnippets(); | 88 void FetchSnippets(); |
| 85 // Fetches snippets from the server for specified hosts (overriding | 89 // Fetches snippets from the server for specified hosts (overriding |
| 86 // suggestions from the suggestion service) and adds them to the current ones. | 90 // suggestions from the suggestion service) and adds them to the current ones. |
| 87 void FetchSnippetsFromHosts(const std::set<std::string>& hosts); | 91 void FetchSnippetsFromHosts(const std::set<std::string>& hosts); |
| 88 | 92 |
| 89 // Available snippets. | 93 // Available snippets. |
| 90 const NTPSnippet::PtrVector& snippets() const { return snippets_; } | 94 const NTPSnippet::PtrVector& snippets() const { return snippets_; } |
| 91 | 95 |
| 92 // Returns the list of snippets previously discarded by the user (that are | 96 // Returns the list of snippets previously discarded by the user (that are |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 | 135 |
| 132 private: | 136 private: |
| 133 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceWithSyncTest, | 137 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceWithSyncTest, |
| 134 SyncStateCompatibility); | 138 SyncStateCompatibility); |
| 135 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceWithSyncTest, | 139 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceWithSyncTest, |
| 136 HistorySyncStateChanges); | 140 HistorySyncStateChanges); |
| 137 | 141 |
| 138 // sync_driver::SyncServiceObserver implementation. | 142 // sync_driver::SyncServiceObserver implementation. |
| 139 void OnStateChanged() override; | 143 void OnStateChanged() override; |
| 140 | 144 |
| 145 // Callback for the NTPSnippetsDatabase. |
| 146 void OnDatabaseLoaded(NTPSnippet::PtrVector snippets); |
| 147 |
| 148 // Callback for the SuggestionsService. |
| 141 void OnSuggestionsChanged(const suggestions::SuggestionsProfile& suggestions); | 149 void OnSuggestionsChanged(const suggestions::SuggestionsProfile& suggestions); |
| 150 |
| 151 // Callback for the NTPSnippetsFetcher. |
| 142 void OnFetchFinished(NTPSnippetsFetcher::OptionalSnippets snippets); | 152 void OnFetchFinished(NTPSnippetsFetcher::OptionalSnippets snippets); |
| 143 | 153 |
| 144 // Merges newly available snippets with the previously available list. | 154 // Merges newly available snippets with the previously available list. |
| 145 void MergeSnippets(NTPSnippet::PtrVector new_snippets); | 155 void MergeSnippets(NTPSnippet::PtrVector new_snippets); |
| 146 // TODO(treib): Investigate a better storage, maybe LevelDB or SQLite? | |
| 147 void LoadSnippetsFromPrefs(); | |
| 148 void StoreSnippetsToPrefs(); | |
| 149 | |
| 150 void LoadDiscardedSnippetsFromPrefs(); | |
| 151 void StoreDiscardedSnippetsToPrefs(); | |
| 152 | 156 |
| 153 std::set<std::string> GetSnippetHostsFromPrefs() const; | 157 std::set<std::string> GetSnippetHostsFromPrefs() const; |
| 154 void StoreSnippetHostsToPrefs(const std::set<std::string>& hosts); | 158 void StoreSnippetHostsToPrefs(const std::set<std::string>& hosts); |
| 155 | 159 |
| 156 void LoadingSnippetsFinished(); | 160 void LoadingSnippetsFinished(); |
| 157 | 161 |
| 158 // Checks whether the state of the sync service is incompatible with showing | 162 // Checks whether the state of the sync service is incompatible with showing |
| 159 // snippets. We need history sync to be active. | 163 // snippets. We need history sync to be active. |
| 160 // Note: The state is considered compatible if the service is still | 164 // Note: The state is considered compatible if the service is still |
| 161 // initializing and the sync state is not known. | 165 // initializing and the sync state is not known. |
| 162 bool IsSyncStateIncompatible(); | 166 bool IsSyncStateIncompatible(); |
| 163 | 167 |
| 164 enum class State { | 168 enum class State { |
| 165 INITED, | 169 INITED, // Initial state before the DB has been loaded; no snippets yet. |
| 166 SHUT_DOWN | 170 LOADED, // DB has been loaded and the service is ready for action. |
| 171 SHUT_DOWN // Shutdown has been called, service is inactive. |
| 167 } state_; | 172 } state_; |
| 168 | 173 |
| 169 // When |enabled_| is true the service will fetch snippets from the server | 174 // When |enabled_| is true the service will fetch snippets from the server |
| 170 // using |snippets_fetcher_|, load snippets from device storage, and schedule | 175 // using |snippets_fetcher_|, load snippets from device storage, and schedule |
| 171 // the |scheduler_|. | 176 // the |scheduler_|. |
| 172 bool enabled_; | 177 bool enabled_; |
| 173 | 178 |
| 174 PrefService* pref_service_; | 179 PrefService* pref_service_; |
| 175 | 180 |
| 176 sync_driver::SyncService* sync_service_; | 181 sync_driver::SyncService* sync_service_; |
| 177 | 182 |
| 178 // The observer for the SyncService. When the sync state changes, | 183 // The observer for the SyncService. When the sync state changes, |
| 179 // SyncService will call |OnStateChanged|, which is propagated to the | 184 // SyncService will call |OnStateChanged|, which is propagated to the |
| 180 // snippet observers. | 185 // snippet observers. |
| 181 ScopedObserver<sync_driver::SyncService, sync_driver::SyncServiceObserver> | 186 ScopedObserver<sync_driver::SyncService, sync_driver::SyncServiceObserver> |
| 182 sync_service_observer_; | 187 sync_service_observer_; |
| 183 | 188 |
| 184 suggestions::SuggestionsService* suggestions_service_; | 189 suggestions::SuggestionsService* suggestions_service_; |
| 185 | 190 |
| 186 // The SequencedTaskRunner on which file system operations will be run. | |
| 187 scoped_refptr<base::SequencedTaskRunner> file_task_runner_; | |
| 188 | |
| 189 // All current suggestions (i.e. not discarded ones). | 191 // All current suggestions (i.e. not discarded ones). |
| 190 NTPSnippet::PtrVector snippets_; | 192 NTPSnippet::PtrVector snippets_; |
| 191 | 193 |
| 192 // Suggestions that the user discarded. We keep these around until they expire | 194 // Suggestions that the user discarded. We keep these around until they expire |
| 193 // so we won't re-add them on the next fetch. | 195 // so we won't re-add them on the next fetch. |
| 194 NTPSnippet::PtrVector discarded_snippets_; | 196 NTPSnippet::PtrVector discarded_snippets_; |
| 195 | 197 |
| 196 // The ISO 639-1 code of the language used by the application. | 198 // The ISO 639-1 code of the language used by the application. |
| 197 const std::string application_language_code_; | 199 const std::string application_language_code_; |
| 198 | 200 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 210 std::unique_ptr<SuggestionsSubscription> suggestions_service_subscription_; | 212 std::unique_ptr<SuggestionsSubscription> suggestions_service_subscription_; |
| 211 | 213 |
| 212 // The snippets fetcher. | 214 // The snippets fetcher. |
| 213 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_; | 215 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_; |
| 214 | 216 |
| 215 // Timer that calls us back when the next snippet expires. | 217 // Timer that calls us back when the next snippet expires. |
| 216 base::OneShotTimer expiry_timer_; | 218 base::OneShotTimer expiry_timer_; |
| 217 | 219 |
| 218 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher_; | 220 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher_; |
| 219 | 221 |
| 222 // The database for persisting snippets. |
| 223 std::unique_ptr<NTPSnippetsDatabase> database_; |
| 224 |
| 220 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); | 225 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); |
| 221 }; | 226 }; |
| 222 | 227 |
| 223 class NTPSnippetsServiceObserver { | 228 class NTPSnippetsServiceObserver { |
| 224 public: | 229 public: |
| 225 // Sent every time the service loads a new set of data. | 230 // Sent every time the service loads a new set of data. |
| 226 virtual void NTPSnippetsServiceLoaded() = 0; | 231 virtual void NTPSnippetsServiceLoaded() = 0; |
| 227 // Sent when the service is shutting down. | 232 // Sent when the service is shutting down. |
| 228 virtual void NTPSnippetsServiceShutdown() = 0; | 233 virtual void NTPSnippetsServiceShutdown() = 0; |
| 229 // Sent when the service has been disabled. Can be from explicit user action | 234 // Sent when the service has been disabled. Can be from explicit user action |
| 230 // or because a requirement (e.g. History Sync) is not fulfilled anymore. | 235 // or because a requirement (e.g. History Sync) is not fulfilled anymore. |
| 231 virtual void NTPSnippetsServiceDisabled() = 0; | 236 virtual void NTPSnippetsServiceDisabled() = 0; |
| 232 | 237 |
| 233 protected: | 238 protected: |
| 234 virtual ~NTPSnippetsServiceObserver() {} | 239 virtual ~NTPSnippetsServiceObserver() {} |
| 235 }; | 240 }; |
| 236 | 241 |
| 237 } // namespace ntp_snippets | 242 } // namespace ntp_snippets |
| 238 | 243 |
| 239 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ | 244 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ |
| OLD | NEW |