| 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 | 22 |
| 22 namespace ntp_snippets { | 23 namespace ntp_snippets { |
| 23 | 24 |
| 24 class NTPSnippetsServiceObserver; | 25 class NTPSnippetsServiceObserver; |
| 25 | 26 |
| 26 // Stores and vend fresh content data for the NTP. | 27 // Stores and vend fresh content data for the NTP. |
| 27 class NTPSnippetsService : public KeyedService, NTPSnippetsFetcher::Observer { | 28 class NTPSnippetsService : public KeyedService, NTPSnippetsFetcher::Observer { |
| 28 public: | 29 public: |
| 29 using NTPSnippetStorage = std::vector<scoped_ptr<NTPSnippet>>; | 30 using NTPSnippetStorage = std::vector<scoped_ptr<NTPSnippet>>; |
| 30 using const_iterator = | 31 using const_iterator = |
| 31 InnerIterator<NTPSnippetStorage::const_iterator, const NTPSnippet>; | 32 InnerIterator<NTPSnippetStorage::const_iterator, const NTPSnippet>; |
| 32 | 33 |
| 33 // |application_language_code| should be a ISO 639-1 compliant string. Aka | 34 // |application_language_code| should be a ISO 639-1 compliant string. Aka |
| 34 // 'en' or 'en-US'. Note that this code should only specify the language, not | 35 // 'en' or 'en-US'. Note that this code should only specify the language, not |
| 35 // the locale, so 'en_US' (english language with US locale) and 'en-GB_US' | 36 // the locale, so 'en_US' (english language with US locale) and 'en-GB_US' |
| 36 // (British english person in the US) are not language code. | 37 // (British english person in the US) are not language code. |
| 37 NTPSnippetsService(scoped_refptr<base::SequencedTaskRunner> file_task_runner, | 38 NTPSnippetsService(scoped_refptr<base::SequencedTaskRunner> file_task_runner, |
| 38 const std::string& application_language_code, | 39 const std::string& application_language_code, |
| 40 NTPSnippetsScheduler* scheduler, |
| 39 scoped_ptr<NTPSnippetsFetcher> snippets_fetcher); | 41 scoped_ptr<NTPSnippetsFetcher> snippets_fetcher); |
| 40 ~NTPSnippetsService() override; | 42 ~NTPSnippetsService() override; |
| 41 | 43 |
| 44 void Init(bool enabled); |
| 45 |
| 42 // Fetches snippets from the server. |overwrite| is true if existing snippets | 46 // Fetches snippets from the server. |overwrite| is true if existing snippets |
| 43 // should be overwritten. | 47 // should be overwritten. |
| 44 void FetchSnippets(bool overwrite); | 48 void FetchSnippets(bool overwrite); |
| 45 | 49 |
| 46 // Inherited from KeyedService. | 50 // Inherited from KeyedService. |
| 47 void Shutdown() override; | 51 void Shutdown() override; |
| 48 | 52 |
| 49 // True once the data is loaded in memory and available to loop over. | 53 // True once the data is loaded in memory and available to loop over. |
| 50 bool is_loaded() { return loaded_; } | 54 bool is_loaded() { return loaded_; } |
| 51 | 55 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 | 96 |
| 93 // All the suggestions. | 97 // All the suggestions. |
| 94 NTPSnippetStorage snippets_; | 98 NTPSnippetStorage snippets_; |
| 95 | 99 |
| 96 // The ISO 639-1 code of the language used by the application. | 100 // The ISO 639-1 code of the language used by the application. |
| 97 const std::string application_language_code_; | 101 const std::string application_language_code_; |
| 98 | 102 |
| 99 // The observers. | 103 // The observers. |
| 100 base::ObserverList<NTPSnippetsServiceObserver> observers_; | 104 base::ObserverList<NTPSnippetsServiceObserver> observers_; |
| 101 | 105 |
| 106 // Scheduler for fetching snippets. Not owned. |
| 107 NTPSnippetsScheduler* scheduler_; |
| 108 |
| 102 // The snippets fetcher | 109 // The snippets fetcher |
| 103 scoped_ptr<NTPSnippetsFetcher> snippets_fetcher_; | 110 scoped_ptr<NTPSnippetsFetcher> snippets_fetcher_; |
| 104 | 111 |
| 105 // The callback from the snippets fetcher | 112 // The callback from the snippets fetcher |
| 106 scoped_ptr<NTPSnippetsFetcher::SnippetsAvailableCallbackList::Subscription> | 113 scoped_ptr<NTPSnippetsFetcher::SnippetsAvailableCallbackList::Subscription> |
| 107 snippets_fetcher_callback_; | 114 snippets_fetcher_callback_; |
| 108 | 115 |
| 109 base::WeakPtrFactory<NTPSnippetsService> weak_ptr_factory_; | 116 base::WeakPtrFactory<NTPSnippetsService> weak_ptr_factory_; |
| 110 | 117 |
| 111 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); | 118 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); |
| 112 }; | 119 }; |
| 113 | 120 |
| 114 class NTPSnippetsServiceObserver { | 121 class NTPSnippetsServiceObserver { |
| 115 public: | 122 public: |
| 116 // Send everytime the service loads a new set of data. | 123 // Send everytime the service loads a new set of data. |
| 117 virtual void NTPSnippetsServiceLoaded(NTPSnippetsService* service) = 0; | 124 virtual void NTPSnippetsServiceLoaded(NTPSnippetsService* service) = 0; |
| 118 // Send when the service is shutting down. | 125 // Send when the service is shutting down. |
| 119 virtual void NTPSnippetsServiceShutdown(NTPSnippetsService* service) = 0; | 126 virtual void NTPSnippetsServiceShutdown(NTPSnippetsService* service) = 0; |
| 120 | 127 |
| 121 protected: | 128 protected: |
| 122 virtual ~NTPSnippetsServiceObserver() {} | 129 virtual ~NTPSnippetsServiceObserver() {} |
| 123 }; | 130 }; |
| 124 | 131 |
| 125 } // namespace ntp_snippets | 132 } // namespace ntp_snippets |
| 126 | 133 |
| 127 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ | 134 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ |
| OLD | NEW |