synced_bookmark_tracker_entity.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2022 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/sync_bookmarks/synced_bookmark_tracker_entity.h"
  5. #include <utility>
  6. #include "base/base64.h"
  7. #include "base/check.h"
  8. #include "base/hash/hash.h"
  9. #include "base/hash/sha1.h"
  10. #include "base/memory/ptr_util.h"
  11. #include "base/metrics/histogram_macros.h"
  12. #include "base/trace_event/memory_usage_estimator.h"
  13. #include "components/bookmarks/browser/bookmark_node.h"
  14. #include "components/sync/protocol/entity_data.h"
  15. #include "components/sync/protocol/entity_metadata.pb.h"
  16. #include "components/sync/protocol/entity_specifics.pb.h"
  17. #include "components/sync/protocol/proto_memory_estimations.h"
  18. #include "components/sync/protocol/unique_position.pb.h"
  19. namespace sync_bookmarks {
  20. namespace {
  21. void HashSpecifics(const sync_pb::EntitySpecifics& specifics,
  22. std::string* hash) {
  23. DCHECK_GT(specifics.ByteSize(), 0);
  24. base::Base64Encode(base::SHA1HashString(specifics.SerializeAsString()), hash);
  25. }
  26. } // namespace
  27. SyncedBookmarkTrackerEntity::SyncedBookmarkTrackerEntity(
  28. const bookmarks::BookmarkNode* bookmark_node,
  29. sync_pb::EntityMetadata metadata)
  30. : bookmark_node_(bookmark_node), metadata_(std::move(metadata)) {
  31. if (bookmark_node) {
  32. DCHECK(!metadata_.is_deleted());
  33. } else {
  34. DCHECK(metadata_.is_deleted());
  35. }
  36. }
  37. SyncedBookmarkTrackerEntity::~SyncedBookmarkTrackerEntity() = default;
  38. bool SyncedBookmarkTrackerEntity::IsUnsynced() const {
  39. return metadata_.sequence_number() > metadata_.acked_sequence_number();
  40. }
  41. bool SyncedBookmarkTrackerEntity::MatchesData(
  42. const syncer::EntityData& data) const {
  43. if (metadata_.is_deleted() || data.is_deleted()) {
  44. // In case of deletion, no need to check the specifics.
  45. return metadata_.is_deleted() == data.is_deleted();
  46. }
  47. return MatchesSpecificsHash(data.specifics);
  48. }
  49. bool SyncedBookmarkTrackerEntity::MatchesSpecificsHash(
  50. const sync_pb::EntitySpecifics& specifics) const {
  51. DCHECK(!metadata_.is_deleted());
  52. DCHECK_GT(specifics.ByteSize(), 0);
  53. std::string hash;
  54. HashSpecifics(specifics, &hash);
  55. return hash == metadata_.specifics_hash();
  56. }
  57. bool SyncedBookmarkTrackerEntity::MatchesFaviconHash(
  58. const std::string& favicon_png_bytes) const {
  59. DCHECK(!metadata_.is_deleted());
  60. return metadata_.bookmark_favicon_hash() ==
  61. base::PersistentHash(favicon_png_bytes);
  62. }
  63. syncer::ClientTagHash SyncedBookmarkTrackerEntity::GetClientTagHash() const {
  64. return syncer::ClientTagHash::FromHashed(metadata_.client_tag_hash());
  65. }
  66. size_t SyncedBookmarkTrackerEntity::EstimateMemoryUsage() const {
  67. using base::trace_event::EstimateMemoryUsage;
  68. size_t memory_usage = 0;
  69. // Include the size of the pointer to the bookmark node.
  70. memory_usage += sizeof(bookmark_node_);
  71. memory_usage += EstimateMemoryUsage(metadata_);
  72. return memory_usage;
  73. }
  74. } // namespace sync_bookmarks