| 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& last_status() const { |
| 84 return last_fetch_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 void RemoveExpiredSnippets(); | 157 void LoadingSnippetsFinished(); |
| 152 | 158 |
| 153 bool enabled_; | 159 bool enabled_; |
| 154 | 160 |
| 155 PrefService* pref_service_; | 161 PrefService* pref_service_; |
| 156 | 162 |
| 157 suggestions::SuggestionsService* suggestions_service_; | 163 suggestions::SuggestionsService* suggestions_service_; |
| 158 | 164 |
| 159 // The SequencedTaskRunner on which file system operations will be run. | 165 // The SequencedTaskRunner on which file system operations will be run. |
| 160 scoped_refptr<base::SequencedTaskRunner> file_task_runner_; | 166 scoped_refptr<base::SequencedTaskRunner> file_task_runner_; |
| 161 | 167 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 182 suggestions::SuggestionsService::ResponseCallbackList::Subscription; | 188 suggestions::SuggestionsService::ResponseCallbackList::Subscription; |
| 183 scoped_ptr<SuggestionsSubscription> suggestions_service_subscription_; | 189 scoped_ptr<SuggestionsSubscription> suggestions_service_subscription_; |
| 184 | 190 |
| 185 // The snippets fetcher. | 191 // The snippets fetcher. |
| 186 scoped_ptr<NTPSnippetsFetcher> snippets_fetcher_; | 192 scoped_ptr<NTPSnippetsFetcher> snippets_fetcher_; |
| 187 | 193 |
| 188 // The subscription to the snippets fetcher. | 194 // The subscription to the snippets fetcher. |
| 189 scoped_ptr<NTPSnippetsFetcher::SnippetsAvailableCallbackList::Subscription> | 195 scoped_ptr<NTPSnippetsFetcher::SnippetsAvailableCallbackList::Subscription> |
| 190 snippets_fetcher_subscription_; | 196 snippets_fetcher_subscription_; |
| 191 | 197 |
| 198 std::string last_fetch_status_; |
| 199 |
| 192 // Timer that calls us back when the next snippet expires. | 200 // Timer that calls us back when the next snippet expires. |
| 193 base::OneShotTimer expiry_timer_; | 201 base::OneShotTimer expiry_timer_; |
| 194 | 202 |
| 195 ParseJSONCallback parse_json_callback_; | 203 ParseJSONCallback parse_json_callback_; |
| 196 | 204 |
| 197 base::WeakPtrFactory<NTPSnippetsService> weak_ptr_factory_; | 205 base::WeakPtrFactory<NTPSnippetsService> weak_ptr_factory_; |
| 198 | 206 |
| 199 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); | 207 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); |
| 200 }; | 208 }; |
| 201 | 209 |
| 202 class NTPSnippetsServiceObserver { | 210 class NTPSnippetsServiceObserver { |
| 203 public: | 211 public: |
| 204 // Sent every time the service loads a new set of data. | 212 // Sent every time the service loads a new set of data. |
| 205 virtual void NTPSnippetsServiceLoaded() = 0; | 213 virtual void NTPSnippetsServiceLoaded() = 0; |
| 206 // Sent when the service is shutting down. | 214 // Sent when the service is shutting down. |
| 207 virtual void NTPSnippetsServiceShutdown() = 0; | 215 virtual void NTPSnippetsServiceShutdown() = 0; |
| 208 | 216 |
| 209 protected: | 217 protected: |
| 210 virtual ~NTPSnippetsServiceObserver() {} | 218 virtual ~NTPSnippetsServiceObserver() {} |
| 211 }; | 219 }; |
| 212 | 220 |
| 213 } // namespace ntp_snippets | 221 } // namespace ntp_snippets |
| 214 | 222 |
| 215 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ | 223 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ |
| OLD | NEW |