OLD | NEW |
---|---|
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 #ifndef COMPONENTS_NTP_SNIPPETS_NTP_SNIPPET_H_ | 5 #ifndef COMPONENTS_NTP_SNIPPETS_NTP_SNIPPET_H_ |
6 #define COMPONENTS_NTP_SNIPPETS_NTP_SNIPPET_H_ | 6 #define COMPONENTS_NTP_SNIPPETS_NTP_SNIPPET_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | |
10 | 11 |
11 #include "base/macros.h" | 12 #include "base/macros.h" |
12 #include "base/time/time.h" | 13 #include "base/time/time.h" |
13 #include "url/gurl.h" | 14 #include "url/gurl.h" |
14 | 15 |
15 namespace base { | 16 namespace base { |
16 class DictionaryValue; | 17 class DictionaryValue; |
17 } | 18 } |
18 | 19 |
19 namespace ntp_snippets { | 20 namespace ntp_snippets { |
20 | 21 |
22 struct SnippetSource { | |
23 SnippetSource(const GURL& url, | |
24 const std::string& publisher_name, | |
25 const GURL& amp_url) | |
26 : url(url), publisher_name(publisher_name), amp_url(amp_url) {} | |
27 GURL url; | |
28 std::string publisher_name; | |
29 GURL amp_url; | |
30 }; | |
31 | |
21 // Stores and vend fresh content data for the NTP. This is a dumb class with no | 32 // Stores and vend fresh content data for the NTP. This is a dumb class with no |
22 // smarts at all, all the logic is in the service. | 33 // smarts at all, all the logic is in the service. |
23 class NTPSnippet { | 34 class NTPSnippet { |
24 public: | 35 public: |
25 // Creates a new snippet with the given URL. URL must be valid. | 36 // Creates a new snippet with the given URL. URL must be valid. |
26 NTPSnippet(const GURL& url); | 37 NTPSnippet(const GURL& url); |
27 | 38 |
28 ~NTPSnippet(); | 39 ~NTPSnippet(); |
29 | 40 |
30 // Creates an NTPSnippet from a dictionary. Returns a null pointer if the | 41 // Creates an NTPSnippet from a dictionary. Returns a null pointer if the |
31 // dictionary doesn't contain at least a url. The keys in the dictionary are | 42 // dictionary doesn't contain at least a url. The keys in the dictionary are |
32 // expected to be the same as the property name, with exceptions documented in | 43 // expected to be the same as the property name, with exceptions documented in |
33 // the property comment. | 44 // the property comment. |
34 static std::unique_ptr<NTPSnippet> CreateFromDictionary( | 45 static std::unique_ptr<NTPSnippet> CreateFromDictionary( |
35 const base::DictionaryValue& dict); | 46 const base::DictionaryValue& dict); |
36 | 47 |
37 std::unique_ptr<base::DictionaryValue> ToDictionary() const; | 48 std::unique_ptr<base::DictionaryValue> ToDictionary() const; |
38 | 49 |
39 // URL of the page described by this snippet. | 50 // URL of the page described by this snippet. |
40 const GURL& url() const { return url_; } | 51 const GURL& url() const { return url_; } |
52 void set_url(const GURL& url) { url_ = url; } | |
41 | 53 |
42 // Subtitle to identify the site the snippet is from. | 54 // Subtitle to identify the site the snippet is from. |
43 const std::string& site_title() const { return site_title_; } | 55 const std::string& site_title() const { return site_title_; } |
44 void set_site_title(const std::string& site_title) { | 56 void set_site_title(const std::string& site_title) { |
45 site_title_ = site_title; | 57 site_title_ = site_title; |
46 } | 58 } |
47 | 59 |
48 // Favicon for the site. Do not use to directly retrieve the favicon. | 60 // Favicon for the site. Do not use to directly retrieve the favicon. |
61 // Optional data | |
Marc Treib
2016/04/27 07:03:09
So far all of the fields, except for the URL, have
May
2016/04/27 16:45:09
Removed favicon actually, since we don't get that
Marc Treib
2016/04/28 08:50:13
Okay, makes sense, thanks!
| |
49 const GURL& favicon_url() const { return favicon_url_; } | 62 const GURL& favicon_url() const { return favicon_url_; } |
50 void set_favicon_url(const GURL& favicon_url) { favicon_url_ = favicon_url; } | 63 void set_favicon_url(const GURL& favicon_url) { favicon_url_ = favicon_url; } |
51 | 64 |
52 // Title of the snippet. | 65 // Title of the snippet. |
53 const std::string& title() const { return title_; } | 66 const std::string& title() const { return title_; } |
54 void set_title(const std::string& title) { title_ = title; } | 67 void set_title(const std::string& title) { title_ = title; } |
55 | 68 |
56 // Summary or relevant extract from the content. | 69 // Summary or relevant extract from the content. |
57 const std::string& snippet() const { return snippet_; } | 70 const std::string& snippet() const { return snippet_; } |
58 void set_snippet(const std::string& snippet) { snippet_ = snippet; } | 71 void set_snippet(const std::string& snippet) { snippet_ = snippet; } |
(...skipping 13 matching lines...) Expand all Loading... | |
72 publish_date_ = publish_date; | 85 publish_date_ = publish_date; |
73 } | 86 } |
74 | 87 |
75 // After this expiration date this snippet should no longer be presented to | 88 // After this expiration date this snippet should no longer be presented to |
76 // the user. | 89 // the user. |
77 const base::Time& expiry_date() const { return expiry_date_; } | 90 const base::Time& expiry_date() const { return expiry_date_; } |
78 void set_expiry_date(const base::Time& expiry_date) { | 91 void set_expiry_date(const base::Time& expiry_date) { |
79 expiry_date_ = expiry_date; | 92 expiry_date_ = expiry_date; |
80 } | 93 } |
81 | 94 |
95 // Optional data | |
82 const GURL& amp_url() const { return amp_url_; } | 96 const GURL& amp_url() const { return amp_url_; } |
83 void set_amp_url(const GURL& amp_url) { amp_url_ = amp_url; } | 97 void set_amp_url(const GURL& amp_url) { amp_url_ = amp_url; } |
84 | 98 |
99 const std::vector<SnippetSource> get_sources() const { return sources_; } | |
Marc Treib
2016/04/27 07:03:09
Return by ref? Also just "sources", no "get".
May
2016/04/27 16:45:08
Done.
| |
100 | |
101 bool is_valid_snippet() const { | |
Marc Treib
2016/04/27 07:03:09
nit: just "is_valid" please (though IMO "valid" mi
May
2016/04/27 16:45:09
Changed to complete(). Better..?
Marc Treib
2016/04/28 08:50:13
Yes, thank you!
| |
102 return url_.is_valid() && !site_title_.empty() && !title_.empty() && | |
103 !snippet_.empty() && salient_image_url_.is_valid() && | |
104 !publish_date_.is_null() && !expiry_date_.is_null(); | |
105 } | |
106 | |
85 // Public for testing. | 107 // Public for testing. |
86 static base::Time TimeFromJsonString(const std::string& timestamp_str); | 108 static base::Time TimeFromJsonString(const std::string& timestamp_str); |
87 static std::string TimeToJsonString(const base::Time& time); | 109 static std::string TimeToJsonString(const base::Time& time); |
88 | 110 |
89 private: | 111 private: |
90 const GURL url_; | 112 GURL url_; |
91 std::string site_title_; | 113 std::string site_title_; |
92 std::string title_; | 114 std::string title_; |
93 GURL favicon_url_; | 115 GURL favicon_url_; |
94 GURL salient_image_url_; | 116 GURL salient_image_url_; |
95 std::string snippet_; | 117 std::string snippet_; |
96 base::Time publish_date_; | 118 base::Time publish_date_; |
97 base::Time expiry_date_; | 119 base::Time expiry_date_; |
98 GURL amp_url_; | 120 GURL amp_url_; |
Marc Treib
2016/04/27 07:03:09
A few fields are now effectively stored twice. Can
May
2016/04/27 16:45:08
Done. I kept url_ because we do have a url from Ch
Marc Treib
2016/04/28 08:50:13
Alright, fair enough.
| |
99 | 121 |
122 std::vector<SnippetSource> sources_; | |
Marc Treib
2016/04/27 07:03:09
Or in fact: Do we actually need to store all the s
May
2016/04/27 16:45:08
I do it so that we can effectively de-dupe later a
Marc Treib
2016/04/28 08:50:13
Acknowledged.
| |
123 | |
100 DISALLOW_COPY_AND_ASSIGN(NTPSnippet); | 124 DISALLOW_COPY_AND_ASSIGN(NTPSnippet); |
101 }; | 125 }; |
102 | 126 |
103 } // namespace ntp_snippets | 127 } // namespace ntp_snippets |
104 | 128 |
105 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPET_H_ | 129 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPET_H_ |
OLD | NEW |