Chromium Code Reviews| 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 <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 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 215 FetchSnippetsFromHosts(GetSuggestionsHosts()); | 215 FetchSnippetsFromHosts(GetSuggestionsHosts()); |
| 216 } | 216 } |
| 217 | 217 |
| 218 void NTPSnippetsService::FetchSnippetsFromHosts( | 218 void NTPSnippetsService::FetchSnippetsFromHosts( |
| 219 const std::set<std::string>& hosts) { | 219 const std::set<std::string>& hosts) { |
| 220 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 220 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 221 switches::kDontRestrict)) { | 221 switches::kDontRestrict)) { |
| 222 snippets_fetcher_->FetchSnippets(std::set<std::string>()); | 222 snippets_fetcher_->FetchSnippets(std::set<std::string>()); |
| 223 return; | 223 return; |
| 224 } | 224 } |
| 225 if (!hosts.empty()) | 225 if (!hosts.empty()) { |
| 226 snippets_fetcher_->FetchSnippets(hosts); | 226 snippets_fetcher_->FetchSnippets(hosts); |
| 227 } else { | |
| 228 snippets_fetcher_last_status_ = "Cannot fetch for empty hosts list."; | |
|
Bernhard Bauer
2016/04/21 16:28:41
Can you extract these into constants and move to t
jkrcal
2016/04/22 09:30:28
Done.
What is the reason (apart from having Engli
Bernhard Bauer
2016/04/22 09:42:17
It's mostly that the status messages are part of t
jkrcal
2016/04/22 11:11:59
Thanks for explaining!
| |
| 229 LoadingSnippetsFinished(); | |
| 230 } | |
| 227 } | 231 } |
| 228 | 232 |
| 229 void NTPSnippetsService::RescheduleFetching() { | 233 void NTPSnippetsService::RescheduleFetching() { |
| 230 // The scheduler only exists on Android so far, it's null on other platforms. | 234 // The scheduler only exists on Android so far, it's null on other platforms. |
| 231 if (!scheduler_) | 235 if (!scheduler_) |
| 232 return; | 236 return; |
| 233 | 237 |
| 234 if (enabled_) { | 238 if (enabled_) { |
| 235 base::Time now = base::Time::Now(); | 239 base::Time now = base::Time::Now(); |
| 236 scheduler_->Schedule( | 240 scheduler_->Schedule( |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 307 StoreSnippetsToPrefs(); | 311 StoreSnippetsToPrefs(); |
| 308 StoreSnippetHostsToPrefs(hosts); | 312 StoreSnippetHostsToPrefs(hosts); |
| 309 | 313 |
| 310 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, | 314 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, |
| 311 NTPSnippetsServiceLoaded()); | 315 NTPSnippetsServiceLoaded()); |
| 312 | 316 |
| 313 FetchSnippetsFromHosts(hosts); | 317 FetchSnippetsFromHosts(hosts); |
| 314 } | 318 } |
| 315 | 319 |
| 316 void NTPSnippetsService::OnSnippetsDownloaded( | 320 void NTPSnippetsService::OnSnippetsDownloaded( |
| 317 const std::string& snippets_json) { | 321 const std::string& snippets_json, const std::string& status) { |
| 322 snippets_fetcher_last_status_ = status; | |
|
Marc Treib
2016/04/21 13:47:20
Now we also get here if the download failed (and s
jkrcal
2016/04/22 09:30:28
Done.
Good point, thanks! This is something I've
| |
| 323 | |
| 318 parse_json_callback_.Run( | 324 parse_json_callback_.Run( |
| 319 snippets_json, base::Bind(&NTPSnippetsService::OnJsonParsed, | 325 snippets_json, base::Bind(&NTPSnippetsService::OnJsonParsed, |
| 320 weak_ptr_factory_.GetWeakPtr(), snippets_json), | 326 weak_ptr_factory_.GetWeakPtr(), snippets_json), |
| 321 base::Bind(&NTPSnippetsService::OnJsonError, | 327 base::Bind(&NTPSnippetsService::OnJsonError, |
| 322 weak_ptr_factory_.GetWeakPtr(), snippets_json)); | 328 weak_ptr_factory_.GetWeakPtr(), snippets_json)); |
| 323 } | 329 } |
| 324 | 330 |
| 325 void NTPSnippetsService::OnJsonParsed(const std::string& snippets_json, | 331 void NTPSnippetsService::OnJsonParsed(const std::string& snippets_json, |
| 326 scoped_ptr<base::Value> parsed) { | 332 scoped_ptr<base::Value> parsed) { |
| 327 LOG_IF(WARNING, !LoadFromValue(*parsed)) << "Received invalid snippets: " | 333 bool success = LoadFromValue(*parsed); |
|
Marc Treib
2016/04/21 13:47:20
No need for the "success" variable, just say if(Lo
jkrcal
2016/04/22 09:30:28
Done.
| |
| 328 << snippets_json; | 334 if (!success) { |
| 335 LOG(WARNING) << "Received invalid snippets: " << snippets_json; | |
| 336 snippets_fetcher_last_status_ = "Invalid / empty list."; | |
| 337 } else { | |
| 338 snippets_fetcher_last_status_ = "OK"; | |
| 339 } | |
| 340 | |
| 341 LoadingSnippetsFinished(); | |
| 329 } | 342 } |
| 330 | 343 |
| 331 void NTPSnippetsService::OnJsonError(const std::string& snippets_json, | 344 void NTPSnippetsService::OnJsonError(const std::string& snippets_json, |
| 332 const std::string& error) { | 345 const std::string& error) { |
| 333 LOG(WARNING) << "Received invalid JSON (" << error << "): " << snippets_json; | 346 LOG(WARNING) << "Received invalid JSON (" << error << "): " << snippets_json; |
|
Marc Treib
2016/04/21 13:47:20
Set snippets_fetcher_last_status_ to something mea
jkrcal
2016/04/22 09:30:28
Done.
| |
| 347 | |
| 348 LoadingSnippetsFinished(); | |
| 334 } | 349 } |
| 335 | 350 |
| 336 bool NTPSnippetsService::LoadFromValue(const base::Value& value) { | 351 bool NTPSnippetsService::LoadFromValue(const base::Value& value) { |
| 337 const base::DictionaryValue* top_dict = nullptr; | 352 const base::DictionaryValue* top_dict = nullptr; |
| 338 if (!value.GetAsDictionary(&top_dict)) | 353 if (!value.GetAsDictionary(&top_dict)) |
| 339 return false; | 354 return false; |
| 340 | 355 |
| 341 const base::ListValue* list = nullptr; | 356 const base::ListValue* list = nullptr; |
| 342 if (!top_dict->GetList("recos", &list)) | 357 if (!top_dict->GetList("recos", &list)) |
| 343 return false; | 358 return false; |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 369 auto it = std::find_if(snippets_.begin(), snippets_.end(), | 384 auto it = std::find_if(snippets_.begin(), snippets_.end(), |
| 370 [&url](const scoped_ptr<NTPSnippet>& old_snippet) { | 385 [&url](const scoped_ptr<NTPSnippet>& old_snippet) { |
| 371 return old_snippet->url() == url; | 386 return old_snippet->url() == url; |
| 372 }); | 387 }); |
| 373 if (it != snippets_.end()) | 388 if (it != snippets_.end()) |
| 374 *it = std::move(snippet); | 389 *it = std::move(snippet); |
| 375 else | 390 else |
| 376 snippets_.push_back(std::move(snippet)); | 391 snippets_.push_back(std::move(snippet)); |
| 377 } | 392 } |
| 378 | 393 |
| 379 // Immediately remove any already-expired snippets. This will also notify our | |
| 380 // observers and schedule the expiry timer. | |
| 381 RemoveExpiredSnippets(); | |
| 382 | |
| 383 return true; | 394 return true; |
| 384 } | 395 } |
| 385 | 396 |
| 386 void NTPSnippetsService::LoadSnippetsFromPrefs() { | 397 void NTPSnippetsService::LoadSnippetsFromPrefs() { |
| 387 bool success = LoadFromListValue(*pref_service_->GetList(prefs::kSnippets)); | 398 bool success = LoadFromListValue(*pref_service_->GetList(prefs::kSnippets)); |
| 388 DCHECK(success) << "Failed to parse snippets from prefs"; | 399 DCHECK(success) << "Failed to parse snippets from prefs"; |
| 400 | |
| 401 LoadingSnippetsFinished(); | |
| 389 } | 402 } |
| 390 | 403 |
| 391 void NTPSnippetsService::StoreSnippetsToPrefs() { | 404 void NTPSnippetsService::StoreSnippetsToPrefs() { |
| 392 pref_service_->Set(prefs::kSnippets, *SnippetsToListValue(snippets_)); | 405 pref_service_->Set(prefs::kSnippets, *SnippetsToListValue(snippets_)); |
| 393 } | 406 } |
| 394 | 407 |
| 395 void NTPSnippetsService::LoadDiscardedSnippetsFromPrefs() { | 408 void NTPSnippetsService::LoadDiscardedSnippetsFromPrefs() { |
| 396 discarded_snippets_.clear(); | 409 discarded_snippets_.clear(); |
| 397 bool success = AddSnippetsFromListValue( | 410 bool success = AddSnippetsFromListValue( |
| 398 *pref_service_->GetList(prefs::kDiscardedSnippets), | 411 *pref_service_->GetList(prefs::kDiscardedSnippets), |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 426 } | 439 } |
| 427 | 440 |
| 428 bool NTPSnippetsService::HasDiscardedSnippet(const GURL& url) const { | 441 bool NTPSnippetsService::HasDiscardedSnippet(const GURL& url) const { |
| 429 auto it = std::find_if(discarded_snippets_.begin(), discarded_snippets_.end(), | 442 auto it = std::find_if(discarded_snippets_.begin(), discarded_snippets_.end(), |
| 430 [&url](const scoped_ptr<NTPSnippet>& snippet) { | 443 [&url](const scoped_ptr<NTPSnippet>& snippet) { |
| 431 return snippet->url() == url; | 444 return snippet->url() == url; |
| 432 }); | 445 }); |
| 433 return it != discarded_snippets_.end(); | 446 return it != discarded_snippets_.end(); |
| 434 } | 447 } |
| 435 | 448 |
| 436 void NTPSnippetsService::RemoveExpiredSnippets() { | 449 void NTPSnippetsService::LoadingSnippetsFinished() { |
| 450 // Remove expired snippets. | |
| 437 base::Time expiry = base::Time::Now(); | 451 base::Time expiry = base::Time::Now(); |
| 438 | 452 |
| 439 snippets_.erase( | 453 snippets_.erase( |
| 440 std::remove_if(snippets_.begin(), snippets_.end(), | 454 std::remove_if(snippets_.begin(), snippets_.end(), |
| 441 [&expiry](const scoped_ptr<NTPSnippet>& snippet) { | 455 [&expiry](const scoped_ptr<NTPSnippet>& snippet) { |
| 442 return snippet->expiry_date() <= expiry; | 456 return snippet->expiry_date() <= expiry; |
| 443 }), | 457 }), |
| 444 snippets_.end()); | 458 snippets_.end()); |
| 445 StoreSnippetsToPrefs(); | 459 StoreSnippetsToPrefs(); |
| 446 | 460 |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 463 for (const auto& snippet : snippets_) { | 477 for (const auto& snippet : snippets_) { |
| 464 if (snippet->expiry_date() < next_expiry) | 478 if (snippet->expiry_date() < next_expiry) |
| 465 next_expiry = snippet->expiry_date(); | 479 next_expiry = snippet->expiry_date(); |
| 466 } | 480 } |
| 467 for (const auto& snippet : discarded_snippets_) { | 481 for (const auto& snippet : discarded_snippets_) { |
| 468 if (snippet->expiry_date() < next_expiry) | 482 if (snippet->expiry_date() < next_expiry) |
| 469 next_expiry = snippet->expiry_date(); | 483 next_expiry = snippet->expiry_date(); |
| 470 } | 484 } |
| 471 DCHECK_GT(next_expiry, expiry); | 485 DCHECK_GT(next_expiry, expiry); |
| 472 expiry_timer_.Start(FROM_HERE, next_expiry - expiry, | 486 expiry_timer_.Start(FROM_HERE, next_expiry - expiry, |
| 473 base::Bind(&NTPSnippetsService::RemoveExpiredSnippets, | 487 base::Bind(&NTPSnippetsService::LoadingSnippetsFinished, |
| 474 base::Unretained(this))); | 488 base::Unretained(this))); |
| 475 } | 489 } |
| 476 | 490 |
| 477 } // namespace ntp_snippets | 491 } // namespace ntp_snippets |
| OLD | NEW |