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<std::unique_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 NTPSnippetsFetcher* snippets_fetcher); | |
Marc Treib
2016/02/10 10:44:43
scoped_ptr<NTPSnippetsFetcher>
to make clear that
May
2016/02/10 18:16:47
Done.
| |
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 | 56 // 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 | 57 // properties of a snippet (url, title, site_title, etc...). The url is the |
49 // only mandatory value. | 58 // only mandatory value. |
50 bool LoadFromJSONString(const std::string& str); | 59 bool LoadFromJSONString(const std::string& str); |
Marc Treib
2016/02/10 10:44:43
I think this can be private?
May
2016/02/10 18:16:47
Done.
| |
51 | 60 |
52 // Number of snippets available. Can only be called when is_loaded() is true. | 61 // Number of snippets available. Can only be called when is_loaded() is true. |
53 NTPSnippetStorage::size_type size() { | 62 NTPSnippetStorage::size_type size() { |
54 DCHECK(loaded_); | 63 DCHECK(loaded_); |
55 return snippets_.size(); | 64 return snippets_.size(); |
56 } | 65 } |
57 | 66 |
58 // The snippets can be iterated upon only via a const_iterator. Recommended | 67 // The snippets can be iterated upon only via a const_iterator. Recommended |
59 // way to iterate is as follow: | 68 // way to iterate is as follow: |
60 // | 69 // |
61 // NTPSnippetsService service; // Assume is set. | 70 // NTPSnippetsService service; // Assume is set. |
62 // for (auto& snippet : *service) { | 71 // for (auto& snippet : *service) { |
63 // // Snippet here is a const object. | 72 // // Snippet here is a const object. |
64 // } | 73 // } |
65 const_iterator begin() { | 74 const_iterator begin() { |
66 DCHECK(loaded_); | 75 DCHECK(loaded_); |
67 return const_iterator(snippets_.begin()); | 76 return const_iterator(snippets_.begin()); |
68 } | 77 } |
69 const_iterator end() { | 78 const_iterator end() { |
70 DCHECK(loaded_); | 79 DCHECK(loaded_); |
71 return const_iterator(snippets_.end()); | 80 return const_iterator(snippets_.end()); |
72 } | 81 } |
73 | 82 |
74 private: | 83 private: |
84 void OnFileReadDone(const std::string& json); | |
85 void OnSnippetsDownloaded(const base::FilePath download_path); | |
Marc Treib
2016/02/10 10:44:43
&
May
2016/02/10 18:16:47
Done.
| |
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::NTPSnippetsFetcherCallbackList::Subscription> | |
107 snippets_fetcher_callback_; | |
108 | |
109 base::WeakPtrFactory<NTPSnippetsService> weak_ptr_factory_; | |
110 | |
84 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); | 111 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); |
85 }; | 112 }; |
86 | 113 |
87 class NTPSnippetsServiceObserver { | 114 class NTPSnippetsServiceObserver { |
88 public: | 115 public: |
89 // Send everytime the service loads a new set of data. | 116 // Send everytime the service loads a new set of data. |
90 virtual void NTPSnippetsServiceLoaded(NTPSnippetsService* service) = 0; | 117 virtual void NTPSnippetsServiceLoaded(NTPSnippetsService* service) = 0; |
91 // Send when the service is shutting down. | 118 // Send when the service is shutting down. |
92 virtual void NTPSnippetsServiceShutdown(NTPSnippetsService* service) = 0; | 119 virtual void NTPSnippetsServiceShutdown(NTPSnippetsService* service) = 0; |
93 | 120 |
94 protected: | 121 protected: |
95 virtual ~NTPSnippetsServiceObserver() {} | 122 virtual ~NTPSnippetsServiceObserver() {} |
96 }; | 123 }; |
97 | 124 |
98 } // namespace ntp_snippets | 125 } // namespace ntp_snippets |
99 | 126 |
100 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ | 127 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ |
OLD | NEW |