| 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"; |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 } | 136 } |
| 136 } | 137 } |
| 137 } | 138 } |
| 138 snippet->set_source_index(best_source_index); | 139 snippet->set_source_index(best_source_index); |
| 139 | 140 |
| 140 if (snippet->sources_.empty()) { | 141 if (snippet->sources_.empty()) { |
| 141 DLOG(WARNING) << "No sources found for article " << url_str; | 142 DLOG(WARNING) << "No sources found for article " << url_str; |
| 142 return nullptr; | 143 return nullptr; |
| 143 } | 144 } |
| 144 | 145 |
| 146 double score; |
| 147 if (dict.GetDouble(kScore, &score)) |
| 148 snippet->set_score(score); |
| 149 |
| 145 return snippet; | 150 return snippet; |
| 146 } | 151 } |
| 147 | 152 |
| 148 // static | 153 // static |
| 149 bool NTPSnippet::AddFromListValue(const base::ListValue& list, | 154 bool NTPSnippet::AddFromListValue(const base::ListValue& list, |
| 150 PtrVector* snippets) { | 155 PtrVector* snippets) { |
| 151 for (const base::Value* const value : list) { | 156 for (const base::Value* const value : list) { |
| 152 const base::DictionaryValue* dict = nullptr; | 157 const base::DictionaryValue* dict = nullptr; |
| 153 if (!value->GetAsDictionary(&dict)) | 158 if (!value->GetAsDictionary(&dict)) |
| 154 return false; | 159 return false; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 source.publisher_name); | 196 source.publisher_name); |
| 192 | 197 |
| 193 corpus_infos_list->Append(std::move(corpus_info_dict)); | 198 corpus_infos_list->Append(std::move(corpus_info_dict)); |
| 194 } | 199 } |
| 195 | 200 |
| 196 dict->Set(kSourceCorpusInfo, std::move(corpus_infos_list)); | 201 dict->Set(kSourceCorpusInfo, std::move(corpus_infos_list)); |
| 197 | 202 |
| 198 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue); | 203 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue); |
| 199 result->Set(kContentInfo, std::move(dict)); | 204 result->Set(kContentInfo, std::move(dict)); |
| 200 | 205 |
| 206 result->SetDouble(kScore, score_); |
| 207 |
| 201 return result; | 208 return result; |
| 202 } | 209 } |
| 203 | 210 |
| 204 // static | 211 // static |
| 205 base::Time NTPSnippet::TimeFromJsonString(const std::string& timestamp_str) { | 212 base::Time NTPSnippet::TimeFromJsonString(const std::string& timestamp_str) { |
| 206 int64_t timestamp; | 213 int64_t timestamp; |
| 207 if (!base::StringToInt64(timestamp_str, ×tamp)) { | 214 if (!base::StringToInt64(timestamp_str, ×tamp)) { |
| 208 // Even if there's an error in the conversion, some garbage data may still | 215 // Even if there's an error in the conversion, some garbage data may still |
| 209 // be written to the output var, so reset it. | 216 // be written to the output var, so reset it. |
| 210 timestamp = 0; | 217 timestamp = 0; |
| 211 } | 218 } |
| 212 return base::Time::UnixEpoch() + base::TimeDelta::FromSeconds(timestamp); | 219 return base::Time::UnixEpoch() + base::TimeDelta::FromSeconds(timestamp); |
| 213 } | 220 } |
| 214 | 221 |
| 215 // static | 222 // static |
| 216 std::string NTPSnippet::TimeToJsonString(const base::Time& time) { | 223 std::string NTPSnippet::TimeToJsonString(const base::Time& time) { |
| 217 return base::Int64ToString((time - base::Time::UnixEpoch()).InSeconds()); | 224 return base::Int64ToString((time - base::Time::UnixEpoch()).InSeconds()); |
| 218 } | 225 } |
| 219 | 226 |
| 220 } // namespace ntp_snippets | 227 } // namespace ntp_snippets |
| OLD | NEW |