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 <set> | 10 #include <set> |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 72 | 72 |
| 73 // Inherited from KeyedService. | 73 // Inherited from KeyedService. |
| 74 void Shutdown() override; | 74 void Shutdown() override; |
| 75 | 75 |
| 76 // Fetches snippets from the server and adds them to the current ones. | 76 // Fetches snippets from the server and adds them to the current ones. |
| 77 void FetchSnippets(); | 77 void FetchSnippets(); |
| 78 // Fetches snippets from the server for specified hosts (overriding | 78 // Fetches snippets from the server for specified hosts (overriding |
| 79 // suggestions from the suggestion service) and adds them to the current ones. | 79 // suggestions from the suggestion service) and adds them to the current ones. |
| 80 void FetchSnippetsFromHosts(const std::set<std::string>& hosts); | 80 void FetchSnippetsFromHosts(const std::set<std::string>& hosts); |
| 81 | 81 |
| 82 // Returns the last status message from the snippets fetcher. | |
| 83 const std::string get_last_status() const { | |
|
Marc Treib
2016/04/21 13:47:21
Just "last_status", no "get".
Also, return a refer
jkrcal
2016/04/22 09:30:28
Done.
| |
| 84 return snippets_fetcher_last_status_; | |
| 85 } | |
| 86 | |
| 82 // (Re)schedules the periodic fetching of snippets. This is necessary because | 87 // (Re)schedules the periodic fetching of snippets. This is necessary because |
| 83 // the schedule depends on the time of day | 88 // the schedule depends on the time of day |
| 84 void RescheduleFetching(); | 89 void RescheduleFetching(); |
| 85 | 90 |
| 86 // Deletes all currently stored snippets. | 91 // Deletes all currently stored snippets. |
| 87 void ClearSnippets(); | 92 void ClearSnippets(); |
| 88 | 93 |
| 89 // Discards the snippet with the given |url|, if it exists. Returns true iff | 94 // Discards the snippet with the given |url|, if it exists. Returns true iff |
| 90 // a snippet was discarded. | 95 // a snippet was discarded. |
| 91 bool DiscardSnippet(const GURL& url); | 96 bool DiscardSnippet(const GURL& url); |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 116 // for (auto& snippet : *service) { | 121 // for (auto& snippet : *service) { |
| 117 // // |snippet| here is a const object. | 122 // // |snippet| here is a const object. |
| 118 // } | 123 // } |
| 119 const_iterator begin() const { return const_iterator(snippets_.begin()); } | 124 const_iterator begin() const { return const_iterator(snippets_.begin()); } |
| 120 const_iterator end() const { return const_iterator(snippets_.end()); } | 125 const_iterator end() const { return const_iterator(snippets_.end()); } |
| 121 | 126 |
| 122 private: | 127 private: |
| 123 friend class NTPSnippetsServiceTest; | 128 friend class NTPSnippetsServiceTest; |
| 124 | 129 |
| 125 void OnSuggestionsChanged(const suggestions::SuggestionsProfile& suggestions); | 130 void OnSuggestionsChanged(const suggestions::SuggestionsProfile& suggestions); |
| 126 void OnSnippetsDownloaded(const std::string& snippets_json); | 131 void OnSnippetsDownloaded(const std::string& snippets_json, |
| 132 const std::string& status); | |
| 127 | 133 |
| 128 void OnJsonParsed(const std::string& snippets_json, | 134 void OnJsonParsed(const std::string& snippets_json, |
| 129 scoped_ptr<base::Value> parsed); | 135 scoped_ptr<base::Value> parsed); |
| 130 void OnJsonError(const std::string& snippets_json, const std::string& error); | 136 void OnJsonError(const std::string& snippets_json, const std::string& error); |
| 131 | 137 |
| 132 // Expects a top-level dictionary containing a "recos" list, which will be | 138 // Expects a top-level dictionary containing a "recos" list, which will be |
| 133 // passed to LoadFromListValue(). | 139 // passed to LoadFromListValue(). |
| 134 bool LoadFromValue(const base::Value& value); | 140 bool LoadFromValue(const base::Value& value); |
| 135 | 141 |
| 136 // Expects a list of dictionaries each containing a "contentInfo" dictionary | 142 // Expects a list of dictionaries each containing a "contentInfo" dictionary |
| 137 // with keys matching the properties of a snippet (url, title, site_title, | 143 // with keys matching the properties of a snippet (url, title, site_title, |
| 138 // etc...). The URL is the only mandatory value. | 144 // etc...). The URL is the only mandatory value. |
| 139 bool LoadFromListValue(const base::ListValue& list); | 145 bool LoadFromListValue(const base::ListValue& list); |
| 140 | 146 |
| 141 // TODO(treib): Investigate a better storage, maybe LevelDB or SQLite? | 147 // TODO(treib): Investigate a better storage, maybe LevelDB or SQLite? |
| 142 void LoadSnippetsFromPrefs(); | 148 void LoadSnippetsFromPrefs(); |
| 143 void StoreSnippetsToPrefs(); | 149 void StoreSnippetsToPrefs(); |
| 144 | 150 |
| 145 void LoadDiscardedSnippetsFromPrefs(); | 151 void LoadDiscardedSnippetsFromPrefs(); |
| 146 void StoreDiscardedSnippetsToPrefs(); | 152 void StoreDiscardedSnippetsToPrefs(); |
| 147 | 153 |
| 148 std::set<std::string> GetSnippetHostsFromPrefs() const; | 154 std::set<std::string> GetSnippetHostsFromPrefs() const; |
| 149 void StoreSnippetHostsToPrefs(const std::set<std::string>& hosts); | 155 void StoreSnippetHostsToPrefs(const std::set<std::string>& hosts); |
| 150 | 156 |
| 151 bool HasDiscardedSnippet(const GURL& url) const; | 157 bool HasDiscardedSnippet(const GURL& url) const; |
| 152 | 158 |
| 153 void RemoveExpiredSnippets(); | 159 void LoadingSnippetsFinished(); |
| 154 | 160 |
| 155 bool enabled_; | 161 bool enabled_; |
| 156 | 162 |
| 157 PrefService* pref_service_; | 163 PrefService* pref_service_; |
| 158 | 164 |
| 159 suggestions::SuggestionsService* suggestions_service_; | 165 suggestions::SuggestionsService* suggestions_service_; |
| 160 | 166 |
| 161 // The SequencedTaskRunner on which file system operations will be run. | 167 // The SequencedTaskRunner on which file system operations will be run. |
| 162 scoped_refptr<base::SequencedTaskRunner> file_task_runner_; | 168 scoped_refptr<base::SequencedTaskRunner> file_task_runner_; |
| 163 | 169 |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 184 suggestions::SuggestionsService::ResponseCallbackList::Subscription; | 190 suggestions::SuggestionsService::ResponseCallbackList::Subscription; |
| 185 scoped_ptr<SuggestionsSubscription> suggestions_service_subscription_; | 191 scoped_ptr<SuggestionsSubscription> suggestions_service_subscription_; |
| 186 | 192 |
| 187 // The snippets fetcher. | 193 // The snippets fetcher. |
| 188 scoped_ptr<NTPSnippetsFetcher> snippets_fetcher_; | 194 scoped_ptr<NTPSnippetsFetcher> snippets_fetcher_; |
| 189 | 195 |
| 190 // The subscription to the snippets fetcher. | 196 // The subscription to the snippets fetcher. |
| 191 scoped_ptr<NTPSnippetsFetcher::SnippetsAvailableCallbackList::Subscription> | 197 scoped_ptr<NTPSnippetsFetcher::SnippetsAvailableCallbackList::Subscription> |
| 192 snippets_fetcher_subscription_; | 198 snippets_fetcher_subscription_; |
| 193 | 199 |
| 200 std::string snippets_fetcher_last_status_; | |
|
Marc Treib
2016/04/21 13:47:21
IMO this name is a bit misleading, ad it doesn't a
jkrcal
2016/04/22 09:30:28
Done.
| |
| 201 | |
| 194 // Timer that calls us back when the next snippet expires. | 202 // Timer that calls us back when the next snippet expires. |
| 195 base::OneShotTimer expiry_timer_; | 203 base::OneShotTimer expiry_timer_; |
| 196 | 204 |
| 197 ParseJSONCallback parse_json_callback_; | 205 ParseJSONCallback parse_json_callback_; |
| 198 | 206 |
| 199 base::WeakPtrFactory<NTPSnippetsService> weak_ptr_factory_; | 207 base::WeakPtrFactory<NTPSnippetsService> weak_ptr_factory_; |
| 200 | 208 |
| 201 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); | 209 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); |
| 202 }; | 210 }; |
| 203 | 211 |
| 204 class NTPSnippetsServiceObserver { | 212 class NTPSnippetsServiceObserver { |
| 205 public: | 213 public: |
| 206 // Sent every time the service loads a new set of data. | 214 // Sent every time the service loads a new set of data. |
| 207 virtual void NTPSnippetsServiceLoaded() = 0; | 215 virtual void NTPSnippetsServiceLoaded() = 0; |
| 208 // Sent when the service is shutting down. | 216 // Sent when the service is shutting down. |
| 209 virtual void NTPSnippetsServiceShutdown() = 0; | 217 virtual void NTPSnippetsServiceShutdown() = 0; |
| 210 | 218 |
| 211 protected: | 219 protected: |
| 212 virtual ~NTPSnippetsServiceObserver() {} | 220 virtual ~NTPSnippetsServiceObserver() {} |
| 213 }; | 221 }; |
| 214 | 222 |
| 215 } // namespace ntp_snippets | 223 } // namespace ntp_snippets |
| 216 | 224 |
| 217 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ | 225 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ |
| OLD | NEW |