Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / sync / glue / bookmark_change_processor.h
blob6ec5b5cd9a04ca8607bcd5134469872034778ee6
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_
8 #include <vector>
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"
18 class Profile;
20 namespace base {
21 class RefCountedMemory;
24 namespace syncer {
25 class WriteNode;
26 class WriteTransaction;
27 } // namespace syncer
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 bookmarks::BookmarkModelObserver,
36 public sync_driver::ChangeProcessor {
37 public:
38 BookmarkChangeProcessor(Profile* profile,
39 BookmarkModelAssociator* model_associator,
40 sync_driver::DataTypeErrorHandler* error_handler);
41 ~BookmarkChangeProcessor() override;
43 // bookmarks::BookmarkModelObserver:
44 // BookmarkModel -> sync API model change application.
45 void BookmarkModelLoaded(bookmarks::BookmarkModel* model,
46 bool ids_reassigned) override;
47 void BookmarkModelBeingDeleted(bookmarks::BookmarkModel* model) override;
48 void BookmarkNodeMoved(bookmarks::BookmarkModel* model,
49 const bookmarks::BookmarkNode* old_parent,
50 int old_index,
51 const bookmarks::BookmarkNode* new_parent,
52 int new_index) override;
53 void BookmarkNodeAdded(bookmarks::BookmarkModel* model,
54 const bookmarks::BookmarkNode* parent,
55 int index) override;
56 void OnWillRemoveBookmarks(bookmarks::BookmarkModel* model,
57 const bookmarks::BookmarkNode* parent,
58 int old_index,
59 const bookmarks::BookmarkNode* node) override;
60 void BookmarkNodeRemoved(bookmarks::BookmarkModel* model,
61 const bookmarks::BookmarkNode* parent,
62 int old_index,
63 const bookmarks::BookmarkNode* node,
64 const std::set<GURL>& no_longer_bookmarked) override;
65 void BookmarkAllUserNodesRemoved(bookmarks::BookmarkModel* model,
66 const std::set<GURL>& removed_urls) override;
67 void BookmarkNodeChanged(bookmarks::BookmarkModel* model,
68 const bookmarks::BookmarkNode* node) override;
69 void BookmarkMetaInfoChanged(bookmarks::BookmarkModel* model,
70 const bookmarks::BookmarkNode* node) override;
71 void BookmarkNodeFaviconChanged(bookmarks::BookmarkModel* model,
72 const bookmarks::BookmarkNode* node) override;
73 void BookmarkNodeChildrenReordered(
74 bookmarks::BookmarkModel* model,
75 const bookmarks::BookmarkNode* node) override;
77 // The change processor implementation, responsible for applying changes from
78 // the sync model to the bookmarks model.
79 void ApplyChangesFromSyncModel(
80 const syncer::BaseTransaction* trans,
81 int64 model_version,
82 const syncer::ImmutableChangeRecordList& changes) override;
84 // The following methods are static and hence may be invoked at any time, and
85 // do not depend on having a running ChangeProcessor.
86 // TODO(stanisc): considier refactoring these methods out of this class.
88 // Updates the title, URL, creation time and favicon of the bookmark |node|
89 // with data taken from the |sync_node| sync node.
90 static void UpdateBookmarkWithSyncData(const syncer::BaseNode& sync_node,
91 bookmarks::BookmarkModel* model,
92 const bookmarks::BookmarkNode* node,
93 Profile* profile);
95 // Creates a bookmark node under the given parent node from the given sync
96 // node. Returns the newly created node. The created node is placed at the
97 // specified index among the parent's children.
98 static const bookmarks::BookmarkNode* CreateBookmarkNode(
99 const syncer::BaseNode* sync_node,
100 const bookmarks::BookmarkNode* parent,
101 bookmarks::BookmarkModel* model,
102 Profile* profile,
103 int index);
105 // Overload of CreateBookmarkNode function above that helps to avoid
106 // converting / parsing the bookmark title and URL multiple times.
107 static const bookmarks::BookmarkNode* CreateBookmarkNode(
108 const base::string16& title,
109 const GURL& url,
110 const syncer::BaseNode* sync_node,
111 const bookmarks::BookmarkNode* parent,
112 bookmarks::BookmarkModel* model,
113 Profile* profile,
114 int index);
116 // Sets the favicon of the given bookmark node from the given sync node.
117 // Returns whether the favicon was set in the bookmark node.
118 // |profile| is the profile that contains the HistoryService and BookmarkModel
119 // for the bookmark in question.
120 static bool SetBookmarkFavicon(const syncer::BaseNode* sync_node,
121 const bookmarks::BookmarkNode* bookmark_node,
122 bookmarks::BookmarkModel* model,
123 Profile* profile);
125 // Applies the 1x favicon |bitmap_data| and |icon_url| to |bookmark_node|.
126 // |profile| is the profile that contains the HistoryService and BookmarkModel
127 // for the bookmark in question.
128 static void ApplyBookmarkFavicon(
129 const bookmarks::BookmarkNode* bookmark_node,
130 Profile* profile,
131 const GURL& icon_url,
132 const scoped_refptr<base::RefCountedMemory>& bitmap_data);
134 // Sets the favicon of the given sync node from the given bookmark node.
135 static void SetSyncNodeFavicon(const bookmarks::BookmarkNode* bookmark_node,
136 bookmarks::BookmarkModel* model,
137 syncer::WriteNode* sync_node);
139 // Treat the |index|th child of |parent| as a newly added node, and create a
140 // corresponding node in the sync domain using |trans|. All properties
141 // will be transferred to the new node. A node corresponding to |parent|
142 // must already exist and be associated for this call to succeed. Returns
143 // the ID of the just-created node, or if creation fails, kInvalidID.
144 static int64 CreateSyncNode(const bookmarks::BookmarkNode* parent,
145 bookmarks::BookmarkModel* model,
146 int index,
147 syncer::WriteTransaction* trans,
148 BookmarkModelAssociator* associator,
149 sync_driver::DataTypeErrorHandler* error_handler);
151 // Update |bookmark_node|'s sync node.
152 static int64 UpdateSyncNode(const bookmarks::BookmarkNode* bookmark_node,
153 bookmarks::BookmarkModel* model,
154 syncer::WriteTransaction* trans,
155 BookmarkModelAssociator* associator,
156 sync_driver::DataTypeErrorHandler* error_handler);
158 // Tombstone |topmost_sync_node| node and all its children in the sync domain
159 // using transaction |trans|. Returns the number of removed nodes.
160 static int RemoveSyncNodeHierarchy(syncer::WriteTransaction* trans,
161 syncer::WriteNode* topmost_sync_node,
162 BookmarkModelAssociator* associator);
164 // Update transaction version of |model| and |nodes| to |new_version| if
165 // it's valid.
166 static void UpdateTransactionVersion(
167 int64 new_version,
168 bookmarks::BookmarkModel* model,
169 const std::vector<const bookmarks::BookmarkNode*>& nodes);
171 protected:
172 void StartImpl() override;
174 private:
175 enum MoveOrCreate {
176 MOVE,
177 CREATE,
180 // Retrieves the meta info from the given sync node.
181 static scoped_ptr<bookmarks::BookmarkNode::MetaInfoMap> GetBookmarkMetaInfo(
182 const syncer::BaseNode* sync_node);
184 // Sets the meta info of the given sync node from the given bookmark node.
185 static void SetSyncNodeMetaInfo(const bookmarks::BookmarkNode* node,
186 syncer::WriteNode* sync_node);
188 // Helper function used to fix the position of a sync node so that it matches
189 // the position of a corresponding bookmark model node. |parent| and
190 // |index| identify the bookmark model position. |dst| is the node whose
191 // position is to be fixed. If |operation| is CREATE, treat |dst| as an
192 // uncreated node and set its position via InitByCreation(); otherwise,
193 // |dst| is treated as an existing node, and its position will be set via
194 // SetPosition(). |trans| is the transaction to which |dst| belongs. Returns
195 // false on failure.
196 static bool PlaceSyncNode(MoveOrCreate operation,
197 const bookmarks::BookmarkNode* parent,
198 int index,
199 syncer::WriteTransaction* trans,
200 syncer::WriteNode* dst,
201 BookmarkModelAssociator* associator);
203 // Copy properties (but not position) from |src| to |dst|.
204 static void UpdateSyncNodeProperties(
205 const bookmarks::BookmarkNode* src,
206 bookmarks::BookmarkModel* model,
207 syncer::WriteNode* dst,
208 sync_driver::DataTypeErrorHandler* error_handler);
210 // Helper function to encode a bookmark's favicon into raw PNG data.
211 static void EncodeFavicon(const bookmarks::BookmarkNode* src,
212 bookmarks::BookmarkModel* model,
213 scoped_refptr<base::RefCountedMemory>* dst);
215 // Remove all sync nodes, except the permanent nodes.
216 void RemoveAllSyncNodes();
218 // Remove all the sync nodes associated with |node| and its children.
219 void RemoveSyncNodeHierarchy(const bookmarks::BookmarkNode* node);
221 // Remove all children of |sync_node|. Returns the number of removed
222 // children.
223 static int RemoveAllChildNodes(syncer::WriteTransaction* trans,
224 int64 sync_id,
225 BookmarkModelAssociator* associator);
227 // Remove |sync_node|. It should not have any children.
228 static void RemoveOneSyncNode(syncer::WriteNode* sync_node,
229 BookmarkModelAssociator* associator);
231 // Creates or updates a sync node associated with |node|.
232 void CreateOrUpdateSyncNode(const bookmarks::BookmarkNode* node);
234 // Returns false if |node| should not be synced.
235 bool CanSyncNode(const bookmarks::BookmarkNode* node);
237 // The bookmark model we are processing changes from. Non-NULL when
238 // |running_| is true.
239 bookmarks::BookmarkModel* bookmark_model_;
241 Profile* profile_;
243 // The two models should be associated according to this ModelAssociator.
244 BookmarkModelAssociator* model_associator_;
246 DISALLOW_COPY_AND_ASSIGN(BookmarkChangeProcessor);
249 } // namespace browser_sync
251 #endif // CHROME_BROWSER_SYNC_GLUE_BOOKMARK_CHANGE_PROCESSOR_H_