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

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: . 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
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
24 class SnippetProto; 23 class SnippetProto;
25 24
26 struct SnippetSource { 25 struct SnippetSource {
27 SnippetSource(const GURL& url, 26 SnippetSource(const GURL& url,
28 const std::string& publisher_name, 27 const std::string& publisher_name,
29 const GURL& amp_url) 28 const GURL& amp_url)
30 : url(url), publisher_name(publisher_name), amp_url(amp_url) {} 29 : url(url), publisher_name(publisher_name), amp_url(amp_url) {}
31 GURL url; 30 GURL url;
32 std::string publisher_name; 31 std::string publisher_name;
33 GURL amp_url; 32 GURL amp_url;
34 }; 33 };
35 34
36 class NTPSnippet { 35 class NTPSnippet {
37 public: 36 public:
38 using PtrVector = std::vector<std::unique_ptr<NTPSnippet>>; 37 using PtrVector = std::vector<std::unique_ptr<NTPSnippet>>;
39 38
40 // Creates a new snippet with the given |id|. 39 // Creates a new snippet with the given |id|.
41 // Public for testing only - create snippets using the Create* methods below. 40 // Public for testing only - create snippets using the Create* methods below.
42 // TODO(treib): Make this private and add a CreateSnippetForTest? 41 // TODO(treib): Make this private and add a CreateSnippetForTest?
43 explicit NTPSnippet(const std::string& id); 42 explicit NTPSnippet(const std::string& id, int remote_category_id);
Bernhard Bauer 2016/10/10 13:54:54 Nit: no explicit anymore
Marc Treib 2016/10/10 15:54:47 Done.
44 43
45 ~NTPSnippet(); 44 ~NTPSnippet();
46 45
47 // Creates an NTPSnippet from a dictionary, as returned by Chrome Reader. 46 // 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 47 // 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 48 // snippet. The keys in the dictionary are expected to be the same as the
50 // property name, with exceptions documented in the property comment. 49 // property name, with exceptions documented in the property comment.
51 static std::unique_ptr<NTPSnippet> CreateFromChromeReaderDictionary( 50 static std::unique_ptr<NTPSnippet> CreateFromChromeReaderDictionary(
52 const base::DictionaryValue& dict); 51 const base::DictionaryValue& dict);
53 52
54 // Creates an NTPSnippet from a dictionary, as returned by Chrome Content 53 // Creates an NTPSnippet from a dictionary, as returned by Chrome Content
55 // Suggestions. Returns a null pointer if the dictionary doesn't correspond to 54 // Suggestions. Returns a null pointer if the dictionary doesn't correspond to
56 // a valid snippet. Maps field names to Chrome Reader field names. 55 // a valid snippet. Maps field names to Chrome Reader field names.
57 static std::unique_ptr<NTPSnippet> CreateFromContentSuggestionsDictionary( 56 static std::unique_ptr<NTPSnippet> CreateFromContentSuggestionsDictionary(
58 const base::DictionaryValue& dict); 57 const base::DictionaryValue& dict,
58 int remote_category_id);
59 59
60 // Creates an NTPSnippet from a protocol buffer. Returns a null pointer if the 60 // Creates an NTPSnippet from a protocol buffer. Returns a null pointer if the
61 // protocol buffer doesn't correspond to a valid snippet. 61 // protocol buffer doesn't correspond to a valid snippet.
62 static std::unique_ptr<NTPSnippet> CreateFromProto(const SnippetProto& proto); 62 static std::unique_ptr<NTPSnippet> CreateFromProto(const SnippetProto& proto);
63 63
64 // Creates a protocol buffer corresponding to this snippet, for persisting. 64 // Creates a protocol buffer corresponding to this snippet, for persisting.
65 SnippetProto ToProto() const; 65 SnippetProto ToProto() const;
66 66
67 // A unique ID for identifying the snippet. If initialized by 67 // A unique ID for identifying the snippet. If initialized by
68 // CreateFromChromeReaderDictionary() the relevant key is 'url'. 68 // 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_; } 69 const std::string& id() const { return id_; }
72 70
73 // Title of the snippet. 71 // Title of the snippet.
74 const std::string& title() const { return title_; } 72 const std::string& title() const { return title_; }
75 void set_title(const std::string& title) { title_ = title; } 73 void set_title(const std::string& title) { title_ = title; }
76 74
77 // Summary or relevant extract from the content. 75 // Summary or relevant extract from the content.
78 const std::string& snippet() const { return snippet_; } 76 const std::string& snippet() const { return snippet_; }
79 void set_snippet(const std::string& snippet) { snippet_ = snippet; } 77 void set_snippet(const std::string& snippet) { snippet_ = snippet; }
80 78
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
120 !publish_date().is_null() && !expiry_date().is_null() && 118 !publish_date().is_null() && !expiry_date().is_null() &&
121 !best_source().publisher_name.empty(); 119 !best_source().publisher_name.empty();
122 } 120 }
123 121
124 float score() const { return score_; } 122 float score() const { return score_; }
125 void set_score(float score) { score_ = score; } 123 void set_score(float score) { score_ = score; }
126 124
127 bool is_dismissed() const { return is_dismissed_; } 125 bool is_dismissed() const { return is_dismissed_; }
128 void set_dismissed(bool dismissed) { is_dismissed_ = dismissed; } 126 void set_dismissed(bool dismissed) { is_dismissed_ = dismissed; }
129 127
128 // The ID of the remote category this snippet belongs to, for use with
129 // CategoryFactory::FromRemoteCategory.
130 int remote_category_id() const { return remote_category_id_; }
131
130 // Public for testing. 132 // Public for testing.
131 static base::Time TimeFromJsonString(const std::string& timestamp_str); 133 static base::Time TimeFromJsonString(const std::string& timestamp_str);
132 static std::string TimeToJsonString(const base::Time& time); 134 static std::string TimeToJsonString(const base::Time& time);
133 135
134 private: 136 private:
135 void FindBestSource(); 137 void FindBestSource();
136 138
137 std::string id_; 139 std::string id_;
138 std::string title_; 140 std::string title_;
139 GURL salient_image_url_; 141 GURL salient_image_url_;
140 std::string snippet_; 142 std::string snippet_;
141 base::Time publish_date_; 143 base::Time publish_date_;
142 base::Time expiry_date_; 144 base::Time expiry_date_;
143 float score_; 145 float score_;
144 bool is_dismissed_; 146 bool is_dismissed_;
147 int remote_category_id_;
145 148
146 size_t best_source_index_; 149 size_t best_source_index_;
147 150
148 std::vector<SnippetSource> sources_; 151 std::vector<SnippetSource> sources_;
149 152
150 DISALLOW_COPY_AND_ASSIGN(NTPSnippet); 153 DISALLOW_COPY_AND_ASSIGN(NTPSnippet);
151 }; 154 };
152 155
153 } // namespace ntp_snippets 156 } // namespace ntp_snippets
154 157
155 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_NTP_SNIPPET_H_ 158 #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') | components/ntp_snippets/remote/ntp_snippet.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698