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/timer/timer.h" | 19 #include "base/timer/timer.h" |
20 #include "components/keyed_service/core/keyed_service.h" | 20 #include "components/keyed_service/core/keyed_service.h" |
21 #include "components/ntp_snippets/ntp_snippet.h" | 21 #include "components/ntp_snippets/ntp_snippet.h" |
22 #include "components/ntp_snippets/ntp_snippets_fetcher.h" | 22 #include "components/ntp_snippets/ntp_snippets_fetcher.h" |
23 #include "components/ntp_snippets/ntp_snippets_scheduler.h" | 23 #include "components/ntp_snippets/ntp_snippets_scheduler.h" |
24 #include "components/suggestions/suggestions_service.h" | 24 #include "components/suggestions/suggestions_service.h" |
25 #include "components/sync_driver/sync_service_observer.h" | 25 #include "components/sync_driver/sync_service_observer.h" |
26 | 26 |
27 class PrefRegistrySimple; | 27 class PrefRegistrySimple; |
28 class PrefService; | 28 class PrefService; |
29 class SigninManagerBase; | |
29 | 30 |
30 namespace base { | 31 namespace base { |
31 class Value; | 32 class Value; |
32 } | 33 } |
33 | 34 |
34 namespace gfx { | 35 namespace gfx { |
35 class Image; | 36 class Image; |
36 } | 37 } |
37 | 38 |
38 namespace image_fetcher { | 39 namespace image_fetcher { |
39 class ImageFetcher; | 40 class ImageFetcher; |
40 } | 41 } |
41 | 42 |
42 namespace suggestions { | 43 namespace suggestions { |
43 class SuggestionsProfile; | 44 class SuggestionsProfile; |
44 } | 45 } |
45 | 46 |
46 namespace sync_driver { | 47 namespace sync_driver { |
47 class SyncService; | 48 class SyncService; |
48 } | 49 } |
49 | 50 |
50 namespace ntp_snippets { | 51 namespace ntp_snippets { |
51 | 52 |
52 enum class DisabledReason { | 53 // A Java counterpart will be generated for this enum. |
54 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.chrome.browser.ntp.snippets | |
noyau (Ping after 24h)
2016/06/21 14:28:55
This is only true for android, please amend the co
dgn
2016/06/21 15:47:39
Yes, the java_cpp_enum I added to the BUILD.gn fil
| |
55 enum class DisabledReason : int { | |
53 // Snippets are enabled | 56 // Snippets are enabled |
54 NONE, | 57 NONE, |
55 // Snippets have been disabled as part of the service configuration. | 58 // Snippets have been disabled as part of the service configuration. |
56 EXPLICITLY_DISABLED, | 59 EXPLICITLY_DISABLED, |
60 // The user is not signed in, and the service requires it to be enabled. | |
61 SIGNED_OUT, | |
62 // Sync is not enabled, and the service requires it to be enabled. | |
63 SYNC_DISABLED, | |
64 // The service requires passphrase encryption to be disabled. | |
65 PASSPHRASE_ENCRYPTION_ENABLED, | |
noyau (Ping after 24h)
2016/06/21 14:28:55
To be honest I don't like the idea of adding to th
dgn
2016/06/21 15:47:39
Very valid concern. We are working on some refacto
dgn
2016/06/24 20:25:20
Moved the dependencies to NTPSnippetStatusService.
markusheintz_
2016/06/28 14:22:53
I agree with Eric that we need to split this into
| |
57 // History sync is not enabled, and the service requires it to be enabled. | 66 // History sync is not enabled, and the service requires it to be enabled. |
58 HISTORY_SYNC_DISABLED, | 67 HISTORY_SYNC_DISABLED, |
59 // The sync service is not completely initialized, and the status is unknown. | 68 // The sync service is not completely initialized, and the status is unknown. |
60 HISTORY_SYNC_STATE_UNKNOWN | 69 HISTORY_SYNC_STATE_UNKNOWN |
61 }; | 70 }; |
62 | 71 |
63 class NTPSnippetsDatabase; | 72 class NTPSnippetsDatabase; |
64 class NTPSnippetsServiceObserver; | 73 class NTPSnippetsServiceObserver; |
65 | 74 |
66 // Stores and vends fresh content data for the NTP. | 75 // Stores and vends fresh content data for the NTP. |
67 class NTPSnippetsService : public KeyedService, | 76 class NTPSnippetsService : public KeyedService, |
68 public sync_driver::SyncServiceObserver { | 77 public sync_driver::SyncServiceObserver { |
69 public: | 78 public: |
70 using ImageFetchedCallback = | 79 using ImageFetchedCallback = |
71 base::Callback<void(const std::string& snippet_id, const gfx::Image&)>; | 80 base::Callback<void(const std::string& snippet_id, const gfx::Image&)>; |
72 | 81 |
73 // |application_language_code| should be a ISO 639-1 compliant string, e.g. | 82 // |application_language_code| should be a ISO 639-1 compliant string, e.g. |
74 // 'en' or 'en-US'. Note that this code should only specify the language, not | 83 // 'en' or 'en-US'. Note that this code should only specify the language, not |
75 // the locale, so 'en_US' (English language with US locale) and 'en-GB_US' | 84 // the locale, so 'en_US' (English language with US locale) and 'en-GB_US' |
76 // (British English person in the US) are not language codes. | 85 // (British English person in the US) are not language codes. |
77 NTPSnippetsService(bool enabled, | 86 NTPSnippetsService(bool enabled, |
78 PrefService* pref_service, | 87 PrefService* pref_service, |
88 SigninManagerBase* signin_manager, | |
79 sync_driver::SyncService* sync_service, | 89 sync_driver::SyncService* sync_service, |
80 suggestions::SuggestionsService* suggestions_service, | 90 suggestions::SuggestionsService* suggestions_service, |
81 const std::string& application_language_code, | 91 const std::string& application_language_code, |
82 NTPSnippetsScheduler* scheduler, | 92 NTPSnippetsScheduler* scheduler, |
83 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher, | 93 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher, |
84 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher, | 94 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher, |
85 std::unique_ptr<NTPSnippetsDatabase> database); | 95 std::unique_ptr<NTPSnippetsDatabase> database); |
86 ~NTPSnippetsService() override; | 96 ~NTPSnippetsService() override; |
87 | 97 |
88 static void RegisterProfilePrefs(PrefRegistrySimple* registry); | 98 static void RegisterProfilePrefs(PrefRegistrySimple* registry); |
(...skipping 24 matching lines...) Expand all Loading... | |
113 // Returns the list of snippets previously discarded by the user (that are | 123 // Returns the list of snippets previously discarded by the user (that are |
114 // not expired yet). | 124 // not expired yet). |
115 const NTPSnippet::PtrVector& discarded_snippets() const { | 125 const NTPSnippet::PtrVector& discarded_snippets() const { |
116 return discarded_snippets_; | 126 return discarded_snippets_; |
117 } | 127 } |
118 | 128 |
119 const NTPSnippetsFetcher* snippets_fetcher() const { | 129 const NTPSnippetsFetcher* snippets_fetcher() const { |
120 return snippets_fetcher_.get(); | 130 return snippets_fetcher_.get(); |
121 } | 131 } |
122 | 132 |
133 // Returns a reason why the service is disabled, or DisabledReason::NONE | |
134 // if it's not. | |
135 DisabledReason disabled_reason() const { return disabled_reason_; } | |
136 | |
123 // (Re)schedules the periodic fetching of snippets. This is necessary because | 137 // (Re)schedules the periodic fetching of snippets. This is necessary because |
124 // the schedule depends on the time of day. | 138 // the schedule depends on the time of day. |
125 void RescheduleFetching(); | 139 void RescheduleFetching(); |
126 | 140 |
127 // Fetches the image for the snippet with the given |snippet_id| and runs the | 141 // Fetches the image for the snippet with the given |snippet_id| and runs the |
128 // |callback|. If that snippet doesn't exist or the fetch fails, the callback | 142 // |callback|. If that snippet doesn't exist or the fetch fails, the callback |
129 // gets a null image. | 143 // gets a null image. |
130 void FetchSnippetImage(const std::string& snippet_id, | 144 void FetchSnippetImage(const std::string& snippet_id, |
131 const ImageFetchedCallback& callback); | 145 const ImageFetchedCallback& callback); |
132 | 146 |
133 // Deletes all currently stored snippets. | 147 // Deletes all currently stored snippets. |
134 void ClearSnippets(); | 148 void ClearSnippets(); |
135 | 149 |
136 // Discards the snippet with the given |snippet_id|, if it exists. Returns | 150 // Discards the snippet with the given |snippet_id|, if it exists. Returns |
137 // true iff a snippet was discarded. | 151 // true iff a snippet was discarded. |
138 bool DiscardSnippet(const std::string& snippet_id); | 152 bool DiscardSnippet(const std::string& snippet_id); |
139 | 153 |
140 // Clears the lists of snippets previously discarded by the user. | 154 // Clears the lists of snippets previously discarded by the user. |
141 void ClearDiscardedSnippets(); | 155 void ClearDiscardedSnippets(); |
142 | 156 |
143 // Returns the lists of suggestion hosts the snippets are restricted to. | 157 // Returns the lists of suggestion hosts the snippets are restricted to. |
144 std::set<std::string> GetSuggestionsHosts() const; | 158 std::set<std::string> GetSuggestionsHosts() const; |
145 | 159 |
146 // Observer accessors. | 160 // Observer accessors. |
147 void AddObserver(NTPSnippetsServiceObserver* observer); | 161 void AddObserver(NTPSnippetsServiceObserver* observer); |
148 void RemoveObserver(NTPSnippetsServiceObserver* observer); | 162 void RemoveObserver(NTPSnippetsServiceObserver* observer); |
149 | 163 |
150 // Returns a reason why the service could be disabled, or DisabledReason::NONE | |
151 // if it's not. | |
152 DisabledReason GetDisabledReason() const; | |
153 | |
154 // Returns the maximum number of snippets that will be shown at once. | 164 // Returns the maximum number of snippets that will be shown at once. |
155 static int GetMaxSnippetCountForTesting(); | 165 static int GetMaxSnippetCountForTesting(); |
156 | 166 |
157 private: | 167 private: |
158 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceTest, SyncStateCompatibility); | 168 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceTest, SyncStateCompatibility); |
159 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceTest, HistorySyncStateChanges); | 169 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceTest, HistorySyncStateChanges); |
160 | 170 |
161 // Possible state transitions: | 171 // Possible state transitions: |
162 // +------- NOT_INITED ------+ | 172 // +------- NOT_INITED ------+ |
163 // | / \ | | 173 // | / \ | |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
215 // Removes the expired snippets (including discarded) from the service and the | 225 // Removes the expired snippets (including discarded) from the service and the |
216 // database, and schedules another pass for the next expiration. | 226 // database, and schedules another pass for the next expiration. |
217 void ClearExpiredSnippets(); | 227 void ClearExpiredSnippets(); |
218 | 228 |
219 // Completes the initialization phase of the service, registering the last | 229 // Completes the initialization phase of the service, registering the last |
220 // observers. This is done after construction, once the database is loaded. | 230 // observers. This is done after construction, once the database is loaded. |
221 void FinishInitialization(); | 231 void FinishInitialization(); |
222 | 232 |
223 void LoadingSnippetsFinished(); | 233 void LoadingSnippetsFinished(); |
224 | 234 |
235 // Updates |disabled_reason_| (see |GetNewDisabledReason()|) and notifies | |
236 // observers of changes if necessary. | |
237 void UpdateDisabledReason(); | |
238 | |
239 // Utility method that looks at the state of the service and its dependencies | |
240 // to determine if and why it should be disabled. | |
241 DisabledReason GetNewDisabledReason() const; | |
242 | |
225 // Returns whether the service should be enabled or disable depending on its | 243 // Returns whether the service should be enabled or disable depending on its |
226 // internal state and the state of its dependencies. | 244 // internal state and the state of its dependencies. |
227 State GetStateForDependenciesStatus(); | 245 State GetStateForDependenciesStatus(); |
228 | 246 |
229 // Verifies state transitions (see |State|'s documentation) and applies them. | 247 // Verifies state transitions (see |State|'s documentation) and applies them. |
230 // Does nothing if called with the current state. | 248 // Does nothing if called with the current state. |
231 void EnterState(State state); | 249 void EnterState(State state); |
232 | 250 |
233 // Enables the service and triggers a fetch if required. Do not call directly, | 251 // Enables the service and triggers a fetch if required. Do not call directly, |
234 // use |EnterState| instead. | 252 // use |EnterState| instead. |
235 void EnterStateEnabled(bool fetch_snippets); | 253 void EnterStateEnabled(bool fetch_snippets); |
236 | 254 |
237 // Disables the service. Do not call directly, use |EnterState| instead. | 255 // Disables the service. Do not call directly, use |EnterState| instead. |
238 void EnterStateDisabled(); | 256 void EnterStateDisabled(); |
239 | 257 |
240 // Applies the effects of the transition to the SHUT_DOWN state. Do not call | 258 // Applies the effects of the transition to the SHUT_DOWN state. Do not call |
241 // directly, use |EnterState| instead. | 259 // directly, use |EnterState| instead. |
242 void EnterStateShutdown(); | 260 void EnterStateShutdown(); |
243 | 261 |
244 void ClearDeprecatedPrefs(); | 262 void ClearDeprecatedPrefs(); |
245 | 263 |
246 State state_; | 264 State state_; |
247 | 265 |
248 // The service was set up to be disabled and should not be enabled by any | 266 DisabledReason disabled_reason_; |
249 // state change. We track this to make sure we clear scheduled tasks, which | |
250 // persist even when Chrome is stopped. | |
251 bool explicitly_disabled_; | |
252 | 267 |
253 PrefService* pref_service_; | 268 PrefService* pref_service_; |
254 | 269 |
270 SigninManagerBase* signin_manager_; | |
271 | |
255 sync_driver::SyncService* sync_service_; | 272 sync_driver::SyncService* sync_service_; |
256 | 273 |
257 // The observer for the SyncService. When the sync state changes, | 274 // The observer for the SyncService. When the sync state changes, |
258 // SyncService will call |OnStateChanged|, which is propagated to the | 275 // SyncService will call |OnStateChanged|, which is propagated to the |
259 // snippet observers. | 276 // snippet observers. |
260 ScopedObserver<sync_driver::SyncService, sync_driver::SyncServiceObserver> | 277 ScopedObserver<sync_driver::SyncService, sync_driver::SyncServiceObserver> |
261 sync_service_observer_; | 278 sync_service_observer_; |
262 | 279 |
263 suggestions::SuggestionsService* suggestions_service_; | 280 suggestions::SuggestionsService* suggestions_service_; |
264 | 281 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
302 | 319 |
303 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); | 320 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); |
304 }; | 321 }; |
305 | 322 |
306 class NTPSnippetsServiceObserver { | 323 class NTPSnippetsServiceObserver { |
307 public: | 324 public: |
308 // Sent every time the service loads a new set of data. | 325 // Sent every time the service loads a new set of data. |
309 virtual void NTPSnippetsServiceLoaded() = 0; | 326 virtual void NTPSnippetsServiceLoaded() = 0; |
310 // Sent when the service is shutting down. | 327 // Sent when the service is shutting down. |
311 virtual void NTPSnippetsServiceShutdown() = 0; | 328 virtual void NTPSnippetsServiceShutdown() = 0; |
312 // Sent when the service has been disabled. Can be from explicit user action | 329 // Sent when the state of the service is changing. Something changed in its |
313 // or because a requirement (e.g. History Sync) is not fulfilled anymore. | 330 // dependencies so it's notifying observers about incoming data changes. |
314 virtual void NTPSnippetsServiceDisabled() = 0; | 331 // If the service might be enabled, DisabledReason::NONE will be provided. |
332 virtual void NTPSnippetsServiceDisabledReasonChanged(DisabledReason) = 0; | |
315 | 333 |
316 protected: | 334 protected: |
317 virtual ~NTPSnippetsServiceObserver() {} | 335 virtual ~NTPSnippetsServiceObserver() {} |
318 }; | 336 }; |
319 | 337 |
320 } // namespace ntp_snippets | 338 } // namespace ntp_snippets |
321 | 339 |
322 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ | 340 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ |
OLD | NEW |