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_; } |
41 | 52 |
42 // Subtitle to identify the site the snippet is from. | |
43 const std::string& site_title() const { return site_title_; } | |
44 void set_site_title(const std::string& site_title) { | |
45 site_title_ = site_title; | |
46 } | |
47 | |
48 // Favicon for the site. Do not use to directly retrieve the favicon. | |
49 const GURL& favicon_url() const { return favicon_url_; } | |
50 void set_favicon_url(const GURL& favicon_url) { favicon_url_ = favicon_url; } | |
51 | |
52 // Title of the snippet. | 53 // Title of the snippet. |
53 const std::string& title() const { return title_; } | 54 const std::string& title() const { return title_; } |
54 void set_title(const std::string& title) { title_ = title; } | 55 void set_title(const std::string& title) { title_ = title; } |
55 | 56 |
56 // Summary or relevant extract from the content. | 57 // Summary or relevant extract from the content. |
57 const std::string& snippet() const { return snippet_; } | 58 const std::string& snippet() const { return snippet_; } |
58 void set_snippet(const std::string& snippet) { snippet_ = snippet; } | 59 void set_snippet(const std::string& snippet) { snippet_ = snippet; } |
59 | 60 |
60 // Link to an image representative of the content. Do not fetch this image | 61 // Link to an image representative of the content. Do not fetch this image |
61 // directly. If initialized by CreateFromDictionary() the relevant key is | 62 // directly. If initialized by CreateFromDictionary() the relevant key is |
(...skipping 10 matching lines...) Expand all Loading... |
72 publish_date_ = publish_date; | 73 publish_date_ = publish_date; |
73 } | 74 } |
74 | 75 |
75 // After this expiration date this snippet should no longer be presented to | 76 // After this expiration date this snippet should no longer be presented to |
76 // the user. | 77 // the user. |
77 const base::Time& expiry_date() const { return expiry_date_; } | 78 const base::Time& expiry_date() const { return expiry_date_; } |
78 void set_expiry_date(const base::Time& expiry_date) { | 79 void set_expiry_date(const base::Time& expiry_date) { |
79 expiry_date_ = expiry_date; | 80 expiry_date_ = expiry_date; |
80 } | 81 } |
81 | 82 |
82 const GURL& amp_url() const { return amp_url_; } | 83 size_t source_index() const { return best_source_index_; } |
83 void set_amp_url(const GURL& amp_url) { amp_url_ = amp_url; } | 84 void set_source_index(size_t index) { best_source_index_ = index; } |
| 85 |
| 86 // We should never construct an NTPSnippet object if we don't have any sources |
| 87 // so this should never fail |
| 88 const SnippetSource& best_source() const { |
| 89 return sources_[best_source_index_]; |
| 90 } |
| 91 |
| 92 const std::vector<SnippetSource>& sources() const { return sources_; } |
| 93 void add_source(const SnippetSource& source) { sources_.push_back(source); } |
| 94 |
| 95 // If this snippet has all the data we need to show a full card to the user |
| 96 bool is_complete() const { |
| 97 return url().is_valid() && !sources().empty() && !title().empty() && |
| 98 !snippet().empty() && salient_image_url().is_valid() && |
| 99 !publish_date().is_null() && !expiry_date().is_null() && |
| 100 !best_source().publisher_name.empty(); |
| 101 } |
84 | 102 |
85 // Public for testing. | 103 // Public for testing. |
86 static base::Time TimeFromJsonString(const std::string& timestamp_str); | 104 static base::Time TimeFromJsonString(const std::string& timestamp_str); |
87 static std::string TimeToJsonString(const base::Time& time); | 105 static std::string TimeToJsonString(const base::Time& time); |
88 | 106 |
89 private: | 107 private: |
90 const GURL url_; | 108 GURL url_; |
91 std::string site_title_; | |
92 std::string title_; | 109 std::string title_; |
93 GURL favicon_url_; | |
94 GURL salient_image_url_; | 110 GURL salient_image_url_; |
95 std::string snippet_; | 111 std::string snippet_; |
96 base::Time publish_date_; | 112 base::Time publish_date_; |
97 base::Time expiry_date_; | 113 base::Time expiry_date_; |
98 GURL amp_url_; | 114 GURL amp_url_; |
| 115 size_t best_source_index_; |
| 116 |
| 117 std::vector<SnippetSource> sources_; |
99 | 118 |
100 DISALLOW_COPY_AND_ASSIGN(NTPSnippet); | 119 DISALLOW_COPY_AND_ASSIGN(NTPSnippet); |
101 }; | 120 }; |
102 | 121 |
103 } // namespace ntp_snippets | 122 } // namespace ntp_snippets |
104 | 123 |
105 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPET_H_ | 124 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPET_H_ |
OLD | NEW |