| 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 <memory> | 10 #include <memory> |
| (...skipping 28 matching lines...) Expand all Loading... |
| 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 Loading... |
| 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 Loading... |
| 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 enum class State { |
| 147 NOT_INITED, |
| 148 INITED, |
| 149 SHUT_DOWN |
| 150 } state_; |
| 151 |
| 152 bool enabled_; | 152 bool enabled_; |
| 153 | 153 |
| 154 PrefService* pref_service_; | 154 PrefService* pref_service_; |
| 155 | 155 |
| 156 suggestions::SuggestionsService* suggestions_service_; | 156 suggestions::SuggestionsService* suggestions_service_; |
| 157 | 157 |
| 158 // The SequencedTaskRunner on which file system operations will be run. | 158 // The SequencedTaskRunner on which file system operations will be run. |
| 159 scoped_refptr<base::SequencedTaskRunner> file_task_runner_; | 159 scoped_refptr<base::SequencedTaskRunner> file_task_runner_; |
| 160 | 160 |
| 161 // All current suggestions (i.e. not discarded ones). | 161 // All current suggestions (i.e. not discarded ones). |
| 162 NTPSnippetStorage snippets_; | 162 NTPSnippet::PtrVector snippets_; |
| 163 | 163 |
| 164 // Suggestions that the user discarded. We keep these around until they expire | 164 // Suggestions that the user discarded. We keep these around until they expire |
| 165 // so we won't re-add them on the next fetch. | 165 // so we won't re-add them on the next fetch. |
| 166 NTPSnippetStorage discarded_snippets_; | 166 NTPSnippet::PtrVector discarded_snippets_; |
| 167 | 167 |
| 168 // The ISO 639-1 code of the language used by the application. | 168 // The ISO 639-1 code of the language used by the application. |
| 169 const std::string application_language_code_; | 169 const std::string application_language_code_; |
| 170 | 170 |
| 171 // The observers. | 171 // The observers. |
| 172 base::ObserverList<NTPSnippetsServiceObserver> observers_; | 172 base::ObserverList<NTPSnippetsServiceObserver> observers_; |
| 173 | 173 |
| 174 // Scheduler for fetching snippets. Not owned. | 174 // Scheduler for fetching snippets. Not owned. |
| 175 NTPSnippetsScheduler* scheduler_; | 175 NTPSnippetsScheduler* scheduler_; |
| 176 | 176 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 204 // hosts. (e.g. the user manually removed all MostLikely tiles.) | 204 // hosts. (e.g. the user manually removed all MostLikely tiles.) |
| 205 virtual void NTPSnippetsServiceCleared() = 0; | 205 virtual void NTPSnippetsServiceCleared() = 0; |
| 206 | 206 |
| 207 protected: | 207 protected: |
| 208 virtual ~NTPSnippetsServiceObserver() {} | 208 virtual ~NTPSnippetsServiceObserver() {} |
| 209 }; | 209 }; |
| 210 | 210 |
| 211 } // namespace ntp_snippets | 211 } // namespace ntp_snippets |
| 212 | 212 |
| 213 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ | 213 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ |
| OLD | NEW |