bookmark_remote_updates_handler.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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_REMOTE_UPDATES_HANDLER_H_
  5. #define COMPONENTS_SYNC_BOOKMARKS_BOOKMARK_REMOTE_UPDATES_HANDLER_H_
  6. #include <map>
  7. #include <vector>
  8. #include "base/memory/raw_ptr.h"
  9. #include "components/sync/engine/commit_and_get_updates_types.h"
  10. #include "components/sync_bookmarks/synced_bookmark_tracker.h"
  11. namespace bookmarks {
  12. class BookmarkModel;
  13. class BookmarkNode;
  14. } // namespace bookmarks
  15. namespace favicon {
  16. class FaviconService;
  17. } // namespace favicon
  18. namespace sync_bookmarks {
  19. // Responsible for processing one batch of remote updates received from the sync
  20. // server.
  21. class BookmarkRemoteUpdatesHandler {
  22. public:
  23. // |bookmark_model|, |favicon_service| and |bookmark_tracker| must not be null
  24. // and must outlive this object.
  25. BookmarkRemoteUpdatesHandler(bookmarks::BookmarkModel* bookmark_model,
  26. favicon::FaviconService* favicon_service,
  27. SyncedBookmarkTracker* bookmark_tracker);
  28. BookmarkRemoteUpdatesHandler(const BookmarkRemoteUpdatesHandler&) = delete;
  29. BookmarkRemoteUpdatesHandler& operator=(const BookmarkRemoteUpdatesHandler&) =
  30. delete;
  31. // Processes the updates received from the sync server in |updates| and
  32. // updates the |bookmark_model_| and |bookmark_tracker_| accordingly. If
  33. // |got_new_encryption_requirements| is true, it recommits all tracked
  34. // entities except those in |updates| which should use the latest encryption
  35. // key and hence don't need recommitting.
  36. void Process(const syncer::UpdateResponseDataList& updates,
  37. bool got_new_encryption_requirements);
  38. // Public for testing.
  39. static std::vector<const syncer::UpdateResponseData*>
  40. ReorderValidUpdatesForTest(const syncer::UpdateResponseDataList* updates);
  41. static size_t ComputeChildNodeIndexForTest(
  42. const bookmarks::BookmarkNode* parent,
  43. const sync_pb::UniquePosition& unique_position,
  44. const SyncedBookmarkTracker* bookmark_tracker);
  45. private:
  46. // Reorders incoming updates such that parent creation is before child
  47. // creation and child deletion is before parent deletion, and deletions should
  48. // come last. The returned pointers point to the elements in the original
  49. // |updates|. In this process, invalid updates are filtered out.
  50. static std::vector<const syncer::UpdateResponseData*> ReorderValidUpdates(
  51. const syncer::UpdateResponseDataList* updates);
  52. // Returns the tracked entity that should be affected by a remote change, or
  53. // null if there is none (e.g. indicating a remote creation).
  54. // |should_ignore_update| must not be null and it can be marked as true if the
  55. // function reports that the update should not be processed further (e.g. it
  56. // is invalid).
  57. const SyncedBookmarkTrackerEntity* DetermineLocalTrackedEntityToUpdate(
  58. const syncer::EntityData& update_entity,
  59. bool* should_ignore_update);
  60. // Given a remote update entity, it returns the parent bookmark node of the
  61. // corresponding node. It returns null if the parent node cannot be found.
  62. const bookmarks::BookmarkNode* GetParentNode(
  63. const syncer::EntityData& update_entity) const;
  64. // Processes a remote creation of a bookmark node.
  65. // 1. For permanent folders, they are only registered in |bookmark_tracker_|.
  66. // 2. If the nodes parent cannot be found, the remote creation update is
  67. // ignored.
  68. // 3. Otherwise, a new node is created in the local bookmark model and
  69. // registered in |bookmark_tracker_|.
  70. //
  71. // Returns the newly tracked entity or null if the creation failed.
  72. const SyncedBookmarkTrackerEntity* ProcessCreate(
  73. const syncer::UpdateResponseData& update);
  74. // Processes a remote update of a bookmark node. |update| must not be a
  75. // deletion, and the server_id must be already tracked, otherwise, it is a
  76. // creation that gets handled in ProcessCreate(). |tracked_entity| is
  77. // the tracked entity for that server_id. It is passed as a dependency instead
  78. // of performing a lookup inside ProcessUpdate() to avoid wasting CPU
  79. // cycles for doing another lookup (this code runs on the UI thread).
  80. void ProcessUpdate(const syncer::UpdateResponseData& update,
  81. const SyncedBookmarkTrackerEntity* tracked_entity);
  82. // Processes a remote delete of a bookmark node. |update_entity| must not be a
  83. // deletion. |tracked_entity| is the tracked entity for that server_id. It is
  84. // passed as a dependency instead of performing a lookup inside
  85. // ProcessDelete() to avoid wasting CPU cycles for doing another lookup
  86. // (this code runs on the UI thread).
  87. void ProcessDelete(const syncer::EntityData& update_entity,
  88. const SyncedBookmarkTrackerEntity* tracked_entity);
  89. // Processes a conflict where the bookmark has been changed both locally and
  90. // remotely. It applies the general policy the server wins except in the case
  91. // of remote deletions in which local wins. |tracked_entity| is the tracked
  92. // entity for that server_id. It is passed as a dependency instead of
  93. // performing a lookup inside ProcessDelete() to avoid wasting CPU cycles for
  94. // doing another lookup (this code runs on the UI thread). Returns the tracked
  95. // entity (if any) as a result of resolving the conflict, which is often the
  96. // same as the input |tracked_entity|, but may also be different, including
  97. // null (if the conflict led to untracking).
  98. [[nodiscard]] const SyncedBookmarkTrackerEntity* ProcessConflict(
  99. const syncer::UpdateResponseData& update,
  100. const SyncedBookmarkTrackerEntity* tracked_entity);
  101. // Recursively removes the entities corresponding to |node| and its children
  102. // from |bookmark_tracker_|.
  103. void RemoveEntityAndChildrenFromTracker(const bookmarks::BookmarkNode* node);
  104. // Initiate reupload for the update with |entity_data|. |tracked_entity| must
  105. // not be nullptr.
  106. void ReuploadEntityIfNeeded(
  107. const syncer::EntityData& entity_data,
  108. const SyncedBookmarkTrackerEntity* tracked_entity);
  109. const raw_ptr<bookmarks::BookmarkModel> bookmark_model_;
  110. const raw_ptr<favicon::FaviconService> favicon_service_;
  111. const raw_ptr<SyncedBookmarkTracker> bookmark_tracker_;
  112. };
  113. } // namespace sync_bookmarks
  114. #endif // COMPONENTS_SYNC_BOOKMARKS_BOOKMARK_REMOTE_UPDATES_HANDLER_H_