blob: 322e3fc49a7d276eb46b85e08f40a8bcbc0944c3 [file] [log] [blame]
Mohamed Amir Yosefa89b53f62018-08-02 20:17:061// 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
5#ifndef COMPONENTS_SYNC_BOOKMARKS_BOOKMARK_MODEL_MERGER_H_
6#define COMPONENTS_SYNC_BOOKMARKS_BOOKMARK_MODEL_MERGER_H_
7
8#include <unordered_map>
9#include <vector>
10
11#include "base/macros.h"
12#include "components/sync/engine/non_blocking_sync_common.h"
13
14namespace bookmarks {
15class BookmarkModel;
16class BookmarkNode;
17} // namespace bookmarks
18
Mohamed Amir Yosef7266a9a2018-08-13 14:38:3319namespace favicon {
20class FaviconService;
21} // namespace favicon
22
Mohamed Amir Yosefa89b53f62018-08-02 20:17:0623namespace sync_bookmarks {
24
25class SyncedBookmarkTracker;
26
27// Responsible for merging local and remote bookmark models when bookmark sync
28// is enabled for the first time by the user (i.e. no sync metadata exists
29// locally), so we need a best-effort merge based on similarity. It implements
30// similar logic to that in BookmarkModelAssociator::AssociateModels() to be
31// used by the BookmarkModelTypeProcessor().
32class BookmarkModelMerger {
33 public:
Mohamed Amir Yosef7266a9a2018-08-13 14:38:3334 // |updates|, |bookmark_model|, |favicon_service| and |bookmark_tracker| must
35 // not be null and must outlive this object.
Mohamed Amir Yosefa89b53f62018-08-02 20:17:0636 BookmarkModelMerger(const syncer::UpdateResponseDataList* updates,
37 bookmarks::BookmarkModel* bookmark_model,
Mohamed Amir Yosef7266a9a2018-08-13 14:38:3338 favicon::FaviconService* favicon_service,
Mohamed Amir Yosefa89b53f62018-08-02 20:17:0639 SyncedBookmarkTracker* bookmark_tracker);
40
41 ~BookmarkModelMerger();
42
43 // Merges the remote bookmark model represented as the |updates| received from
44 // the sync server and local bookmark model |bookmark_model|, and updates the
45 // model and |bookmark_tracker| (all of which are injected in the constructor)
46 // accordingly. On return, there will be a 1:1 mapping between bookmark nodes
47 // and metadata entities in the injected tracker.
48 void Merge();
49
50 private:
51 // Merges a local and a remote subtrees. The input nodes are two equivalent
52 // local and remote nodes. This method tries to recursively match their
53 // children. It updates the |bookmark_tracker_| accordingly.
54 void MergeSubtree(const bookmarks::BookmarkNode* local_node,
55 const syncer::UpdateResponseData* remote_update);
56
57 // Creates a local bookmark node for a |remote_update|. The local node is
58 // created under |local_parent| at position |index|. If they the remote node
59 // has children, this method recursively creates them as well. It updates the
60 // |bookmark_tracker_| accordingly.
61 void ProcessRemoteCreation(const syncer::UpdateResponseData* remote_update,
62 const bookmarks::BookmarkNode* local_parent,
63 int index);
64
65 // Creates a server counter-part for the local node at position |index|
66 // under |parent|. If the local node has children, corresponding server nodes
67 // are created recursively. It updates the |bookmark_tracker_| accordingly and
68 // new nodes are marked to be committed.
69 void ProcessLocalCreation(const bookmarks::BookmarkNode* parent, int index);
70
71 // Gets the bookmark node corresponding to a permanent folder.
72 // |update_entity| must contain server_defined_unique_tag that is used to
73 // determine the corresponding permanent node.
74 const bookmarks::BookmarkNode* GetPermanentFolder(
75 const syncer::EntityData& update_entity) const;
76
77 const syncer::UpdateResponseDataList* const updates_;
78 bookmarks::BookmarkModel* const bookmark_model_;
Mohamed Amir Yosef7266a9a2018-08-13 14:38:3379 favicon::FaviconService* const favicon_service_;
Mohamed Amir Yosefa89b53f62018-08-02 20:17:0680 SyncedBookmarkTracker* const bookmark_tracker_;
81 // Stores the tree of |updates_| as a map from a remote node to a
82 // vector of remote children. It's constructed in the c'tor.
83 const std::unordered_map<const syncer::UpdateResponseData*,
84 std::vector<const syncer::UpdateResponseData*>>
85 updates_tree_;
86
87 DISALLOW_COPY_AND_ASSIGN(BookmarkModelMerger);
88};
89
90} // namespace sync_bookmarks
91
92#endif // COMPONENTS_SYNC_BOOKMARKS_BOOKMARK_MODEL_MERGER_H_