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

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

Issue 1737563003: [NTP Snippets] Persist the set of snippets in a pref (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@snippets_merge
Patch Set: noyau 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
« no previous file with comments | « components/ntp_snippets/ntp_snippet.h ('k') | components/ntp_snippets/ntp_snippets_service.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 #include "base/values.h" 7 #include "base/values.h"
7 8
9 namespace {
10
11 const char kUrl[] = "url";
12 const char kSiteTitle[] = "site_title";
13 const char kTitle[] = "title";
14 const char kFaviconUrl[] = "favicon_url";
15 const char kSalientImageUrl[] = "thumbnailUrl";
16 const char kSnippet[] = "snippet";
17 const char kPublishDate[] = "creationTimestampSec";
18 const char kExpiryDate[] = "expiryTimestampSec";
19
20 } // namespace
21
8 namespace ntp_snippets { 22 namespace ntp_snippets {
9 23
10 NTPSnippet::NTPSnippet(const GURL& url) : url_(url) { 24 NTPSnippet::NTPSnippet(const GURL& url) : url_(url) {
11 DCHECK(url_.is_valid() && !url.is_empty()); 25 DCHECK(url_.is_valid());
12 } 26 }
13 27
14 NTPSnippet::~NTPSnippet() {} 28 NTPSnippet::~NTPSnippet() {}
15 29
16 // static 30 // static
17 scoped_ptr<NTPSnippet> NTPSnippet::NTPSnippetFromDictionary( 31 scoped_ptr<NTPSnippet> NTPSnippet::CreateFromDictionary(
18 const base::DictionaryValue& dict) { 32 const base::DictionaryValue& dict) {
19 // Need at least the url. 33 // Need at least the url.
20 std::string url; 34 std::string url;
21 if (!dict.GetString("url", &url)) 35 if (!dict.GetString("url", &url))
22 return nullptr; 36 return nullptr;
23 37
38 // TODO(treib,noyau): Need to check that the URL is valid first, or remove
39 // the DCHECK in the constructor.
24 scoped_ptr<NTPSnippet> snippet(new NTPSnippet(GURL(url))); 40 scoped_ptr<NTPSnippet> snippet(new NTPSnippet(GURL(url)));
25 41
26 std::string site_title; 42 std::string site_title;
27 if (dict.GetString("site_title", &site_title)) 43 if (dict.GetString(kSiteTitle, &site_title))
28 snippet->set_site_title(site_title); 44 snippet->set_site_title(site_title);
45 std::string title;
46 if (dict.GetString(kTitle, &title))
47 snippet->set_title(title);
29 std::string favicon_url; 48 std::string favicon_url;
30 if (dict.GetString("favicon_url", &favicon_url)) 49 if (dict.GetString(kFaviconUrl, &favicon_url))
31 snippet->set_favicon_url(GURL(favicon_url)); 50 snippet->set_favicon_url(GURL(favicon_url));
32 std::string title; 51 std::string salient_image_url;
33 if (dict.GetString("title", &title)) 52 if (dict.GetString(kSalientImageUrl, &salient_image_url))
34 snippet->set_title(title); 53 snippet->set_salient_image_url(GURL(salient_image_url));
35 std::string snippet_str; 54 std::string snippet_str;
36 if (dict.GetString("snippet", &snippet_str)) 55 if (dict.GetString(kSnippet, &snippet_str))
37 snippet->set_snippet(snippet_str); 56 snippet->set_snippet(snippet_str);
38 std::string salient_image_url;
39 if (dict.GetString("thumbnailUrl", &salient_image_url))
40 snippet->set_salient_image_url(GURL(salient_image_url));
41 int creation_timestamp; 57 int creation_timestamp;
42 if (dict.GetInteger("creationTimestampSec", &creation_timestamp)) { 58 if (dict.GetInteger(kPublishDate, &creation_timestamp)) {
43 snippet->set_publish_date(base::Time::UnixEpoch() + 59 snippet->set_publish_date(base::Time::UnixEpoch() +
44 base::TimeDelta::FromSeconds(creation_timestamp)); 60 base::TimeDelta::FromSeconds(creation_timestamp));
45 } 61 }
46 // TODO: Dates in json? 62 // TODO: Dates in json?
47 63
48 return snippet; 64 return snippet;
49 } 65 }
50 66
67 scoped_ptr<base::DictionaryValue> NTPSnippet::ToDictionary() const {
68 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
69
70 dict->SetString(kUrl, url_.spec());
71 if (!site_title_.empty())
72 dict->SetString(kSiteTitle, site_title_);
73 if (!title_.empty())
74 dict->SetString(kTitle, title_);
75 if (favicon_url_.is_valid())
76 dict->SetString(kFaviconUrl, favicon_url_.spec());
77 if (salient_image_url_.is_valid())
78 dict->SetString(kSalientImageUrl, salient_image_url_.spec());
79 if (!snippet_.empty())
80 dict->SetString(kSnippet, snippet_);
81 if (!publish_date_.is_null()) {
82 dict->SetInteger(kPublishDate,
83 (publish_date_ - base::Time::UnixEpoch()).InSeconds());
84 }
85 if (!expiry_date_.is_null()) {
86 dict->SetInteger(kExpiryDate,
87 (expiry_date_ - base::Time::UnixEpoch()).InSeconds());
88 }
89
90 return dict;
91 }
92
51 } // namespace ntp_snippets 93 } // namespace ntp_snippets
OLDNEW
« no previous file with comments | « components/ntp_snippets/ntp_snippet.h ('k') | components/ntp_snippets/ntp_snippets_service.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698