send_tab_to_self_entry.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/send_tab_to_self/send_tab_to_self_entry.h"
  5. #include <memory>
  6. #include "base/check.h"
  7. #include "base/guid.h"
  8. #include "base/memory/ptr_util.h"
  9. #include "base/strings/string_util.h"
  10. #include "components/send_tab_to_self/proto/send_tab_to_self.pb.h"
  11. #include "components/sync/protocol/send_tab_to_self_specifics.pb.h"
  12. namespace send_tab_to_self {
  13. namespace {
  14. // Converts a time object to the format used in sync protobufs (ms since the
  15. // Windows epoch).
  16. int64_t TimeToProtoTime(const base::Time t) {
  17. return t.ToDeltaSinceWindowsEpoch().InMicroseconds();
  18. }
  19. // Converts a time field from sync protobufs to a time object.
  20. base::Time ProtoTimeToTime(int64_t proto_t) {
  21. return base::Time::FromDeltaSinceWindowsEpoch(base::Microseconds(proto_t));
  22. }
  23. } // namespace
  24. SendTabToSelfEntry::SendTabToSelfEntry(
  25. const std::string& guid,
  26. const GURL& url,
  27. const std::string& title,
  28. base::Time shared_time,
  29. const std::string& device_name,
  30. const std::string& target_device_sync_cache_guid)
  31. : guid_(guid),
  32. url_(url),
  33. title_(title),
  34. device_name_(device_name),
  35. target_device_sync_cache_guid_(target_device_sync_cache_guid),
  36. shared_time_(shared_time),
  37. notification_dismissed_(false),
  38. opened_(false) {
  39. DCHECK(!guid_.empty());
  40. DCHECK(url_.is_valid());
  41. }
  42. SendTabToSelfEntry::~SendTabToSelfEntry() {}
  43. SendTabToSelfEntry::SendTabToSelfEntry(const SendTabToSelfEntry&) = default;
  44. const std::string& SendTabToSelfEntry::GetGUID() const {
  45. return guid_;
  46. }
  47. const GURL& SendTabToSelfEntry::GetURL() const {
  48. return url_;
  49. }
  50. const std::string& SendTabToSelfEntry::GetTitle() const {
  51. return title_;
  52. }
  53. base::Time SendTabToSelfEntry::GetSharedTime() const {
  54. return shared_time_;
  55. }
  56. const std::string& SendTabToSelfEntry::GetDeviceName() const {
  57. return device_name_;
  58. }
  59. const std::string& SendTabToSelfEntry::GetTargetDeviceSyncCacheGuid() const {
  60. return target_device_sync_cache_guid_;
  61. }
  62. bool SendTabToSelfEntry::IsOpened() const {
  63. return opened_;
  64. }
  65. void SendTabToSelfEntry::MarkOpened() {
  66. opened_ = true;
  67. }
  68. void SendTabToSelfEntry::SetNotificationDismissed(bool notification_dismissed) {
  69. notification_dismissed_ = notification_dismissed;
  70. }
  71. bool SendTabToSelfEntry::GetNotificationDismissed() const {
  72. return notification_dismissed_;
  73. }
  74. SendTabToSelfLocal SendTabToSelfEntry::AsLocalProto() const {
  75. SendTabToSelfLocal local_entry;
  76. auto* pb_entry = local_entry.mutable_specifics();
  77. pb_entry->set_guid(GetGUID());
  78. pb_entry->set_title(GetTitle());
  79. pb_entry->set_url(GetURL().spec());
  80. pb_entry->set_shared_time_usec(TimeToProtoTime(GetSharedTime()));
  81. pb_entry->set_device_name(GetDeviceName());
  82. pb_entry->set_target_device_sync_cache_guid(GetTargetDeviceSyncCacheGuid());
  83. pb_entry->set_opened(IsOpened());
  84. pb_entry->set_notification_dismissed(GetNotificationDismissed());
  85. return local_entry;
  86. }
  87. std::unique_ptr<SendTabToSelfEntry> SendTabToSelfEntry::FromProto(
  88. const sync_pb::SendTabToSelfSpecifics& pb_entry,
  89. base::Time now) {
  90. std::string guid(pb_entry.guid());
  91. if (guid.empty()) {
  92. return nullptr;
  93. }
  94. GURL url(pb_entry.url());
  95. if (!url.is_valid()) {
  96. return nullptr;
  97. }
  98. base::Time shared_time = ProtoTimeToTime(pb_entry.shared_time_usec());
  99. if (shared_time > now) {
  100. shared_time = now;
  101. }
  102. // Protobuf parsing enforces utf8 encoding for all strings.
  103. auto entry = std::make_unique<SendTabToSelfEntry>(
  104. guid, url, pb_entry.title(), shared_time, pb_entry.device_name(),
  105. pb_entry.target_device_sync_cache_guid());
  106. if (pb_entry.opened()) {
  107. entry->MarkOpened();
  108. }
  109. if (pb_entry.notification_dismissed()) {
  110. entry->SetNotificationDismissed(true);
  111. }
  112. return entry;
  113. }
  114. std::unique_ptr<SendTabToSelfEntry> SendTabToSelfEntry::FromLocalProto(
  115. const SendTabToSelfLocal& local_entry,
  116. base::Time now) {
  117. // No fields are currently read from the local proto.
  118. return FromProto(local_entry.specifics(), now);
  119. }
  120. bool SendTabToSelfEntry::IsExpired(base::Time current_time) const {
  121. return (current_time.ToDeltaSinceWindowsEpoch() -
  122. GetSharedTime().ToDeltaSinceWindowsEpoch() >=
  123. kExpiryTime);
  124. }
  125. std::unique_ptr<SendTabToSelfEntry> SendTabToSelfEntry::FromRequiredFields(
  126. const std::string& guid,
  127. const GURL& url,
  128. const std::string& target_device_sync_cache_guid) {
  129. if (guid.empty() || !url.is_valid()) {
  130. return nullptr;
  131. }
  132. return std::make_unique<SendTabToSelfEntry>(guid, url, "", base::Time(), "",
  133. target_device_sync_cache_guid);
  134. }
  135. } // namespace send_tab_to_self