Mohamed Amir Yosef | b3f9a66 | 2018-06-19 17:04:30 | [diff] [blame] | 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 | |
| 5 | #ifndef COMPONENTS_SYNC_BOOKMARKS_BOOKMARK_MODEL_OBSERVER_IMPL_H_ |
| 6 | #define COMPONENTS_SYNC_BOOKMARKS_BOOKMARK_MODEL_OBSERVER_IMPL_H_ |
| 7 | |
| 8 | #include <set> |
Mohamed Amir Yosef | c7097a1 | 2018-07-02 18:24:16 | [diff] [blame] | 9 | #include <string> |
Mohamed Amir Yosef | b3f9a66 | 2018-06-19 17:04:30 | [diff] [blame] | 10 | |
| 11 | #include "base/callback.h" |
| 12 | #include "components/bookmarks/browser/bookmark_model_observer.h" |
Mohamed Amir Yosef | 9098655 | 2018-07-03 15:34:45 | [diff] [blame] | 13 | #include "components/bookmarks/browser/bookmark_node.h" |
Mohamed Amir Yosef | b3f9a66 | 2018-06-19 17:04:30 | [diff] [blame] | 14 | #include "url/gurl.h" |
| 15 | |
Mohamed Amir Yosef | c7097a1 | 2018-07-02 18:24:16 | [diff] [blame] | 16 | namespace syncer { |
| 17 | class UniquePosition; |
| 18 | } |
| 19 | |
Mohamed Amir Yosef | b3f9a66 | 2018-06-19 17:04:30 | [diff] [blame] | 20 | namespace sync_bookmarks { |
| 21 | |
| 22 | class SyncedBookmarkTracker; |
| 23 | |
| 24 | // Class for listening to local changes in the bookmark model and updating |
| 25 | // metadata in SyncedBookmarkTracker, such that ultimately the processor exposes |
| 26 | // those local changes to the sync engine. |
| 27 | class BookmarkModelObserverImpl : public bookmarks::BookmarkModelObserver { |
| 28 | public: |
| 29 | // |bookmark_tracker_| must not be null and must outlive this object. |
| 30 | BookmarkModelObserverImpl( |
| 31 | const base::RepeatingClosure& nudge_for_commit_closure, |
| 32 | SyncedBookmarkTracker* bookmark_tracker); |
| 33 | ~BookmarkModelObserverImpl() override; |
| 34 | |
| 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 | int old_index, |
| 42 | const bookmarks::BookmarkNode* new_parent, |
| 43 | int new_index) override; |
| 44 | void BookmarkNodeAdded(bookmarks::BookmarkModel* model, |
| 45 | const bookmarks::BookmarkNode* parent, |
| 46 | int index) override; |
Mohamed Amir Yosef | 3fd347d | 2018-07-18 13:37:51 | [diff] [blame] | 47 | void OnWillRemoveBookmarks(bookmarks::BookmarkModel* model, |
| 48 | const bookmarks::BookmarkNode* parent, |
| 49 | int old_index, |
| 50 | const bookmarks::BookmarkNode* node) override; |
Mohamed Amir Yosef | b3f9a66 | 2018-06-19 17:04:30 | [diff] [blame] | 51 | void BookmarkNodeRemoved(bookmarks::BookmarkModel* model, |
| 52 | const bookmarks::BookmarkNode* parent, |
| 53 | int old_index, |
| 54 | const bookmarks::BookmarkNode* node, |
| 55 | const std::set<GURL>& removed_urls) override; |
| 56 | void BookmarkAllUserNodesRemoved(bookmarks::BookmarkModel* model, |
| 57 | const std::set<GURL>& removed_urls) override; |
| 58 | void BookmarkNodeChanged(bookmarks::BookmarkModel* model, |
| 59 | const bookmarks::BookmarkNode* node) override; |
Mohamed Amir Yosef | 9098655 | 2018-07-03 15:34:45 | [diff] [blame] | 60 | void BookmarkMetaInfoChanged(bookmarks::BookmarkModel* model, |
| 61 | const bookmarks::BookmarkNode* node) override; |
Mohamed Amir Yosef | b3f9a66 | 2018-06-19 17:04:30 | [diff] [blame] | 62 | void BookmarkNodeFaviconChanged(bookmarks::BookmarkModel* model, |
| 63 | const bookmarks::BookmarkNode* node) override; |
| 64 | void BookmarkNodeChildrenReordered( |
| 65 | bookmarks::BookmarkModel* model, |
| 66 | const bookmarks::BookmarkNode* node) override; |
| 67 | |
| 68 | private: |
Mohamed Amir Yosef | c7097a1 | 2018-07-02 18:24:16 | [diff] [blame] | 69 | syncer::UniquePosition ComputePosition(const bookmarks::BookmarkNode& parent, |
| 70 | int index, |
| 71 | const std::string& sync_id); |
| 72 | |
Mohamed Amir Yosef | 3fd347d | 2018-07-18 13:37:51 | [diff] [blame] | 73 | // Processes the deletion of a bookmake node and updates the |
| 74 | // |bookmark_tracker_| accordingly. If |node| is a bookmark, it gets marked |
| 75 | // as deleted and that it requires a commit. If it's a folder, it recurses |
| 76 | // over all children before processing the folder itself. |
| 77 | void ProcessDelete(const bookmarks::BookmarkNode* parent, |
| 78 | const bookmarks::BookmarkNode* node); |
| 79 | |
Mohamed Amir Yosef | b3f9a66 | 2018-06-19 17:04:30 | [diff] [blame] | 80 | // Points to the tracker owned by the processor. It keeps the mapping between |
| 81 | // bookmark nodes and corresponding sync server entities. |
| 82 | SyncedBookmarkTracker* const bookmark_tracker_; |
| 83 | |
| 84 | // The callback used to inform the sync engine that there are local changes to |
| 85 | // be committed. |
| 86 | const base::RepeatingClosure nudge_for_commit_closure_; |
| 87 | |
| 88 | DISALLOW_COPY_AND_ASSIGN(BookmarkModelObserverImpl); |
| 89 | }; |
| 90 | |
| 91 | } // namespace sync_bookmarks |
| 92 | |
| 93 | #endif // COMPONENTS_SYNC_BOOKMARKS_BOOKMARK_MODEL_OBSERVER_IMPL_H_ |