| 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 <string> | 10 #include <string> |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 const ParseJSONCallback& parse_json_callback); | 65 const ParseJSONCallback& parse_json_callback); |
| 66 ~NTPSnippetsService() override; | 66 ~NTPSnippetsService() override; |
| 67 | 67 |
| 68 static void RegisterProfilePrefs(PrefRegistrySimple* registry); | 68 static void RegisterProfilePrefs(PrefRegistrySimple* registry); |
| 69 | 69 |
| 70 void Init(bool enabled); | 70 void Init(bool enabled); |
| 71 | 71 |
| 72 // Inherited from KeyedService. | 72 // Inherited from KeyedService. |
| 73 void Shutdown() override; | 73 void Shutdown() override; |
| 74 | 74 |
| 75 // True once the data is loaded in memory and available to loop over. | |
| 76 bool is_loaded() { return loaded_; } | |
| 77 | |
| 78 // Fetches snippets from the server and adds them to the current ones. | 75 // Fetches snippets from the server and adds them to the current ones. |
| 79 void FetchSnippets(); | 76 void FetchSnippets(); |
| 80 | 77 |
| 81 // Discards the snippet with the given |url|, if it exists. Returns true iff | 78 // Discards the snippet with the given |url|, if it exists. Returns true iff |
| 82 // a snippet was discarded. | 79 // a snippet was discarded. |
| 83 bool DiscardSnippet(const GURL& url); | 80 bool DiscardSnippet(const GURL& url); |
| 84 | 81 |
| 85 // Observer accessors. | 82 // Observer accessors. |
| 86 void AddObserver(NTPSnippetsServiceObserver* observer); | 83 void AddObserver(NTPSnippetsServiceObserver* observer); |
| 87 void RemoveObserver(NTPSnippetsServiceObserver* observer); | 84 void RemoveObserver(NTPSnippetsServiceObserver* observer); |
| 88 | 85 |
| 89 // Number of snippets available. Can only be called when is_loaded() is true. | 86 // Number of snippets available. |
| 90 size_t size() const { | 87 size_t size() const { return snippets_.size(); } |
| 91 DCHECK(loaded_); | |
| 92 return snippets_.size(); | |
| 93 } | |
| 94 | 88 |
| 95 // The snippets can be iterated upon only via a const_iterator. Recommended | 89 // The snippets can be iterated upon only via a const_iterator. Recommended |
| 96 // way to iterate is as follows: | 90 // way to iterate is as follows: |
| 97 // | 91 // |
| 98 // NTPSnippetsService* service; // Assume is set. | 92 // NTPSnippetsService* service; // Assume is set. |
| 99 // for (auto& snippet : *service) { | 93 // for (auto& snippet : *service) { |
| 100 // // |snippet| here is a const object. | 94 // // |snippet| here is a const object. |
| 101 // } | 95 // } |
| 102 const_iterator begin() const { | 96 const_iterator begin() const { return const_iterator(snippets_.begin()); } |
| 103 DCHECK(loaded_); | 97 const_iterator end() const { return const_iterator(snippets_.end()); } |
| 104 return const_iterator(snippets_.begin()); | |
| 105 } | |
| 106 const_iterator end() const { | |
| 107 DCHECK(loaded_); | |
| 108 return const_iterator(snippets_.end()); | |
| 109 } | |
| 110 | 98 |
| 111 private: | 99 private: |
| 112 friend class NTPSnippetsServiceTest; | 100 friend class NTPSnippetsServiceTest; |
| 113 | 101 |
| 114 void OnSuggestionsChanged(const suggestions::SuggestionsProfile& suggestions); | 102 void OnSuggestionsChanged(const suggestions::SuggestionsProfile& suggestions); |
| 115 void OnSnippetsDownloaded(const std::string& snippets_json); | 103 void OnSnippetsDownloaded(const std::string& snippets_json); |
| 116 | 104 |
| 117 void OnJsonParsed(const std::string& snippets_json, | 105 void OnJsonParsed(const std::string& snippets_json, |
| 118 scoped_ptr<base::Value> parsed); | 106 scoped_ptr<base::Value> parsed); |
| 119 void OnJsonError(const std::string& snippets_json, const std::string& error); | 107 void OnJsonError(const std::string& snippets_json, const std::string& error); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 137 void StoreDiscardedSnippetsToPrefs(); | 125 void StoreDiscardedSnippetsToPrefs(); |
| 138 | 126 |
| 139 bool HasDiscardedSnippet(const GURL& url) const; | 127 bool HasDiscardedSnippet(const GURL& url) const; |
| 140 | 128 |
| 141 void RemoveExpiredSnippets(); | 129 void RemoveExpiredSnippets(); |
| 142 | 130 |
| 143 PrefService* pref_service_; | 131 PrefService* pref_service_; |
| 144 | 132 |
| 145 suggestions::SuggestionsService* suggestions_service_; | 133 suggestions::SuggestionsService* suggestions_service_; |
| 146 | 134 |
| 147 // True if the suggestions are loaded. | |
| 148 bool loaded_; | |
| 149 | |
| 150 // The SequencedTaskRunner on which file system operations will be run. | 135 // The SequencedTaskRunner on which file system operations will be run. |
| 151 scoped_refptr<base::SequencedTaskRunner> file_task_runner_; | 136 scoped_refptr<base::SequencedTaskRunner> file_task_runner_; |
| 152 | 137 |
| 153 // All current suggestions (i.e. not discarded ones). | 138 // All current suggestions (i.e. not discarded ones). |
| 154 NTPSnippetStorage snippets_; | 139 NTPSnippetStorage snippets_; |
| 155 | 140 |
| 156 // Suggestions that the user discarded. We keep these around until they expire | 141 // Suggestions that the user discarded. We keep these around until they expire |
| 157 // so we won't re-add them on the next fetch. | 142 // so we won't re-add them on the next fetch. |
| 158 NTPSnippetStorage discarded_snippets_; | 143 NTPSnippetStorage discarded_snippets_; |
| 159 | 144 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 185 | 170 |
| 186 const ParseJSONCallback parse_json_callback_; | 171 const ParseJSONCallback parse_json_callback_; |
| 187 | 172 |
| 188 base::WeakPtrFactory<NTPSnippetsService> weak_ptr_factory_; | 173 base::WeakPtrFactory<NTPSnippetsService> weak_ptr_factory_; |
| 189 | 174 |
| 190 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); | 175 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); |
| 191 }; | 176 }; |
| 192 | 177 |
| 193 class NTPSnippetsServiceObserver { | 178 class NTPSnippetsServiceObserver { |
| 194 public: | 179 public: |
| 195 // Send everytime the service loads a new set of data. | 180 // Sent every time the service loads a new set of data. |
| 196 virtual void NTPSnippetsServiceLoaded(NTPSnippetsService* service) = 0; | 181 virtual void NTPSnippetsServiceLoaded(NTPSnippetsService* service) = 0; |
| 197 // Send when the service is shutting down. | 182 // Sent when the service is shutting down. |
| 198 virtual void NTPSnippetsServiceShutdown(NTPSnippetsService* service) = 0; | 183 virtual void NTPSnippetsServiceShutdown(NTPSnippetsService* service) = 0; |
| 199 | 184 |
| 200 protected: | 185 protected: |
| 201 virtual ~NTPSnippetsServiceObserver() {} | 186 virtual ~NTPSnippetsServiceObserver() {} |
| 202 }; | 187 }; |
| 203 | 188 |
| 204 } // namespace ntp_snippets | 189 } // namespace ntp_snippets |
| 205 | 190 |
| 206 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ | 191 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ |
| OLD | NEW |