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

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

Issue 1976153002: Various NTPSnippetsService cleanups: (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Shutdown() in tests 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>
(...skipping 28 matching lines...) Expand all
39 class SuggestionsProfile; 39 class SuggestionsProfile;
40 } 40 }
41 41
42 namespace ntp_snippets { 42 namespace ntp_snippets {
43 43
44 class NTPSnippetsServiceObserver; 44 class NTPSnippetsServiceObserver;
45 45
46 // Stores and vends fresh content data for the NTP. 46 // Stores and vends fresh content data for the NTP.
47 class NTPSnippetsService : public KeyedService { 47 class NTPSnippetsService : public KeyedService {
48 public: 48 public:
49 using NTPSnippetStorage = NTPSnippet::PtrVector;
50
51 using ImageFetchedCallback = 49 using ImageFetchedCallback =
52 base::Callback<void(const std::string& snippet_id, const SkBitmap*)>; 50 base::Callback<void(const std::string& snippet_id, const SkBitmap*)>;
53 51
54 // |application_language_code| should be a ISO 639-1 compliant string, e.g. 52 // |application_language_code| should be a ISO 639-1 compliant string, e.g.
55 // 'en' or 'en-US'. Note that this code should only specify the language, not 53 // 'en' or 'en-US'. Note that this code should only specify the language, not
56 // the locale, so 'en_US' (English language with US locale) and 'en-GB_US' 54 // the locale, so 'en_US' (English language with US locale) and 'en-GB_US'
57 // (British English person in the US) are not language codes. 55 // (British English person in the US) are not language codes.
58 NTPSnippetsService( 56 NTPSnippetsService(
59 PrefService* pref_service, 57 PrefService* pref_service,
60 suggestions::SuggestionsService* suggestions_service, 58 suggestions::SuggestionsService* suggestions_service,
(...skipping 11 matching lines...) Expand all
72 // Inherited from KeyedService. 70 // Inherited from KeyedService.
73 void Shutdown() override; 71 void Shutdown() override;
74 72
75 // Fetches snippets from the server and adds them to the current ones. 73 // Fetches snippets from the server and adds them to the current ones.
76 void FetchSnippets(); 74 void FetchSnippets();
77 // Fetches snippets from the server for specified hosts (overriding 75 // Fetches snippets from the server for specified hosts (overriding
78 // suggestions from the suggestion service) and adds them to the current ones. 76 // suggestions from the suggestion service) and adds them to the current ones.
79 void FetchSnippetsFromHosts(const std::set<std::string>& hosts); 77 void FetchSnippetsFromHosts(const std::set<std::string>& hosts);
80 78
81 // Available snippets. 79 // Available snippets.
82 const NTPSnippetStorage& snippets() const { return snippets_; } 80 const NTPSnippet::PtrVector& snippets() const { return snippets_; }
83 81
84 // Returns the list of snippets previously discarded by the user (that are 82 // Returns the list of snippets previously discarded by the user (that are
85 // not expired yet). 83 // not expired yet).
86 const NTPSnippetStorage& discarded_snippets() const { 84 const NTPSnippet::PtrVector& discarded_snippets() const {
87 return discarded_snippets_; 85 return discarded_snippets_;
88 } 86 }
89 87
90 // Returns the last status message from the snippets fetcher. 88 // Returns the last status message from the snippets fetcher.
91 const std::string& last_status() const { 89 const std::string& last_status() const {
92 return snippets_fetcher_->last_status(); 90 return snippets_fetcher_->last_status();
93 } 91 }
94 92
95 // Returns the last json from the snippets fetcher. 93 // Returns the last json from the snippets fetcher.
96 const std::string& last_json() const { 94 const std::string& last_json() const {
(...skipping 27 matching lines...) Expand all
124 void AddObserver(NTPSnippetsServiceObserver* observer); 122 void AddObserver(NTPSnippetsServiceObserver* observer);
125 void RemoveObserver(NTPSnippetsServiceObserver* observer); 123 void RemoveObserver(NTPSnippetsServiceObserver* observer);
126 124
127 // Returns the maximum number of snippets that will be shown at once. 125 // Returns the maximum number of snippets that will be shown at once.
128 static int GetMaxSnippetCountForTesting(); 126 static int GetMaxSnippetCountForTesting();
129 127
130 private: 128 private:
131 void OnSuggestionsChanged(const suggestions::SuggestionsProfile& suggestions); 129 void OnSuggestionsChanged(const suggestions::SuggestionsProfile& suggestions);
132 void OnFetchFinished(NTPSnippetsFetcher::OptionalSnippets snippets); 130 void OnFetchFinished(NTPSnippetsFetcher::OptionalSnippets snippets);
133 131
134 // Expects a top-level dictionary containing a "recos" list, each element of
135 // which will be parsed as a snippet.
136 bool LoadFromFetchedValue(const base::Value& value);
137
138 // Merges newly available snippets with the previously available list. 132 // Merges newly available snippets with the previously available list.
139 void MergeSnippets(NTPSnippetStorage new_snippets); 133 void MergeSnippets(NTPSnippet::PtrVector new_snippets);
140 // TODO(treib): Investigate a better storage, maybe LevelDB or SQLite? 134 // TODO(treib): Investigate a better storage, maybe LevelDB or SQLite?
141 void LoadSnippetsFromPrefs(); 135 void LoadSnippetsFromPrefs();
142 void StoreSnippetsToPrefs(); 136 void StoreSnippetsToPrefs();
143 137
144 void LoadDiscardedSnippetsFromPrefs(); 138 void LoadDiscardedSnippetsFromPrefs();
145 void StoreDiscardedSnippetsToPrefs(); 139 void StoreDiscardedSnippetsToPrefs();
146 140
147 std::set<std::string> GetSnippetHostsFromPrefs() const; 141 std::set<std::string> GetSnippetHostsFromPrefs() const;
148 void StoreSnippetHostsToPrefs(const std::set<std::string>& hosts); 142 void StoreSnippetHostsToPrefs(const std::set<std::string>& hosts);
149 143
150 void LoadingSnippetsFinished(); 144 void LoadingSnippetsFinished();
151 145
146 bool did_init_;
Bernhard Bauer 2016/05/13 16:04:05 Alternative would be to use a tri-state enum.
Bernhard Bauer 2016/05/17 09:23:40 ^^^ WDYT?
Marc Treib 2016/05/17 11:21:18 I didn't forget, just wanted to sort out the other
147 bool did_shutdown_;
148
152 bool enabled_; 149 bool enabled_;
153 150
154 PrefService* pref_service_; 151 PrefService* pref_service_;
155 152
156 suggestions::SuggestionsService* suggestions_service_; 153 suggestions::SuggestionsService* suggestions_service_;
157 154
158 // The SequencedTaskRunner on which file system operations will be run. 155 // The SequencedTaskRunner on which file system operations will be run.
159 scoped_refptr<base::SequencedTaskRunner> file_task_runner_; 156 scoped_refptr<base::SequencedTaskRunner> file_task_runner_;
160 157
161 // All current suggestions (i.e. not discarded ones). 158 // All current suggestions (i.e. not discarded ones).
162 NTPSnippetStorage snippets_; 159 NTPSnippet::PtrVector snippets_;
163 160
164 // Suggestions that the user discarded. We keep these around until they expire 161 // Suggestions that the user discarded. We keep these around until they expire
165 // so we won't re-add them on the next fetch. 162 // so we won't re-add them on the next fetch.
166 NTPSnippetStorage discarded_snippets_; 163 NTPSnippet::PtrVector discarded_snippets_;
167 164
168 // The ISO 639-1 code of the language used by the application. 165 // The ISO 639-1 code of the language used by the application.
169 const std::string application_language_code_; 166 const std::string application_language_code_;
170 167
171 // The observers. 168 // The observers.
172 base::ObserverList<NTPSnippetsServiceObserver> observers_; 169 base::ObserverList<NTPSnippetsServiceObserver> observers_;
173 170
174 // Scheduler for fetching snippets. Not owned. 171 // Scheduler for fetching snippets. Not owned.
175 NTPSnippetsScheduler* scheduler_; 172 NTPSnippetsScheduler* scheduler_;
176 173
(...skipping 22 matching lines...) Expand all
199 // Sent when the service is shutting down. 196 // Sent when the service is shutting down.
200 virtual void NTPSnippetsServiceShutdown() = 0; 197 virtual void NTPSnippetsServiceShutdown() = 0;
201 198
202 protected: 199 protected:
203 virtual ~NTPSnippetsServiceObserver() {} 200 virtual ~NTPSnippetsServiceObserver() {}
204 }; 201 };
205 202
206 } // namespace ntp_snippets 203 } // namespace ntp_snippets
207 204
208 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ 205 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_
OLDNEW
« no previous file with comments | « no previous file | components/ntp_snippets/ntp_snippets_service.cc » ('j') | components/ntp_snippets/ntp_snippets_service.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698