| 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/observer_list.h" | 15 #include "base/observer_list.h" |
| 16 #include "base/sequenced_task_runner.h" |
| 15 #include "components/keyed_service/core/keyed_service.h" | 17 #include "components/keyed_service/core/keyed_service.h" |
| 16 #include "components/ntp_snippets/inner_iterator.h" | 18 #include "components/ntp_snippets/inner_iterator.h" |
| 17 #include "components/ntp_snippets/ntp_snippet.h" | 19 #include "components/ntp_snippets/ntp_snippet.h" |
| 20 #include "components/ntp_snippets/ntp_snippets_fetcher.h" |
| 18 | 21 |
| 19 namespace ntp_snippets { | 22 namespace ntp_snippets { |
| 20 | 23 |
| 21 class NTPSnippetsServiceObserver; | 24 class NTPSnippetsServiceObserver; |
| 22 | 25 |
| 23 // Stores and vend fresh content data for the NTP. | 26 // Stores and vend fresh content data for the NTP. |
| 24 class NTPSnippetsService : public KeyedService { | 27 class NTPSnippetsService : public KeyedService, NTPSnippetsFetcher::Observer { |
| 25 public: | 28 public: |
| 26 using NTPSnippetStorage = std::vector<std::unique_ptr<NTPSnippet>>; | 29 using NTPSnippetStorage = std::vector<scoped_ptr<NTPSnippet>>; |
| 27 using const_iterator = | 30 using const_iterator = |
| 28 InnerIterator<NTPSnippetStorage::const_iterator, const NTPSnippet>; | 31 InnerIterator<NTPSnippetStorage::const_iterator, const NTPSnippet>; |
| 29 | 32 |
| 30 // |application_language_code| should be a ISO 639-1 compliant string. Aka | 33 // |application_language_code| should be a ISO 639-1 compliant string. Aka |
| 31 // 'en' or 'en-US'. Note that this code should only specify the language, not | 34 // 'en' or 'en-US'. Note that this code should only specify the language, not |
| 32 // the locale, so 'en_US' (english language with US locale) and 'en-GB_US' | 35 // the locale, so 'en_US' (english language with US locale) and 'en-GB_US' |
| 33 // (British english person in the US) are not language code. | 36 // (British english person in the US) are not language code. |
| 34 explicit NTPSnippetsService(const std::string& application_language_code); | 37 NTPSnippetsService(scoped_refptr<base::SequencedTaskRunner> file_task_runner, |
| 38 const std::string& application_language_code, |
| 39 scoped_ptr<NTPSnippetsFetcher> snippets_fetcher); |
| 35 ~NTPSnippetsService() override; | 40 ~NTPSnippetsService() override; |
| 36 | 41 |
| 42 // Fetches snippets from the server. |overwrite| is true if existing snippets |
| 43 // should be overwritten. |
| 44 void FetchSnippets(bool overwrite); |
| 45 |
| 37 // Inherited from KeyedService. | 46 // Inherited from KeyedService. |
| 38 void Shutdown() override; | 47 void Shutdown() override; |
| 39 | 48 |
| 40 // True once the data is loaded in memory and available to loop over. | 49 // True once the data is loaded in memory and available to loop over. |
| 41 bool is_loaded() { return loaded_; } | 50 bool is_loaded() { return loaded_; } |
| 42 | 51 |
| 43 // Observer accessors. | 52 // Observer accessors. |
| 44 void AddObserver(NTPSnippetsServiceObserver* observer); | 53 void AddObserver(NTPSnippetsServiceObserver* observer); |
| 45 void RemoveObserver(NTPSnippetsServiceObserver* observer); | 54 void RemoveObserver(NTPSnippetsServiceObserver* observer); |
| 46 | 55 |
| 47 // Expects the JSON to be a list of dictionaries with keys matching the | |
| 48 // properties of a snippet (url, title, site_title, etc...). The url is the | |
| 49 // only mandatory value. | |
| 50 bool LoadFromJSONString(const std::string& str); | |
| 51 | |
| 52 // Number of snippets available. Can only be called when is_loaded() is true. | 56 // Number of snippets available. Can only be called when is_loaded() is true. |
| 53 NTPSnippetStorage::size_type size() { | 57 NTPSnippetStorage::size_type size() { |
| 54 DCHECK(loaded_); | 58 DCHECK(loaded_); |
| 55 return snippets_.size(); | 59 return snippets_.size(); |
| 56 } | 60 } |
| 57 | 61 |
| 58 // The snippets can be iterated upon only via a const_iterator. Recommended | 62 // The snippets can be iterated upon only via a const_iterator. Recommended |
| 59 // way to iterate is as follow: | 63 // way to iterate is as follow: |
| 60 // | 64 // |
| 61 // NTPSnippetsService service; // Assume is set. | 65 // NTPSnippetsService service; // Assume is set. |
| 62 // for (auto& snippet : *service) { | 66 // for (auto& snippet : *service) { |
| 63 // // Snippet here is a const object. | 67 // // Snippet here is a const object. |
| 64 // } | 68 // } |
| 65 const_iterator begin() { | 69 const_iterator begin() { |
| 66 DCHECK(loaded_); | 70 DCHECK(loaded_); |
| 67 return const_iterator(snippets_.begin()); | 71 return const_iterator(snippets_.begin()); |
| 68 } | 72 } |
| 69 const_iterator end() { | 73 const_iterator end() { |
| 70 DCHECK(loaded_); | 74 DCHECK(loaded_); |
| 71 return const_iterator(snippets_.end()); | 75 return const_iterator(snippets_.end()); |
| 72 } | 76 } |
| 73 | 77 |
| 74 private: | 78 private: |
| 79 void OnFileReadDone(const std::string& json); |
| 80 void OnSnippetsDownloaded(const base::FilePath& download_path); |
| 81 |
| 82 // Expects the JSON to be a list of dictionaries with keys matching the |
| 83 // properties of a snippet (url, title, site_title, etc...). The url is the |
| 84 // only mandatory value. |
| 85 bool LoadFromJSONString(const std::string& str); |
| 86 |
| 75 // True if the suggestions are loaded. | 87 // True if the suggestions are loaded. |
| 76 bool loaded_; | 88 bool loaded_; |
| 89 |
| 90 // The SequencedTaskRunner on which file system operations will be run. |
| 91 scoped_refptr<base::SequencedTaskRunner> file_task_runner_; |
| 92 |
| 77 // All the suggestions. | 93 // All the suggestions. |
| 78 NTPSnippetStorage snippets_; | 94 NTPSnippetStorage snippets_; |
| 95 |
| 79 // The ISO 639-1 code of the language used by the application. | 96 // The ISO 639-1 code of the language used by the application. |
| 80 const std::string application_language_code_; | 97 const std::string application_language_code_; |
| 98 |
| 81 // The observers. | 99 // The observers. |
| 82 base::ObserverList<NTPSnippetsServiceObserver> observers_; | 100 base::ObserverList<NTPSnippetsServiceObserver> observers_; |
| 83 | 101 |
| 102 // The snippets fetcher |
| 103 scoped_ptr<NTPSnippetsFetcher> snippets_fetcher_; |
| 104 |
| 105 // The callback from the snippets fetcher |
| 106 scoped_ptr<NTPSnippetsFetcher::SnippetsAvailableCallbackList::Subscription> |
| 107 snippets_fetcher_callback_; |
| 108 |
| 109 base::WeakPtrFactory<NTPSnippetsService> weak_ptr_factory_; |
| 110 |
| 111 friend class NTPSnippetsServiceTest; |
| 112 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceTest, Loop); |
| 113 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceTest, Full); |
| 114 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceTest, ObserverLoaded); |
| 115 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceTest, ObserverNotLoaded); |
| 116 |
| 84 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); | 117 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); |
| 85 }; | 118 }; |
| 86 | 119 |
| 87 class NTPSnippetsServiceObserver { | 120 class NTPSnippetsServiceObserver { |
| 88 public: | 121 public: |
| 89 // Send everytime the service loads a new set of data. | 122 // Send everytime the service loads a new set of data. |
| 90 virtual void NTPSnippetsServiceLoaded(NTPSnippetsService* service) = 0; | 123 virtual void NTPSnippetsServiceLoaded(NTPSnippetsService* service) = 0; |
| 91 // Send when the service is shutting down. | 124 // Send when the service is shutting down. |
| 92 virtual void NTPSnippetsServiceShutdown(NTPSnippetsService* service) = 0; | 125 virtual void NTPSnippetsServiceShutdown(NTPSnippetsService* service) = 0; |
| 93 | 126 |
| 94 protected: | 127 protected: |
| 95 virtual ~NTPSnippetsServiceObserver() {} | 128 virtual ~NTPSnippetsServiceObserver() {} |
| 96 }; | 129 }; |
| 97 | 130 |
| 98 } // namespace ntp_snippets | 131 } // namespace ntp_snippets |
| 99 | 132 |
| 100 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ | 133 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ |
| OLD | NEW |