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 #include "components/ntp_snippets/ntp_snippet.h" | 5 #include "components/ntp_snippets/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 #include "components/ntp_snippets/content_suggestion.h" | 11 |
12 #include "components/ntp_snippets/content_suggestion_category.h" | |
13 #include "components/ntp_snippets/content_suggestions_provider_type.h" | |
14 #include "components/ntp_snippets/proto/ntp_snippets.pb.h" | 12 #include "components/ntp_snippets/proto/ntp_snippets.pb.h" |
15 | 13 |
16 namespace { | 14 namespace { |
17 | 15 |
18 // dict.Get() specialization for base::Time values | 16 // dict.Get() specialization for base::Time values |
19 bool GetTimeValue(const base::DictionaryValue& dict, | 17 bool GetTimeValue(const base::DictionaryValue& dict, |
20 const std::string& key, | 18 const std::string& key, |
21 base::Time* time) { | 19 base::Time* time) { |
22 std::string time_value; | 20 std::string time_value; |
23 return dict.GetString(key, &time_value) && | 21 return dict.GetString(key, &time_value) && |
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 source_proto->set_url(source.url.spec()); | 237 source_proto->set_url(source.url.spec()); |
240 if (!source.publisher_name.empty()) | 238 if (!source.publisher_name.empty()) |
241 source_proto->set_publisher_name(source.publisher_name); | 239 source_proto->set_publisher_name(source.publisher_name); |
242 if (source.amp_url.is_valid()) | 240 if (source.amp_url.is_valid()) |
243 source_proto->set_amp_url(source.amp_url.spec()); | 241 source_proto->set_amp_url(source.amp_url.spec()); |
244 } | 242 } |
245 | 243 |
246 return result; | 244 return result; |
247 } | 245 } |
248 | 246 |
249 std::unique_ptr<ContentSuggestion> NTPSnippet::ToContentSuggestion() const { | |
250 std::unique_ptr<ContentSuggestion> result(new ContentSuggestion( | |
251 id_, ContentSuggestionsProviderType::ARTICLES, | |
252 ContentSuggestionCategory::ARTICLE, best_source().url)); | |
253 result->set_amp_url(best_source().amp_url); | |
254 result->set_title(title_); | |
255 result->set_snippet_text(snippet_); | |
256 result->set_publish_date(publish_date_); | |
257 result->set_publisher_name(best_source().publisher_name); | |
258 result->set_score(score_); | |
259 return result; | |
260 } | |
261 | |
262 // static | 247 // static |
263 base::Time NTPSnippet::TimeFromJsonString(const std::string& timestamp_str) { | 248 base::Time NTPSnippet::TimeFromJsonString(const std::string& timestamp_str) { |
264 int64_t timestamp; | 249 int64_t timestamp; |
265 if (!base::StringToInt64(timestamp_str, ×tamp)) { | 250 if (!base::StringToInt64(timestamp_str, ×tamp)) { |
266 // Even if there's an error in the conversion, some garbage data may still | 251 // Even if there's an error in the conversion, some garbage data may still |
267 // be written to the output var, so reset it. | 252 // be written to the output var, so reset it. |
268 timestamp = 0; | 253 timestamp = 0; |
269 } | 254 } |
270 return base::Time::UnixEpoch() + base::TimeDelta::FromSeconds(timestamp); | 255 return base::Time::UnixEpoch() + base::TimeDelta::FromSeconds(timestamp); |
271 } | 256 } |
(...skipping 19 matching lines...) Expand all Loading... |
291 best_source_index_ = i; | 276 best_source_index_ = i; |
292 if (!source.amp_url.is_empty()) { | 277 if (!source.amp_url.is_empty()) { |
293 // This is the best possible source, stop looking. | 278 // This is the best possible source, stop looking. |
294 break; | 279 break; |
295 } | 280 } |
296 } | 281 } |
297 } | 282 } |
298 } | 283 } |
299 | 284 |
300 } // namespace ntp_snippets | 285 } // namespace ntp_snippets |
OLD | NEW |