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 #include "components/ntp_snippets/ntp_snippets_service.h" | 5 #include "components/ntp_snippets/ntp_snippets_service.h" |
6 | 6 |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
11 #include "base/path_service.h" | 11 #include "base/path_service.h" |
12 #include "base/task_runner_util.h" | 12 #include "base/task_runner_util.h" |
13 #include "base/values.h" | 13 #include "base/values.h" |
14 #include "components/ntp_snippets/pref_names.h" | |
15 #include "components/pref_registry/pref_registry_syncable.h" | |
16 #include "components/prefs/pref_service.h" | |
14 | 17 |
15 namespace { | 18 namespace { |
16 | 19 |
17 // TODO(crbug.com/587857): This is an extremely small value, for development. | 20 // TODO(crbug.com/587857): This is an extremely small value, for development. |
18 // Replace it by something sensible and add a command line param to control it. | 21 // Replace it by something sensible and add a command line param to control it. |
19 const int kDefaultFetchingIntervalSeconds = 60; | 22 const int kDefaultFetchingIntervalSeconds = 60; |
20 | 23 |
21 bool ReadFileToString(const base::FilePath& path, std::string* data) { | 24 bool ReadFileToString(const base::FilePath& path, std::string* data) { |
22 DCHECK(data); | 25 DCHECK(data); |
23 bool success = base::ReadFileToString(path, data); | 26 bool success = base::ReadFileToString(path, data); |
24 DLOG_IF(ERROR, !success) << "Error reading file " << path.LossyDisplayName(); | 27 DLOG_IF(ERROR, !success) << "Error reading file " << path.LossyDisplayName(); |
25 return success; | 28 return success; |
26 } | 29 } |
27 | 30 |
28 } // namespace | 31 } // namespace |
29 | 32 |
30 namespace ntp_snippets { | 33 namespace ntp_snippets { |
31 | 34 |
32 NTPSnippetsService::NTPSnippetsService( | 35 NTPSnippetsService::NTPSnippetsService( |
36 PrefService* pref_service, | |
33 scoped_refptr<base::SequencedTaskRunner> file_task_runner, | 37 scoped_refptr<base::SequencedTaskRunner> file_task_runner, |
34 const std::string& application_language_code, | 38 const std::string& application_language_code, |
35 NTPSnippetsScheduler* scheduler, | 39 NTPSnippetsScheduler* scheduler, |
36 scoped_ptr<NTPSnippetsFetcher> snippets_fetcher) | 40 scoped_ptr<NTPSnippetsFetcher> snippets_fetcher) |
37 : loaded_(false), | 41 : pref_service_(pref_service), |
42 loaded_(false), | |
38 file_task_runner_(file_task_runner), | 43 file_task_runner_(file_task_runner), |
39 application_language_code_(application_language_code), | 44 application_language_code_(application_language_code), |
40 scheduler_(scheduler), | 45 scheduler_(scheduler), |
41 snippets_fetcher_(std::move(snippets_fetcher)), | 46 snippets_fetcher_(std::move(snippets_fetcher)), |
42 weak_ptr_factory_(this) { | 47 weak_ptr_factory_(this) { |
43 snippets_fetcher_callback_ = snippets_fetcher_->AddCallback( | 48 snippets_fetcher_callback_ = snippets_fetcher_->AddCallback( |
44 base::Bind(&NTPSnippetsService::OnSnippetsDownloaded, | 49 base::Bind(&NTPSnippetsService::OnSnippetsDownloaded, |
45 weak_ptr_factory_.GetWeakPtr())); | 50 weak_ptr_factory_.GetWeakPtr())); |
46 } | 51 } |
47 | 52 |
48 NTPSnippetsService::~NTPSnippetsService() {} | 53 NTPSnippetsService::~NTPSnippetsService() {} |
49 | 54 |
55 // static | |
56 void NTPSnippetsService::RegisterProfilePrefs( | |
57 user_prefs::PrefRegistrySyncable* registry) { | |
58 registry->RegisterListPref(prefs::kSnippets); | |
59 } | |
60 | |
50 void NTPSnippetsService::Init(bool enabled) { | 61 void NTPSnippetsService::Init(bool enabled) { |
51 // If enabled, get snippets immediately. If we've downloaded them before, | 62 // If enabled, get any existing snippets immediately from prefs. |
52 // this will just read from disk. | 63 if (enabled) { |
53 if (enabled) | 64 LoadFromPrefs(); |
54 FetchSnippets(false); | 65 |
66 // If we don't have any snippets yet, start a fetch. | |
67 if (snippets_.empty()) | |
68 FetchSnippets(false); | |
69 } | |
55 | 70 |
56 // The scheduler only exists on Android so far, it's null on other platforms. | 71 // The scheduler only exists on Android so far, it's null on other platforms. |
57 if (!scheduler_) | 72 if (!scheduler_) |
58 return; | 73 return; |
59 | 74 |
60 if (enabled) | 75 if (enabled) |
61 scheduler_->Schedule(kDefaultFetchingIntervalSeconds); | 76 scheduler_->Schedule(kDefaultFetchingIntervalSeconds); |
62 else | 77 else |
63 scheduler_->Unschedule(); | 78 scheduler_->Unschedule(); |
64 } | 79 } |
(...skipping 11 matching lines...) Expand all Loading... | |
76 void NTPSnippetsService::AddObserver(NTPSnippetsServiceObserver* observer) { | 91 void NTPSnippetsService::AddObserver(NTPSnippetsServiceObserver* observer) { |
77 observers_.AddObserver(observer); | 92 observers_.AddObserver(observer); |
78 if (loaded_) | 93 if (loaded_) |
79 observer->NTPSnippetsServiceLoaded(this); | 94 observer->NTPSnippetsServiceLoaded(this); |
80 } | 95 } |
81 | 96 |
82 void NTPSnippetsService::RemoveObserver(NTPSnippetsServiceObserver* observer) { | 97 void NTPSnippetsService::RemoveObserver(NTPSnippetsServiceObserver* observer) { |
83 observers_.RemoveObserver(observer); | 98 observers_.RemoveObserver(observer); |
84 } | 99 } |
85 | 100 |
101 void NTPSnippetsService::OnSnippetsDownloaded( | |
102 const base::FilePath& download_path) { | |
103 std::string* downloaded_data = new std::string; | |
104 base::PostTaskAndReplyWithResult( | |
105 file_task_runner_.get(), FROM_HERE, | |
106 base::Bind(&ReadFileToString, download_path, downloaded_data), | |
107 base::Bind(&NTPSnippetsService::OnFileReadDone, | |
108 weak_ptr_factory_.GetWeakPtr(), base::Owned(downloaded_data))); | |
109 } | |
110 | |
86 void NTPSnippetsService::OnFileReadDone(const std::string* json, bool success) { | 111 void NTPSnippetsService::OnFileReadDone(const std::string* json, bool success) { |
87 if (!success) | 112 if (!success) |
88 return; | 113 return; |
89 | 114 |
90 DCHECK(json); | 115 DCHECK(json); |
91 LoadFromJSONString(*json); | 116 LoadFromJSONString(*json); |
117 StoreToPrefs(); | |
92 } | 118 } |
93 | 119 |
94 bool NTPSnippetsService::LoadFromJSONString(const std::string& str) { | 120 bool NTPSnippetsService::LoadFromJSONString(const std::string& str) { |
95 scoped_ptr<base::Value> deserialized = base::JSONReader::Read(str); | 121 scoped_ptr<base::Value> deserialized = base::JSONReader::Read(str); |
96 if (!deserialized) | 122 if (!deserialized) |
97 return false; | 123 return false; |
98 | 124 |
99 const base::DictionaryValue* top_dict = nullptr; | 125 const base::DictionaryValue* top_dict = nullptr; |
100 if (!deserialized->GetAsDictionary(&top_dict)) | 126 if (!deserialized->GetAsDictionary(&top_dict)) |
101 return false; | 127 return false; |
102 | 128 |
103 const base::ListValue* list = nullptr; | 129 const base::ListValue* list = nullptr; |
104 if (!top_dict->GetList("recos", &list)) | 130 if (!top_dict->GetList("recos", &list)) |
105 return false; | 131 return false; |
106 | 132 |
107 for (const base::Value* const value : *list) { | 133 return LoadFromJSONList(*list); |
134 } | |
135 | |
136 bool NTPSnippetsService::LoadFromJSONList(const base::ListValue& list) { | |
137 for (const base::Value* const value : list) { | |
108 const base::DictionaryValue* dict = nullptr; | 138 const base::DictionaryValue* dict = nullptr; |
109 if (!value->GetAsDictionary(&dict)) | 139 if (!value->GetAsDictionary(&dict)) |
110 return false; | 140 return false; |
111 | 141 |
112 const base::DictionaryValue* content = nullptr; | 142 const base::DictionaryValue* content = nullptr; |
113 if (!dict->GetDictionary("contentInfo", &content)) | 143 if (!dict->GetDictionary("contentInfo", &content)) |
114 return false; | 144 return false; |
115 scoped_ptr<NTPSnippet> snippet = | 145 scoped_ptr<NTPSnippet> snippet = NTPSnippet::CreateFromDictionary(*content); |
116 NTPSnippet::NTPSnippetFromDictionary(*content); | |
117 if (!snippet) | 146 if (!snippet) |
118 return false; | 147 return false; |
119 | 148 |
120 // Check if we already have a snippet with the same URL. If so, replace it | 149 // Check if we already have a snippet with the same URL. If so, replace it |
121 // rather than adding a duplicate. | 150 // rather than adding a duplicate. |
122 const GURL& url = snippet->url(); | 151 const GURL& url = snippet->url(); |
123 auto it = std::find_if(snippets_.begin(), snippets_.end(), | 152 auto it = std::find_if(snippets_.begin(), snippets_.end(), |
124 [&url](const scoped_ptr<NTPSnippet>& old_snippet) { | 153 [&url](const scoped_ptr<NTPSnippet>& old_snippet) { |
125 return old_snippet->url() == url; | 154 return old_snippet->url() == url; |
126 }); | 155 }); |
127 if (it != snippets_.end()) | 156 if (it != snippets_.end()) |
128 *it = std::move(snippet); | 157 *it = std::move(snippet); |
129 else | 158 else |
130 snippets_.push_back(std::move(snippet)); | 159 snippets_.push_back(std::move(snippet)); |
131 } | 160 } |
132 loaded_ = true; | 161 loaded_ = true; |
133 | 162 |
134 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, | 163 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, |
135 NTPSnippetsServiceLoaded(this)); | 164 NTPSnippetsServiceLoaded(this)); |
136 return true; | 165 return true; |
137 } | 166 } |
138 | 167 |
139 void NTPSnippetsService::OnSnippetsDownloaded( | 168 void NTPSnippetsService::LoadFromPrefs() { |
140 const base::FilePath& download_path) { | 169 // |pref_service_| can be null in tests. |
141 std::string* downloaded_data = new std::string; | 170 if (!pref_service_) |
142 base::PostTaskAndReplyWithResult( | 171 return; |
143 file_task_runner_.get(), FROM_HERE, | 172 if (!LoadFromJSONList(*pref_service_->GetList(prefs::kSnippets))) |
144 base::Bind(&ReadFileToString, download_path, downloaded_data), | 173 LOG(ERROR) << "Failed to parse snippets from prefs"; |
145 base::Bind(&NTPSnippetsService::OnFileReadDone, | 174 } |
146 weak_ptr_factory_.GetWeakPtr(), base::Owned(downloaded_data))); | 175 |
176 void NTPSnippetsService::StoreToPrefs() { | |
177 // |pref_service_| can be null in tests. | |
178 if (!pref_service_) | |
179 return; | |
180 base::ListValue list; | |
181 for (const auto& snippet : snippets_) { | |
182 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue); | |
183 dict->Set("contentInfo", snippet->ToDictionary()); | |
noyau (Ping after 24h)
2016/02/29 15:57:14
"contentInfo should probably be a constant.
Marc Treib
2016/02/29 17:07:38
Done.
| |
184 list.Append(std::move(dict)); | |
185 } | |
186 pref_service_->Set(prefs::kSnippets, list); | |
147 } | 187 } |
148 | 188 |
149 } // namespace ntp_snippets | 189 } // namespace ntp_snippets |
OLD | NEW |