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> |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 42 namespace suggestions { | 42 namespace suggestions { |
| 43 class SuggestionsProfile; | 43 class SuggestionsProfile; |
| 44 } | 44 } |
| 45 | 45 |
| 46 namespace sync_driver { | 46 namespace sync_driver { |
| 47 class SyncService; | 47 class SyncService; |
| 48 } | 48 } |
| 49 | 49 |
| 50 namespace ntp_snippets { | 50 namespace ntp_snippets { |
| 51 | 51 |
| 52 enum class DisabledReason { | |
| 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 // The sync service is not completely initialized, and the status is unknown. | |
| 60 HISTORY_SYNC_STATE_UNKNOWN | |
| 61 }; | |
| 62 | |
| 52 class NTPSnippetsDatabase; | 63 class NTPSnippetsDatabase; |
| 53 class NTPSnippetsServiceObserver; | 64 class NTPSnippetsServiceObserver; |
| 54 | 65 |
| 55 // Stores and vends fresh content data for the NTP. | 66 // Stores and vends fresh content data for the NTP. |
| 56 class NTPSnippetsService : public KeyedService, | 67 class NTPSnippetsService : public KeyedService, |
| 57 public sync_driver::SyncServiceObserver { | 68 public sync_driver::SyncServiceObserver { |
| 58 public: | 69 public: |
| 59 using ImageFetchedCallback = | 70 using ImageFetchedCallback = |
| 60 base::Callback<void(const std::string& snippet_id, const gfx::Image&)>; | 71 base::Callback<void(const std::string& snippet_id, const gfx::Image&)>; |
| 61 | 72 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 72 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher, | 83 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher, |
| 73 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher, | 84 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher, |
| 74 std::unique_ptr<NTPSnippetsDatabase> database); | 85 std::unique_ptr<NTPSnippetsDatabase> database); |
| 75 ~NTPSnippetsService() override; | 86 ~NTPSnippetsService() override; |
| 76 | 87 |
| 77 static void RegisterProfilePrefs(PrefRegistrySimple* registry); | 88 static void RegisterProfilePrefs(PrefRegistrySimple* registry); |
| 78 | 89 |
| 79 // Inherited from KeyedService. | 90 // Inherited from KeyedService. |
| 80 void Shutdown() override; | 91 void Shutdown() override; |
| 81 | 92 |
| 82 // Returns whether the initial set of snippets has been loaded from the | 93 // Returns whether the service is ready. While this is false, the list of |
| 83 // database. While this is false, the list of snippets will be empty. | 94 // snippets will be empty, and all modifications to it (fetch, discard, etc) |
| 84 bool loaded() const { return state_ == State::LOADED; } | 95 // will be ignored. |
| 96 bool ready() const { return state_ == State::READY; } | |
| 97 | |
| 98 // Returns whether the service is initialized. While this is false, some | |
| 99 // calls may trigger DCHECKs. | |
| 100 bool initialized() const { return ready() || state_ == State::DISABLED; } | |
| 85 | 101 |
| 86 // Fetches snippets from the server and adds them to the current ones. | 102 // Fetches snippets from the server and adds them to the current ones. |
| 87 void FetchSnippets(); | 103 void FetchSnippets(); |
| 88 // Fetches snippets from the server for specified hosts (overriding | 104 // Fetches snippets from the server for specified hosts (overriding |
| 89 // suggestions from the suggestion service) and adds them to the current ones. | 105 // suggestions from the suggestion service) and adds them to the current ones. |
| 90 // Only called from chrome://snippets-internals, DO NOT USE otherwise! | 106 // Only called from chrome://snippets-internals, DO NOT USE otherwise! |
| 91 // Ignored while |loaded()| is false. | 107 // Ignored while |loaded()| is false. |
| 92 void FetchSnippetsFromHosts(const std::set<std::string>& hosts); | 108 void FetchSnippetsFromHosts(const std::set<std::string>& hosts); |
| 93 | 109 |
| 94 // Available snippets. | 110 // Available snippets. |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 124 // Clears the lists of snippets previously discarded by the user. | 140 // Clears the lists of snippets previously discarded by the user. |
| 125 void ClearDiscardedSnippets(); | 141 void ClearDiscardedSnippets(); |
| 126 | 142 |
| 127 // Returns the lists of suggestion hosts the snippets are restricted to. | 143 // Returns the lists of suggestion hosts the snippets are restricted to. |
| 128 std::set<std::string> GetSuggestionsHosts() const; | 144 std::set<std::string> GetSuggestionsHosts() const; |
| 129 | 145 |
| 130 // Observer accessors. | 146 // Observer accessors. |
| 131 void AddObserver(NTPSnippetsServiceObserver* observer); | 147 void AddObserver(NTPSnippetsServiceObserver* observer); |
| 132 void RemoveObserver(NTPSnippetsServiceObserver* observer); | 148 void RemoveObserver(NTPSnippetsServiceObserver* observer); |
| 133 | 149 |
| 150 // Returns a reason why the service could be disabled, or DisabledReason::NONE | |
| 151 // if it's not. | |
| 152 DisabledReason GetDisabledReason() const; | |
| 153 | |
| 134 // Returns the maximum number of snippets that will be shown at once. | 154 // Returns the maximum number of snippets that will be shown at once. |
| 135 static int GetMaxSnippetCountForTesting(); | 155 static int GetMaxSnippetCountForTesting(); |
| 136 | 156 |
| 137 private: | 157 private: |
| 138 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceWithSyncTest, | 158 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceWithSyncTest, |
| 139 SyncStateCompatibility); | 159 SyncStateCompatibility); |
| 140 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceWithSyncTest, | 160 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceWithSyncTest, |
| 141 HistorySyncStateChanges); | 161 HistorySyncStateChanges); |
| 142 | 162 |
| 163 // Possible state transitions: | |
| 164 // +------- NOT_INITED ------+ | |
| 165 // | / \ | | |
| 166 // | READY <--> DISABLED <-+ | |
| 167 // | \ / | |
| 168 // +-----> SHUT_DOWN | |
| 169 enum class State { | |
| 170 // The service has just been created. Can change to states: | |
| 171 // - DISABLED: if the constructor was called with |enabled == false| . In | |
| 172 // that case the service will stay disabled until it is shut | |
| 173 // down. It can also enter this state after the database is | |
| 174 // done loading and GetStateForDependenciesStatus identifies | |
| 175 // the next state to be DISABLED. | |
| 176 // - READY: if GetStateForDependenciesStatus returns it, after the database | |
| 177 // is done loading. | |
| 178 NOT_INITED, | |
| 179 | |
| 180 // The service registered observers, timers, etc. and is ready to answer to | |
| 181 // queries, fetch snippets... Can change to states: | |
| 182 // - READY: noop | |
|
Marc Treib
2016/06/07 08:55:39
nit: I'd remove the "noop" comments, IMO they don'
dgn
2016/06/07 11:22:47
I added them since transitions from states not wri
Marc Treib
2016/06/07 11:50:37
Acknowledged.
| |
| 183 // - DISABLED: when the global Chrome state changes, for example after | |
| 184 // |OnStateChanged| is called and sync is disabled. | |
| 185 // - SHUT_DOWN: when |Shutdown| is called, during the browser shutdown. | |
| 186 READY, | |
| 187 | |
| 188 // The service is disabled and unregistered the related resources. | |
| 189 // Can change to states: | |
| 190 // - DISABLED: noop | |
| 191 // - READY: when the global Chrome state changes, for example after | |
| 192 // |OnStateChanged| is called and sync is enabled. | |
| 193 // - SHUT_DOWN: when |Shutdown| is called, during the browser shutdown. | |
| 194 DISABLED, | |
| 195 | |
| 196 // The service shutdown and can't be used anymore. This state is checked | |
| 197 // for early exit in callbacks from observers. | |
| 198 SHUT_DOWN | |
| 199 }; | |
| 200 | |
| 143 // sync_driver::SyncServiceObserver implementation. | 201 // sync_driver::SyncServiceObserver implementation. |
| 144 void OnStateChanged() override; | 202 void OnStateChanged() override; |
| 145 | 203 |
| 146 // Callback for the NTPSnippetsDatabase. | 204 // Callback for the NTPSnippetsDatabase. |
| 147 void OnDatabaseLoaded(NTPSnippet::PtrVector snippets); | 205 void OnDatabaseLoaded(NTPSnippet::PtrVector snippets); |
| 148 | 206 |
| 149 // Callback for the SuggestionsService. | 207 // Callback for the SuggestionsService. |
| 150 void OnSuggestionsChanged(const suggestions::SuggestionsProfile& suggestions); | 208 void OnSuggestionsChanged(const suggestions::SuggestionsProfile& suggestions); |
| 151 | 209 |
| 152 // Callback for the NTPSnippetsFetcher. | 210 // Callback for the NTPSnippetsFetcher. |
| 153 void OnFetchFinished(NTPSnippetsFetcher::OptionalSnippets snippets); | 211 void OnFetchFinished(NTPSnippetsFetcher::OptionalSnippets snippets); |
| 154 | 212 |
| 155 // Merges newly available snippets with the previously available list. | 213 // Merges newly available snippets with the previously available list. |
| 156 void MergeSnippets(NTPSnippet::PtrVector new_snippets); | 214 void MergeSnippets(NTPSnippet::PtrVector new_snippets); |
| 157 | 215 |
| 158 std::set<std::string> GetSnippetHostsFromPrefs() const; | 216 std::set<std::string> GetSnippetHostsFromPrefs() const; |
| 159 void StoreSnippetHostsToPrefs(const std::set<std::string>& hosts); | 217 void StoreSnippetHostsToPrefs(const std::set<std::string>& hosts); |
| 160 | 218 |
| 161 void LoadingSnippetsFinished(); | 219 void LoadingSnippetsFinished(); |
| 162 | 220 |
| 163 // Checks whether the state of the sync service is incompatible with showing | 221 // Returns whether the service should be enabled or disable depending on its |
| 164 // snippets. We need history sync to be active. | 222 // internal state and the state of its dependencies. |
| 165 // Note: The state is considered compatible if the service is still | 223 State GetStateForDependenciesStatus(); |
| 166 // initializing and the sync state is not known. | 224 |
| 167 bool IsSyncStateIncompatible(); | 225 // Verifies state transitions (see |State|'s documentation) and applies them. |
| 226 void EnterState(State state); | |
| 227 | |
| 228 // Enables the service and triggers a fetch if required. Do not call directly, | |
| 229 // use |EnterState| instead. | |
| 230 void EnterStateEnabled(bool fetch_snippets); | |
| 231 | |
| 232 // Disables the service. Do not call directly, use |EnterState| instead. | |
| 233 void EnterStateDisabled(); | |
| 234 | |
| 235 // Applies the effects of the transition to the SHUT_DOWN state. Do not call | |
| 236 // directly, use |EnterState| instead. | |
| 237 void EnterStateShutdown(); | |
| 168 | 238 |
| 169 void ClearDeprecatedPrefs(); | 239 void ClearDeprecatedPrefs(); |
| 170 | 240 |
| 171 enum class State { | 241 State state_; |
| 172 INITED, // Initial state before the DB has been loaded; no snippets yet. | |
| 173 LOADED, // DB has been loaded and the service is ready for action. | |
| 174 SHUT_DOWN // Shutdown has been called, service is inactive. | |
| 175 } state_; | |
| 176 | 242 |
| 177 // When |enabled_| is true the service will fetch snippets from the server | 243 // The service was set up to be disabled and should not be enabled by any |
| 178 // using |snippets_fetcher_|, load snippets from device storage, and schedule | 244 // state change. We track this to make sure we clear scheduled tasks, which |
| 179 // the |scheduler_|. | 245 // persist even when Chrome is stopped. |
| 180 bool enabled_; | 246 bool explicitly_disabled_; |
| 181 | 247 |
| 182 PrefService* pref_service_; | 248 PrefService* pref_service_; |
| 183 | 249 |
| 184 sync_driver::SyncService* sync_service_; | 250 sync_driver::SyncService* sync_service_; |
| 185 | 251 |
| 186 // The observer for the SyncService. When the sync state changes, | 252 // The observer for the SyncService. When the sync state changes, |
| 187 // SyncService will call |OnStateChanged|, which is propagated to the | 253 // SyncService will call |OnStateChanged|, which is propagated to the |
| 188 // snippet observers. | 254 // snippet observers. |
| 189 ScopedObserver<sync_driver::SyncService, sync_driver::SyncServiceObserver> | 255 ScopedObserver<sync_driver::SyncService, sync_driver::SyncServiceObserver> |
| 190 sync_service_observer_; | 256 sync_service_observer_; |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 242 // or because a requirement (e.g. History Sync) is not fulfilled anymore. | 308 // or because a requirement (e.g. History Sync) is not fulfilled anymore. |
| 243 virtual void NTPSnippetsServiceDisabled() = 0; | 309 virtual void NTPSnippetsServiceDisabled() = 0; |
| 244 | 310 |
| 245 protected: | 311 protected: |
| 246 virtual ~NTPSnippetsServiceObserver() {} | 312 virtual ~NTPSnippetsServiceObserver() {} |
| 247 }; | 313 }; |
| 248 | 314 |
| 249 } // namespace ntp_snippets | 315 } // namespace ntp_snippets |
| 250 | 316 |
| 251 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ | 317 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ |
| OLD | NEW |