Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(495)

Side by Side Diff: components/ntp_snippets/ntp_snippets_service.h

Issue 1737563003: [NTP Snippets] Persist the set of snippets in a pref (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@snippets_merge
Patch Set: noyau review Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/ntp_snippets/ntp_snippet.cc ('k') | components/ntp_snippets/ntp_snippets_service.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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,
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
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 // TODO(treib): Investigate a better storage, maybe LevelDB or SQLite?
110 void LoadFromPrefs();
111 void StoreToPrefs();
112
113 PrefService* pref_service_;
114
91 // True if the suggestions are loaded. 115 // True if the suggestions are loaded.
92 bool loaded_; 116 bool loaded_;
93 117
94 // The SequencedTaskRunner on which file system operations will be run. 118 // The SequencedTaskRunner on which file system operations will be run.
95 scoped_refptr<base::SequencedTaskRunner> file_task_runner_; 119 scoped_refptr<base::SequencedTaskRunner> file_task_runner_;
96 120
97 // All the suggestions. 121 // All the suggestions.
98 NTPSnippetStorage snippets_; 122 NTPSnippetStorage snippets_;
99 123
100 // The ISO 639-1 code of the language used by the application. 124 // The ISO 639-1 code of the language used by the application.
(...skipping 30 matching lines...) Expand all
131 // Send when the service is shutting down. 155 // Send when the service is shutting down.
132 virtual void NTPSnippetsServiceShutdown(NTPSnippetsService* service) = 0; 156 virtual void NTPSnippetsServiceShutdown(NTPSnippetsService* service) = 0;
133 157
134 protected: 158 protected:
135 virtual ~NTPSnippetsServiceObserver() {} 159 virtual ~NTPSnippetsServiceObserver() {}
136 }; 160 };
137 161
138 } // namespace ntp_snippets 162 } // namespace ntp_snippets
139 163
140 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ 164 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_
OLDNEW
« no previous file with comments | « components/ntp_snippets/ntp_snippet.cc ('k') | components/ntp_snippets/ntp_snippets_service.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698