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

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

Issue 2402323002: [NTP Snippets] Persist non-article remote suggestions in the DB (Closed)
Patch Set: review Created 4 years, 2 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 | « no previous file | components/ntp_snippets/remote/ntp_snippet.cc » ('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 #ifndef COMPONENTS_NTP_SNIPPETS_REMOTE_NTP_SNIPPET_H_ 5 #ifndef COMPONENTS_NTP_SNIPPETS_REMOTE_NTP_SNIPPET_H_
6 #define COMPONENTS_NTP_SNIPPETS_REMOTE_NTP_SNIPPET_H_ 6 #define COMPONENTS_NTP_SNIPPETS_REMOTE_NTP_SNIPPET_H_
7 7
8 #include <map> 8 #include <map>
9 #include <memory> 9 #include <memory>
10 #include <string> 10 #include <string>
11 #include <vector> 11 #include <vector>
12 12
13 #include "base/macros.h" 13 #include "base/macros.h"
14 #include "base/time/time.h" 14 #include "base/time/time.h"
15 #include "components/ntp_snippets/category.h"
16 #include "url/gurl.h" 15 #include "url/gurl.h"
17 16
18 namespace base { 17 namespace base {
19 class DictionaryValue; 18 class DictionaryValue;
20 } // namespace base 19 } // namespace base
21 20
22 namespace ntp_snippets { 21 namespace ntp_snippets {
23 22
23 extern const int kArticlesRemoteId;
24
24 class SnippetProto; 25 class SnippetProto;
25 26
26 struct SnippetSource { 27 struct SnippetSource {
27 SnippetSource(const GURL& url, 28 SnippetSource(const GURL& url,
28 const std::string& publisher_name, 29 const std::string& publisher_name,
29 const GURL& amp_url) 30 const GURL& amp_url)
30 : url(url), publisher_name(publisher_name), amp_url(amp_url) {} 31 : url(url), publisher_name(publisher_name), amp_url(amp_url) {}
31 GURL url; 32 GURL url;
32 std::string publisher_name; 33 std::string publisher_name;
33 GURL amp_url; 34 GURL amp_url;
34 }; 35 };
35 36
36 class NTPSnippet { 37 class NTPSnippet {
37 public: 38 public:
38 using PtrVector = std::vector<std::unique_ptr<NTPSnippet>>; 39 using PtrVector = std::vector<std::unique_ptr<NTPSnippet>>;
39 40
40 // Creates a new snippet with the given |id|. 41 // Creates a new snippet with the given |id|.
41 // Public for testing only - create snippets using the Create* methods below. 42 // Public for testing only - create snippets using the Create* methods below.
42 // TODO(treib): Make this private and add a CreateSnippetForTest? 43 // TODO(treib): Make this private and add a CreateSnippetForTest?
43 explicit NTPSnippet(const std::string& id); 44 NTPSnippet(const std::string& id, int remote_category_id);
44 45
45 ~NTPSnippet(); 46 ~NTPSnippet();
46 47
47 // Creates an NTPSnippet from a dictionary, as returned by Chrome Reader. 48 // Creates an NTPSnippet from a dictionary, as returned by Chrome Reader.
48 // Returns a null pointer if the dictionary doesn't correspond to a valid 49 // Returns a null pointer if the dictionary doesn't correspond to a valid
49 // snippet. The keys in the dictionary are expected to be the same as the 50 // snippet. The keys in the dictionary are expected to be the same as the
50 // property name, with exceptions documented in the property comment. 51 // property name, with exceptions documented in the property comment.
51 static std::unique_ptr<NTPSnippet> CreateFromChromeReaderDictionary( 52 static std::unique_ptr<NTPSnippet> CreateFromChromeReaderDictionary(
52 const base::DictionaryValue& dict); 53 const base::DictionaryValue& dict);
53 54
54 // Creates an NTPSnippet from a dictionary, as returned by Chrome Content 55 // Creates an NTPSnippet from a dictionary, as returned by Chrome Content
55 // Suggestions. Returns a null pointer if the dictionary doesn't correspond to 56 // Suggestions. Returns a null pointer if the dictionary doesn't correspond to
56 // a valid snippet. Maps field names to Chrome Reader field names. 57 // a valid snippet. Maps field names to Chrome Reader field names.
57 static std::unique_ptr<NTPSnippet> CreateFromContentSuggestionsDictionary( 58 static std::unique_ptr<NTPSnippet> CreateFromContentSuggestionsDictionary(
58 const base::DictionaryValue& dict); 59 const base::DictionaryValue& dict,
60 int remote_category_id);
59 61
60 // Creates an NTPSnippet from a protocol buffer. Returns a null pointer if the 62 // Creates an NTPSnippet from a protocol buffer. Returns a null pointer if the
61 // protocol buffer doesn't correspond to a valid snippet. 63 // protocol buffer doesn't correspond to a valid snippet.
62 static std::unique_ptr<NTPSnippet> CreateFromProto(const SnippetProto& proto); 64 static std::unique_ptr<NTPSnippet> CreateFromProto(const SnippetProto& proto);
63 65
64 // Creates a protocol buffer corresponding to this snippet, for persisting. 66 // Creates a protocol buffer corresponding to this snippet, for persisting.
65 SnippetProto ToProto() const; 67 SnippetProto ToProto() const;
66 68
67 // A unique ID for identifying the snippet. If initialized by 69 // A unique ID for identifying the snippet. If initialized by
68 // CreateFromChromeReaderDictionary() the relevant key is 'url'. 70 // CreateFromChromeReaderDictionary() the relevant key is 'url'.
69 // TODO(treib): For now, the ID has to be a valid URL spec, otherwise
70 // fetching the salient image will fail. See TODO in ntp_snippets_service.cc.
71 const std::string& id() const { return id_; } 71 const std::string& id() const { return id_; }
72 72
73 // Title of the snippet. 73 // Title of the snippet.
74 const std::string& title() const { return title_; } 74 const std::string& title() const { return title_; }
75 void set_title(const std::string& title) { title_ = title; } 75 void set_title(const std::string& title) { title_ = title; }
76 76
77 // Summary or relevant extract from the content. 77 // Summary or relevant extract from the content.
78 const std::string& snippet() const { return snippet_; } 78 const std::string& snippet() const { return snippet_; }
79 void set_snippet(const std::string& snippet) { snippet_ = snippet; } 79 void set_snippet(const std::string& snippet) { snippet_ = snippet; }
80 80
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
120 !publish_date().is_null() && !expiry_date().is_null() && 120 !publish_date().is_null() && !expiry_date().is_null() &&
121 !best_source().publisher_name.empty(); 121 !best_source().publisher_name.empty();
122 } 122 }
123 123
124 float score() const { return score_; } 124 float score() const { return score_; }
125 void set_score(float score) { score_ = score; } 125 void set_score(float score) { score_ = score; }
126 126
127 bool is_dismissed() const { return is_dismissed_; } 127 bool is_dismissed() const { return is_dismissed_; }
128 void set_dismissed(bool dismissed) { is_dismissed_ = dismissed; } 128 void set_dismissed(bool dismissed) { is_dismissed_ = dismissed; }
129 129
130 // The ID of the remote category this snippet belongs to, for use with
131 // CategoryFactory::FromRemoteCategory.
132 int remote_category_id() const { return remote_category_id_; }
133
130 // Public for testing. 134 // Public for testing.
131 static base::Time TimeFromJsonString(const std::string& timestamp_str); 135 static base::Time TimeFromJsonString(const std::string& timestamp_str);
132 static std::string TimeToJsonString(const base::Time& time); 136 static std::string TimeToJsonString(const base::Time& time);
133 137
134 private: 138 private:
135 void FindBestSource(); 139 void FindBestSource();
136 140
137 std::string id_; 141 std::string id_;
138 std::string title_; 142 std::string title_;
139 GURL salient_image_url_; 143 GURL salient_image_url_;
140 std::string snippet_; 144 std::string snippet_;
141 base::Time publish_date_; 145 base::Time publish_date_;
142 base::Time expiry_date_; 146 base::Time expiry_date_;
143 float score_; 147 float score_;
144 bool is_dismissed_; 148 bool is_dismissed_;
149 int remote_category_id_;
145 150
146 size_t best_source_index_; 151 size_t best_source_index_;
147 152
148 std::vector<SnippetSource> sources_; 153 std::vector<SnippetSource> sources_;
149 154
150 DISALLOW_COPY_AND_ASSIGN(NTPSnippet); 155 DISALLOW_COPY_AND_ASSIGN(NTPSnippet);
151 }; 156 };
152 157
153 } // namespace ntp_snippets 158 } // namespace ntp_snippets
154 159
155 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_NTP_SNIPPET_H_ 160 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_NTP_SNIPPET_H_
OLDNEW
« no previous file with comments | « no previous file | components/ntp_snippets/remote/ntp_snippet.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698