Chromium Code Reviews| 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 } else { | |
| 263 snippet->should_notify_ = false; | |
|
jkrcal
2017/01/04 07:34:32
nit: is this needed? should_notify_ is initially f
sfiera
2017/01/05 14:03:56
I guess not. Removed.
| |
| 264 } | |
| 265 } | |
| 266 | |
| 249 return snippet; | 267 return snippet; |
| 250 } | 268 } |
| 251 | 269 |
| 252 // static | 270 // static |
| 253 std::unique_ptr<NTPSnippet> NTPSnippet::CreateFromProto( | 271 std::unique_ptr<NTPSnippet> NTPSnippet::CreateFromProto( |
| 254 const SnippetProto& proto) { | 272 const SnippetProto& proto) { |
| 255 // Need at least the id. | 273 // Need at least the id. |
| 256 if (proto.ids_size() == 0 || proto.ids(0).empty()) { | 274 if (proto.ids_size() == 0 || proto.ids(0).empty()) { |
| 257 return nullptr; | 275 return nullptr; |
| 258 } | 276 } |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 348 if (!publisher_name_.empty()) { | 366 if (!publisher_name_.empty()) { |
| 349 source_proto->set_publisher_name(publisher_name_); | 367 source_proto->set_publisher_name(publisher_name_); |
| 350 } | 368 } |
| 351 if (amp_url_.is_valid()) { | 369 if (amp_url_.is_valid()) { |
| 352 source_proto->set_amp_url(amp_url_.spec()); | 370 source_proto->set_amp_url(amp_url_.spec()); |
| 353 } | 371 } |
| 354 | 372 |
| 355 return result; | 373 return result; |
| 356 } | 374 } |
| 357 | 375 |
| 376 ContentSuggestion NTPSnippet::ToContentSuggestion(Category category) const { | |
| 377 GURL url = url_; | |
| 378 if (base::FeatureList::IsEnabled(kPreferAmpUrlsFeature) && | |
| 379 !amp_url_.is_empty()) { | |
| 380 url = amp_url_; | |
| 381 } | |
| 382 ContentSuggestion suggestion(category, id(), url); | |
| 383 suggestion.set_title(base::UTF8ToUTF16(title_)); | |
| 384 suggestion.set_snippet_text(base::UTF8ToUTF16(snippet_)); | |
| 385 suggestion.set_publish_date(publish_date_); | |
| 386 suggestion.set_publisher_name(base::UTF8ToUTF16(publisher_name_)); | |
| 387 suggestion.set_score(score_); | |
| 388 if (should_notify_) { | |
| 389 NotificationExtra extra; | |
| 390 extra.deadline = notification_deadline_; | |
| 391 suggestion.set_notification_extra( | |
| 392 base::MakeUnique<NotificationExtra>(extra)); | |
| 393 } | |
| 394 return suggestion; | |
| 395 } | |
| 396 | |
| 358 // static | 397 // static |
| 359 base::Time NTPSnippet::TimeFromJsonString(const std::string& timestamp_str) { | 398 base::Time NTPSnippet::TimeFromJsonString(const std::string& timestamp_str) { |
| 360 int64_t timestamp; | 399 int64_t timestamp; |
| 361 if (!base::StringToInt64(timestamp_str, ×tamp)) { | 400 if (!base::StringToInt64(timestamp_str, ×tamp)) { |
| 362 // Even if there's an error in the conversion, some garbage data may still | 401 // Even if there's an error in the conversion, some garbage data may still |
| 363 // be written to the output var, so reset it. | 402 // be written to the output var, so reset it. |
| 364 DLOG(WARNING) << "Invalid json timestamp: " << timestamp_str; | 403 DLOG(WARNING) << "Invalid json timestamp: " << timestamp_str; |
| 365 timestamp = 0; | 404 timestamp = 0; |
| 366 } | 405 } |
| 367 return base::Time::UnixEpoch() + base::TimeDelta::FromSeconds(timestamp); | 406 return base::Time::UnixEpoch() + base::TimeDelta::FromSeconds(timestamp); |
| 368 } | 407 } |
| 369 | 408 |
| 370 // static | 409 // static |
| 371 std::string NTPSnippet::TimeToJsonString(const base::Time& time) { | 410 std::string NTPSnippet::TimeToJsonString(const base::Time& time) { |
| 372 return base::Int64ToString((time - base::Time::UnixEpoch()).InSeconds()); | 411 return base::Int64ToString((time - base::Time::UnixEpoch()).InSeconds()); |
| 373 } | 412 } |
| 374 | 413 |
| 375 // static | 414 // static |
| 376 std::unique_ptr<NTPSnippet> NTPSnippet::MakeUnique( | 415 std::unique_ptr<NTPSnippet> NTPSnippet::MakeUnique( |
| 377 const std::vector<std::string>& ids, int remote_category_id) { | 416 const std::vector<std::string>& ids, int remote_category_id) { |
| 378 return base::WrapUnique(new NTPSnippet(ids, remote_category_id)); | 417 return base::WrapUnique(new NTPSnippet(ids, remote_category_id)); |
| 379 } | 418 } |
| 380 | 419 |
| 381 } // namespace ntp_snippets | 420 } // namespace ntp_snippets |
| OLD | NEW |