| 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 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 | 68 |
| 69 static void RegisterProfilePrefs(PrefRegistrySimple* registry); | 69 static void RegisterProfilePrefs(PrefRegistrySimple* registry); |
| 70 | 70 |
| 71 void Init(bool enabled); | 71 void Init(bool enabled); |
| 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 |
| 79 // suggestions from the suggestion service) and adds them to the current ones. |
| 80 void FetchSnippetsFromHosts(const std::set<std::string>& hosts); |
| 81 |
| 82 // Deletes all currently stored snippets. |
| 83 void ClearSnippets(); |
| 78 | 84 |
| 79 // Discards the snippet with the given |url|, if it exists. Returns true iff | 85 // Discards the snippet with the given |url|, if it exists. Returns true iff |
| 80 // a snippet was discarded. | 86 // a snippet was discarded. |
| 81 bool DiscardSnippet(const GURL& url); | 87 bool DiscardSnippet(const GURL& url); |
| 82 | 88 |
| 89 // Returns the lists of snippets previously discarded by the user (that are |
| 90 // not expired yet). |
| 91 const NTPSnippetStorage& discarded_snippets() const { |
| 92 return discarded_snippets_; |
| 93 } |
| 94 |
| 95 // Clears the lists of snippets previously discarded by the user. |
| 96 void ClearDiscardedSnippets(); |
| 97 |
| 98 // Returns the lists of suggestion hosts the snippets are restricted to. |
| 99 std::set<std::string> GetSuggestionsHosts() const; |
| 100 |
| 83 // Observer accessors. | 101 // Observer accessors. |
| 84 void AddObserver(NTPSnippetsServiceObserver* observer); | 102 void AddObserver(NTPSnippetsServiceObserver* observer); |
| 85 void RemoveObserver(NTPSnippetsServiceObserver* observer); | 103 void RemoveObserver(NTPSnippetsServiceObserver* observer); |
| 86 | 104 |
| 87 // Number of snippets available. | 105 // Number of snippets available. |
| 88 size_t size() const { return snippets_.size(); } | 106 size_t size() const { return snippets_.size(); } |
| 89 | 107 |
| 90 // The snippets can be iterated upon only via a const_iterator. Recommended | 108 // The snippets can be iterated upon only via a const_iterator. Recommended |
| 91 // way to iterate is as follows: | 109 // way to iterate is as follows: |
| 92 // | 110 // |
| 93 // NTPSnippetsService* service; // Assume is set. | 111 // NTPSnippetsService* service; // Assume is set. |
| 94 // for (auto& snippet : *service) { | 112 // for (auto& snippet : *service) { |
| 95 // // |snippet| here is a const object. | 113 // // |snippet| here is a const object. |
| 96 // } | 114 // } |
| 97 const_iterator begin() const { return const_iterator(snippets_.begin()); } | 115 const_iterator begin() const { return const_iterator(snippets_.begin()); } |
| 98 const_iterator end() const { return const_iterator(snippets_.end()); } | 116 const_iterator end() const { return const_iterator(snippets_.end()); } |
| 99 | 117 |
| 100 private: | 118 private: |
| 101 friend class NTPSnippetsServiceTest; | 119 friend class NTPSnippetsServiceTest; |
| 102 | 120 |
| 103 void OnSuggestionsChanged(const suggestions::SuggestionsProfile& suggestions); | 121 void OnSuggestionsChanged(const suggestions::SuggestionsProfile& suggestions); |
| 104 void OnSnippetsDownloaded(const std::string& snippets_json); | 122 void OnSnippetsDownloaded(const std::string& snippets_json); |
| 105 | 123 |
| 106 void OnJsonParsed(const std::string& snippets_json, | 124 void OnJsonParsed(const std::string& snippets_json, |
| 107 scoped_ptr<base::Value> parsed); | 125 scoped_ptr<base::Value> parsed); |
| 108 void OnJsonError(const std::string& snippets_json, const std::string& error); | 126 void OnJsonError(const std::string& snippets_json, const std::string& error); |
| 109 | 127 |
| 110 void FetchSnippetsImpl(const std::set<std::string>& hosts); | |
| 111 | |
| 112 // Expects a top-level dictionary containing a "recos" list, which will be | 128 // Expects a top-level dictionary containing a "recos" list, which will be |
| 113 // passed to LoadFromListValue(). | 129 // passed to LoadFromListValue(). |
| 114 bool LoadFromValue(const base::Value& value); | 130 bool LoadFromValue(const base::Value& value); |
| 115 | 131 |
| 116 // Expects a list of dictionaries each containing a "contentInfo" dictionary | 132 // Expects a list of dictionaries each containing a "contentInfo" dictionary |
| 117 // with keys matching the properties of a snippet (url, title, site_title, | 133 // with keys matching the properties of a snippet (url, title, site_title, |
| 118 // etc...). The URL is the only mandatory value. | 134 // etc...). The URL is the only mandatory value. |
| 119 bool LoadFromListValue(const base::ListValue& list); | 135 bool LoadFromListValue(const base::ListValue& list); |
| 120 | 136 |
| 121 // TODO(treib): Investigate a better storage, maybe LevelDB or SQLite? | 137 // TODO(treib): Investigate a better storage, maybe LevelDB or SQLite? |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 // Sent when the service is shutting down. | 202 // Sent when the service is shutting down. |
| 187 virtual void NTPSnippetsServiceShutdown() = 0; | 203 virtual void NTPSnippetsServiceShutdown() = 0; |
| 188 | 204 |
| 189 protected: | 205 protected: |
| 190 virtual ~NTPSnippetsServiceObserver() {} | 206 virtual ~NTPSnippetsServiceObserver() {} |
| 191 }; | 207 }; |
| 192 | 208 |
| 193 } // namespace ntp_snippets | 209 } // namespace ntp_snippets |
| 194 | 210 |
| 195 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ | 211 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ |
| OLD | NEW |