ExtensionSyncService cleanup: process uninstalls in one step
[chromium-blink-merge.git] / chrome / browser / sync / glue / bookmark_change_processor.h
blob798be4d745403d622a23a3713343631f62b2d196
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 BookmarkNodeRemoved(bookmarks::BookmarkModel* model,
57 const bookmarks::BookmarkNode* parent,
58 int index,
59 const bookmarks::BookmarkNode* node,
60 const std::set<GURL>& removed_urls) override;
61 void BookmarkAllUserNodesRemoved(bookmarks::BookmarkModel* model,
62 const std::set<GURL>& removed_urls) override;
63 void BookmarkNodeChanged(bookmarks::BookmarkModel* model,
64 const bookmarks::BookmarkNode* node) override;
65 void BookmarkMetaInfoChanged(bookmarks::BookmarkModel* model,
66 const bookmarks::BookmarkNode* node) override;
67 void BookmarkNodeFaviconChanged(bookmarks::BookmarkModel* model,
68 const bookmarks::BookmarkNode* node) override;
69 void BookmarkNodeChildrenReordered(
70 bookmarks::BookmarkModel* model,
71 const bookmarks::BookmarkNode* node) override;
73 // The change processor implementation, responsible for applying changes from
74 // the sync model to the bookmarks model.
75 void ApplyChangesFromSyncModel(
76 const syncer::BaseTransaction* trans,
77 int64 model_version,
78 const syncer::ImmutableChangeRecordList& changes) override;
80 // The following methods are static and hence may be invoked at any time, and
81 // do not depend on having a running ChangeProcessor.
82 // TODO(stanisc): considier refactoring these methods out of this class.
84 // Updates the title, URL, creation time and favicon of the bookmark |node|
85 // with data taken from the |sync_node| sync node.
86 static void UpdateBookmarkWithSyncData(const syncer::BaseNode& sync_node,
87 bookmarks::BookmarkModel* model,
88 const bookmarks::BookmarkNode* node,
89 Profile* profile);
91 // Creates a bookmark node under the given parent node from the given sync
92 // node. Returns the newly created node. The created node is placed at the
93 // specified index among the parent's children.
94 static const bookmarks::BookmarkNode* CreateBookmarkNode(
95 const syncer::BaseNode* sync_node,
96 const bookmarks::BookmarkNode* parent,
97 bookmarks::BookmarkModel* model,
98 Profile* profile,
99 int index);
101 // Overload of CreateBookmarkNode function above that helps to avoid
102 // converting / parsing the bookmark title and URL multiple times.
103 static const bookmarks::BookmarkNode* CreateBookmarkNode(
104 const base::string16& title,
105 const GURL& url,
106 const syncer::BaseNode* sync_node,
107 const bookmarks::BookmarkNode* parent,
108 bookmarks::BookmarkModel* model,
109 Profile* profile,
110 int index);
112 // Sets the favicon of the given bookmark node from the given sync node.
113 // Returns whether the favicon was set in the bookmark node.
114 // |profile| is the profile that contains the HistoryService and BookmarkModel
115 // for the bookmark in question.
116 static bool SetBookmarkFavicon(const syncer::BaseNode* sync_node,
117 const bookmarks::BookmarkNode* bookmark_node,
118 bookmarks::BookmarkModel* model,
119 Profile* profile);
121 // Applies the 1x favicon |bitmap_data| and |icon_url| to |bookmark_node|.
122 // |profile| is the profile that contains the HistoryService and BookmarkModel
123 // for the bookmark in question.
124 static void ApplyBookmarkFavicon(
125 const bookmarks::BookmarkNode* bookmark_node,
126 Profile* profile,
127 const GURL& icon_url,
128 const scoped_refptr<base::RefCountedMemory>& bitmap_data);
130 // Sets the favicon of the given sync node from the given bookmark node.
131 static void SetSyncNodeFavicon(const bookmarks::BookmarkNode* bookmark_node,
132 bookmarks::BookmarkModel* model,
133 syncer::WriteNode* sync_node);
135 // Treat the |index|th child of |parent| as a newly added node, and create a
136 // corresponding node in the sync domain using |trans|. All properties
137 // will be transferred to the new node. A node corresponding to |parent|
138 // must already exist and be associated for this call to succeed. Returns
139 // the ID of the just-created node, or if creation fails, kInvalidID.
140 static int64 CreateSyncNode(const bookmarks::BookmarkNode* parent,
141 bookmarks::BookmarkModel* model,
142 int index,
143 syncer::WriteTransaction* trans,
144 BookmarkModelAssociator* associator,
145 sync_driver::DataTypeErrorHandler* error_handler);
147 // Update |bookmark_node|'s sync node.
148 static int64 UpdateSyncNode(const bookmarks::BookmarkNode* bookmark_node,
149 bookmarks::BookmarkModel* model,
150 syncer::WriteTransaction* trans,
151 BookmarkModelAssociator* associator,
152 sync_driver::DataTypeErrorHandler* error_handler);
154 // Tombstone |topmost_sync_node| node and all its children in the sync domain
155 // using transaction |trans|. Returns the number of removed nodes.
156 static int RemoveSyncNodeHierarchy(syncer::WriteTransaction* trans,
157 syncer::WriteNode* topmost_sync_node,
158 BookmarkModelAssociator* associator);
160 // Update transaction version of |model| and |nodes| to |new_version| if
161 // it's valid.
162 static void UpdateTransactionVersion(
163 int64 new_version,
164 bookmarks::BookmarkModel* model,
165 const std::vector<const bookmarks::BookmarkNode*>& nodes);
167 protected:
168 void StartImpl() override;
170 private:
171 enum MoveOrCreate {
172 MOVE,
173 CREATE,
176 // Retrieves the meta info from the given sync node.
177 static scoped_ptr<bookmarks::BookmarkNode::MetaInfoMap> GetBookmarkMetaInfo(
178 const syncer::BaseNode* sync_node);
180 // Sets the meta info of the given sync node from the given bookmark node.
181 static void SetSyncNodeMetaInfo(const bookmarks::BookmarkNode* node,
182 syncer::WriteNode* sync_node);
184 // Helper function used to fix the position of a sync node so that it matches
185 // the position of a corresponding bookmark model node. |parent| and
186 // |index| identify the bookmark model position. |dst| is the node whose
187 // position is to be fixed. If |operation| is CREATE, treat |dst| as an
188 // uncreated node and set its position via InitByCreation(); otherwise,
189 // |dst| is treated as an existing node, and its position will be set via
190 // SetPosition(). |trans| is the transaction to which |dst| belongs. Returns
191 // false on failure.
192 static bool PlaceSyncNode(MoveOrCreate operation,
193 const bookmarks::BookmarkNode* parent,
194 int index,
195 syncer::WriteTransaction* trans,
196 syncer::WriteNode* dst,
197 BookmarkModelAssociator* associator);
199 // Copy properties (but not position) from |src| to |dst|.
200 static void UpdateSyncNodeProperties(
201 const bookmarks::BookmarkNode* src,
202 bookmarks::BookmarkModel* model,
203 syncer::WriteNode* dst,
204 sync_driver::DataTypeErrorHandler* error_handler);
206 // Helper function to encode a bookmark's favicon into raw PNG data.
207 static void EncodeFavicon(const bookmarks::BookmarkNode* src,
208 bookmarks::BookmarkModel* model,
209 scoped_refptr<base::RefCountedMemory>* dst);
211 // Remove all sync nodes, except the permanent nodes.
212 void RemoveAllSyncNodes();
214 // Remove all the sync nodes associated with |node| and its children.
215 void RemoveSyncNodeHierarchy(const bookmarks::BookmarkNode* node);
217 // Remove all children of |sync_node|. Returns the number of removed
218 // children.
219 static int RemoveAllChildNodes(syncer::WriteTransaction* trans,
220 int64 sync_id,
221 BookmarkModelAssociator* associator);
223 // Remove |sync_node|. It should not have any children.
224 static void RemoveOneSyncNode(syncer::WriteNode* sync_node,
225 BookmarkModelAssociator* associator);
227 // Creates or updates a sync node associated with |node|.
228 void CreateOrUpdateSyncNode(const bookmarks::BookmarkNode* node);
230 // Returns false if |node| should not be synced.
231 bool CanSyncNode(const bookmarks::BookmarkNode* node);
233 // The bookmark model we are processing changes from. Non-NULL when
234 // |running_| is true.
235 bookmarks::BookmarkModel* bookmark_model_;
237 Profile* profile_;
239 // The two models should be associated according to this ModelAssociator.
240 BookmarkModelAssociator* model_associator_;
242 DISALLOW_COPY_AND_ASSIGN(BookmarkChangeProcessor);
245 } // namespace browser_sync
247 #endif // CHROME_BROWSER_SYNC_GLUE_BOOKMARK_CHANGE_PROCESSOR_H_