| 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/remote/ntp_snippet.h" | 5 #include "components/ntp_snippets/remote/ntp_snippet.h" |
| 6 | 6 |
| 7 #include "base/feature_list.h" |
| 7 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
| 8 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 9 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| 11 #include "base/strings/utf_string_conversions.h" |
| 10 #include "base/values.h" | 12 #include "base/values.h" |
| 11 #include "components/ntp_snippets/category.h" | 13 #include "components/ntp_snippets/category.h" |
| 14 #include "components/ntp_snippets/features.h" |
| 12 #include "components/ntp_snippets/remote/proto/ntp_snippets.pb.h" | 15 #include "components/ntp_snippets/remote/proto/ntp_snippets.pb.h" |
| 13 | 16 |
| 14 namespace { | 17 namespace { |
| 15 | 18 |
| 16 struct SnippetSource { | 19 struct SnippetSource { |
| 17 SnippetSource(const GURL& url, | 20 SnippetSource(const GURL& url, |
| 18 const std::string& publisher_name, | 21 const std::string& publisher_name, |
| 19 const GURL& amp_url) | 22 const GURL& amp_url) |
| 20 : url(url), publisher_name(publisher_name), amp_url(amp_url) {} | 23 : url(url), publisher_name(publisher_name), amp_url(amp_url) {} |
| 21 GURL url; | 24 GURL url; |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 kArticlesRemoteId, | 81 kArticlesRemoteId, |
| 79 "kArticlesRemoteId has a wrong value?!"); | 82 "kArticlesRemoteId has a wrong value?!"); |
| 80 | 83 |
| 81 const int kChromeReaderDefaultExpiryTimeMins = 3 * 24 * 60; | 84 const int kChromeReaderDefaultExpiryTimeMins = 3 * 24 * 60; |
| 82 | 85 |
| 83 NTPSnippet::NTPSnippet(const std::vector<std::string>& ids, | 86 NTPSnippet::NTPSnippet(const std::vector<std::string>& ids, |
| 84 int remote_category_id) | 87 int remote_category_id) |
| 85 : ids_(ids), | 88 : ids_(ids), |
| 86 score_(0), | 89 score_(0), |
| 87 is_dismissed_(false), | 90 is_dismissed_(false), |
| 88 remote_category_id_(remote_category_id) {} | 91 remote_category_id_(remote_category_id), |
| 92 should_notify_(false) {} |
| 89 | 93 |
| 90 NTPSnippet::~NTPSnippet() = default; | 94 NTPSnippet::~NTPSnippet() = default; |
| 91 | 95 |
| 92 // static | 96 // static |
| 93 std::unique_ptr<NTPSnippet> NTPSnippet::CreateFromChromeReaderDictionary( | 97 std::unique_ptr<NTPSnippet> NTPSnippet::CreateFromChromeReaderDictionary( |
| 94 const base::DictionaryValue& dict) { | 98 const base::DictionaryValue& dict) { |
| 95 const base::DictionaryValue* content = nullptr; | 99 const base::DictionaryValue* content = nullptr; |
| 96 if (!dict.GetDictionary("contentInfo", &content)) { | 100 if (!dict.GetDictionary("contentInfo", &content)) { |
| 97 return nullptr; | 101 return nullptr; |
| 98 } | 102 } |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 239 return nullptr; | 243 return nullptr; |
| 240 } | 244 } |
| 241 GetURLValue(dict, "ampUrl", &snippet->amp_url_); // May fail; OK. | 245 GetURLValue(dict, "ampUrl", &snippet->amp_url_); // May fail; OK. |
| 242 // TODO(sfiera): also favicon URL. | 246 // TODO(sfiera): also favicon URL. |
| 243 | 247 |
| 244 double score; | 248 double score; |
| 245 if (dict.GetDouble("score", &score)) { | 249 if (dict.GetDouble("score", &score)) { |
| 246 snippet->score_ = score; | 250 snippet->score_ = score; |
| 247 } | 251 } |
| 248 | 252 |
| 253 const base::DictionaryValue* notification_info = nullptr; |
| 254 if (dict.GetDictionary("notificationInfo", ¬ification_info)) { |
| 255 if (notification_info->GetBoolean("shouldNotify", |
| 256 &snippet->should_notify_) && |
| 257 snippet->should_notify_) { |
| 258 if (!GetTimeValue(*notification_info, "deadline", |
| 259 &snippet->notification_deadline_)) { |
| 260 snippet->notification_deadline_ = base::Time::Max(); |
| 261 } |
| 262 } |
| 263 } |
| 264 |
| 249 return snippet; | 265 return snippet; |
| 250 } | 266 } |
| 251 | 267 |
| 252 // static | 268 // static |
| 253 std::unique_ptr<NTPSnippet> NTPSnippet::CreateFromProto( | 269 std::unique_ptr<NTPSnippet> NTPSnippet::CreateFromProto( |
| 254 const SnippetProto& proto) { | 270 const SnippetProto& proto) { |
| 255 // Need at least the id. | 271 // Need at least the id. |
| 256 if (proto.ids_size() == 0 || proto.ids(0).empty()) { | 272 if (proto.ids_size() == 0 || proto.ids(0).empty()) { |
| 257 return nullptr; | 273 return nullptr; |
| 258 } | 274 } |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 348 if (!publisher_name_.empty()) { | 364 if (!publisher_name_.empty()) { |
| 349 source_proto->set_publisher_name(publisher_name_); | 365 source_proto->set_publisher_name(publisher_name_); |
| 350 } | 366 } |
| 351 if (amp_url_.is_valid()) { | 367 if (amp_url_.is_valid()) { |
| 352 source_proto->set_amp_url(amp_url_.spec()); | 368 source_proto->set_amp_url(amp_url_.spec()); |
| 353 } | 369 } |
| 354 | 370 |
| 355 return result; | 371 return result; |
| 356 } | 372 } |
| 357 | 373 |
| 374 ContentSuggestion NTPSnippet::ToContentSuggestion(Category category) const { |
| 375 GURL url = url_; |
| 376 if (base::FeatureList::IsEnabled(kPreferAmpUrlsFeature) && |
| 377 !amp_url_.is_empty()) { |
| 378 url = amp_url_; |
| 379 } |
| 380 ContentSuggestion suggestion(category, id(), url); |
| 381 suggestion.set_title(base::UTF8ToUTF16(title_)); |
| 382 suggestion.set_snippet_text(base::UTF8ToUTF16(snippet_)); |
| 383 suggestion.set_publish_date(publish_date_); |
| 384 suggestion.set_publisher_name(base::UTF8ToUTF16(publisher_name_)); |
| 385 suggestion.set_score(score_); |
| 386 if (should_notify_) { |
| 387 NotificationExtra extra; |
| 388 extra.deadline = notification_deadline_; |
| 389 suggestion.set_notification_extra( |
| 390 base::MakeUnique<NotificationExtra>(extra)); |
| 391 } |
| 392 return suggestion; |
| 393 } |
| 394 |
| 358 // static | 395 // static |
| 359 base::Time NTPSnippet::TimeFromJsonString(const std::string& timestamp_str) { | 396 base::Time NTPSnippet::TimeFromJsonString(const std::string& timestamp_str) { |
| 360 int64_t timestamp; | 397 int64_t timestamp; |
| 361 if (!base::StringToInt64(timestamp_str, ×tamp)) { | 398 if (!base::StringToInt64(timestamp_str, ×tamp)) { |
| 362 // Even if there's an error in the conversion, some garbage data may still | 399 // Even if there's an error in the conversion, some garbage data may still |
| 363 // be written to the output var, so reset it. | 400 // be written to the output var, so reset it. |
| 364 DLOG(WARNING) << "Invalid json timestamp: " << timestamp_str; | 401 DLOG(WARNING) << "Invalid json timestamp: " << timestamp_str; |
| 365 timestamp = 0; | 402 timestamp = 0; |
| 366 } | 403 } |
| 367 return base::Time::UnixEpoch() + base::TimeDelta::FromSeconds(timestamp); | 404 return base::Time::UnixEpoch() + base::TimeDelta::FromSeconds(timestamp); |
| 368 } | 405 } |
| 369 | 406 |
| 370 // static | 407 // static |
| 371 std::string NTPSnippet::TimeToJsonString(const base::Time& time) { | 408 std::string NTPSnippet::TimeToJsonString(const base::Time& time) { |
| 372 return base::Int64ToString((time - base::Time::UnixEpoch()).InSeconds()); | 409 return base::Int64ToString((time - base::Time::UnixEpoch()).InSeconds()); |
| 373 } | 410 } |
| 374 | 411 |
| 375 // static | 412 // static |
| 376 std::unique_ptr<NTPSnippet> NTPSnippet::MakeUnique( | 413 std::unique_ptr<NTPSnippet> NTPSnippet::MakeUnique( |
| 377 const std::vector<std::string>& ids, int remote_category_id) { | 414 const std::vector<std::string>& ids, int remote_category_id) { |
| 378 return base::WrapUnique(new NTPSnippet(ids, remote_category_id)); | 415 return base::WrapUnique(new NTPSnippet(ids, remote_category_id)); |
| 379 } | 416 } |
| 380 | 417 |
| 381 } // namespace ntp_snippets | 418 } // namespace ntp_snippets |
| OLD | NEW |