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 }; | |
| 60 | |
| 52 class NTPSnippetsDatabase; | 61 class NTPSnippetsDatabase; |
| 53 class NTPSnippetsServiceObserver; | 62 class NTPSnippetsServiceObserver; |
| 54 | 63 |
| 55 // Stores and vends fresh content data for the NTP. | 64 // Stores and vends fresh content data for the NTP. |
| 56 class NTPSnippetsService : public KeyedService, | 65 class NTPSnippetsService : public KeyedService, |
| 57 public sync_driver::SyncServiceObserver { | 66 public sync_driver::SyncServiceObserver { |
| 58 public: | 67 public: |
| 59 using ImageFetchedCallback = | 68 using ImageFetchedCallback = |
| 60 base::Callback<void(const std::string& snippet_id, const gfx::Image&)>; | 69 base::Callback<void(const std::string& snippet_id, const gfx::Image&)>; |
| 61 | 70 |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 124 // Clears the lists of snippets previously discarded by the user. | 133 // Clears the lists of snippets previously discarded by the user. |
| 125 void ClearDiscardedSnippets(); | 134 void ClearDiscardedSnippets(); |
| 126 | 135 |
| 127 // Returns the lists of suggestion hosts the snippets are restricted to. | 136 // Returns the lists of suggestion hosts the snippets are restricted to. |
| 128 std::set<std::string> GetSuggestionsHosts() const; | 137 std::set<std::string> GetSuggestionsHosts() const; |
| 129 | 138 |
| 130 // Observer accessors. | 139 // Observer accessors. |
| 131 void AddObserver(NTPSnippetsServiceObserver* observer); | 140 void AddObserver(NTPSnippetsServiceObserver* observer); |
| 132 void RemoveObserver(NTPSnippetsServiceObserver* observer); | 141 void RemoveObserver(NTPSnippetsServiceObserver* observer); |
| 133 | 142 |
| 143 // Returns a reason why the service could be disabled, or DisabledReason::NONE | |
| 144 // if it's not. | |
| 145 DisabledReason GetDisabledReason(); | |
| 146 | |
| 134 // Returns the maximum number of snippets that will be shown at once. | 147 // Returns the maximum number of snippets that will be shown at once. |
| 135 static int GetMaxSnippetCountForTesting(); | 148 static int GetMaxSnippetCountForTesting(); |
| 136 | 149 |
| 137 private: | 150 private: |
| 138 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceWithSyncTest, | 151 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceWithSyncTest, |
| 139 SyncStateCompatibility); | 152 SyncStateCompatibility); |
| 140 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceWithSyncTest, | 153 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceWithSyncTest, |
| 141 HistorySyncStateChanges); | 154 HistorySyncStateChanges); |
| 142 | 155 |
| 156 // Possible state transitions: | |
| 157 // NOT_INITED ---------------+ | |
| 158 // v | | |
| 159 // +--------- INITED <------+ | | |
| 160 // | v | | | |
| 161 // | LOADED = READY -> DISABLED <-+ | |
| 162 // | v | | |
| 163 // +------> SHUT_DOWN <------+ | |
| 164 enum class State { | |
| 165 NOT_INITED, | |
| 166 INITED, | |
| 167 LOADED, | |
| 168 READY = LOADED, | |
|
Marc Treib
2016/05/24 09:04:46
Why READY and LOADED? Can we stick with just one?
dgn
2016/05/24 18:13:30
That was in case we have more intermediate states,
| |
| 169 DISABLED, | |
| 170 SHUT_DOWN | |
| 171 }; | |
| 172 | |
| 143 // sync_driver::SyncServiceObserver implementation. | 173 // sync_driver::SyncServiceObserver implementation. |
| 144 void OnStateChanged() override; | 174 void OnStateChanged() override; |
| 145 | 175 |
| 146 // Callback for the NTPSnippetsDatabase. | 176 // Callback for the NTPSnippetsDatabase. |
| 147 void OnSnippetsLoaded(NTPSnippet::PtrVector snippets); | 177 void OnSnippetsLoaded(NTPSnippet::PtrVector snippets); |
| 148 | 178 |
| 149 // Callback for the SuggestionsService. | 179 // Callback for the SuggestionsService. |
| 150 void OnSuggestionsChanged(const suggestions::SuggestionsProfile& suggestions); | 180 void OnSuggestionsChanged(const suggestions::SuggestionsProfile& suggestions); |
| 151 | 181 |
| 152 // Callback for the NTPSnippetsFetcher. | 182 // Callback for the NTPSnippetsFetcher. |
| 153 void OnFetchFinished(NTPSnippetsFetcher::OptionalSnippets snippets); | 183 void OnFetchFinished(NTPSnippetsFetcher::OptionalSnippets snippets); |
| 154 | 184 |
| 155 // Merges newly available snippets with the previously available list. | 185 // Merges newly available snippets with the previously available list. |
| 156 void MergeSnippets(NTPSnippet::PtrVector new_snippets); | 186 void MergeSnippets(NTPSnippet::PtrVector new_snippets); |
| 157 | 187 |
| 158 std::set<std::string> GetSnippetHostsFromPrefs() const; | 188 std::set<std::string> GetSnippetHostsFromPrefs() const; |
| 159 void StoreSnippetHostsToPrefs(const std::set<std::string>& hosts); | 189 void StoreSnippetHostsToPrefs(const std::set<std::string>& hosts); |
| 160 | 190 |
| 161 void LoadingSnippetsFinished(); | 191 void LoadingSnippetsFinished(); |
| 162 | 192 |
| 163 // Checks whether the state of the sync service is incompatible with showing | 193 // Enable the service. Do not call directly, use |EnterState| instead. |
| 164 // snippets. We need history sync to be active. | 194 void Enable(); |
| 165 // Note: The state is considered compatible if the service is still | |
| 166 // initializing and the sync state is not known. | |
| 167 bool IsSyncStateIncompatible(); | |
| 168 | 195 |
| 169 enum class State { | 196 // Disable the service. Do not call directly, use |EnterState| instead. |
| 170 NOT_INITED, | 197 void Disable(); |
| 171 INITED, | |
| 172 LOADED, | |
| 173 SHUT_DOWN | |
| 174 } state_; | |
| 175 | 198 |
| 176 bool enabled_; | 199 // Applies state transitions (see |State|'s documentation )and verifies them. |
| 200 void EnterState(State state); | |
| 201 | |
| 202 State state_; | |
| 203 | |
| 204 // The service was set up to be disabled and should not be enabled by any | |
| 205 // state change. We track this to make sure we clear scheduled tasks, which | |
| 206 // persist even when Chrome is stopped. | |
| 207 bool explicitly_disabled_; | |
| 177 | 208 |
| 178 PrefService* pref_service_; | 209 PrefService* pref_service_; |
| 179 | 210 |
| 180 sync_driver::SyncService* sync_service_; | 211 sync_driver::SyncService* sync_service_; |
| 181 | 212 |
| 182 // The observer for the SyncService. When the sync state changes, | 213 // The observer for the SyncService. When the sync state changes, |
| 183 // SyncService will call |OnStateChanged|, which is propagated to the | 214 // SyncService will call |OnStateChanged|, which is propagated to the |
| 184 // snippet observers. | 215 // snippet observers. |
| 185 ScopedObserver<sync_driver::SyncService, sync_driver::SyncServiceObserver> | 216 ScopedObserver<sync_driver::SyncService, sync_driver::SyncServiceObserver> |
| 186 sync_service_observer_; | 217 sync_service_observer_; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 234 // or because a requirement (e.g. History Sync) is not fulfilled anymore. | 265 // or because a requirement (e.g. History Sync) is not fulfilled anymore. |
| 235 virtual void NTPSnippetsServiceDisabled() = 0; | 266 virtual void NTPSnippetsServiceDisabled() = 0; |
| 236 | 267 |
| 237 protected: | 268 protected: |
| 238 virtual ~NTPSnippetsServiceObserver() {} | 269 virtual ~NTPSnippetsServiceObserver() {} |
| 239 }; | 270 }; |
| 240 | 271 |
| 241 } // namespace ntp_snippets | 272 } // namespace ntp_snippets |
| 242 | 273 |
| 243 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ | 274 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ |
| OLD | NEW |