| 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. |
| 84 bool loaded() const { return state_ == State::LOADED; } |
| 85 |
| 83 // Fetches snippets from the server and adds them to the current ones. | 86 // Fetches snippets from the server and adds them to the current ones. |
| 84 void FetchSnippets(); | 87 void FetchSnippets(); |
| 85 // Fetches snippets from the server for specified hosts (overriding | 88 // Fetches snippets from the server for specified hosts (overriding |
| 86 // suggestions from the suggestion service) and adds them to the current ones. | 89 // suggestions from the suggestion service) and adds them to the current ones. |
| 90 // Only called from chrome://snippets-internals, DO NOT USE otherwise! |
| 91 // Ignored while |loaded()| is false. |
| 87 void FetchSnippetsFromHosts(const std::set<std::string>& hosts); | 92 void FetchSnippetsFromHosts(const std::set<std::string>& hosts); |
| 88 | 93 |
| 89 // Available snippets. | 94 // Available snippets. |
| 90 const NTPSnippet::PtrVector& snippets() const { return snippets_; } | 95 const NTPSnippet::PtrVector& snippets() const { return snippets_; } |
| 91 | 96 |
| 92 // Returns the list of snippets previously discarded by the user (that are | 97 // Returns the list of snippets previously discarded by the user (that are |
| 93 // not expired yet). | 98 // not expired yet). |
| 94 const NTPSnippet::PtrVector& discarded_snippets() const { | 99 const NTPSnippet::PtrVector& discarded_snippets() const { |
| 95 return discarded_snippets_; | 100 return discarded_snippets_; |
| 96 } | 101 } |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 | 136 |
| 132 private: | 137 private: |
| 133 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceWithSyncTest, | 138 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceWithSyncTest, |
| 134 SyncStateCompatibility); | 139 SyncStateCompatibility); |
| 135 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceWithSyncTest, | 140 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceWithSyncTest, |
| 136 HistorySyncStateChanges); | 141 HistorySyncStateChanges); |
| 137 | 142 |
| 138 // sync_driver::SyncServiceObserver implementation. | 143 // sync_driver::SyncServiceObserver implementation. |
| 139 void OnStateChanged() override; | 144 void OnStateChanged() override; |
| 140 | 145 |
| 146 // Callback for the NTPSnippetsDatabase. |
| 147 void OnDatabaseLoaded(NTPSnippet::PtrVector snippets); |
| 148 |
| 149 // Callback for the SuggestionsService. |
| 141 void OnSuggestionsChanged(const suggestions::SuggestionsProfile& suggestions); | 150 void OnSuggestionsChanged(const suggestions::SuggestionsProfile& suggestions); |
| 151 |
| 152 // Callback for the NTPSnippetsFetcher. |
| 142 void OnFetchFinished(NTPSnippetsFetcher::OptionalSnippets snippets); | 153 void OnFetchFinished(NTPSnippetsFetcher::OptionalSnippets snippets); |
| 143 | 154 |
| 144 // Merges newly available snippets with the previously available list. | 155 // Merges newly available snippets with the previously available list. |
| 145 void MergeSnippets(NTPSnippet::PtrVector new_snippets); | 156 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 | 157 |
| 153 std::set<std::string> GetSnippetHostsFromPrefs() const; | 158 std::set<std::string> GetSnippetHostsFromPrefs() const; |
| 154 void StoreSnippetHostsToPrefs(const std::set<std::string>& hosts); | 159 void StoreSnippetHostsToPrefs(const std::set<std::string>& hosts); |
| 155 | 160 |
| 156 void LoadingSnippetsFinished(); | 161 void LoadingSnippetsFinished(); |
| 157 | 162 |
| 158 // Checks whether the state of the sync service is incompatible with showing | 163 // Checks whether the state of the sync service is incompatible with showing |
| 159 // snippets. We need history sync to be active. | 164 // snippets. We need history sync to be active. |
| 160 // Note: The state is considered compatible if the service is still | 165 // Note: The state is considered compatible if the service is still |
| 161 // initializing and the sync state is not known. | 166 // initializing and the sync state is not known. |
| 162 bool IsSyncStateIncompatible(); | 167 bool IsSyncStateIncompatible(); |
| 163 | 168 |
| 169 void ClearDeprecatedPrefs(); |
| 170 |
| 164 enum class State { | 171 enum class State { |
| 165 INITED, | 172 INITED, // Initial state before the DB has been loaded; no snippets yet. |
| 166 SHUT_DOWN | 173 LOADED, // DB has been loaded and the service is ready for action. |
| 174 SHUT_DOWN // Shutdown has been called, service is inactive. |
| 167 } state_; | 175 } state_; |
| 168 | 176 |
| 169 // When |enabled_| is true the service will fetch snippets from the server | 177 // When |enabled_| is true the service will fetch snippets from the server |
| 170 // using |snippets_fetcher_|, load snippets from device storage, and schedule | 178 // using |snippets_fetcher_|, load snippets from device storage, and schedule |
| 171 // the |scheduler_|. | 179 // the |scheduler_|. |
| 172 bool enabled_; | 180 bool enabled_; |
| 173 | 181 |
| 174 PrefService* pref_service_; | 182 PrefService* pref_service_; |
| 175 | 183 |
| 176 sync_driver::SyncService* sync_service_; | 184 sync_driver::SyncService* sync_service_; |
| 177 | 185 |
| 178 // The observer for the SyncService. When the sync state changes, | 186 // The observer for the SyncService. When the sync state changes, |
| 179 // SyncService will call |OnStateChanged|, which is propagated to the | 187 // SyncService will call |OnStateChanged|, which is propagated to the |
| 180 // snippet observers. | 188 // snippet observers. |
| 181 ScopedObserver<sync_driver::SyncService, sync_driver::SyncServiceObserver> | 189 ScopedObserver<sync_driver::SyncService, sync_driver::SyncServiceObserver> |
| 182 sync_service_observer_; | 190 sync_service_observer_; |
| 183 | 191 |
| 184 suggestions::SuggestionsService* suggestions_service_; | 192 suggestions::SuggestionsService* suggestions_service_; |
| 185 | 193 |
| 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). | 194 // All current suggestions (i.e. not discarded ones). |
| 190 NTPSnippet::PtrVector snippets_; | 195 NTPSnippet::PtrVector snippets_; |
| 191 | 196 |
| 192 // Suggestions that the user discarded. We keep these around until they expire | 197 // Suggestions that the user discarded. We keep these around until they expire |
| 193 // so we won't re-add them on the next fetch. | 198 // so we won't re-add them on the next fetch. |
| 194 NTPSnippet::PtrVector discarded_snippets_; | 199 NTPSnippet::PtrVector discarded_snippets_; |
| 195 | 200 |
| 196 // The ISO 639-1 code of the language used by the application. | 201 // The ISO 639-1 code of the language used by the application. |
| 197 const std::string application_language_code_; | 202 const std::string application_language_code_; |
| 198 | 203 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 210 std::unique_ptr<SuggestionsSubscription> suggestions_service_subscription_; | 215 std::unique_ptr<SuggestionsSubscription> suggestions_service_subscription_; |
| 211 | 216 |
| 212 // The snippets fetcher. | 217 // The snippets fetcher. |
| 213 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_; | 218 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_; |
| 214 | 219 |
| 215 // Timer that calls us back when the next snippet expires. | 220 // Timer that calls us back when the next snippet expires. |
| 216 base::OneShotTimer expiry_timer_; | 221 base::OneShotTimer expiry_timer_; |
| 217 | 222 |
| 218 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher_; | 223 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher_; |
| 219 | 224 |
| 225 // The database for persisting snippets. |
| 226 std::unique_ptr<NTPSnippetsDatabase> database_; |
| 227 |
| 228 // Set to true if FetchSnippets is called before the database has been loaded. |
| 229 // The fetch will be executed after the database load finishes. |
| 230 bool fetch_after_load_; |
| 231 |
| 220 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); | 232 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); |
| 221 }; | 233 }; |
| 222 | 234 |
| 223 class NTPSnippetsServiceObserver { | 235 class NTPSnippetsServiceObserver { |
| 224 public: | 236 public: |
| 225 // Sent every time the service loads a new set of data. | 237 // Sent every time the service loads a new set of data. |
| 226 virtual void NTPSnippetsServiceLoaded() = 0; | 238 virtual void NTPSnippetsServiceLoaded() = 0; |
| 227 // Sent when the service is shutting down. | 239 // Sent when the service is shutting down. |
| 228 virtual void NTPSnippetsServiceShutdown() = 0; | 240 virtual void NTPSnippetsServiceShutdown() = 0; |
| 229 // Sent when the service has been disabled. Can be from explicit user action | 241 // 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. | 242 // or because a requirement (e.g. History Sync) is not fulfilled anymore. |
| 231 virtual void NTPSnippetsServiceDisabled() = 0; | 243 virtual void NTPSnippetsServiceDisabled() = 0; |
| 232 | 244 |
| 233 protected: | 245 protected: |
| 234 virtual ~NTPSnippetsServiceObserver() {} | 246 virtual ~NTPSnippetsServiceObserver() {} |
| 235 }; | 247 }; |
| 236 | 248 |
| 237 } // namespace ntp_snippets | 249 } // namespace ntp_snippets |
| 238 | 250 |
| 239 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ | 251 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ |
| OLD | NEW |