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

Unified Diff: components/ntp_snippets/ntp_snippet.cc

Issue 1878993002: [NTP Snippets] Clean up NTPSnippetsServiceTests (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 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 side-by-side diff with in-line comments
Download patch
Index: components/ntp_snippets/ntp_snippet.cc
diff --git a/components/ntp_snippets/ntp_snippet.cc b/components/ntp_snippets/ntp_snippet.cc
index d21e4913fd1a0d9e7baee767990326068f33f510..863f5770a6f147d07ce91c8e7bdd1d2f26b99f97 100644
--- a/components/ntp_snippets/ntp_snippet.cc
+++ b/components/ntp_snippets/ntp_snippet.cc
@@ -56,29 +56,13 @@ std::unique_ptr<NTPSnippet> NTPSnippet::CreateFromDictionary(
std::string snippet_str;
if (dict.GetString(kSnippet, &snippet_str))
snippet->set_snippet(snippet_str);
- // creationTimestampSec is a uint64, which is stored using strings
+ // The creation and expiry timestamps are uint64s which are stored as strings.
std::string creation_timestamp_str;
- if (dict.GetString(kPublishDate, &creation_timestamp_str)) {
- int64_t creation_timestamp = 0;
- if (!base::StringToInt64(creation_timestamp_str, &creation_timestamp)) {
- // Even if there's an error in the conversion, some garbage data may still
- // be written to the output var, so reset it
- creation_timestamp = 0;
- }
- snippet->set_publish_date(base::Time::UnixEpoch() +
- base::TimeDelta::FromSeconds(creation_timestamp));
- }
+ if (dict.GetString(kPublishDate, &creation_timestamp_str))
+ snippet->set_publish_date(TimeFromJsonString(creation_timestamp_str));
std::string expiry_timestamp_str;
- if (dict.GetString(kExpiryDate, &expiry_timestamp_str)) {
- int64_t expiry_timestamp = 0;
- if (!base::StringToInt64(expiry_timestamp_str, &expiry_timestamp)) {
- // Even if there's an error in the conversion, some garbage data may still
- // be written to the output var, so reset it
- expiry_timestamp = 0;
- }
- snippet->set_expiry_date(base::Time::UnixEpoch() +
- base::TimeDelta::FromSeconds(expiry_timestamp));
- }
+ if (dict.GetString(kExpiryDate, &expiry_timestamp_str))
+ snippet->set_expiry_date(TimeFromJsonString(expiry_timestamp_str));
return snippet;
}
@@ -97,18 +81,28 @@ std::unique_ptr<base::DictionaryValue> NTPSnippet::ToDictionary() const {
dict->SetString(kSalientImageUrl, salient_image_url_.spec());
if (!snippet_.empty())
dict->SetString(kSnippet, snippet_);
- if (!publish_date_.is_null()) {
- dict->SetString(kPublishDate,
- base::Int64ToString(
- (publish_date_ - base::Time::UnixEpoch()).InSeconds()));
- }
- if (!expiry_date_.is_null()) {
- dict->SetString(kExpiryDate,
- base::Int64ToString(
- (expiry_date_ - base::Time::UnixEpoch()).InSeconds()));
- }
+ if (!publish_date_.is_null())
+ dict->SetString(kPublishDate, TimeToJsonString(publish_date_));
+ if (!expiry_date_.is_null())
+ dict->SetString(kExpiryDate, TimeToJsonString(expiry_date_));
return dict;
}
+// static
+base::Time NTPSnippet::TimeFromJsonString(const std::string& timestamp_str) {
+ int64_t timestamp;
+ if (!base::StringToInt64(timestamp_str, &timestamp)) {
+ // Even if there's an error in the conversion, some garbage data may still
+ // be written to the output var, so reset it.
+ timestamp = 0;
+ }
+ return base::Time::UnixEpoch() + base::TimeDelta::FromSeconds(timestamp);
+}
+
+// static
+std::string NTPSnippet::TimeToJsonString(const base::Time& time) {
+ return base::Int64ToString((time - base::Time::UnixEpoch()).InSeconds());
+}
+
} // namespace ntp_snippets

Powered by Google App Engine
This is Rietveld 408576698