| 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"; |
| 14 const char kContentInfo[] = "contentInfo"; |
| 15 |
| 13 const char kUrl[] = "url"; | 16 const char kUrl[] = "url"; |
| 14 const char kTitle[] = "title"; | 17 const char kTitle[] = "title"; |
| 15 const char kSalientImageUrl[] = "thumbnailUrl"; | 18 const char kSalientImageUrl[] = "thumbnailUrl"; |
| 16 const char kSnippet[] = "snippet"; | 19 const char kSnippet[] = "snippet"; |
| 17 const char kPublishDate[] = "creationTimestampSec"; | 20 const char kPublishDate[] = "creationTimestampSec"; |
| 18 const char kExpiryDate[] = "expiryTimestampSec"; | 21 const char kExpiryDate[] = "expiryTimestampSec"; |
| 19 const char kSiteTitle[] = "sourceName"; | 22 const char kSiteTitle[] = "sourceName"; |
| 20 const char kPublisherData[] = "publisherData"; | 23 const char kPublisherData[] = "publisherData"; |
| 21 const char kCorpusId[] = "corpusId"; | 24 const char kCorpusId[] = "corpusId"; |
| 22 const char kSourceCorpusInfo[] = "sourceCorpusInfo"; | 25 const char kSourceCorpusInfo[] = "sourceCorpusInfo"; |
| 23 const char kAmpUrl[] = "ampUrl"; | 26 const char kAmpUrl[] = "ampUrl"; |
| 24 | 27 |
| 25 } // namespace | 28 } // namespace |
| 26 | 29 |
| 27 namespace ntp_snippets { | 30 namespace ntp_snippets { |
| 28 | 31 |
| 29 NTPSnippet::NTPSnippet(const GURL& url) : url_(url), best_source_index_(0) { | 32 NTPSnippet::NTPSnippet(const GURL& url) : url_(url), best_source_index_(0) { |
| 30 DCHECK(url_.is_valid()); | 33 DCHECK(url_.is_valid()); |
| 31 } | 34 } |
| 32 | 35 |
| 33 NTPSnippet::~NTPSnippet() {} | 36 NTPSnippet::~NTPSnippet() {} |
| 34 | 37 |
| 35 // static | 38 // static |
| 36 std::unique_ptr<NTPSnippet> NTPSnippet::CreateFromDictionary( | 39 std::unique_ptr<NTPSnippet> NTPSnippet::CreateFromDictionary( |
| 37 const base::DictionaryValue& dict) { | 40 const base::DictionaryValue& dict) { |
| 41 const base::DictionaryValue* content = nullptr; |
| 42 if (!dict.GetDictionary(kContentInfo, &content)) |
| 43 return nullptr; |
| 44 |
| 38 // Need at least the url. | 45 // Need at least the url. |
| 39 std::string url_str; | 46 std::string url_str; |
| 40 if (!dict.GetString("url", &url_str)) | 47 if (!content->GetString("url", &url_str)) |
| 41 return nullptr; | 48 return nullptr; |
| 42 GURL url(url_str); | 49 GURL url(url_str); |
| 43 if (!url.is_valid()) | 50 if (!url.is_valid()) |
| 44 return nullptr; | 51 return nullptr; |
| 45 | 52 |
| 46 std::unique_ptr<NTPSnippet> snippet(new NTPSnippet(url)); | 53 std::unique_ptr<NTPSnippet> snippet(new NTPSnippet(url)); |
| 47 | 54 |
| 48 std::string title; | 55 std::string title; |
| 49 if (dict.GetString(kTitle, &title)) | 56 if (content->GetString(kTitle, &title)) |
| 50 snippet->set_title(title); | 57 snippet->set_title(title); |
| 51 std::string salient_image_url; | 58 std::string salient_image_url; |
| 52 if (dict.GetString(kSalientImageUrl, &salient_image_url)) | 59 if (content->GetString(kSalientImageUrl, &salient_image_url)) |
| 53 snippet->set_salient_image_url(GURL(salient_image_url)); | 60 snippet->set_salient_image_url(GURL(salient_image_url)); |
| 54 std::string snippet_str; | 61 std::string snippet_str; |
| 55 if (dict.GetString(kSnippet, &snippet_str)) | 62 if (content->GetString(kSnippet, &snippet_str)) |
| 56 snippet->set_snippet(snippet_str); | 63 snippet->set_snippet(snippet_str); |
| 57 // The creation and expiry timestamps are uint64s which are stored as strings. | 64 // The creation and expiry timestamps are uint64s which are stored as strings. |
| 58 std::string creation_timestamp_str; | 65 std::string creation_timestamp_str; |
| 59 if (dict.GetString(kPublishDate, &creation_timestamp_str)) | 66 if (content->GetString(kPublishDate, &creation_timestamp_str)) |
| 60 snippet->set_publish_date(TimeFromJsonString(creation_timestamp_str)); | 67 snippet->set_publish_date(TimeFromJsonString(creation_timestamp_str)); |
| 61 std::string expiry_timestamp_str; | 68 std::string expiry_timestamp_str; |
| 62 if (dict.GetString(kExpiryDate, &expiry_timestamp_str)) | 69 if (content->GetString(kExpiryDate, &expiry_timestamp_str)) |
| 63 snippet->set_expiry_date(TimeFromJsonString(expiry_timestamp_str)); | 70 snippet->set_expiry_date(TimeFromJsonString(expiry_timestamp_str)); |
| 64 | 71 |
| 65 const base::ListValue* corpus_infos_list = nullptr; | 72 const base::ListValue* corpus_infos_list = nullptr; |
| 66 if (!dict.GetList(kSourceCorpusInfo, &corpus_infos_list)) { | 73 if (!content->GetList(kSourceCorpusInfo, &corpus_infos_list)) { |
| 67 DLOG(WARNING) << "No sources found for article " << title; | 74 DLOG(WARNING) << "No sources found for article " << title; |
| 68 return nullptr; | 75 return nullptr; |
| 69 } | 76 } |
| 70 | 77 |
| 71 for (base::Value* value : *corpus_infos_list) { | 78 for (base::Value* value : *corpus_infos_list) { |
| 72 const base::DictionaryValue* dict_value = nullptr; | 79 const base::DictionaryValue* dict_value = nullptr; |
| 73 if (!value->GetAsDictionary(&dict_value)) { | 80 if (!value->GetAsDictionary(&dict_value)) { |
| 74 DLOG(WARNING) << "Invalid source info for article " << url_str; | 81 DLOG(WARNING) << "Invalid source info for article " << url_str; |
| 75 continue; | 82 continue; |
| 76 } | 83 } |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 } | 136 } |
| 130 } | 137 } |
| 131 } | 138 } |
| 132 snippet->set_source_index(best_source_index); | 139 snippet->set_source_index(best_source_index); |
| 133 | 140 |
| 134 if (snippet->sources_.empty()) { | 141 if (snippet->sources_.empty()) { |
| 135 DLOG(WARNING) << "No sources found for article " << url_str; | 142 DLOG(WARNING) << "No sources found for article " << url_str; |
| 136 return nullptr; | 143 return nullptr; |
| 137 } | 144 } |
| 138 | 145 |
| 146 double score; |
| 147 if (dict.GetDouble(kScore, &score)) |
| 148 snippet->set_score(score); |
| 149 |
| 139 return snippet; | 150 return snippet; |
| 140 } | 151 } |
| 141 | 152 |
| 142 std::unique_ptr<base::DictionaryValue> NTPSnippet::ToDictionary() const { | 153 std::unique_ptr<base::DictionaryValue> NTPSnippet::ToDictionary() const { |
| 143 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue); | 154 std::unique_ptr<base::DictionaryValue> content(new base::DictionaryValue); |
| 144 | 155 |
| 145 dict->SetString(kUrl, url_.spec()); | 156 content->SetString(kUrl, url_.spec()); |
| 146 if (!title_.empty()) | 157 if (!title_.empty()) |
| 147 dict->SetString(kTitle, title_); | 158 content->SetString(kTitle, title_); |
| 148 if (salient_image_url_.is_valid()) | 159 if (salient_image_url_.is_valid()) |
| 149 dict->SetString(kSalientImageUrl, salient_image_url_.spec()); | 160 content->SetString(kSalientImageUrl, salient_image_url_.spec()); |
| 150 if (!snippet_.empty()) | 161 if (!snippet_.empty()) |
| 151 dict->SetString(kSnippet, snippet_); | 162 content->SetString(kSnippet, snippet_); |
| 152 if (!publish_date_.is_null()) | 163 if (!publish_date_.is_null()) |
| 153 dict->SetString(kPublishDate, TimeToJsonString(publish_date_)); | 164 content->SetString(kPublishDate, TimeToJsonString(publish_date_)); |
| 154 if (!expiry_date_.is_null()) | 165 if (!expiry_date_.is_null()) |
| 155 dict->SetString(kExpiryDate, TimeToJsonString(expiry_date_)); | 166 content->SetString(kExpiryDate, TimeToJsonString(expiry_date_)); |
| 156 | 167 if (amp_url_.is_valid()) { |
| 157 std::unique_ptr<base::ListValue> corpus_infos_list(new base::ListValue); | 168 std::unique_ptr<base::ListValue> corpus_infos_list(new base::ListValue); |
| 158 for (const SnippetSource& source : sources_) { | |
| 159 std::unique_ptr<base::DictionaryValue> corpus_info_dict( | 169 std::unique_ptr<base::DictionaryValue> corpus_info_dict( |
| 160 new base::DictionaryValue); | 170 new base::DictionaryValue); |
| 161 | 171 |
| 162 corpus_info_dict->SetString(kCorpusId, source.url.spec()); | 172 corpus_info_dict->SetString(kCorpusId, source.url.spec()); |
| 163 if (!source.amp_url.is_empty()) | 173 if (!source.amp_url.is_empty()) |
| 164 corpus_info_dict->SetString(kAmpUrl, source.amp_url.spec()); | 174 corpus_info_dict->SetString(kAmpUrl, source.amp_url.spec()); |
| 165 if (!source.publisher_name.empty()) | 175 if (!source.publisher_name.empty()) |
| 166 corpus_info_dict->SetString( | 176 corpus_info_dict->SetString( |
| 167 base::StringPrintf("%s.%s", kPublisherData, kSiteTitle), | 177 base::StringPrintf("%s.%s", kPublisherData, kSiteTitle), |
| 168 source.publisher_name); | 178 source.publisher_name); |
| 169 | 179 |
| 170 corpus_infos_list->Append(std::move(corpus_info_dict)); | 180 corpus_infos_list->Append(std::move(corpus_info_dict)); |
| 171 } | 181 } |
| 172 | 182 |
| 173 dict->Set(kSourceCorpusInfo, std::move(corpus_infos_list)); | 183 dict->Set(kSourceCorpusInfo, std::move(corpus_infos_list)); |
| 174 | 184 |
| 185 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue); |
| 186 dict->Set(kContentInfo, std::move(content)); |
| 187 |
| 188 dict->SetDouble(kScore, score_); |
| 189 |
| 175 return dict; | 190 return dict; |
| 176 } | 191 } |
| 177 | 192 |
| 178 // static | 193 // static |
| 179 base::Time NTPSnippet::TimeFromJsonString(const std::string& timestamp_str) { | 194 base::Time NTPSnippet::TimeFromJsonString(const std::string& timestamp_str) { |
| 180 int64_t timestamp; | 195 int64_t timestamp; |
| 181 if (!base::StringToInt64(timestamp_str, ×tamp)) { | 196 if (!base::StringToInt64(timestamp_str, ×tamp)) { |
| 182 // Even if there's an error in the conversion, some garbage data may still | 197 // Even if there's an error in the conversion, some garbage data may still |
| 183 // be written to the output var, so reset it. | 198 // be written to the output var, so reset it. |
| 184 timestamp = 0; | 199 timestamp = 0; |
| 185 } | 200 } |
| 186 return base::Time::UnixEpoch() + base::TimeDelta::FromSeconds(timestamp); | 201 return base::Time::UnixEpoch() + base::TimeDelta::FromSeconds(timestamp); |
| 187 } | 202 } |
| 188 | 203 |
| 189 // static | 204 // static |
| 190 std::string NTPSnippet::TimeToJsonString(const base::Time& time) { | 205 std::string NTPSnippet::TimeToJsonString(const base::Time& time) { |
| 191 return base::Int64ToString((time - base::Time::UnixEpoch()).InSeconds()); | 206 return base::Int64ToString((time - base::Time::UnixEpoch()).InSeconds()); |
| 192 } | 207 } |
| 193 | 208 |
| 194 } // namespace ntp_snippets | 209 } // namespace ntp_snippets |
| OLD | NEW |