| 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(NTPSnippetsServiceTest, SyncStateCompatibility); |
| 139 SyncStateCompatibility); | 159 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceTest, HistorySyncStateChanges); |
| 140 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceWithSyncTest, | 160 |
| 141 HistorySyncStateChanges); | 161 // Possible state transitions: |
| 162 // +------- NOT_INITED ------+ |
| 163 // | / \ | |
| 164 // | READY <--> DISABLED <-+ |
| 165 // | \ / |
| 166 // +-----> SHUT_DOWN |
| 167 enum class State { |
| 168 // The service has just been created. Can change to states: |
| 169 // - DISABLED: if the constructor was called with |enabled == false| . In |
| 170 // that case the service will stay disabled until it is shut |
| 171 // down. It can also enter this state after the database is |
| 172 // done loading and GetStateForDependenciesStatus identifies |
| 173 // the next state to be DISABLED. |
| 174 // - READY: if GetStateForDependenciesStatus returns it, after the database |
| 175 // is done loading. |
| 176 NOT_INITED, |
| 177 |
| 178 // The service registered observers, timers, etc. and is ready to answer to |
| 179 // queries, fetch snippets... Can change to states: |
| 180 // - READY: noop |
| 181 // - DISABLED: when the global Chrome state changes, for example after |
| 182 // |OnStateChanged| is called and sync is disabled. |
| 183 // - SHUT_DOWN: when |Shutdown| is called, during the browser shutdown. |
| 184 READY, |
| 185 |
| 186 // The service is disabled and unregistered the related resources. |
| 187 // Can change to states: |
| 188 // - DISABLED: noop |
| 189 // - READY: when the global Chrome state changes, for example after |
| 190 // |OnStateChanged| is called and sync is enabled. |
| 191 // - SHUT_DOWN: when |Shutdown| is called, during the browser shutdown. |
| 192 DISABLED, |
| 193 |
| 194 // The service shutdown and can't be used anymore. This state is checked |
| 195 // for early exit in callbacks from observers. |
| 196 SHUT_DOWN |
| 197 }; |
| 142 | 198 |
| 143 // sync_driver::SyncServiceObserver implementation. | 199 // sync_driver::SyncServiceObserver implementation. |
| 144 void OnStateChanged() override; | 200 void OnStateChanged() override; |
| 145 | 201 |
| 146 // Callback for the NTPSnippetsDatabase. | 202 // Callback for the NTPSnippetsDatabase. |
| 147 void OnDatabaseLoaded(NTPSnippet::PtrVector snippets); | 203 void OnDatabaseLoaded(NTPSnippet::PtrVector snippets); |
| 148 | 204 |
| 149 // Callback for the SuggestionsService. | 205 // Callback for the SuggestionsService. |
| 150 void OnSuggestionsChanged(const suggestions::SuggestionsProfile& suggestions); | 206 void OnSuggestionsChanged(const suggestions::SuggestionsProfile& suggestions); |
| 151 | 207 |
| 152 // Callback for the NTPSnippetsFetcher. | 208 // Callback for the NTPSnippetsFetcher. |
| 153 void OnFetchFinished(NTPSnippetsFetcher::OptionalSnippets snippets); | 209 void OnFetchFinished(NTPSnippetsFetcher::OptionalSnippets snippets); |
| 154 | 210 |
| 155 // Merges newly available snippets with the previously available list. | 211 // Merges newly available snippets with the previously available list. |
| 156 void MergeSnippets(NTPSnippet::PtrVector new_snippets); | 212 void MergeSnippets(NTPSnippet::PtrVector new_snippets); |
| 157 | 213 |
| 158 std::set<std::string> GetSnippetHostsFromPrefs() const; | 214 std::set<std::string> GetSnippetHostsFromPrefs() const; |
| 159 void StoreSnippetHostsToPrefs(const std::set<std::string>& hosts); | 215 void StoreSnippetHostsToPrefs(const std::set<std::string>& hosts); |
| 160 | 216 |
| 161 void LoadingSnippetsFinished(); | 217 void LoadingSnippetsFinished(); |
| 162 | 218 |
| 163 // Checks whether the state of the sync service is incompatible with showing | 219 // Returns whether the service should be enabled or disable depending on its |
| 164 // snippets. We need history sync to be active. | 220 // internal state and the state of its dependencies. |
| 165 // Note: The state is considered compatible if the service is still | 221 State GetStateForDependenciesStatus(); |
| 166 // initializing and the sync state is not known. | 222 |
| 167 bool IsSyncStateIncompatible(); | 223 // Verifies state transitions (see |State|'s documentation) and applies them. |
| 224 void EnterState(State state); |
| 225 |
| 226 // Enables the service and triggers a fetch if required. Do not call directly, |
| 227 // use |EnterState| instead. |
| 228 void EnterStateEnabled(bool fetch_snippets); |
| 229 |
| 230 // Disables the service. Do not call directly, use |EnterState| instead. |
| 231 void EnterStateDisabled(); |
| 232 |
| 233 // Applies the effects of the transition to the SHUT_DOWN state. Do not call |
| 234 // directly, use |EnterState| instead. |
| 235 void EnterStateShutdown(); |
| 168 | 236 |
| 169 void ClearDeprecatedPrefs(); | 237 void ClearDeprecatedPrefs(); |
| 170 | 238 |
| 171 enum class State { | 239 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 | 240 |
| 177 // When |enabled_| is true the service will fetch snippets from the server | 241 // 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 | 242 // state change. We track this to make sure we clear scheduled tasks, which |
| 179 // the |scheduler_|. | 243 // persist even when Chrome is stopped. |
| 180 bool enabled_; | 244 bool explicitly_disabled_; |
| 181 | 245 |
| 182 PrefService* pref_service_; | 246 PrefService* pref_service_; |
| 183 | 247 |
| 184 sync_driver::SyncService* sync_service_; | 248 sync_driver::SyncService* sync_service_; |
| 185 | 249 |
| 186 // The observer for the SyncService. When the sync state changes, | 250 // The observer for the SyncService. When the sync state changes, |
| 187 // SyncService will call |OnStateChanged|, which is propagated to the | 251 // SyncService will call |OnStateChanged|, which is propagated to the |
| 188 // snippet observers. | 252 // snippet observers. |
| 189 ScopedObserver<sync_driver::SyncService, sync_driver::SyncServiceObserver> | 253 ScopedObserver<sync_driver::SyncService, sync_driver::SyncServiceObserver> |
| 190 sync_service_observer_; | 254 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. | 306 // or because a requirement (e.g. History Sync) is not fulfilled anymore. |
| 243 virtual void NTPSnippetsServiceDisabled() = 0; | 307 virtual void NTPSnippetsServiceDisabled() = 0; |
| 244 | 308 |
| 245 protected: | 309 protected: |
| 246 virtual ~NTPSnippetsServiceObserver() {} | 310 virtual ~NTPSnippetsServiceObserver() {} |
| 247 }; | 311 }; |
| 248 | 312 |
| 249 } // namespace ntp_snippets | 313 } // namespace ntp_snippets |
| 250 | 314 |
| 251 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ | 315 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ |
| OLD | NEW |