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/observer_list.h" | 14 #include "base/observer_list.h" |
15 #include "base/sequenced_task_runner.h" | |
15 #include "components/keyed_service/core/keyed_service.h" | 16 #include "components/keyed_service/core/keyed_service.h" |
16 #include "components/ntp_snippets/inner_iterator.h" | 17 #include "components/ntp_snippets/inner_iterator.h" |
17 #include "components/ntp_snippets/ntp_snippet.h" | 18 #include "components/ntp_snippets/ntp_snippet.h" |
19 #include "components/ntp_snippets/ntp_snippets_fetcher.h" | |
18 | 20 |
19 namespace ntp_snippets { | 21 namespace ntp_snippets { |
20 | 22 |
21 class NTPSnippetsServiceObserver; | 23 class NTPSnippetsServiceObserver; |
22 | 24 |
23 // Stores and vend fresh content data for the NTP. | 25 // Stores and vend fresh content data for the NTP. |
24 class NTPSnippetsService : public KeyedService { | 26 class NTPSnippetsService : public KeyedService, NTPSnippetsFetcher::Observer { |
25 public: | 27 public: |
26 using NTPSnippetStorage = std::vector<std::unique_ptr<NTPSnippet>>; | 28 using NTPSnippetStorage = std::vector<std::unique_ptr<NTPSnippet>>; |
Bernhard Bauer
2016/02/08 18:19:27
This should use scoped_ptr, not std::unique_ptr.
May
2016/02/09 17:38:54
Why should it be a scoped_ptr? We construct the NT
Marc Treib
2016/02/10 10:44:43
scoped_ptr and unique_ptr are essentially the same
Bernhard Bauer
2016/02/10 10:48:44
Yup, but it's just that we usually use scoped_ptr
May
2016/02/10 18:16:46
Done.
| |
27 using const_iterator = | 29 using const_iterator = |
28 InnerIterator<NTPSnippetStorage::const_iterator, const NTPSnippet>; | 30 InnerIterator<NTPSnippetStorage::const_iterator, const NTPSnippet>; |
29 | 31 |
30 // |application_language_code| should be a ISO 639-1 compliant string. Aka | 32 // |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 | 33 // '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' | 34 // 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. | 35 // (British english person in the US) are not language code. |
34 explicit NTPSnippetsService(const std::string& application_language_code); | 36 explicit NTPSnippetsService( |
Bernhard Bauer
2016/02/08 18:19:27
`explicit` is only necessary for single-argument c
May
2016/02/09 17:38:54
Done.
| |
37 scoped_refptr<base::SequencedTaskRunner> file_task_runner, | |
38 const std::string& application_language_code, | |
39 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 |
(...skipping 17 matching lines...) Expand all Loading... | |
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 |
83 // NTPSnippetsFetcher::Observer overrides: | |
84 void OnNTPSnippetsDownloaded(); | |
Bernhard Bauer
2016/02/08 18:19:27
If this really overrides a method, it should have
May
2016/02/09 17:38:54
No idea why it didn't complain. But anyway I remov
| |
85 | |
74 private: | 86 private: |
87 void OnFileReadDone(std::string& json); | |
Marc Treib
2016/02/09 09:17:54
const std::string&
May
2016/02/09 17:38:54
Done.
| |
88 | |
75 // True if the suggestions are loaded. | 89 // True if the suggestions are loaded. |
76 bool loaded_; | 90 bool loaded_; |
91 | |
92 // The SequencedTaskRunner on which file system operations will be run. | |
93 scoped_refptr<base::SequencedTaskRunner> file_task_runner_; | |
94 | |
77 // All the suggestions. | 95 // All the suggestions. |
78 NTPSnippetStorage snippets_; | 96 NTPSnippetStorage snippets_; |
97 | |
79 // The ISO 639-1 code of the language used by the application. | 98 // The ISO 639-1 code of the language used by the application. |
80 const std::string application_language_code_; | 99 const std::string application_language_code_; |
100 | |
81 // The observers. | 101 // The observers. |
82 base::ObserverList<NTPSnippetsServiceObserver> observers_; | 102 base::ObserverList<NTPSnippetsServiceObserver> observers_; |
83 | 103 |
104 // The snippets fetcher | |
105 scoped_ptr<NTPSnippetsFetcher> snippets_fetcher_; | |
106 | |
84 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); | 107 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); |
85 }; | 108 }; |
86 | 109 |
87 class NTPSnippetsServiceObserver { | 110 class NTPSnippetsServiceObserver { |
88 public: | 111 public: |
89 // Send everytime the service loads a new set of data. | 112 // Send everytime the service loads a new set of data. |
90 virtual void NTPSnippetsServiceLoaded(NTPSnippetsService* service) = 0; | 113 virtual void NTPSnippetsServiceLoaded(NTPSnippetsService* service) = 0; |
91 // Send when the service is shutting down. | 114 // Send when the service is shutting down. |
92 virtual void NTPSnippetsServiceShutdown(NTPSnippetsService* service) = 0; | 115 virtual void NTPSnippetsServiceShutdown(NTPSnippetsService* service) = 0; |
93 | 116 |
94 protected: | 117 protected: |
95 virtual ~NTPSnippetsServiceObserver() {} | 118 virtual ~NTPSnippetsServiceObserver() {} |
96 }; | 119 }; |
97 | 120 |
98 } // namespace ntp_snippets | 121 } // namespace ntp_snippets |
99 | 122 |
100 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ | 123 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ |
OLD | NEW |