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

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

Issue 1743333002: [NTP Snippets] Implement snippets expiry (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@snippets_persist
Patch Set: review Created 4 years, 9 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/values.h" 7 #include "base/values.h"
8 8
9 namespace { 9 namespace {
10 10
(...skipping 13 matching lines...) Expand all
24 NTPSnippet::NTPSnippet(const GURL& url) : url_(url) { 24 NTPSnippet::NTPSnippet(const GURL& url) : url_(url) {
25 DCHECK(url_.is_valid()); 25 DCHECK(url_.is_valid());
26 } 26 }
27 27
28 NTPSnippet::~NTPSnippet() {} 28 NTPSnippet::~NTPSnippet() {}
29 29
30 // static 30 // static
31 scoped_ptr<NTPSnippet> NTPSnippet::CreateFromDictionary( 31 scoped_ptr<NTPSnippet> NTPSnippet::CreateFromDictionary(
32 const base::DictionaryValue& dict) { 32 const base::DictionaryValue& dict) {
33 // Need at least the url. 33 // Need at least the url.
34 std::string url; 34 std::string url_str;
35 if (!dict.GetString("url", &url)) 35 if (!dict.GetString("url", &url_str))
36 return nullptr;
37 GURL url(url_str);
38 if (!url.is_valid())
36 return nullptr; 39 return nullptr;
37 40
38 // TODO(treib,noyau): Need to check that the URL is valid first, or remove 41 scoped_ptr<NTPSnippet> snippet(new NTPSnippet(url));
39 // the DCHECK in the constructor.
40 scoped_ptr<NTPSnippet> snippet(new NTPSnippet(GURL(url)));
41 42
42 std::string site_title; 43 std::string site_title;
43 if (dict.GetString(kSiteTitle, &site_title)) 44 if (dict.GetString(kSiteTitle, &site_title))
44 snippet->set_site_title(site_title); 45 snippet->set_site_title(site_title);
45 std::string title; 46 std::string title;
46 if (dict.GetString(kTitle, &title)) 47 if (dict.GetString(kTitle, &title))
47 snippet->set_title(title); 48 snippet->set_title(title);
48 std::string favicon_url; 49 std::string favicon_url;
49 if (dict.GetString(kFaviconUrl, &favicon_url)) 50 if (dict.GetString(kFaviconUrl, &favicon_url))
50 snippet->set_favicon_url(GURL(favicon_url)); 51 snippet->set_favicon_url(GURL(favicon_url));
51 std::string salient_image_url; 52 std::string salient_image_url;
52 if (dict.GetString(kSalientImageUrl, &salient_image_url)) 53 if (dict.GetString(kSalientImageUrl, &salient_image_url))
53 snippet->set_salient_image_url(GURL(salient_image_url)); 54 snippet->set_salient_image_url(GURL(salient_image_url));
54 std::string snippet_str; 55 std::string snippet_str;
55 if (dict.GetString(kSnippet, &snippet_str)) 56 if (dict.GetString(kSnippet, &snippet_str))
56 snippet->set_snippet(snippet_str); 57 snippet->set_snippet(snippet_str);
57 int creation_timestamp; 58 int creation_timestamp;
58 if (dict.GetInteger(kPublishDate, &creation_timestamp)) { 59 if (dict.GetInteger(kPublishDate, &creation_timestamp)) {
59 snippet->set_publish_date(base::Time::UnixEpoch() + 60 snippet->set_publish_date(base::Time::UnixEpoch() +
60 base::TimeDelta::FromSeconds(creation_timestamp)); 61 base::TimeDelta::FromSeconds(creation_timestamp));
61 } 62 }
62 // TODO: Dates in json? 63 int expiry_timestamp;
64 if (dict.GetInteger(kExpiryDate, &expiry_timestamp)) {
65 snippet->set_expiry_date(base::Time::UnixEpoch() +
66 base::TimeDelta::FromSeconds(expiry_timestamp));
67 }
63 68
64 return snippet; 69 return snippet;
65 } 70 }
66 71
67 scoped_ptr<base::DictionaryValue> NTPSnippet::ToDictionary() const { 72 scoped_ptr<base::DictionaryValue> NTPSnippet::ToDictionary() const {
68 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue); 73 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
69 74
70 dict->SetString(kUrl, url_.spec()); 75 dict->SetString(kUrl, url_.spec());
71 if (!site_title_.empty()) 76 if (!site_title_.empty())
72 dict->SetString(kSiteTitle, site_title_); 77 dict->SetString(kSiteTitle, site_title_);
(...skipping 11 matching lines...) Expand all
84 } 89 }
85 if (!expiry_date_.is_null()) { 90 if (!expiry_date_.is_null()) {
86 dict->SetInteger(kExpiryDate, 91 dict->SetInteger(kExpiryDate,
87 (expiry_date_ - base::Time::UnixEpoch()).InSeconds()); 92 (expiry_date_ - base::Time::UnixEpoch()).InSeconds());
88 } 93 }
89 94
90 return dict; 95 return dict;
91 } 96 }
92 97
93 } // namespace ntp_snippets 98 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698