1 // Copyright (c) 2012 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.
5 #ifndef CHROME_BROWSER_SYNC_GLUE_BOOKMARK_CHANGE_PROCESSOR_H_
6 #define CHROME_BROWSER_SYNC_GLUE_BOOKMARK_CHANGE_PROCESSOR_H_
10 #include "base/compiler_specific.h"
11 #include "chrome/browser/sync/glue/bookmark_model_associator.h"
12 #include "chrome/browser/sync/glue/sync_backend_host.h"
13 #include "components/bookmarks/browser/bookmark_model_observer.h"
14 #include "components/bookmarks/browser/bookmark_node.h"
15 #include "components/sync_driver/change_processor.h"
16 #include "components/sync_driver/data_type_error_handler.h"
21 class RefCountedMemory;
26 class WriteTransaction;
29 namespace browser_sync {
31 // This class is responsible for taking changes from the BookmarkModel
32 // and applying them to the sync API 'syncable' model, and vice versa.
33 // All operations and use of this class are from the UI thread.
34 // This is currently bookmarks specific.
35 class BookmarkChangeProcessor : public BookmarkModelObserver,
36 public sync_driver::ChangeProcessor {
38 BookmarkChangeProcessor(Profile* profile,
39 BookmarkModelAssociator* model_associator,
40 sync_driver::DataTypeErrorHandler* error_handler);
41 ~BookmarkChangeProcessor() override;
43 // BookmarkModelObserver implementation.
44 // BookmarkModel -> sync API model change application.
45 void BookmarkModelLoaded(BookmarkModel* model, bool ids_reassigned) override;
46 void BookmarkModelBeingDeleted(BookmarkModel* model) override;
47 void BookmarkNodeMoved(BookmarkModel* model,
48 const BookmarkNode* old_parent,
50 const BookmarkNode* new_parent,
51 int new_index) override;
52 void BookmarkNodeAdded(BookmarkModel* model,
53 const BookmarkNode* parent,
55 void BookmarkNodeRemoved(BookmarkModel* model,
56 const BookmarkNode* parent,
58 const BookmarkNode* node,
59 const std::set<GURL>& removed_urls) override;
60 void BookmarkAllUserNodesRemoved(BookmarkModel* model,
61 const std::set<GURL>& removed_urls) override;
62 void BookmarkNodeChanged(BookmarkModel* model,
63 const BookmarkNode* node) override;
64 void BookmarkMetaInfoChanged(BookmarkModel* model,
65 const BookmarkNode* node) override;
66 void BookmarkNodeFaviconChanged(BookmarkModel* model,
67 const BookmarkNode* node) override;
68 void BookmarkNodeChildrenReordered(BookmarkModel* model,
69 const BookmarkNode* node) override;
71 // The change processor implementation, responsible for applying changes from
72 // the sync model to the bookmarks model.
73 void ApplyChangesFromSyncModel(
74 const syncer::BaseTransaction* trans,
76 const syncer::ImmutableChangeRecordList& changes) override;
78 // The following methods are static and hence may be invoked at any time, and
79 // do not depend on having a running ChangeProcessor.
81 // Updates the title, URL, creation time and favicon of the bookmark |node|
82 // with data taken from the |sync_node| sync node.
83 static void UpdateBookmarkWithSyncData(
84 const syncer::BaseNode& sync_node,
86 const BookmarkNode* node,
89 // Creates a bookmark node under the given parent node from the given sync
90 // node. Returns the newly created node. The created node is placed at the
91 // specified index among the parent's children.
92 static const BookmarkNode* CreateBookmarkNode(
93 syncer::BaseNode* sync_node,
94 const BookmarkNode* parent,
99 // Sets the favicon of the given bookmark node from the given sync node.
100 // Returns whether the favicon was set in the bookmark node.
101 // |profile| is the profile that contains the HistoryService and BookmarkModel
102 // for the bookmark in question.
103 static bool SetBookmarkFavicon(const syncer::BaseNode* sync_node,
104 const BookmarkNode* bookmark_node,
105 BookmarkModel* model,
108 // Applies the 1x favicon |bitmap_data| and |icon_url| to |bookmark_node|.
109 // |profile| is the profile that contains the HistoryService and BookmarkModel
110 // for the bookmark in question.
111 static void ApplyBookmarkFavicon(
112 const BookmarkNode* bookmark_node,
114 const GURL& icon_url,
115 const scoped_refptr<base::RefCountedMemory>& bitmap_data);
117 // Sets the favicon of the given sync node from the given bookmark node.
118 static void SetSyncNodeFavicon(const BookmarkNode* bookmark_node,
119 BookmarkModel* model,
120 syncer::WriteNode* sync_node);
122 // Treat the |index|th child of |parent| as a newly added node, and create a
123 // corresponding node in the sync domain using |trans|. All properties
124 // will be transferred to the new node. A node corresponding to |parent|
125 // must already exist and be associated for this call to succeed. Returns
126 // the ID of the just-created node, or if creation fails, kInvalidID.
127 static int64 CreateSyncNode(const BookmarkNode* parent,
128 BookmarkModel* model,
130 syncer::WriteTransaction* trans,
131 BookmarkModelAssociator* associator,
132 sync_driver::DataTypeErrorHandler* error_handler);
134 // Update |bookmark_node|'s sync node.
135 static int64 UpdateSyncNode(const BookmarkNode* bookmark_node,
136 BookmarkModel* model,
137 syncer::WriteTransaction* trans,
138 BookmarkModelAssociator* associator,
139 sync_driver::DataTypeErrorHandler* error_handler);
141 // Update transaction version of |model| and |nodes| to |new_version| if
143 static void UpdateTransactionVersion(
145 BookmarkModel* model,
146 const std::vector<const BookmarkNode*>& nodes);
149 void StartImpl() override;
157 // Retrieves the meta info from the given sync node.
158 static scoped_ptr<BookmarkNode::MetaInfoMap> GetBookmarkMetaInfo(
159 const syncer::BaseNode* sync_node);
161 // Sets the meta info of the given sync node from the given bookmark node.
162 static void SetSyncNodeMetaInfo(const BookmarkNode* node,
163 syncer::WriteNode* sync_node);
165 // Helper function used to fix the position of a sync node so that it matches
166 // the position of a corresponding bookmark model node. |parent| and
167 // |index| identify the bookmark model position. |dst| is the node whose
168 // position is to be fixed. If |operation| is CREATE, treat |dst| as an
169 // uncreated node and set its position via InitByCreation(); otherwise,
170 // |dst| is treated as an existing node, and its position will be set via
171 // SetPosition(). |trans| is the transaction to which |dst| belongs. Returns
173 static bool PlaceSyncNode(MoveOrCreate operation,
174 const BookmarkNode* parent,
176 syncer::WriteTransaction* trans,
177 syncer::WriteNode* dst,
178 BookmarkModelAssociator* associator);
180 // Copy properties (but not position) from |src| to |dst|.
181 static void UpdateSyncNodeProperties(const BookmarkNode* src,
182 BookmarkModel* model,
183 syncer::WriteNode* dst);
185 // Helper function to encode a bookmark's favicon into raw PNG data.
186 static void EncodeFavicon(const BookmarkNode* src,
187 BookmarkModel* model,
188 scoped_refptr<base::RefCountedMemory>* dst);
190 // Remove |sync_node|. It should not have any children
191 void RemoveOneSyncNode(syncer::WriteNode* sync_node);
193 // Remove all sync nodes, except the permanent nodes.
194 void RemoveAllSyncNodes();
196 // Remove all children of the bookmark node with bookmark node id:
197 // |topmost_node_id|.
198 void RemoveAllChildNodes(syncer::WriteTransaction* trans,
199 const int64& topmost_node_id);
201 // Remove all the sync nodes associated with |node| and its children.
202 void RemoveSyncNodeHierarchy(const BookmarkNode* node);
204 // Creates or updates a sync node associated with |node|.
205 void CreateOrUpdateSyncNode(const BookmarkNode* node);
207 // Returns false if |node| should not be synced.
208 bool CanSyncNode(const BookmarkNode* node);
210 // The bookmark model we are processing changes from. Non-NULL when
211 // |running_| is true.
212 BookmarkModel* bookmark_model_;
216 // The two models should be associated according to this ModelAssociator.
217 BookmarkModelAssociator* model_associator_;
219 DISALLOW_COPY_AND_ASSIGN(BookmarkChangeProcessor);
222 } // namespace browser_sync
224 #endif // CHROME_BROWSER_SYNC_GLUE_BOOKMARK_CHANGE_PROCESSOR_H_