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

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

Issue 1921553004: Add favicon and publisher name to snippet cards (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Code review comments. Will address test issues in another update. Created 4 years, 7 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 #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 void set_url(const GURL& url) { url_ = url; }
Marc Treib 2016/04/28 08:50:14 I think set_url isn't needed anymore now?
May 2016/04/28 18:01:53 Done.
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 53
52 // Title of the snippet. 54 // Title of the snippet.
53 const std::string& title() const { return title_; } 55 const std::string& title() const { return title_; }
54 void set_title(const std::string& title) { title_ = title; } 56 void set_title(const std::string& title) { title_ = title; }
55 57
56 // Summary or relevant extract from the content. 58 // Summary or relevant extract from the content.
57 const std::string& snippet() const { return snippet_; } 59 const std::string& snippet() const { return snippet_; }
58 void set_snippet(const std::string& snippet) { snippet_ = snippet; } 60 void set_snippet(const std::string& snippet) { snippet_ = snippet; }
59 61
60 // Link to an image representative of the content. Do not fetch this image 62 // Link to an image representative of the content. Do not fetch this image
(...skipping 11 matching lines...) Expand all
72 publish_date_ = publish_date; 74 publish_date_ = publish_date;
73 } 75 }
74 76
75 // After this expiration date this snippet should no longer be presented to 77 // After this expiration date this snippet should no longer be presented to
76 // the user. 78 // the user.
77 const base::Time& expiry_date() const { return expiry_date_; } 79 const base::Time& expiry_date() const { return expiry_date_; }
78 void set_expiry_date(const base::Time& expiry_date) { 80 void set_expiry_date(const base::Time& expiry_date) {
79 expiry_date_ = expiry_date; 81 expiry_date_ = expiry_date;
80 } 82 }
81 83
82 const GURL& amp_url() const { return amp_url_; } 84 size_t source_index() const { return best_source_index_; }
83 void set_amp_url(const GURL& amp_url) { amp_url_ = amp_url; } 85 void set_source_index(size_t index) { best_source_index_ = index; }
86
87 // We should never construct an NTPSnippet object if we don't have any sources
88 // so this should never fail
89 const SnippetSource& best_source() const {
90 return sources_[best_source_index_];
91 }
92
93 const std::vector<SnippetSource>& sources() const { return sources_; }
94 void add_source(SnippetSource& source) { sources_.push_back(source); }
Bernhard Bauer 2016/04/28 14:19:20 Const reference
May 2016/04/28 18:01:53 Done.
95
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_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698