synced_bookmark_tracker_entity.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. #ifndef COMPONENTS_SYNC_BOOKMARKS_SYNCED_BOOKMARK_TRACKER_ENTITY_H_
  5. #define COMPONENTS_SYNC_BOOKMARKS_SYNCED_BOOKMARK_TRACKER_ENTITY_H_
  6. #include <string>
  7. #include "base/memory/raw_ptr.h"
  8. #include "components/sync/base/client_tag_hash.h"
  9. #include "components/sync/protocol/entity_metadata.pb.h"
  10. namespace sync_pb {
  11. class EntitySpecifics;
  12. } // namespace sync_pb
  13. namespace bookmarks {
  14. class BookmarkNode;
  15. } // namespace bookmarks
  16. namespace syncer {
  17. struct EntityData;
  18. } // namespace syncer
  19. namespace sync_bookmarks {
  20. // This class manages the metadata corresponding to an individual BookmarkNode
  21. // instance. It is analogous to the more generic syncer::ProcessorEntity, which
  22. // is not reused for bookmarks for historic reasons.
  23. class SyncedBookmarkTrackerEntity {
  24. public:
  25. // |bookmark_node| can be null for tombstones.
  26. SyncedBookmarkTrackerEntity(const bookmarks::BookmarkNode* bookmark_node,
  27. sync_pb::EntityMetadata metadata);
  28. SyncedBookmarkTrackerEntity(const SyncedBookmarkTrackerEntity&) = delete;
  29. SyncedBookmarkTrackerEntity(SyncedBookmarkTrackerEntity&&) = delete;
  30. ~SyncedBookmarkTrackerEntity();
  31. SyncedBookmarkTrackerEntity& operator=(const SyncedBookmarkTrackerEntity&) =
  32. delete;
  33. SyncedBookmarkTrackerEntity& operator=(SyncedBookmarkTrackerEntity&&) =
  34. delete;
  35. // Returns true if this data is out of sync with the server.
  36. // A commit may or may not be in progress at this time.
  37. bool IsUnsynced() const;
  38. // Check whether |data| matches the stored specifics hash. It also compares
  39. // parent information (which is included in specifics).
  40. bool MatchesData(const syncer::EntityData& data) const;
  41. // Check whether |specifics| matches the stored specifics_hash.
  42. bool MatchesSpecificsHash(const sync_pb::EntitySpecifics& specifics) const;
  43. // Check whether |favicon_png_bytes| matches the stored
  44. // bookmark_favicon_hash.
  45. bool MatchesFaviconHash(const std::string& favicon_png_bytes) const;
  46. // Returns null for tombstones.
  47. const bookmarks::BookmarkNode* bookmark_node() const {
  48. return bookmark_node_;
  49. }
  50. // Used in local deletions to mark and entity as a tombstone.
  51. void clear_bookmark_node() { bookmark_node_ = nullptr; }
  52. // Used when replacing a node in order to update its otherwise immutable
  53. // GUID.
  54. void set_bookmark_node(const bookmarks::BookmarkNode* bookmark_node) {
  55. bookmark_node_ = bookmark_node;
  56. }
  57. const sync_pb::EntityMetadata& metadata() const { return metadata_; }
  58. sync_pb::EntityMetadata* MutableMetadata() { return &metadata_; }
  59. bool commit_may_have_started() const { return commit_may_have_started_; }
  60. void set_commit_may_have_started(bool value) {
  61. commit_may_have_started_ = value;
  62. }
  63. syncer::ClientTagHash GetClientTagHash() const;
  64. // Returns the estimate of dynamically allocated memory in bytes.
  65. size_t EstimateMemoryUsage() const;
  66. private:
  67. // Null for tombstones.
  68. raw_ptr<const bookmarks::BookmarkNode> bookmark_node_;
  69. // Serializable Sync metadata.
  70. sync_pb::EntityMetadata metadata_;
  71. // Whether there could be a commit sent to the server for this entity. It's
  72. // used to protect against sending tombstones for entities that have never
  73. // been sent to the server. It's only briefly false between the time was
  74. // first added to the tracker until the first commit request is sent to the
  75. // server. The tracker sets it to true in the constructor because this code
  76. // path is only executed in production when loading from disk.
  77. bool commit_may_have_started_ = false;
  78. };
  79. } // namespace sync_bookmarks
  80. #endif // COMPONENTS_SYNC_BOOKMARKS_SYNCED_BOOKMARK_TRACKER_ENTITY_H_