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

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

Issue 1883523002: chrome://snippets-internals page for debugging NTP snippets. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Third code review ( rebase-update) Created 4 years, 8 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
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 #include "components/ntp_snippets/ntp_snippets_service.h" 5 #include "components/ntp_snippets/ntp_snippets_service.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/command_line.h" 10 #include "base/command_line.h"
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 return GetFetchingInterval(switches::kFetchingIntervalWifiSeconds, 61 return GetFetchingInterval(switches::kFetchingIntervalWifiSeconds,
62 kFetchingIntervalWifiSeconds); 62 kFetchingIntervalWifiSeconds);
63 } 63 }
64 64
65 base::TimeDelta GetFetchingIntervalFallback() { 65 base::TimeDelta GetFetchingIntervalFallback() {
66 return GetFetchingInterval(switches::kFetchingIntervalFallbackSeconds, 66 return GetFetchingInterval(switches::kFetchingIntervalFallbackSeconds,
67 kFetchingIntervalFallbackSeconds); 67 kFetchingIntervalFallbackSeconds);
68 } 68 }
69 69
70 // Extracts the hosts from |suggestions| and returns them in a set. 70 // Extracts the hosts from |suggestions| and returns them in a set.
71 std::set<std::string> GetSuggestionsHosts( 71 std::set<std::string> GetSuggestionsHostsImpl(
72 const SuggestionsProfile& suggestions) { 72 const SuggestionsProfile& suggestions) {
73 std::set<std::string> hosts; 73 std::set<std::string> hosts;
74 for (int i = 0; i < suggestions.suggestions_size(); ++i) { 74 for (int i = 0; i < suggestions.suggestions_size(); ++i) {
75 const ChromeSuggestion& suggestion = suggestions.suggestions(i); 75 const ChromeSuggestion& suggestion = suggestions.suggestions(i);
76 GURL url(suggestion.url()); 76 GURL url(suggestion.url());
77 if (url.is_valid()) 77 if (url.is_valid())
78 hosts.insert(url.host()); 78 hosts.insert(url.host());
79 } 79 }
80 return hosts; 80 return hosts;
81 } 81 }
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 scheduler_->Unschedule(); 175 scheduler_->Unschedule();
176 } 176 }
177 } 177 }
178 178
179 void NTPSnippetsService::Shutdown() { 179 void NTPSnippetsService::Shutdown() {
180 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, 180 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_,
181 NTPSnippetsServiceShutdown()); 181 NTPSnippetsServiceShutdown());
182 } 182 }
183 183
184 void NTPSnippetsService::FetchSnippets() { 184 void NTPSnippetsService::FetchSnippets() {
185 FetchSnippetsFromHosts(GetSuggestionsHosts());
186 }
187
188 void NTPSnippetsService::FetchSnippetsFromHosts(
189 const std::set<std::string>& hosts) {
190 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
191 switches::kDontRestrict)) {
192 snippets_fetcher_->FetchSnippets(std::set<std::string>());
193 return;
194 }
195 if (!hosts.empty())
196 snippets_fetcher_->FetchSnippets(hosts);
197 }
198
199 void NTPSnippetsService::ClearSnippets() {
200 snippets_.clear();
201
202 StoreSnippetsToPrefs();
203
204 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_,
205 NTPSnippetsServiceLoaded());
206 }
207
208 std::set<std::string> NTPSnippetsService::GetSuggestionsHosts() const {
185 // |suggestions_service_| can be null in tests. 209 // |suggestions_service_| can be null in tests.
186 if (!suggestions_service_) 210 if (!suggestions_service_) {
Marc Treib 2016/04/15 09:00:30 nit: no braces
jkrcal 2016/04/15 14:11:51 Done.
187 return; 211 return std::set<std::string>();
212 }
188 213
189 FetchSnippetsImpl(GetSuggestionsHosts( 214 // TODO(treib) this should just call GetSnippetHostsFromPrefs
190 suggestions_service_->GetSuggestionsDataFromCache())); 215 return GetSuggestionsHostsImpl(
216 suggestions_service_->GetSuggestionsDataFromCache());
191 } 217 }
192 218
193 bool NTPSnippetsService::DiscardSnippet(const GURL& url) { 219 bool NTPSnippetsService::DiscardSnippet(const GURL& url) {
194 auto it = std::find_if(snippets_.begin(), snippets_.end(), 220 auto it = std::find_if(snippets_.begin(), snippets_.end(),
195 [&url](const scoped_ptr<NTPSnippet>& snippet) { 221 [&url](const scoped_ptr<NTPSnippet>& snippet) {
196 return snippet->url() == url; 222 return snippet->url() == url;
197 }); 223 });
198 if (it == snippets_.end()) 224 if (it == snippets_.end())
199 return false; 225 return false;
200 discarded_snippets_.push_back(std::move(*it)); 226 discarded_snippets_.push_back(std::move(*it));
201 snippets_.erase(it); 227 snippets_.erase(it);
202 StoreDiscardedSnippetsToPrefs(); 228 StoreDiscardedSnippetsToPrefs();
203 StoreSnippetsToPrefs(); 229 StoreSnippetsToPrefs();
230 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_,
231 NTPSnippetsServiceLoaded());
204 return true; 232 return true;
205 } 233 }
206 234
235 void NTPSnippetsService::ClearDiscardedSnippets() {
236 discarded_snippets_.clear();
237 StoreDiscardedSnippetsToPrefs();
238 FetchSnippets();
239 }
240
207 void NTPSnippetsService::AddObserver(NTPSnippetsServiceObserver* observer) { 241 void NTPSnippetsService::AddObserver(NTPSnippetsServiceObserver* observer) {
208 observers_.AddObserver(observer); 242 observers_.AddObserver(observer);
209 observer->NTPSnippetsServiceLoaded(); 243 observer->NTPSnippetsServiceLoaded();
Marc Treib 2016/04/15 09:00:30 Heads-up: dgn's https://codereview.chromium.org/18
jkrcal 2016/04/15 14:11:51 Thanks, I need to fix it!
210 } 244 }
211 245
212 void NTPSnippetsService::RemoveObserver(NTPSnippetsServiceObserver* observer) { 246 void NTPSnippetsService::RemoveObserver(NTPSnippetsServiceObserver* observer) {
213 observers_.RemoveObserver(observer); 247 observers_.RemoveObserver(observer);
214 } 248 }
215 249
216 void NTPSnippetsService::OnSuggestionsChanged( 250 void NTPSnippetsService::OnSuggestionsChanged(
217 const SuggestionsProfile& suggestions) { 251 const SuggestionsProfile& suggestions) {
218 std::set<std::string> hosts = GetSuggestionsHosts(suggestions); 252 std::set<std::string> hosts = GetSuggestionsHostsImpl(suggestions);
219 if (hosts == GetSnippetHostsFromPrefs()) 253 if (hosts == GetSnippetHostsFromPrefs())
220 return; 254 return;
221 255
222 // Remove existing snippets that aren't in the suggestions anymore. 256 // Remove existing snippets that aren't in the suggestions anymore.
223 snippets_.erase( 257 snippets_.erase(
224 std::remove_if(snippets_.begin(), snippets_.end(), 258 std::remove_if(snippets_.begin(), snippets_.end(),
225 [&hosts](const scoped_ptr<NTPSnippet>& snippet) { 259 [&hosts](const scoped_ptr<NTPSnippet>& snippet) {
226 return !hosts.count(snippet->url().host()); 260 return !hosts.count(snippet->url().host());
227 }), 261 }),
228 snippets_.end()); 262 snippets_.end());
229 263
230 StoreSnippetsToPrefs(); 264 StoreSnippetsToPrefs();
231 StoreSnippetHostsToPrefs(hosts); 265 StoreSnippetHostsToPrefs(hosts);
232 266
233 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, 267 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_,
234 NTPSnippetsServiceLoaded()); 268 NTPSnippetsServiceLoaded());
235 269
236 FetchSnippetsImpl(hosts); 270 FetchSnippetsFromHosts(hosts);
237 } 271 }
238 272
239 void NTPSnippetsService::OnSnippetsDownloaded( 273 void NTPSnippetsService::OnSnippetsDownloaded(
240 const std::string& snippets_json) { 274 const std::string& snippets_json) {
241 parse_json_callback_.Run( 275 parse_json_callback_.Run(
242 snippets_json, base::Bind(&NTPSnippetsService::OnJsonParsed, 276 snippets_json, base::Bind(&NTPSnippetsService::OnJsonParsed,
243 weak_ptr_factory_.GetWeakPtr(), snippets_json), 277 weak_ptr_factory_.GetWeakPtr(), snippets_json),
244 base::Bind(&NTPSnippetsService::OnJsonError, 278 base::Bind(&NTPSnippetsService::OnJsonError,
245 weak_ptr_factory_.GetWeakPtr(), snippets_json)); 279 weak_ptr_factory_.GetWeakPtr(), snippets_json));
246 } 280 }
247 281
248 void NTPSnippetsService::OnJsonParsed(const std::string& snippets_json, 282 void NTPSnippetsService::OnJsonParsed(const std::string& snippets_json,
249 scoped_ptr<base::Value> parsed) { 283 scoped_ptr<base::Value> parsed) {
250 LOG_IF(WARNING, !LoadFromValue(*parsed)) << "Received invalid snippets: " 284 LOG_IF(WARNING, !LoadFromValue(*parsed)) << "Received invalid snippets: "
251 << snippets_json; 285 << snippets_json;
252 } 286 }
253 287
254 void NTPSnippetsService::OnJsonError(const std::string& snippets_json, 288 void NTPSnippetsService::OnJsonError(const std::string& snippets_json,
255 const std::string& error) { 289 const std::string& error) {
256 LOG(WARNING) << "Received invalid JSON (" << error << "): " << snippets_json; 290 LOG(WARNING) << "Received invalid JSON (" << error << "): " << snippets_json;
257 } 291 }
258 292
259 void NTPSnippetsService::FetchSnippetsImpl(
260 const std::set<std::string>& hosts) {
261 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
262 switches::kDontRestrict)) {
263 snippets_fetcher_->FetchSnippets(std::set<std::string>());
264 return;
265 }
266 if (!hosts.empty())
267 snippets_fetcher_->FetchSnippets(hosts);
268 }
269
270 bool NTPSnippetsService::LoadFromValue(const base::Value& value) { 293 bool NTPSnippetsService::LoadFromValue(const base::Value& value) {
271 const base::DictionaryValue* top_dict = nullptr; 294 const base::DictionaryValue* top_dict = nullptr;
272 if (!value.GetAsDictionary(&top_dict)) 295 if (!value.GetAsDictionary(&top_dict))
273 return false; 296 return false;
274 297
275 const base::ListValue* list = nullptr; 298 const base::ListValue* list = nullptr;
276 if (!top_dict->GetList("recos", &list)) 299 if (!top_dict->GetList("recos", &list))
277 return false; 300 return false;
278 301
279 return LoadFromListValue(*list); 302 return LoadFromListValue(*list);
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
402 if (snippet->expiry_date() < next_expiry) 425 if (snippet->expiry_date() < next_expiry)
403 next_expiry = snippet->expiry_date(); 426 next_expiry = snippet->expiry_date();
404 } 427 }
405 DCHECK_GT(next_expiry, expiry); 428 DCHECK_GT(next_expiry, expiry);
406 expiry_timer_.Start(FROM_HERE, next_expiry - expiry, 429 expiry_timer_.Start(FROM_HERE, next_expiry - expiry,
407 base::Bind(&NTPSnippetsService::RemoveExpiredSnippets, 430 base::Bind(&NTPSnippetsService::RemoveExpiredSnippets,
408 base::Unretained(this))); 431 base::Unretained(this)));
409 } 432 }
410 433
411 } // namespace ntp_snippets 434 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698