| 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/strings/string_number_conversions.h" | 7 #include "base/strings/string_number_conversions.h" |
| 8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "components/ntp_snippets/proto/ntp_snippets.pb.h" | 10 #include "components/ntp_snippets/proto/ntp_snippets.pb.h" |
| 11 #include "components/ntp_snippets/snippet.h" |
| 12 #include "components/ntp_snippets/snippet_category.h" |
| 13 #include "components/ntp_snippets/snippet_provider_type.h" |
| 11 | 14 |
| 12 namespace { | 15 namespace { |
| 13 | 16 |
| 14 const char kScore[] = "score"; | 17 const char kScore[] = "score"; |
| 15 const char kContentInfo[] = "contentInfo"; | 18 const char kContentInfo[] = "contentInfo"; |
| 16 | 19 |
| 17 const char kId[] = "url"; | 20 const char kId[] = "url"; |
| 18 const char kTitle[] = "title"; | 21 const char kTitle[] = "title"; |
| 19 const char kSalientImageUrl[] = "thumbnailUrl"; | 22 const char kSalientImageUrl[] = "thumbnailUrl"; |
| 20 const char kSnippet[] = "snippet"; | 23 const char kSnippet[] = "snippet"; |
| 21 const char kPublishDate[] = "creationTimestampSec"; | 24 const char kPublishDate[] = "creationTimestampSec"; |
| 22 const char kExpiryDate[] = "expiryTimestampSec"; | 25 const char kExpiryDate[] = "expiryTimestampSec"; |
| 23 const char kSiteTitle[] = "sourceName"; | 26 const char kSiteTitle[] = "sourceName"; |
| 24 const char kPublisherData[] = "publisherData"; | 27 const char kPublisherData[] = "publisherData"; |
| 25 const char kCorpusId[] = "corpusId"; | 28 const char kCorpusId[] = "corpusId"; |
| 26 const char kSourceCorpusInfo[] = "sourceCorpusInfo"; | 29 const char kSourceCorpusInfo[] = "sourceCorpusInfo"; |
| 27 const char kAmpUrl[] = "ampUrl"; | 30 const char kAmpUrl[] = "ampUrl"; |
| 28 | 31 |
| 29 } // namespace | 32 } // namespace |
| 30 | 33 |
| 34 // TODO IMPORTANT NOTE: This class will move to a sub-namespace and be renamed |
| 35 // ArticleSnippet. It will only be used for snippets from Chrome Reader. |
| 36 // When accessing snippets for all usages towards the UI, use the new "Snippet" |
| 37 // class through the "SnippetService" instead. |
| 38 |
| 31 namespace ntp_snippets { | 39 namespace ntp_snippets { |
| 32 | 40 |
| 33 NTPSnippet::NTPSnippet(const std::string& id) | 41 NTPSnippet::NTPSnippet(const std::string& id) |
| 34 : id_(id), score_(0), is_discarded_(false), best_source_index_(0) {} | 42 : id_(id), score_(0), is_discarded_(false), best_source_index_(0) {} |
| 35 | 43 |
| 36 NTPSnippet::~NTPSnippet() {} | 44 NTPSnippet::~NTPSnippet() {} |
| 37 | 45 |
| 38 // static | 46 // static |
| 39 std::unique_ptr<NTPSnippet> NTPSnippet::CreateFromDictionary( | 47 std::unique_ptr<NTPSnippet> NTPSnippet::CreateFromDictionary( |
| 40 const base::DictionaryValue& dict) { | 48 const base::DictionaryValue& dict) { |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 source_proto->set_url(source.url.spec()); | 222 source_proto->set_url(source.url.spec()); |
| 215 if (!source.publisher_name.empty()) | 223 if (!source.publisher_name.empty()) |
| 216 source_proto->set_publisher_name(source.publisher_name); | 224 source_proto->set_publisher_name(source.publisher_name); |
| 217 if (source.amp_url.is_valid()) | 225 if (source.amp_url.is_valid()) |
| 218 source_proto->set_amp_url(source.amp_url.spec()); | 226 source_proto->set_amp_url(source.amp_url.spec()); |
| 219 } | 227 } |
| 220 | 228 |
| 221 return result; | 229 return result; |
| 222 } | 230 } |
| 223 | 231 |
| 232 std::unique_ptr<Snippet> NTPSnippet::ToSnippet() const { |
| 233 std::unique_ptr<Snippet> result(new Snippet( |
| 234 id_, SnippetProviderType::ARTICLES, SnippetCategory::ARTICLE)); |
| 235 result->set_url(best_source().url); |
| 236 result->set_amp_url(best_source().amp_url); |
| 237 result->set_title(title_); |
| 238 result->set_text_extract(snippet_); |
| 239 result->set_salient_image_url(salient_image_url_); |
| 240 result->set_publish_date(publish_date_); |
| 241 result->set_publisher_name(best_source().publisher_name); |
| 242 result->set_score(score_); |
| 243 return result; |
| 244 } |
| 245 |
| 224 // static | 246 // static |
| 225 base::Time NTPSnippet::TimeFromJsonString(const std::string& timestamp_str) { | 247 base::Time NTPSnippet::TimeFromJsonString(const std::string& timestamp_str) { |
| 226 int64_t timestamp; | 248 int64_t timestamp; |
| 227 if (!base::StringToInt64(timestamp_str, ×tamp)) { | 249 if (!base::StringToInt64(timestamp_str, ×tamp)) { |
| 228 // Even if there's an error in the conversion, some garbage data may still | 250 // Even if there's an error in the conversion, some garbage data may still |
| 229 // be written to the output var, so reset it. | 251 // be written to the output var, so reset it. |
| 230 timestamp = 0; | 252 timestamp = 0; |
| 231 } | 253 } |
| 232 return base::Time::UnixEpoch() + base::TimeDelta::FromSeconds(timestamp); | 254 return base::Time::UnixEpoch() + base::TimeDelta::FromSeconds(timestamp); |
| 233 } | 255 } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 253 best_source_index_ = i; | 275 best_source_index_ = i; |
| 254 if (!source.amp_url.is_empty()) { | 276 if (!source.amp_url.is_empty()) { |
| 255 // This is the best possible source, stop looking. | 277 // This is the best possible source, stop looking. |
| 256 break; | 278 break; |
| 257 } | 279 } |
| 258 } | 280 } |
| 259 } | 281 } |
| 260 } | 282 } |
| 261 | 283 |
| 262 } // namespace ntp_snippets | 284 } // namespace ntp_snippets |
| OLD | NEW |