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

Side by Side 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 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_snippet.h" 5 #include "components/ntp_snippets/ntp_snippet.h"
6 6
7 #include "base/strings/string_number_conversions.h" 7 #include "base/strings/string_number_conversions.h"
8 #include "base/values.h" 8 #include "base/values.h"
9 9
10 namespace { 10 namespace {
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
49 snippet->set_title(title); 49 snippet->set_title(title);
50 std::string favicon_url; 50 std::string favicon_url;
51 if (dict.GetString(kFaviconUrl, &favicon_url)) 51 if (dict.GetString(kFaviconUrl, &favicon_url))
52 snippet->set_favicon_url(GURL(favicon_url)); 52 snippet->set_favicon_url(GURL(favicon_url));
53 std::string salient_image_url; 53 std::string salient_image_url;
54 if (dict.GetString(kSalientImageUrl, &salient_image_url)) 54 if (dict.GetString(kSalientImageUrl, &salient_image_url))
55 snippet->set_salient_image_url(GURL(salient_image_url)); 55 snippet->set_salient_image_url(GURL(salient_image_url));
56 std::string snippet_str; 56 std::string snippet_str;
57 if (dict.GetString(kSnippet, &snippet_str)) 57 if (dict.GetString(kSnippet, &snippet_str))
58 snippet->set_snippet(snippet_str); 58 snippet->set_snippet(snippet_str);
59 // creationTimestampSec is a uint64, which is stored using strings 59 // The creation and expiry timestamps are uint64s which are stored as strings.
60 std::string creation_timestamp_str; 60 std::string creation_timestamp_str;
61 if (dict.GetString(kPublishDate, &creation_timestamp_str)) { 61 if (dict.GetString(kPublishDate, &creation_timestamp_str))
62 int64_t creation_timestamp = 0; 62 snippet->set_publish_date(TimeFromJsonString(creation_timestamp_str));
63 if (!base::StringToInt64(creation_timestamp_str, &creation_timestamp)) {
64 // Even if there's an error in the conversion, some garbage data may still
65 // be written to the output var, so reset it
66 creation_timestamp = 0;
67 }
68 snippet->set_publish_date(base::Time::UnixEpoch() +
69 base::TimeDelta::FromSeconds(creation_timestamp));
70 }
71 std::string expiry_timestamp_str; 63 std::string expiry_timestamp_str;
72 if (dict.GetString(kExpiryDate, &expiry_timestamp_str)) { 64 if (dict.GetString(kExpiryDate, &expiry_timestamp_str))
73 int64_t expiry_timestamp = 0; 65 snippet->set_expiry_date(TimeFromJsonString(expiry_timestamp_str));
74 if (!base::StringToInt64(expiry_timestamp_str, &expiry_timestamp)) {
75 // Even if there's an error in the conversion, some garbage data may still
76 // be written to the output var, so reset it
77 expiry_timestamp = 0;
78 }
79 snippet->set_expiry_date(base::Time::UnixEpoch() +
80 base::TimeDelta::FromSeconds(expiry_timestamp));
81 }
82 66
83 return snippet; 67 return snippet;
84 } 68 }
85 69
86 std::unique_ptr<base::DictionaryValue> NTPSnippet::ToDictionary() const { 70 std::unique_ptr<base::DictionaryValue> NTPSnippet::ToDictionary() const {
87 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue); 71 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
88 72
89 dict->SetString(kUrl, url_.spec()); 73 dict->SetString(kUrl, url_.spec());
90 if (!site_title_.empty()) 74 if (!site_title_.empty())
91 dict->SetString(kSiteTitle, site_title_); 75 dict->SetString(kSiteTitle, site_title_);
92 if (!title_.empty()) 76 if (!title_.empty())
93 dict->SetString(kTitle, title_); 77 dict->SetString(kTitle, title_);
94 if (favicon_url_.is_valid()) 78 if (favicon_url_.is_valid())
95 dict->SetString(kFaviconUrl, favicon_url_.spec()); 79 dict->SetString(kFaviconUrl, favicon_url_.spec());
96 if (salient_image_url_.is_valid()) 80 if (salient_image_url_.is_valid())
97 dict->SetString(kSalientImageUrl, salient_image_url_.spec()); 81 dict->SetString(kSalientImageUrl, salient_image_url_.spec());
98 if (!snippet_.empty()) 82 if (!snippet_.empty())
99 dict->SetString(kSnippet, snippet_); 83 dict->SetString(kSnippet, snippet_);
100 if (!publish_date_.is_null()) { 84 if (!publish_date_.is_null())
101 dict->SetString(kPublishDate, 85 dict->SetString(kPublishDate, TimeToJsonString(publish_date_));
102 base::Int64ToString( 86 if (!expiry_date_.is_null())
103 (publish_date_ - base::Time::UnixEpoch()).InSeconds())); 87 dict->SetString(kExpiryDate, TimeToJsonString(expiry_date_));
104 }
105 if (!expiry_date_.is_null()) {
106 dict->SetString(kExpiryDate,
107 base::Int64ToString(
108 (expiry_date_ - base::Time::UnixEpoch()).InSeconds()));
109 }
110 88
111 return dict; 89 return dict;
112 } 90 }
113 91
92 // static
93 base::Time NTPSnippet::TimeFromJsonString(const std::string& timestamp_str) {
94 int64_t timestamp;
95 if (!base::StringToInt64(timestamp_str, &timestamp)) {
96 // Even if there's an error in the conversion, some garbage data may still
97 // be written to the output var, so reset it.
98 timestamp = 0;
99 }
100 return base::Time::UnixEpoch() + base::TimeDelta::FromSeconds(timestamp);
101 }
102
103 // static
104 std::string NTPSnippet::TimeToJsonString(const base::Time& time) {
105 return base::Int64ToString((time - base::Time::UnixEpoch()).InSeconds());
106 }
107
114 } // namespace ntp_snippets 108 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698