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

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

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 #include "components/ntp_snippets/remote/ntp_snippet.h" 5 #include "components/ntp_snippets/remote/ntp_snippet.h"
6 6
7 #include "base/memory/ptr_util.h" 7 #include "base/memory/ptr_util.h"
8 #include "base/strings/string_number_conversions.h" 8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/stringprintf.h" 9 #include "base/strings/stringprintf.h"
10 #include "base/values.h" 10 #include "base/values.h"
11 11 #include "components/ntp_snippets/category.h"
12 #include "components/ntp_snippets/remote/proto/ntp_snippets.pb.h" 12 #include "components/ntp_snippets/remote/proto/ntp_snippets.pb.h"
13 13
14 namespace ntp_snippets {
15
14 namespace { 16 namespace {
15 17
18 const int kArticlesRemoteId = 1;
Bernhard Bauer 2016/10/10 13:54:54 I'm starting to get the feeling that just using a
Marc Treib 2016/10/10 15:54:47 ...I don't quite follow? You mean essentially stor
Bernhard Bauer 2016/10/10 16:03:12 No, I meant that maybe we should have stored categ
Marc Treib 2016/10/10 16:04:14 Aah - yes, that might have made sense...
19 static_assert(int(KnownCategories::ARTICLES) -
Bernhard Bauer 2016/10/10 13:54:54 Should this be a static_cast<int>()?
Marc Treib 2016/10/10 15:54:47 Sure, done.
20 int(KnownCategories::REMOTE_CATEGORIES_OFFSET) ==
21 kArticlesRemoteId,
22 "kArticlesRemoteId has a wrong value?!");
23
16 // dict.Get() specialization for base::Time values 24 // dict.Get() specialization for base::Time values
17 bool GetTimeValue(const base::DictionaryValue& dict, 25 bool GetTimeValue(const base::DictionaryValue& dict,
18 const std::string& key, 26 const std::string& key,
19 base::Time* time) { 27 base::Time* time) {
20 std::string time_value; 28 std::string time_value;
21 return dict.GetString(key, &time_value) && 29 return dict.GetString(key, &time_value) &&
22 base::Time::FromString(time_value.c_str(), time); 30 base::Time::FromString(time_value.c_str(), time);
23 } 31 }
24 32
25 // dict.Get() specialization for GURL values 33 // dict.Get() specialization for GURL values
26 bool GetURLValue(const base::DictionaryValue& dict, 34 bool GetURLValue(const base::DictionaryValue& dict,
27 const std::string& key, 35 const std::string& key,
28 GURL* url) { 36 GURL* url) {
29 std::string spec; 37 std::string spec;
30 if (!dict.GetString(key, &spec)) { 38 if (!dict.GetString(key, &spec)) {
31 return false; 39 return false;
32 } 40 }
33 *url = GURL(spec); 41 *url = GURL(spec);
34 return url->is_valid(); 42 return url->is_valid();
35 } 43 }
36 44
37 } // namespace 45 } // namespace
38 46
39 namespace ntp_snippets { 47 NTPSnippet::NTPSnippet(const std::string& id, int remote_category_id)
40 48 : id_(id),
41 NTPSnippet::NTPSnippet(const std::string& id) 49 score_(0),
42 : id_(id), score_(0), is_dismissed_(false), best_source_index_(0) {} 50 is_dismissed_(false),
51 remote_category_id_(remote_category_id),
52 best_source_index_(0) {}
43 53
44 NTPSnippet::~NTPSnippet() = default; 54 NTPSnippet::~NTPSnippet() = default;
45 55
46 // static 56 // static
47 std::unique_ptr<NTPSnippet> NTPSnippet::CreateFromChromeReaderDictionary( 57 std::unique_ptr<NTPSnippet> NTPSnippet::CreateFromChromeReaderDictionary(
48 const base::DictionaryValue& dict) { 58 const base::DictionaryValue& dict) {
49 const base::DictionaryValue* content = nullptr; 59 const base::DictionaryValue* content = nullptr;
50 if (!dict.GetDictionary("contentInfo", &content)) 60 if (!dict.GetDictionary("contentInfo", &content))
51 return nullptr; 61 return nullptr;
52 62
53 // Need at least the id. 63 // Need at least the id.
54 std::string id; 64 std::string id;
55 if (!content->GetString("url", &id) || id.empty()) 65 if (!content->GetString("url", &id) || id.empty())
56 return nullptr; 66 return nullptr;
57 67
58 std::unique_ptr<NTPSnippet> snippet(new NTPSnippet(id)); 68 std::unique_ptr<NTPSnippet> snippet(new NTPSnippet(id, kArticlesRemoteId));
59 69
60 std::string title; 70 std::string title;
61 if (content->GetString("title", &title)) 71 if (content->GetString("title", &title))
62 snippet->set_title(title); 72 snippet->set_title(title);
63 std::string salient_image_url; 73 std::string salient_image_url;
64 if (content->GetString("thumbnailUrl", &salient_image_url)) 74 if (content->GetString("thumbnailUrl", &salient_image_url))
65 snippet->set_salient_image_url(GURL(salient_image_url)); 75 snippet->set_salient_image_url(GURL(salient_image_url));
66 std::string snippet_str; 76 std::string snippet_str;
67 if (content->GetString("snippet", &snippet_str)) 77 if (content->GetString("snippet", &snippet_str))
68 snippet->set_snippet(snippet_str); 78 snippet->set_snippet(snippet_str);
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
131 141
132 double score; 142 double score;
133 if (dict.GetDouble("score", &score)) 143 if (dict.GetDouble("score", &score))
134 snippet->set_score(score); 144 snippet->set_score(score);
135 145
136 return snippet; 146 return snippet;
137 } 147 }
138 148
139 // static 149 // static
140 std::unique_ptr<NTPSnippet> NTPSnippet::CreateFromContentSuggestionsDictionary( 150 std::unique_ptr<NTPSnippet> NTPSnippet::CreateFromContentSuggestionsDictionary(
141 const base::DictionaryValue& dict) { 151 const base::DictionaryValue& dict,
152 int remote_category_id) {
142 const base::ListValue* ids; 153 const base::ListValue* ids;
143 std::string id; 154 std::string id;
144 if (!(dict.GetList("ids", &ids) && 155 if (!(dict.GetList("ids", &ids) &&
145 ids->GetString(0, &id))) { // TODO(sfiera): multiple IDs 156 ids->GetString(0, &id))) { // TODO(sfiera): multiple IDs
146 return nullptr; 157 return nullptr;
147 } 158 }
148 159
149 auto snippet = base::MakeUnique<NTPSnippet>(id); 160 auto snippet = base::MakeUnique<NTPSnippet>(id, remote_category_id);
150 snippet->sources_.emplace_back(GURL(), std::string(), GURL()); 161 snippet->sources_.emplace_back(GURL(), std::string(), GURL());
151 auto* source = &snippet->sources_.back(); 162 auto* source = &snippet->sources_.back();
152 snippet->best_source_index_ = 0; 163 snippet->best_source_index_ = 0;
153 164
154 if (!(dict.GetString("title", &snippet->title_) && 165 if (!(dict.GetString("title", &snippet->title_) &&
155 dict.GetString("snippet", &snippet->snippet_) && 166 dict.GetString("snippet", &snippet->snippet_) &&
156 GetTimeValue(dict, "creationTime", &snippet->publish_date_) && 167 GetTimeValue(dict, "creationTime", &snippet->publish_date_) &&
157 GetTimeValue(dict, "expirationTime", &snippet->expiry_date_) && 168 GetTimeValue(dict, "expirationTime", &snippet->expiry_date_) &&
158 GetURLValue(dict, "imageUrl", &snippet->salient_image_url_) && 169 GetURLValue(dict, "imageUrl", &snippet->salient_image_url_) &&
159 dict.GetString("attribution", &source->publisher_name) && 170 dict.GetString("attribution", &source->publisher_name) &&
160 GetURLValue(dict, "fullPageUrl", &source->url))) { 171 GetURLValue(dict, "fullPageUrl", &source->url))) {
161 return nullptr; 172 return nullptr;
162 } 173 }
163 GetURLValue(dict, "ampUrl", &source->amp_url); // May fail; OK. 174 GetURLValue(dict, "ampUrl", &source->amp_url); // May fail; OK.
164 // TODO(sfiera): also favicon URL. 175 // TODO(sfiera): also favicon URL.
165 176
166 snippet->score_ = 0.0; // TODO(sfiera): put score in protocol. 177 snippet->score_ = 0.0; // TODO(sfiera): put score in protocol.
167 178
168 return snippet; 179 return snippet;
169 } 180 }
170 181
171 // static 182 // static
172 std::unique_ptr<NTPSnippet> NTPSnippet::CreateFromProto( 183 std::unique_ptr<NTPSnippet> NTPSnippet::CreateFromProto(
173 const SnippetProto& proto) { 184 const SnippetProto& proto) {
174 // Need at least the id. 185 // Need at least the id.
175 if (!proto.has_id() || proto.id().empty()) 186 if (!proto.has_id() || proto.id().empty())
176 return nullptr; 187 return nullptr;
177 188
178 std::unique_ptr<NTPSnippet> snippet(new NTPSnippet(proto.id())); 189 int remote_category_id = kArticlesRemoteId;
190 if (proto.has_remote_category_id())
191 remote_category_id = proto.remote_category_id();
Bernhard Bauer 2016/10/10 13:54:54 Nit: Maybe use a ternary operator so you only assi
Marc Treib 2016/10/10 15:54:47 Done.
192
193 std::unique_ptr<NTPSnippet> snippet(
194 new NTPSnippet(proto.id(), remote_category_id));
179 195
180 snippet->set_title(proto.title()); 196 snippet->set_title(proto.title());
181 snippet->set_snippet(proto.snippet()); 197 snippet->set_snippet(proto.snippet());
182 snippet->set_salient_image_url(GURL(proto.salient_image_url())); 198 snippet->set_salient_image_url(GURL(proto.salient_image_url()));
183 snippet->set_publish_date( 199 snippet->set_publish_date(
184 base::Time::FromInternalValue(proto.publish_date())); 200 base::Time::FromInternalValue(proto.publish_date()));
185 snippet->set_expiry_date(base::Time::FromInternalValue(proto.expiry_date())); 201 snippet->set_expiry_date(base::Time::FromInternalValue(proto.expiry_date()));
186 snippet->set_score(proto.score()); 202 snippet->set_score(proto.score());
187 snippet->set_dismissed(proto.dismissed()); 203 snippet->set_dismissed(proto.dismissed());
188 204
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 if (!snippet_.empty()) 240 if (!snippet_.empty())
225 result.set_snippet(snippet_); 241 result.set_snippet(snippet_);
226 if (salient_image_url_.is_valid()) 242 if (salient_image_url_.is_valid())
227 result.set_salient_image_url(salient_image_url_.spec()); 243 result.set_salient_image_url(salient_image_url_.spec());
228 if (!publish_date_.is_null()) 244 if (!publish_date_.is_null())
229 result.set_publish_date(publish_date_.ToInternalValue()); 245 result.set_publish_date(publish_date_.ToInternalValue());
230 if (!expiry_date_.is_null()) 246 if (!expiry_date_.is_null())
231 result.set_expiry_date(expiry_date_.ToInternalValue()); 247 result.set_expiry_date(expiry_date_.ToInternalValue());
232 result.set_score(score_); 248 result.set_score(score_);
233 result.set_dismissed(is_dismissed_); 249 result.set_dismissed(is_dismissed_);
250 result.set_remote_category_id(remote_category_id_);
234 251
235 for (const SnippetSource& source : sources_) { 252 for (const SnippetSource& source : sources_) {
236 SnippetSourceProto* source_proto = result.add_sources(); 253 SnippetSourceProto* source_proto = result.add_sources();
237 source_proto->set_url(source.url.spec()); 254 source_proto->set_url(source.url.spec());
238 if (!source.publisher_name.empty()) 255 if (!source.publisher_name.empty())
239 source_proto->set_publisher_name(source.publisher_name); 256 source_proto->set_publisher_name(source.publisher_name);
240 if (source.amp_url.is_valid()) 257 if (source.amp_url.is_valid())
241 source_proto->set_amp_url(source.amp_url.spec()); 258 source_proto->set_amp_url(source.amp_url.spec());
242 } 259 }
243 260
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
276 best_source_index_ = i; 293 best_source_index_ = i;
277 if (!source.amp_url.is_empty()) { 294 if (!source.amp_url.is_empty()) {
278 // This is the best possible source, stop looking. 295 // This is the best possible source, stop looking.
279 break; 296 break;
280 } 297 }
281 } 298 }
282 } 299 }
283 } 300 }
284 301
285 } // namespace ntp_snippets 302 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698