| 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 | 10 |
| 11 namespace { | 11 namespace { |
| 12 | 12 |
| 13 const char kScore[] = "score"; |
| 13 const char kContentInfo[] = "contentInfo"; | 14 const char kContentInfo[] = "contentInfo"; |
| 14 | 15 |
| 15 const char kUrl[] = "url"; | 16 const char kUrl[] = "url"; |
| 16 const char kTitle[] = "title"; | 17 const char kTitle[] = "title"; |
| 17 const char kSalientImageUrl[] = "thumbnailUrl"; | 18 const char kSalientImageUrl[] = "thumbnailUrl"; |
| 18 const char kSnippet[] = "snippet"; | 19 const char kSnippet[] = "snippet"; |
| 19 const char kPublishDate[] = "creationTimestampSec"; | 20 const char kPublishDate[] = "creationTimestampSec"; |
| 20 const char kExpiryDate[] = "expiryTimestampSec"; | 21 const char kExpiryDate[] = "expiryTimestampSec"; |
| 21 const char kSiteTitle[] = "sourceName"; | 22 const char kSiteTitle[] = "sourceName"; |
| 22 const char kPublisherData[] = "publisherData"; | 23 const char kPublisherData[] = "publisherData"; |
| 23 const char kCorpusId[] = "corpusId"; | 24 const char kCorpusId[] = "corpusId"; |
| 24 const char kSourceCorpusInfo[] = "sourceCorpusInfo"; | 25 const char kSourceCorpusInfo[] = "sourceCorpusInfo"; |
| 25 const char kAmpUrl[] = "ampUrl"; | 26 const char kAmpUrl[] = "ampUrl"; |
| 26 | 27 |
| 27 } // namespace | 28 } // namespace |
| 28 | 29 |
| 29 namespace ntp_snippets { | 30 namespace ntp_snippets { |
| 30 | 31 |
| 31 NTPSnippet::NTPSnippet(const GURL& url) : url_(url), best_source_index_(0) { | 32 NTPSnippet::NTPSnippet(const GURL& url) |
| 33 : url_(url), score_(0), best_source_index_(0) { |
| 32 DCHECK(url_.is_valid()); | 34 DCHECK(url_.is_valid()); |
| 33 } | 35 } |
| 34 | 36 |
| 35 NTPSnippet::~NTPSnippet() {} | 37 NTPSnippet::~NTPSnippet() {} |
| 36 | 38 |
| 37 // static | 39 // static |
| 38 std::unique_ptr<NTPSnippet> NTPSnippet::CreateFromDictionary( | 40 std::unique_ptr<NTPSnippet> NTPSnippet::CreateFromDictionary( |
| 39 const base::DictionaryValue& dict) { | 41 const base::DictionaryValue& dict) { |
| 40 const base::DictionaryValue* content = nullptr; | 42 const base::DictionaryValue* content = nullptr; |
| 41 if (!dict.GetDictionary(kContentInfo, &content)) | 43 if (!dict.GetDictionary(kContentInfo, &content)) |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 } | 137 } |
| 136 } | 138 } |
| 137 } | 139 } |
| 138 snippet->set_source_index(best_source_index); | 140 snippet->set_source_index(best_source_index); |
| 139 | 141 |
| 140 if (snippet->sources_.empty()) { | 142 if (snippet->sources_.empty()) { |
| 141 DLOG(WARNING) << "No sources found for article " << url_str; | 143 DLOG(WARNING) << "No sources found for article " << url_str; |
| 142 return nullptr; | 144 return nullptr; |
| 143 } | 145 } |
| 144 | 146 |
| 147 double score; |
| 148 if (dict.GetDouble(kScore, &score)) |
| 149 snippet->set_score(score); |
| 150 |
| 145 return snippet; | 151 return snippet; |
| 146 } | 152 } |
| 147 | 153 |
| 148 // static | 154 // static |
| 149 bool NTPSnippet::AddFromListValue(const base::ListValue& list, | 155 bool NTPSnippet::AddFromListValue(const base::ListValue& list, |
| 150 PtrVector* snippets) { | 156 PtrVector* snippets) { |
| 151 for (const base::Value* const value : list) { | 157 for (const base::Value* const value : list) { |
| 152 const base::DictionaryValue* dict = nullptr; | 158 const base::DictionaryValue* dict = nullptr; |
| 153 if (!value->GetAsDictionary(&dict)) | 159 if (!value->GetAsDictionary(&dict)) |
| 154 return false; | 160 return false; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 source.publisher_name); | 197 source.publisher_name); |
| 192 | 198 |
| 193 corpus_infos_list->Append(std::move(corpus_info_dict)); | 199 corpus_infos_list->Append(std::move(corpus_info_dict)); |
| 194 } | 200 } |
| 195 | 201 |
| 196 dict->Set(kSourceCorpusInfo, std::move(corpus_infos_list)); | 202 dict->Set(kSourceCorpusInfo, std::move(corpus_infos_list)); |
| 197 | 203 |
| 198 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue); | 204 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue); |
| 199 result->Set(kContentInfo, std::move(dict)); | 205 result->Set(kContentInfo, std::move(dict)); |
| 200 | 206 |
| 207 result->SetDouble(kScore, score_); |
| 208 |
| 201 return result; | 209 return result; |
| 202 } | 210 } |
| 203 | 211 |
| 204 // static | 212 // static |
| 205 base::Time NTPSnippet::TimeFromJsonString(const std::string& timestamp_str) { | 213 base::Time NTPSnippet::TimeFromJsonString(const std::string& timestamp_str) { |
| 206 int64_t timestamp; | 214 int64_t timestamp; |
| 207 if (!base::StringToInt64(timestamp_str, ×tamp)) { | 215 if (!base::StringToInt64(timestamp_str, ×tamp)) { |
| 208 // Even if there's an error in the conversion, some garbage data may still | 216 // Even if there's an error in the conversion, some garbage data may still |
| 209 // be written to the output var, so reset it. | 217 // be written to the output var, so reset it. |
| 210 timestamp = 0; | 218 timestamp = 0; |
| 211 } | 219 } |
| 212 return base::Time::UnixEpoch() + base::TimeDelta::FromSeconds(timestamp); | 220 return base::Time::UnixEpoch() + base::TimeDelta::FromSeconds(timestamp); |
| 213 } | 221 } |
| 214 | 222 |
| 215 // static | 223 // static |
| 216 std::string NTPSnippet::TimeToJsonString(const base::Time& time) { | 224 std::string NTPSnippet::TimeToJsonString(const base::Time& time) { |
| 217 return base::Int64ToString((time - base::Time::UnixEpoch()).InSeconds()); | 225 return base::Int64ToString((time - base::Time::UnixEpoch()).InSeconds()); |
| 218 } | 226 } |
| 219 | 227 |
| 220 } // namespace ntp_snippets | 228 } // namespace ntp_snippets |
| OLD | NEW |