Add ENABLE_MEDIA_ROUTER define to builds other than Android and iOS.
[chromium-blink-merge.git] / chrome / browser / sync / glue / bookmark_change_processor.h
blobe23c6e0e14e888914230a3cea388b6f02c0644dd
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.
83 // Updates the title, URL, creation time and favicon of the bookmark |node|
84 // with data taken from the |sync_node| sync node.
85 static void UpdateBookmarkWithSyncData(const syncer::BaseNode& sync_node,
86 bookmarks::BookmarkModel* model,
87 const bookmarks::BookmarkNode* node,
88 Profile* profile);
90 // Creates a bookmark node under the given parent node from the given sync
91 // node. Returns the newly created node. The created node is placed at the
92 // specified index among the parent's children.
93 static const bookmarks::BookmarkNode* CreateBookmarkNode(
94 syncer::BaseNode* sync_node,
95 const bookmarks::BookmarkNode* parent,
96 bookmarks::BookmarkModel* model,
97 Profile* profile,
98 int index);
100 // Sets the favicon of the given bookmark node from the given sync node.
101 // Returns whether the favicon was set in the bookmark node.
102 // |profile| is the profile that contains the HistoryService and BookmarkModel
103 // for the bookmark in question.
104 static bool SetBookmarkFavicon(const syncer::BaseNode* sync_node,
105 const bookmarks::BookmarkNode* bookmark_node,
106 bookmarks::BookmarkModel* model,
107 Profile* profile);
109 // Applies the 1x favicon |bitmap_data| and |icon_url| to |bookmark_node|.
110 // |profile| is the profile that contains the HistoryService and BookmarkModel
111 // for the bookmark in question.
112 static void ApplyBookmarkFavicon(
113 const bookmarks::BookmarkNode* bookmark_node,
114 Profile* profile,
115 const GURL& icon_url,
116 const scoped_refptr<base::RefCountedMemory>& bitmap_data);
118 // Sets the favicon of the given sync node from the given bookmark node.
119 static void SetSyncNodeFavicon(const bookmarks::BookmarkNode* bookmark_node,
120 bookmarks::BookmarkModel* model,
121 syncer::WriteNode* sync_node);
123 // Treat the |index|th child of |parent| as a newly added node, and create a
124 // corresponding node in the sync domain using |trans|. All properties
125 // will be transferred to the new node. A node corresponding to |parent|
126 // must already exist and be associated for this call to succeed. Returns
127 // the ID of the just-created node, or if creation fails, kInvalidID.
128 static int64 CreateSyncNode(const bookmarks::BookmarkNode* parent,
129 bookmarks::BookmarkModel* model,
130 int index,
131 syncer::WriteTransaction* trans,
132 BookmarkModelAssociator* associator,
133 sync_driver::DataTypeErrorHandler* error_handler);
135 // Update |bookmark_node|'s sync node.
136 static int64 UpdateSyncNode(const bookmarks::BookmarkNode* bookmark_node,
137 bookmarks::BookmarkModel* model,
138 syncer::WriteTransaction* trans,
139 BookmarkModelAssociator* associator,
140 sync_driver::DataTypeErrorHandler* error_handler);
142 // Update transaction version of |model| and |nodes| to |new_version| if
143 // it's valid.
144 static void UpdateTransactionVersion(
145 int64 new_version,
146 bookmarks::BookmarkModel* model,
147 const std::vector<const bookmarks::BookmarkNode*>& nodes);
149 protected:
150 void StartImpl() override;
152 private:
153 enum MoveOrCreate {
154 MOVE,
155 CREATE,
158 // Retrieves the meta info from the given sync node.
159 static scoped_ptr<bookmarks::BookmarkNode::MetaInfoMap> GetBookmarkMetaInfo(
160 const syncer::BaseNode* sync_node);
162 // Sets the meta info of the given sync node from the given bookmark node.
163 static void SetSyncNodeMetaInfo(const bookmarks::BookmarkNode* node,
164 syncer::WriteNode* sync_node);
166 // Helper function used to fix the position of a sync node so that it matches
167 // the position of a corresponding bookmark model node. |parent| and
168 // |index| identify the bookmark model position. |dst| is the node whose
169 // position is to be fixed. If |operation| is CREATE, treat |dst| as an
170 // uncreated node and set its position via InitByCreation(); otherwise,
171 // |dst| is treated as an existing node, and its position will be set via
172 // SetPosition(). |trans| is the transaction to which |dst| belongs. Returns
173 // false on failure.
174 static bool PlaceSyncNode(MoveOrCreate operation,
175 const bookmarks::BookmarkNode* parent,
176 int index,
177 syncer::WriteTransaction* trans,
178 syncer::WriteNode* dst,
179 BookmarkModelAssociator* associator);
181 // Copy properties (but not position) from |src| to |dst|.
182 static void UpdateSyncNodeProperties(const bookmarks::BookmarkNode* src,
183 bookmarks::BookmarkModel* model,
184 syncer::WriteNode* dst);
186 // Helper function to encode a bookmark's favicon into raw PNG data.
187 static void EncodeFavicon(const bookmarks::BookmarkNode* src,
188 bookmarks::BookmarkModel* model,
189 scoped_refptr<base::RefCountedMemory>* dst);
191 // Remove |sync_node|. It should not have any children
192 void RemoveOneSyncNode(syncer::WriteNode* sync_node);
194 // Remove all sync nodes, except the permanent nodes.
195 void RemoveAllSyncNodes();
197 // Remove all children of the bookmark node with bookmark node id:
198 // |topmost_node_id|.
199 void RemoveAllChildNodes(syncer::WriteTransaction* trans,
200 const int64& topmost_node_id);
202 // Remove all the sync nodes associated with |node| and its children.
203 void RemoveSyncNodeHierarchy(const bookmarks::BookmarkNode* node);
205 // Creates or updates a sync node associated with |node|.
206 void CreateOrUpdateSyncNode(const bookmarks::BookmarkNode* node);
208 // Returns false if |node| should not be synced.
209 bool CanSyncNode(const bookmarks::BookmarkNode* node);
211 // The bookmark model we are processing changes from. Non-NULL when
212 // |running_| is true.
213 bookmarks::BookmarkModel* bookmark_model_;
215 Profile* profile_;
217 // The two models should be associated according to this ModelAssociator.
218 BookmarkModelAssociator* model_associator_;
220 DISALLOW_COPY_AND_ASSIGN(BookmarkChangeProcessor);
223 } // namespace browser_sync
225 #endif // CHROME_BROWSER_SYNC_GLUE_BOOKMARK_CHANGE_PROCESSOR_H_