bookmark_model_observer_impl.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. #ifndef COMPONENTS_SYNC_BOOKMARKS_BOOKMARK_MODEL_OBSERVER_IMPL_H_
  5. #define COMPONENTS_SYNC_BOOKMARKS_BOOKMARK_MODEL_OBSERVER_IMPL_H_
  6. #include <set>
  7. #include <string>
  8. #include "base/callback.h"
  9. #include "base/memory/raw_ptr.h"
  10. #include "components/bookmarks/browser/bookmark_model_observer.h"
  11. #include "components/bookmarks/browser/bookmark_node.h"
  12. #include "components/sync_bookmarks/synced_bookmark_tracker.h"
  13. #include "url/gurl.h"
  14. namespace sync_pb {
  15. class EntitySpecifics;
  16. }
  17. namespace syncer {
  18. class UniquePosition;
  19. }
  20. namespace sync_bookmarks {
  21. // Class for listening to local changes in the bookmark model and updating
  22. // metadata in SyncedBookmarkTracker, such that ultimately the processor exposes
  23. // those local changes to the sync engine.
  24. class BookmarkModelObserverImpl : public bookmarks::BookmarkModelObserver {
  25. public:
  26. // |bookmark_tracker_| must not be null and must outlive this object.
  27. BookmarkModelObserverImpl(
  28. const base::RepeatingClosure& nudge_for_commit_closure,
  29. base::OnceClosure on_bookmark_model_being_deleted_closure,
  30. SyncedBookmarkTracker* bookmark_tracker);
  31. BookmarkModelObserverImpl(const BookmarkModelObserverImpl&) = delete;
  32. BookmarkModelObserverImpl& operator=(const BookmarkModelObserverImpl&) =
  33. delete;
  34. ~BookmarkModelObserverImpl() override;
  35. // BookmarkModelObserver:
  36. void BookmarkModelLoaded(bookmarks::BookmarkModel* model,
  37. bool ids_reassigned) override;
  38. void BookmarkModelBeingDeleted(bookmarks::BookmarkModel* model) override;
  39. void BookmarkNodeMoved(bookmarks::BookmarkModel* model,
  40. const bookmarks::BookmarkNode* old_parent,
  41. size_t old_index,
  42. const bookmarks::BookmarkNode* new_parent,
  43. size_t new_index) override;
  44. void BookmarkNodeAdded(bookmarks::BookmarkModel* model,
  45. const bookmarks::BookmarkNode* parent,
  46. size_t index) override;
  47. void OnWillRemoveBookmarks(bookmarks::BookmarkModel* model,
  48. const bookmarks::BookmarkNode* parent,
  49. size_t old_index,
  50. const bookmarks::BookmarkNode* node) override;
  51. void BookmarkNodeRemoved(bookmarks::BookmarkModel* model,
  52. const bookmarks::BookmarkNode* parent,
  53. size_t old_index,
  54. const bookmarks::BookmarkNode* node,
  55. const std::set<GURL>& removed_urls) override;
  56. void OnWillRemoveAllUserBookmarks(bookmarks::BookmarkModel* model) override;
  57. void BookmarkAllUserNodesRemoved(bookmarks::BookmarkModel* model,
  58. const std::set<GURL>& removed_urls) override;
  59. void BookmarkNodeChanged(bookmarks::BookmarkModel* model,
  60. const bookmarks::BookmarkNode* node) override;
  61. void BookmarkMetaInfoChanged(bookmarks::BookmarkModel* model,
  62. const bookmarks::BookmarkNode* node) override;
  63. void BookmarkNodeFaviconChanged(bookmarks::BookmarkModel* model,
  64. const bookmarks::BookmarkNode* node) override;
  65. void BookmarkNodeChildrenReordered(
  66. bookmarks::BookmarkModel* model,
  67. const bookmarks::BookmarkNode* node) override;
  68. private:
  69. syncer::UniquePosition ComputePosition(const bookmarks::BookmarkNode& parent,
  70. size_t index,
  71. const std::string& sync_id);
  72. // Process a modification of a local node and updates |bookmark_tracker_|
  73. // accordingly. No-op if the commit can be optimized away, i.e. if |specifics|
  74. // are identical to the previously-known specifics (in hashed form).
  75. void ProcessUpdate(const SyncedBookmarkTrackerEntity* entity,
  76. const sync_pb::EntitySpecifics& specifics);
  77. // Processes the deletion of a bookmake node and updates the
  78. // |bookmark_tracker_| accordingly. If |node| is a bookmark, it gets marked
  79. // as deleted and that it requires a commit. If it's a folder, it recurses
  80. // over all children before processing the folder itself.
  81. void ProcessDelete(const bookmarks::BookmarkNode* node);
  82. // Returns current unique_position from sync metadata for the tracked |node|.
  83. syncer::UniquePosition GetUniquePositionForNode(
  84. const bookmarks::BookmarkNode* node) const;
  85. // Updates the unique position in sync metadata for the tracked |node| and
  86. // returns the new position. A new position is generated based on the left and
  87. // right node's positions. At least one of |prev| and |next| must be valid.
  88. syncer::UniquePosition UpdateUniquePositionForNode(
  89. const bookmarks::BookmarkNode* node,
  90. bookmarks::BookmarkModel* model,
  91. const syncer::UniquePosition& prev,
  92. const syncer::UniquePosition& next);
  93. // Updates unique positions for all children from |parent| starting from
  94. // |start_index| (must not be 0).
  95. void UpdateAllUniquePositionsStartingAt(
  96. const bookmarks::BookmarkNode* parent,
  97. bookmarks::BookmarkModel* bookmark_model,
  98. size_t start_index);
  99. // Points to the tracker owned by the processor. It keeps the mapping between
  100. // bookmark nodes and corresponding sync server entities.
  101. const raw_ptr<SyncedBookmarkTracker> bookmark_tracker_;
  102. // The callback used to inform the sync engine that there are local changes to
  103. // be committed.
  104. const base::RepeatingClosure nudge_for_commit_closure_;
  105. // The callback used to inform the processor that the bookmark is getting
  106. // deleted.
  107. base::OnceClosure on_bookmark_model_being_deleted_closure_;
  108. };
  109. } // namespace sync_bookmarks
  110. #endif // COMPONENTS_SYNC_BOOKMARKS_BOOKMARK_MODEL_OBSERVER_IMPL_H_