| Index: components/ntp_snippets/ntp_snippets_service.cc
|
| diff --git a/components/ntp_snippets/ntp_snippets_service.cc b/components/ntp_snippets/ntp_snippets_service.cc
|
| index 781604d83829cd94d44a3f2bbc93004931897e8c..23fb30839d56895b09086f3bc1b3b4f4c58b3588 100644
|
| --- a/components/ntp_snippets/ntp_snippets_service.cc
|
| +++ b/components/ntp_snippets/ntp_snippets_service.cc
|
| @@ -14,6 +14,7 @@
|
| #include "base/location.h"
|
| #include "base/path_service.h"
|
| #include "base/strings/string_number_conversions.h"
|
| +#include "base/strings/stringprintf.h"
|
| #include "base/task_runner_util.h"
|
| #include "base/time/time.h"
|
| #include "base/values.h"
|
| @@ -42,6 +43,11 @@ const int kWifiFetchingHourMax = 22;
|
|
|
| const int kDefaultExpiryTimeMins = 24 * 60;
|
|
|
| +const char kStatusMessageEmptyHosts[] = "Cannot fetch for empty hosts list.";
|
| +const char kStatusMessageEmptyList[] = "Invalid / empty list.";
|
| +const char kStatusMessageJsonError[] = "Received invalid JSON (error %s)";
|
| +const char kStatusMessageOK[] = "OK";
|
| +
|
| base::TimeDelta GetFetchingInterval(const char* switch_name,
|
| int default_value_seconds) {
|
| int value_seconds = default_value_seconds;
|
| @@ -222,8 +228,12 @@ void NTPSnippetsService::FetchSnippetsFromHosts(
|
| snippets_fetcher_->FetchSnippets(std::set<std::string>());
|
| return;
|
| }
|
| - if (!hosts.empty())
|
| + if (!hosts.empty()) {
|
| snippets_fetcher_->FetchSnippets(hosts);
|
| + } else {
|
| + last_fetch_status_ = kStatusMessageEmptyHosts;
|
| + LoadingSnippetsFinished();
|
| + }
|
| }
|
|
|
| void NTPSnippetsService::RescheduleFetching() {
|
| @@ -314,23 +324,40 @@ void NTPSnippetsService::OnSuggestionsChanged(
|
| }
|
|
|
| void NTPSnippetsService::OnSnippetsDownloaded(
|
| - const std::string& snippets_json) {
|
| - parse_json_callback_.Run(
|
| - snippets_json, base::Bind(&NTPSnippetsService::OnJsonParsed,
|
| - weak_ptr_factory_.GetWeakPtr(), snippets_json),
|
| - base::Bind(&NTPSnippetsService::OnJsonError,
|
| - weak_ptr_factory_.GetWeakPtr(), snippets_json));
|
| + const std::string& snippets_json, const std::string& status) {
|
| + last_fetch_status_ = status;
|
| +
|
| + if (!snippets_json.empty()) {
|
| + parse_json_callback_.Run(
|
| + snippets_json,
|
| + base::Bind(&NTPSnippetsService::OnJsonParsed,
|
| + weak_ptr_factory_.GetWeakPtr(), snippets_json),
|
| + base::Bind(&NTPSnippetsService::OnJsonError,
|
| + weak_ptr_factory_.GetWeakPtr(), snippets_json));
|
| + } else {
|
| + LoadingSnippetsFinished();
|
| + }
|
| }
|
|
|
| void NTPSnippetsService::OnJsonParsed(const std::string& snippets_json,
|
| scoped_ptr<base::Value> parsed) {
|
| - LOG_IF(WARNING, !LoadFromValue(*parsed)) << "Received invalid snippets: "
|
| - << snippets_json;
|
| + if (!LoadFromValue(*parsed)) {
|
| + LOG(WARNING) << "Received invalid snippets: " << snippets_json;
|
| + last_fetch_status_ = kStatusMessageEmptyList;
|
| + } else {
|
| + last_fetch_status_ = kStatusMessageOK;
|
| + }
|
| +
|
| + LoadingSnippetsFinished();
|
| }
|
|
|
| void NTPSnippetsService::OnJsonError(const std::string& snippets_json,
|
| const std::string& error) {
|
| LOG(WARNING) << "Received invalid JSON (" << error << "): " << snippets_json;
|
| + last_fetch_status_ = base::StringPrintf(kStatusMessageJsonError,
|
| + error.c_str());
|
| +
|
| + LoadingSnippetsFinished();
|
| }
|
|
|
| bool NTPSnippetsService::LoadFromValue(const base::Value& value) {
|
| @@ -376,16 +403,14 @@ bool NTPSnippetsService::LoadFromListValue(const base::ListValue& list) {
|
| snippets_.push_back(std::move(snippet));
|
| }
|
|
|
| - // Immediately remove any already-expired snippets. This will also notify our
|
| - // observers and schedule the expiry timer.
|
| - RemoveExpiredSnippets();
|
| -
|
| return true;
|
| }
|
|
|
| void NTPSnippetsService::LoadSnippetsFromPrefs() {
|
| bool success = LoadFromListValue(*pref_service_->GetList(prefs::kSnippets));
|
| DCHECK(success) << "Failed to parse snippets from prefs";
|
| +
|
| + LoadingSnippetsFinished();
|
| }
|
|
|
| void NTPSnippetsService::StoreSnippetsToPrefs() {
|
| @@ -433,7 +458,8 @@ bool NTPSnippetsService::HasDiscardedSnippet(const GURL& url) const {
|
| return it != discarded_snippets_.end();
|
| }
|
|
|
| -void NTPSnippetsService::RemoveExpiredSnippets() {
|
| +void NTPSnippetsService::LoadingSnippetsFinished() {
|
| + // Remove expired snippets.
|
| base::Time expiry = base::Time::Now();
|
|
|
| snippets_.erase(
|
| @@ -470,7 +496,7 @@ void NTPSnippetsService::RemoveExpiredSnippets() {
|
| }
|
| DCHECK_GT(next_expiry, expiry);
|
| expiry_timer_.Start(FROM_HERE, next_expiry - expiry,
|
| - base::Bind(&NTPSnippetsService::RemoveExpiredSnippets,
|
| + base::Bind(&NTPSnippetsService::LoadingSnippetsFinished,
|
| base::Unretained(this)));
|
| }
|
|
|
|
|