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 <string> | 10 #include <string> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
| 15 #include "base/observer_list.h" | 15 #include "base/observer_list.h" |
| 16 #include "base/sequenced_task_runner.h" | 16 #include "base/sequenced_task_runner.h" |
| 17 #include "components/keyed_service/core/keyed_service.h" | 17 #include "components/keyed_service/core/keyed_service.h" |
| 18 #include "components/ntp_snippets/inner_iterator.h" | 18 #include "components/ntp_snippets/inner_iterator.h" |
| 19 #include "components/ntp_snippets/ntp_snippet.h" | 19 #include "components/ntp_snippets/ntp_snippet.h" |
| 20 #include "components/ntp_snippets/ntp_snippets_fetcher.h" | 20 #include "components/ntp_snippets/ntp_snippets_fetcher.h" |
| 21 #include "components/ntp_snippets/ntp_snippets_scheduler.h" | 21 #include "components/ntp_snippets/ntp_snippets_scheduler.h" |
| 22 | 22 |
| 23 class PrefService; | |
| 24 | |
| 25 namespace base { | |
| 26 class FilePath; | |
| 27 class ListValue; | |
| 28 } | |
| 29 | |
| 30 namespace user_prefs { | |
| 31 class PrefRegistrySyncable; | |
| 32 } | |
| 33 | |
| 23 namespace ntp_snippets { | 34 namespace ntp_snippets { |
| 24 | 35 |
| 25 class NTPSnippetsServiceObserver; | 36 class NTPSnippetsServiceObserver; |
| 26 | 37 |
| 27 // Stores and vend fresh content data for the NTP. | 38 // Stores and vend fresh content data for the NTP. |
| 28 class NTPSnippetsService : public KeyedService, NTPSnippetsFetcher::Observer { | 39 class NTPSnippetsService : public KeyedService, NTPSnippetsFetcher::Observer { |
| 29 public: | 40 public: |
| 30 using NTPSnippetStorage = std::vector<scoped_ptr<NTPSnippet>>; | 41 using NTPSnippetStorage = std::vector<scoped_ptr<NTPSnippet>>; |
| 31 using const_iterator = | 42 using const_iterator = |
| 32 InnerIterator<NTPSnippetStorage::const_iterator, const NTPSnippet>; | 43 InnerIterator<NTPSnippetStorage::const_iterator, const NTPSnippet>; |
| 33 | 44 |
| 34 // |application_language_code| should be a ISO 639-1 compliant string. Aka | 45 // |application_language_code| should be a ISO 639-1 compliant string. Aka |
| 35 // 'en' or 'en-US'. Note that this code should only specify the language, not | 46 // 'en' or 'en-US'. Note that this code should only specify the language, not |
| 36 // the locale, so 'en_US' (english language with US locale) and 'en-GB_US' | 47 // the locale, so 'en_US' (english language with US locale) and 'en-GB_US' |
| 37 // (British english person in the US) are not language code. | 48 // (British english person in the US) are not language code. |
| 38 NTPSnippetsService(scoped_refptr<base::SequencedTaskRunner> file_task_runner, | 49 NTPSnippetsService(PrefService* pref_service, |
|
Bernhard Bauer
2016/02/25 17:16:33
Can you please add a TODO to move this to a more s
Marc Treib
2016/02/26 17:06:12
What would be a "more suited storage"? IMO prefs i
Bernhard Bauer
2016/02/26 17:30:48
Prefs are loaded synchronously from disk at startu
Marc Treib
2016/02/29 10:04:42
OTOH, storing this in prefs mean adding a few kB t
| |
| 50 scoped_refptr<base::SequencedTaskRunner> file_task_runner, | |
| 39 const std::string& application_language_code, | 51 const std::string& application_language_code, |
| 40 NTPSnippetsScheduler* scheduler, | 52 NTPSnippetsScheduler* scheduler, |
| 41 scoped_ptr<NTPSnippetsFetcher> snippets_fetcher); | 53 scoped_ptr<NTPSnippetsFetcher> snippets_fetcher); |
| 42 ~NTPSnippetsService() override; | 54 ~NTPSnippetsService() override; |
| 43 | 55 |
| 56 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); | |
| 57 | |
| 44 void Init(bool enabled); | 58 void Init(bool enabled); |
| 45 | 59 |
| 46 // Fetches snippets from the server. |overwrite| is true if existing snippets | 60 // Fetches snippets from the server. |overwrite| is true if existing snippets |
| 47 // should be overwritten. | 61 // should be overwritten. |
| 48 void FetchSnippets(bool overwrite); | 62 void FetchSnippets(bool overwrite); |
| 49 | 63 |
| 50 // Inherited from KeyedService. | 64 // Inherited from KeyedService. |
| 51 void Shutdown() override; | 65 void Shutdown() override; |
| 52 | 66 |
| 53 // True once the data is loaded in memory and available to loop over. | 67 // True once the data is loaded in memory and available to loop over. |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 73 const_iterator begin() { | 87 const_iterator begin() { |
| 74 DCHECK(loaded_); | 88 DCHECK(loaded_); |
| 75 return const_iterator(snippets_.begin()); | 89 return const_iterator(snippets_.begin()); |
| 76 } | 90 } |
| 77 const_iterator end() { | 91 const_iterator end() { |
| 78 DCHECK(loaded_); | 92 DCHECK(loaded_); |
| 79 return const_iterator(snippets_.end()); | 93 return const_iterator(snippets_.end()); |
| 80 } | 94 } |
| 81 | 95 |
| 82 private: | 96 private: |
| 97 void OnSnippetsDownloaded(const base::FilePath& download_path); | |
| 83 void OnFileReadDone(const std::string* json, bool success); | 98 void OnFileReadDone(const std::string* json, bool success); |
| 84 void OnSnippetsDownloaded(const base::FilePath& download_path); | |
| 85 | 99 |
| 86 // Expects the JSON to be a list of dictionaries with keys matching the | 100 // Expects a top-level dictionary containing a "recos" list, which will be |
| 87 // properties of a snippet (url, title, site_title, etc...). The url is the | 101 // passed to |LoadFromJSONList|. |
| 88 // only mandatory value. | |
| 89 bool LoadFromJSONString(const std::string& str); | 102 bool LoadFromJSONString(const std::string& str); |
| 90 | 103 |
| 104 // Expects a list of dictionaries each containing a "contentInfo" dictionary | |
| 105 // with keys matching the properties of a snippet (url, title, site_title, | |
| 106 // etc...). The url is the only mandatory value. | |
| 107 bool LoadFromJSONList(const base::ListValue& list); | |
| 108 | |
| 109 bool LoadFromPrefs(); | |
| 110 void StoreToPrefs(); | |
| 111 | |
| 112 PrefService* pref_service_; | |
| 113 | |
| 91 // True if the suggestions are loaded. | 114 // True if the suggestions are loaded. |
| 92 bool loaded_; | 115 bool loaded_; |
| 93 | 116 |
| 94 // The SequencedTaskRunner on which file system operations will be run. | 117 // The SequencedTaskRunner on which file system operations will be run. |
| 95 scoped_refptr<base::SequencedTaskRunner> file_task_runner_; | 118 scoped_refptr<base::SequencedTaskRunner> file_task_runner_; |
| 96 | 119 |
| 97 // All the suggestions. | 120 // All the suggestions. |
| 98 NTPSnippetStorage snippets_; | 121 NTPSnippetStorage snippets_; |
| 99 | 122 |
| 100 // The ISO 639-1 code of the language used by the application. | 123 // The ISO 639-1 code of the language used by the application. |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 131 // Send when the service is shutting down. | 154 // Send when the service is shutting down. |
| 132 virtual void NTPSnippetsServiceShutdown(NTPSnippetsService* service) = 0; | 155 virtual void NTPSnippetsServiceShutdown(NTPSnippetsService* service) = 0; |
| 133 | 156 |
| 134 protected: | 157 protected: |
| 135 virtual ~NTPSnippetsServiceObserver() {} | 158 virtual ~NTPSnippetsServiceObserver() {} |
| 136 }; | 159 }; |
| 137 | 160 |
| 138 } // namespace ntp_snippets | 161 } // namespace ntp_snippets |
| 139 | 162 |
| 140 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ | 163 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ |
| OLD | NEW |