Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(503)

Side by Side Diff: components/ntp_snippets/ntp_snippets_service.h

Issue 1987333003: [NTP Snippets] Persist snippets in a LevelDB instead of prefs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 class NTPSnippetsDatabase;
53 class NTPSnippetsServiceObserver; 53 class NTPSnippetsServiceObserver;
54 54
55 // Stores and vends fresh content data for the NTP. 55 // Stores and vends fresh content data for the NTP.
56 class NTPSnippetsService : public KeyedService, 56 class NTPSnippetsService : public KeyedService,
57 public sync_driver::SyncServiceObserver { 57 public sync_driver::SyncServiceObserver {
58 public: 58 public:
59 using ImageFetchedCallback = 59 using ImageFetchedCallback =
60 base::Callback<void(const std::string& snippet_id, const gfx::Image&)>; 60 base::Callback<void(const std::string& snippet_id, const gfx::Image&)>;
61 61
62 // |application_language_code| should be a ISO 639-1 compliant string, e.g. 62 // |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 63 // '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' 64 // 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. 65 // (British English person in the US) are not language codes.
66 NTPSnippetsService( 66 NTPSnippetsService(PrefService* pref_service,
67 PrefService* pref_service, 67 sync_driver::SyncService* sync_service,
68 sync_driver::SyncService* sync_service, 68 suggestions::SuggestionsService* suggestions_service,
69 suggestions::SuggestionsService* suggestions_service, 69 const std::string& application_language_code,
70 scoped_refptr<base::SequencedTaskRunner> file_task_runner, 70 NTPSnippetsScheduler* scheduler,
71 const std::string& application_language_code, 71 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher,
72 NTPSnippetsScheduler* scheduler, 72 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher,
73 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher, 73 std::unique_ptr<NTPSnippetsDatabase> database);
74 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher);
75 ~NTPSnippetsService() override; 74 ~NTPSnippetsService() override;
76 75
77 static void RegisterProfilePrefs(PrefRegistrySimple* registry); 76 static void RegisterProfilePrefs(PrefRegistrySimple* registry);
78 77
79 void Init(bool enabled); 78 void Init(bool enabled);
80 79
81 // Inherited from KeyedService. 80 // Inherited from KeyedService.
82 void Shutdown() override; 81 void Shutdown() override;
83 82
83 // Returns whether the initial set of snippets has been loaded from the
84 // database. While this is false, the list of snippets will be empty, and all
85 // modifications to it (fetch, discard, etc) will be ignored.
86 bool loaded() const { return state_ == State::LOADED; }
87
84 // Fetches snippets from the server and adds them to the current ones. 88 // Fetches snippets from the server and adds them to the current ones.
85 void FetchSnippets(); 89 void FetchSnippets();
86 // Fetches snippets from the server for specified hosts (overriding 90 // Fetches snippets from the server for specified hosts (overriding
87 // suggestions from the suggestion service) and adds them to the current ones. 91 // suggestions from the suggestion service) and adds them to the current ones.
88 void FetchSnippetsFromHosts(const std::set<std::string>& hosts); 92 void FetchSnippetsFromHosts(const std::set<std::string>& hosts);
89 93
90 // Available snippets. 94 // Available snippets.
91 const NTPSnippet::PtrVector& snippets() const { return snippets_; } 95 const NTPSnippet::PtrVector& snippets() const { return snippets_; }
92 96
93 // Returns the list of snippets previously discarded by the user (that are 97 // Returns the list of snippets previously discarded by the user (that are
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
132 136
133 private: 137 private:
134 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceWithSyncTest, 138 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceWithSyncTest,
135 SyncStateCompatibility); 139 SyncStateCompatibility);
136 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceWithSyncTest, 140 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceWithSyncTest,
137 HistorySyncStateChanges); 141 HistorySyncStateChanges);
138 142
139 // sync_driver::SyncServiceObserver implementation. 143 // sync_driver::SyncServiceObserver implementation.
140 void OnStateChanged() override; 144 void OnStateChanged() override;
141 145
146 // Callback for the NTPSnippetsDatabase.
147 void OnDatabaseLoaded(NTPSnippet::PtrVector snippets);
148
149 // Callback for the SuggestionsService.
142 void OnSuggestionsChanged(const suggestions::SuggestionsProfile& suggestions); 150 void OnSuggestionsChanged(const suggestions::SuggestionsProfile& suggestions);
151
152 // Callback for the NTPSnippetsFetcher.
143 void OnFetchFinished(NTPSnippetsFetcher::OptionalSnippets snippets); 153 void OnFetchFinished(NTPSnippetsFetcher::OptionalSnippets snippets);
144 154
145 // Merges newly available snippets with the previously available list. 155 // Merges newly available snippets with the previously available list.
146 void MergeSnippets(NTPSnippet::PtrVector new_snippets); 156 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 157
154 std::set<std::string> GetSnippetHostsFromPrefs() const; 158 std::set<std::string> GetSnippetHostsFromPrefs() const;
155 void StoreSnippetHostsToPrefs(const std::set<std::string>& hosts); 159 void StoreSnippetHostsToPrefs(const std::set<std::string>& hosts);
156 160
157 void LoadingSnippetsFinished(); 161 void LoadingSnippetsFinished();
158 162
159 // Checks whether the state of the sync service is incompatible with showing 163 // Checks whether the state of the sync service is incompatible with showing
160 // snippets. We need history sync to be active. 164 // snippets. We need history sync to be active.
161 // Note: The state is considered compatible if the service is still 165 // Note: The state is considered compatible if the service is still
162 // initializing and the sync state is not known. 166 // initializing and the sync state is not known.
163 bool IsSyncStateIncompatible(); 167 bool IsSyncStateIncompatible();
164 168
165 enum class State { 169 enum class State {
166 NOT_INITED, 170 NOT_INITED,
167 INITED, 171 INITED,
Bernhard Bauer 2016/05/25 15:01:15 We should add some comments now for what these sta
Marc Treib 2016/05/27 14:03:12 Sure, done. dgn's https://codereview.chromium.org/
172 LOADED,
168 SHUT_DOWN 173 SHUT_DOWN
169 } state_; 174 } state_;
170 175
171 bool enabled_; 176 bool enabled_;
172 177
173 PrefService* pref_service_; 178 PrefService* pref_service_;
174 179
175 sync_driver::SyncService* sync_service_; 180 sync_driver::SyncService* sync_service_;
176 181
177 // The observer for the SyncService. When the sync state changes, 182 // The observer for the SyncService. When the sync state changes,
178 // SyncService will call |OnStateChanged|, which is propagated to the 183 // SyncService will call |OnStateChanged|, which is propagated to the
179 // snippet observers. 184 // snippet observers.
180 ScopedObserver<sync_driver::SyncService, sync_driver::SyncServiceObserver> 185 ScopedObserver<sync_driver::SyncService, sync_driver::SyncServiceObserver>
181 sync_service_observer_; 186 sync_service_observer_;
182 187
183 suggestions::SuggestionsService* suggestions_service_; 188 suggestions::SuggestionsService* suggestions_service_;
184 189
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). 190 // All current suggestions (i.e. not discarded ones).
189 NTPSnippet::PtrVector snippets_; 191 NTPSnippet::PtrVector snippets_;
Marc Treib 2016/05/23 14:00:58 To be discussed: Should we remove this (and discar
Bernhard Bauer 2016/05/25 15:01:15 Yeah, that probably makes sense -- and it will all
Marc Treib 2016/05/27 14:03:12 It'll change the interface though (getting snippet
Bernhard Bauer 2016/05/27 16:06:43 Hm... y'know, I'm thinking it might be worth front
Marc Treib 2016/05/30 13:22:00 It will require re-thinking the NTPSnippetsService
190 192
191 // Suggestions that the user discarded. We keep these around until they expire 193 // Suggestions that the user discarded. We keep these around until they expire
192 // so we won't re-add them on the next fetch. 194 // so we won't re-add them on the next fetch.
193 NTPSnippet::PtrVector discarded_snippets_; 195 NTPSnippet::PtrVector discarded_snippets_;
194 196
195 // The ISO 639-1 code of the language used by the application. 197 // The ISO 639-1 code of the language used by the application.
196 const std::string application_language_code_; 198 const std::string application_language_code_;
197 199
198 // The observers. 200 // The observers.
199 base::ObserverList<NTPSnippetsServiceObserver> observers_; 201 base::ObserverList<NTPSnippetsServiceObserver> observers_;
200 202
201 // Scheduler for fetching snippets. Not owned. 203 // Scheduler for fetching snippets. Not owned.
202 NTPSnippetsScheduler* scheduler_; 204 NTPSnippetsScheduler* scheduler_;
203 205
204 // The subscription to the SuggestionsService. When the suggestions change, 206 // The subscription to the SuggestionsService. When the suggestions change,
205 // SuggestionsService will call |OnSuggestionsChanged|, which triggers an 207 // SuggestionsService will call |OnSuggestionsChanged|, which triggers an
206 // update to the set of snippets. 208 // update to the set of snippets.
207 using SuggestionsSubscription = 209 using SuggestionsSubscription =
208 suggestions::SuggestionsService::ResponseCallbackList::Subscription; 210 suggestions::SuggestionsService::ResponseCallbackList::Subscription;
209 std::unique_ptr<SuggestionsSubscription> suggestions_service_subscription_; 211 std::unique_ptr<SuggestionsSubscription> suggestions_service_subscription_;
210 212
211 // The snippets fetcher. 213 // The snippets fetcher.
212 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_; 214 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_;
213 215
214 // Timer that calls us back when the next snippet expires. 216 // Timer that calls us back when the next snippet expires.
215 base::OneShotTimer expiry_timer_; 217 base::OneShotTimer expiry_timer_;
216 218
217 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher_; 219 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher_;
218 220
221 // The database for persisting snippets.
222 std::unique_ptr<NTPSnippetsDatabase> database_;
223
219 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); 224 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService);
220 }; 225 };
221 226
222 class NTPSnippetsServiceObserver { 227 class NTPSnippetsServiceObserver {
223 public: 228 public:
224 // Sent every time the service loads a new set of data. 229 // Sent every time the service loads a new set of data.
225 virtual void NTPSnippetsServiceLoaded() = 0; 230 virtual void NTPSnippetsServiceLoaded() = 0;
226 // Sent when the service is shutting down. 231 // Sent when the service is shutting down.
227 virtual void NTPSnippetsServiceShutdown() = 0; 232 virtual void NTPSnippetsServiceShutdown() = 0;
228 // Sent when the service has been disabled. Can be from explicit user action 233 // 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. 234 // or because a requirement (e.g. History Sync) is not fulfilled anymore.
230 virtual void NTPSnippetsServiceDisabled() = 0; 235 virtual void NTPSnippetsServiceDisabled() = 0;
231 236
232 protected: 237 protected:
233 virtual ~NTPSnippetsServiceObserver() {} 238 virtual ~NTPSnippetsServiceObserver() {}
234 }; 239 };
235 240
236 } // namespace ntp_snippets 241 } // namespace ntp_snippets
237 242
238 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ 243 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698