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/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 enum class DisabledReason { | |
|
Marc Treib
2016/05/25 14:47:32
I'd prefer to keep this and State in the NTPSnippe
dgn
2016/06/03 19:02:24
This is going to be used to provide a way for Java
| |
| 53 // Snippets are enabled | |
| 54 NONE, | |
| 55 // Snippets have been disabled as part of the service configuration. | |
| 56 EXPLICITLY_DISABLED, | |
| 57 // History sync is not enabled, and the service requires it to be enabled. | |
| 58 HISTORY_SYNC_DISABLED, | |
| 59 | |
| 60 HISTORY_SYNC_STATE_UNKNOWN | |
| 61 }; | |
| 62 | |
| 63 // Possible state transitions: | |
| 64 // NOT_INITED ------+ | |
| 65 // v | | |
| 66 // +---- ---- INITED | | |
| 67 // | / \ | | |
| 68 // | READY <--> DISABLED <-+ | |
| 69 // | \ / | |
| 70 // +-----> SHUT_DOWN | |
| 71 enum class State { | |
| 72 // The service has just been created. Can change to states: | |
| 73 // - DISABLED if Init(false) was called. In that case the service will stay | |
| 74 // disabled until it is shut down. | |
| 75 // - INITED otherwise | |
| 76 NOT_INITED, | |
|
Marc Treib
2016/05/25 14:47:32
This state was just removed in https://codereview.
dgn
2016/06/03 19:02:24
Left that one in, and removed INITED. the construc
| |
| 77 | |
| 78 // The service passed the initialization, and is now loading snippets from | |
| 79 // the local database. Can change to states: | |
| 80 // - READY: if GetStateForDependenciesStatus returns it. | |
| 81 // - DISABLED: if GetStateForDependenciesStatus returns it. | |
| 82 INITED, | |
| 83 | |
| 84 // The service registered observers, timers, etc. and is ready to answer to | |
| 85 // queries, fetch snippets... Can change to states: | |
| 86 // - READY: noop | |
| 87 // - DISABLED: if the state changed, for example after |OnStateChanged| is | |
| 88 // called | |
| 89 // - SHUT_DOWN: is |Shutdown| is called, during the browser shutdown. | |
| 90 READY, | |
|
Marc Treib
2016/05/30 16:26:15
FYI: Based on Bernhard's comments on my CL, it loo
dgn
2016/06/03 19:02:24
Hum... I ran in another weird case. Will comment t
| |
| 91 | |
| 92 // The service is disabled and unregistered the related resources. | |
| 93 // Can change to states: | |
| 94 // - DISABLED: noop | |
| 95 // - READY: if the state changed, for example after |OnStateChanged| is | |
| 96 // called | |
| 97 // - SHUT_DOWN: is |Shutdown| is called, during the browser shutdown. | |
| 98 DISABLED, | |
| 99 | |
| 100 // The service shutdown and can't be used anymore. This state is checked for | |
| 101 // early exit in callbacks from observers. | |
| 102 SHUT_DOWN | |
| 103 }; | |
| 104 | |
| 105 class NTPSnippetsDatabase; | |
| 53 class NTPSnippetsServiceObserver; | 106 class NTPSnippetsServiceObserver; |
| 54 | 107 |
| 55 // Stores and vends fresh content data for the NTP. | 108 // Stores and vends fresh content data for the NTP. |
| 56 class NTPSnippetsService : public KeyedService, | 109 class NTPSnippetsService : public KeyedService, |
| 57 public sync_driver::SyncServiceObserver { | 110 public sync_driver::SyncServiceObserver { |
| 58 public: | 111 public: |
| 59 using ImageFetchedCallback = | 112 using ImageFetchedCallback = |
| 60 base::Callback<void(const std::string& snippet_id, const gfx::Image&)>; | 113 base::Callback<void(const std::string& snippet_id, const gfx::Image&)>; |
| 61 | 114 |
| 62 // |application_language_code| should be a ISO 639-1 compliant string, e.g. | 115 // |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 | 116 // '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' | 117 // 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. | 118 // (British English person in the US) are not language codes. |
| 66 NTPSnippetsService( | 119 NTPSnippetsService(PrefService* pref_service, |
| 67 PrefService* pref_service, | 120 sync_driver::SyncService* sync_service, |
| 68 sync_driver::SyncService* sync_service, | 121 suggestions::SuggestionsService* suggestions_service, |
| 69 suggestions::SuggestionsService* suggestions_service, | 122 const std::string& application_language_code, |
| 70 scoped_refptr<base::SequencedTaskRunner> file_task_runner, | 123 NTPSnippetsScheduler* scheduler, |
| 71 const std::string& application_language_code, | 124 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher, |
| 72 NTPSnippetsScheduler* scheduler, | 125 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher, |
| 73 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher, | 126 std::unique_ptr<NTPSnippetsDatabase> database); |
| 74 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher); | |
| 75 ~NTPSnippetsService() override; | 127 ~NTPSnippetsService() override; |
| 76 | 128 |
| 77 static void RegisterProfilePrefs(PrefRegistrySimple* registry); | 129 static void RegisterProfilePrefs(PrefRegistrySimple* registry); |
| 78 | 130 |
| 79 void Init(bool enabled); | 131 void Init(bool enabled); |
| 80 | 132 |
| 81 // Inherited from KeyedService. | 133 // Inherited from KeyedService. |
| 82 void Shutdown() override; | 134 void Shutdown() override; |
| 83 | 135 |
| 136 // Returns whether the service is ready. While this is false, the list of | |
| 137 // snippets will be empty, and all modifications to it (fetch, discard, etc) | |
| 138 // will be ignored. | |
| 139 bool ready() const { return state_ == State::READY; } | |
| 140 | |
| 84 // Fetches snippets from the server and adds them to the current ones. | 141 // Fetches snippets from the server and adds them to the current ones. |
| 85 void FetchSnippets(); | 142 void FetchSnippets(); |
| 86 // Fetches snippets from the server for specified hosts (overriding | 143 // Fetches snippets from the server for specified hosts (overriding |
| 87 // suggestions from the suggestion service) and adds them to the current ones. | 144 // suggestions from the suggestion service) and adds them to the current ones. |
| 88 void FetchSnippetsFromHosts(const std::set<std::string>& hosts); | 145 void FetchSnippetsFromHosts(const std::set<std::string>& hosts); |
| 89 | 146 |
| 90 // Available snippets. | 147 // Available snippets. |
| 91 const NTPSnippet::PtrVector& snippets() const { return snippets_; } | 148 const NTPSnippet::PtrVector& snippets() const { return snippets_; } |
| 92 | 149 |
| 93 // Returns the list of snippets previously discarded by the user (that are | 150 // Returns the list of snippets previously discarded by the user (that are |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 120 // Clears the lists of snippets previously discarded by the user. | 177 // Clears the lists of snippets previously discarded by the user. |
| 121 void ClearDiscardedSnippets(); | 178 void ClearDiscardedSnippets(); |
| 122 | 179 |
| 123 // Returns the lists of suggestion hosts the snippets are restricted to. | 180 // Returns the lists of suggestion hosts the snippets are restricted to. |
| 124 std::set<std::string> GetSuggestionsHosts() const; | 181 std::set<std::string> GetSuggestionsHosts() const; |
| 125 | 182 |
| 126 // Observer accessors. | 183 // Observer accessors. |
| 127 void AddObserver(NTPSnippetsServiceObserver* observer); | 184 void AddObserver(NTPSnippetsServiceObserver* observer); |
| 128 void RemoveObserver(NTPSnippetsServiceObserver* observer); | 185 void RemoveObserver(NTPSnippetsServiceObserver* observer); |
| 129 | 186 |
| 187 // Returns a reason why the service could be disabled, or DisabledReason::NONE | |
| 188 // if it's not. | |
| 189 DisabledReason GetDisabledReason(); | |
| 190 | |
| 130 // Returns the maximum number of snippets that will be shown at once. | 191 // Returns the maximum number of snippets that will be shown at once. |
| 131 static int GetMaxSnippetCountForTesting(); | 192 static int GetMaxSnippetCountForTesting(); |
| 132 | 193 |
| 133 private: | 194 private: |
| 134 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceWithSyncTest, | 195 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceWithSyncTest, |
| 135 SyncStateCompatibility); | 196 SyncStateCompatibility); |
| 136 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceWithSyncTest, | 197 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceWithSyncTest, |
| 137 HistorySyncStateChanges); | 198 HistorySyncStateChanges); |
| 138 | 199 |
| 139 // sync_driver::SyncServiceObserver implementation. | 200 // sync_driver::SyncServiceObserver implementation. |
| 140 void OnStateChanged() override; | 201 void OnStateChanged() override; |
| 141 | 202 |
| 203 // Callback for the NTPSnippetsDatabase. | |
| 204 void OnDatabaseLoaded(NTPSnippet::PtrVector snippets); | |
| 205 | |
| 206 // Callback for the SuggestionsService. | |
| 142 void OnSuggestionsChanged(const suggestions::SuggestionsProfile& suggestions); | 207 void OnSuggestionsChanged(const suggestions::SuggestionsProfile& suggestions); |
| 208 | |
| 209 // Callback for the NTPSnippetsFetcher. | |
| 143 void OnFetchFinished(NTPSnippetsFetcher::OptionalSnippets snippets); | 210 void OnFetchFinished(NTPSnippetsFetcher::OptionalSnippets snippets); |
| 144 | 211 |
| 145 // Merges newly available snippets with the previously available list. | 212 // Merges newly available snippets with the previously available list. |
| 146 void MergeSnippets(NTPSnippet::PtrVector new_snippets); | 213 void MergeSnippets(NTPSnippet::PtrVector new_snippets); |
| 147 // TODO(treib): Investigate a better storage, maybe LevelDB or SQLite? | |
| 148 void LoadSnippetsFromPrefs(); | |
| 149 void StoreSnippetsToPrefs(); | |
| 150 | |
| 151 void LoadDiscardedSnippetsFromPrefs(); | |
| 152 void StoreDiscardedSnippetsToPrefs(); | |
| 153 | 214 |
| 154 std::set<std::string> GetSnippetHostsFromPrefs() const; | 215 std::set<std::string> GetSnippetHostsFromPrefs() const; |
| 155 void StoreSnippetHostsToPrefs(const std::set<std::string>& hosts); | 216 void StoreSnippetHostsToPrefs(const std::set<std::string>& hosts); |
| 156 | 217 |
| 157 void LoadingSnippetsFinished(); | 218 void LoadingSnippetsFinished(); |
| 158 | 219 |
| 159 // Checks whether the state of the sync service is incompatible with showing | 220 // Returns whether the service should be enabled or disabled depending on its |
| 160 // snippets. We need history sync to be active. | 221 // internal state and the state of its dependencies. |
| 161 // Note: The state is considered compatible if the service is still | 222 State GetStateForDependenciesStatus(); |
|
dgn
2016/05/24 18:13:30
I'm thinking about moving all the service's state
Marc Treib
2016/05/25 14:47:32
Hm, seems like it would be hard to cleanly separat
| |
| 162 // initializing and the sync state is not known. | |
| 163 bool IsSyncStateIncompatible(); | |
| 164 | 223 |
| 165 enum class State { | 224 // Applies state transitions (see |State|'s documentation )and verifies them. |
| 166 NOT_INITED, | 225 void EnterState(State state); |
| 167 INITED, | |
| 168 SHUT_DOWN | |
| 169 } state_; | |
| 170 | 226 |
| 171 bool enabled_; | 227 // Enable the service. Do not call directly, use |EnterState| instead. |
| 228 void Enable(); | |
| 229 | |
| 230 // Disable the service. Do not call directly, use |EnterState| instead. | |
| 231 void Disable(); | |
| 232 | |
| 233 // Load snippets from the DB. Do not call directly, use |EnterState| instead/ | |
| 234 void LoadFromDB(); | |
| 235 | |
| 236 State state_; | |
| 237 | |
| 238 // The service was set up to be disabled and should not be enabled by any | |
| 239 // state change. We track this to make sure we clear scheduled tasks, which | |
| 240 // persist even when Chrome is stopped. | |
| 241 bool explicitly_disabled_; | |
| 172 | 242 |
| 173 PrefService* pref_service_; | 243 PrefService* pref_service_; |
| 174 | 244 |
| 175 sync_driver::SyncService* sync_service_; | 245 sync_driver::SyncService* sync_service_; |
| 176 | 246 |
| 177 // The observer for the SyncService. When the sync state changes, | 247 // The observer for the SyncService. When the sync state changes, |
| 178 // SyncService will call |OnStateChanged|, which is propagated to the | 248 // SyncService will call |OnStateChanged|, which is propagated to the |
| 179 // snippet observers. | 249 // snippet observers. |
| 180 ScopedObserver<sync_driver::SyncService, sync_driver::SyncServiceObserver> | 250 ScopedObserver<sync_driver::SyncService, sync_driver::SyncServiceObserver> |
| 181 sync_service_observer_; | 251 sync_service_observer_; |
| 182 | 252 |
| 183 suggestions::SuggestionsService* suggestions_service_; | 253 suggestions::SuggestionsService* suggestions_service_; |
| 184 | 254 |
| 185 // The SequencedTaskRunner on which file system operations will be run. | |
| 186 scoped_refptr<base::SequencedTaskRunner> file_task_runner_; | |
| 187 | |
| 188 // All current suggestions (i.e. not discarded ones). | 255 // All current suggestions (i.e. not discarded ones). |
| 189 NTPSnippet::PtrVector snippets_; | 256 NTPSnippet::PtrVector snippets_; |
| 190 | 257 |
| 191 // Suggestions that the user discarded. We keep these around until they expire | 258 // Suggestions that the user discarded. We keep these around until they expire |
| 192 // so we won't re-add them on the next fetch. | 259 // so we won't re-add them on the next fetch. |
| 193 NTPSnippet::PtrVector discarded_snippets_; | 260 NTPSnippet::PtrVector discarded_snippets_; |
| 194 | 261 |
| 195 // The ISO 639-1 code of the language used by the application. | 262 // The ISO 639-1 code of the language used by the application. |
| 196 const std::string application_language_code_; | 263 const std::string application_language_code_; |
| 197 | 264 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 209 std::unique_ptr<SuggestionsSubscription> suggestions_service_subscription_; | 276 std::unique_ptr<SuggestionsSubscription> suggestions_service_subscription_; |
| 210 | 277 |
| 211 // The snippets fetcher. | 278 // The snippets fetcher. |
| 212 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_; | 279 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_; |
| 213 | 280 |
| 214 // Timer that calls us back when the next snippet expires. | 281 // Timer that calls us back when the next snippet expires. |
| 215 base::OneShotTimer expiry_timer_; | 282 base::OneShotTimer expiry_timer_; |
| 216 | 283 |
| 217 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher_; | 284 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher_; |
| 218 | 285 |
| 286 // The database for persisting snippets. | |
| 287 std::unique_ptr<NTPSnippetsDatabase> database_; | |
| 288 | |
| 219 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); | 289 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); |
| 220 }; | 290 }; |
| 221 | 291 |
| 222 class NTPSnippetsServiceObserver { | 292 class NTPSnippetsServiceObserver { |
| 223 public: | 293 public: |
| 224 // Sent every time the service loads a new set of data. | 294 // Sent every time the service loads a new set of data. |
| 225 virtual void NTPSnippetsServiceLoaded() = 0; | 295 virtual void NTPSnippetsServiceLoaded() = 0; |
| 226 // Sent when the service is shutting down. | 296 // Sent when the service is shutting down. |
| 227 virtual void NTPSnippetsServiceShutdown() = 0; | 297 virtual void NTPSnippetsServiceShutdown() = 0; |
| 228 // Sent when the service has been disabled. Can be from explicit user action | 298 // Sent when the service has been disabled. Can be from explicit user action |
| 229 // or because a requirement (e.g. History Sync) is not fulfilled anymore. | 299 // or because a requirement (e.g. History Sync) is not fulfilled anymore. |
| 230 virtual void NTPSnippetsServiceDisabled() = 0; | 300 virtual void NTPSnippetsServiceDisabled() = 0; |
| 231 | 301 |
| 232 protected: | 302 protected: |
| 233 virtual ~NTPSnippetsServiceObserver() {} | 303 virtual ~NTPSnippetsServiceObserver() {} |
| 234 }; | 304 }; |
| 235 | 305 |
| 236 } // namespace ntp_snippets | 306 } // namespace ntp_snippets |
| 237 | 307 |
| 238 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ | 308 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ |
| OLD | NEW |