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/bookmarks/bookmark_model_observer.h"
12 #include "chrome/browser/sync/glue/bookmark_model_associator.h"
13 #include "chrome/browser/sync/glue/change_processor.h"
14 #include "chrome/browser/sync/glue/data_type_error_handler.h"
15 #include "chrome/browser/sync/glue/sync_backend_host.h"
18 class RefCountedMemory
;
23 class WriteTransaction
;
26 namespace browser_sync
{
28 extern const char kBookmarkTransactionVersionKey
[];
30 // This class is responsible for taking changes from the BookmarkModel
31 // and applying them to the sync API 'syncable' model, and vice versa.
32 // All operations and use of this class are from the UI thread.
33 // This is currently bookmarks specific.
34 class BookmarkChangeProcessor
: public BookmarkModelObserver
,
35 public ChangeProcessor
{
37 BookmarkChangeProcessor(BookmarkModelAssociator
* model_associator
,
38 DataTypeErrorHandler
* error_handler
);
39 virtual ~BookmarkChangeProcessor();
41 // BookmarkModelObserver implementation.
42 // BookmarkModel -> sync API model change application.
43 virtual void Loaded(BookmarkModel
* model
, bool ids_reassigned
) OVERRIDE
;
44 virtual void BookmarkModelBeingDeleted(BookmarkModel
* model
) OVERRIDE
;
45 virtual void BookmarkNodeMoved(BookmarkModel
* model
,
46 const BookmarkNode
* old_parent
,
48 const BookmarkNode
* new_parent
,
49 int new_index
) OVERRIDE
;
50 virtual void BookmarkNodeAdded(BookmarkModel
* model
,
51 const BookmarkNode
* parent
,
53 virtual void BookmarkNodeRemoved(BookmarkModel
* model
,
54 const BookmarkNode
* parent
,
56 const BookmarkNode
* node
) OVERRIDE
;
57 virtual void BookmarkAllNodesRemoved(BookmarkModel
* model
) OVERRIDE
;
58 virtual void BookmarkNodeChanged(BookmarkModel
* model
,
59 const BookmarkNode
* node
) OVERRIDE
;
60 virtual void BookmarkNodeFaviconChanged(BookmarkModel
* model
,
61 const BookmarkNode
* node
) OVERRIDE
;
62 virtual void BookmarkNodeChildrenReordered(BookmarkModel
* model
,
63 const BookmarkNode
* node
) OVERRIDE
;
65 // The change processor implementation, responsible for applying changes from
66 // the sync model to the bookmarks model.
67 virtual void ApplyChangesFromSyncModel(
68 const syncer::BaseTransaction
* trans
,
70 const syncer::ImmutableChangeRecordList
& changes
) OVERRIDE
;
72 // Create a bookmark node corresponding to |src| if one is not already
73 // associated with |src|. Returns the node that was created or updated.
74 static const BookmarkNode
* CreateOrUpdateBookmarkNode(
75 syncer::BaseNode
* src
,
78 BookmarkModelAssociator
* model_associator
);
80 // The following methods are static and hence may be invoked at any time,
81 // and do not depend on having a running ChangeProcessor.
82 // Creates a bookmark node under the given parent node from the given sync
83 // node. Returns the newly created node.
84 static const BookmarkNode
* CreateBookmarkNode(
85 syncer::BaseNode
* sync_node
,
86 const BookmarkNode
* parent
,
91 // Sets the favicon of the given bookmark node from the given sync node.
92 // Returns whether the favicon was set in the bookmark node.
93 // |profile| is the profile that contains the HistoryService and BookmarkModel
94 // for the bookmark in question.
95 static bool SetBookmarkFavicon(syncer::BaseNode
* sync_node
,
96 const BookmarkNode
* bookmark_node
,
100 // Applies the 1x favicon |bitmap_data| and |icon_url| to |bookmark_node|.
101 // |profile| is the profile that contains the HistoryService and BookmarkModel
102 // for the bookmark in question.
103 static void ApplyBookmarkFavicon(
104 const BookmarkNode
* bookmark_node
,
106 const GURL
& icon_url
,
107 const scoped_refptr
<base::RefCountedMemory
>& bitmap_data
);
109 // Sets the favicon of the given sync node from the given bookmark node.
110 static void SetSyncNodeFavicon(const BookmarkNode
* bookmark_node
,
111 BookmarkModel
* model
,
112 syncer::WriteNode
* sync_node
);
114 // Treat the |index|th child of |parent| as a newly added node, and create a
115 // corresponding node in the sync domain using |trans|. All properties
116 // will be transferred to the new node. A node corresponding to |parent|
117 // must already exist and be associated for this call to succeed. Returns
118 // the ID of the just-created node, or if creation fails, kInvalidID.
119 static int64
CreateSyncNode(const BookmarkNode
* parent
,
120 BookmarkModel
* model
,
122 syncer::WriteTransaction
* trans
,
123 BookmarkModelAssociator
* associator
,
124 DataTypeErrorHandler
* error_handler
);
126 // Update transaction version of |model| and |nodes| to |new_version| if
128 static void UpdateTransactionVersion(
130 BookmarkModel
* model
,
131 const std::vector
<const BookmarkNode
*>& nodes
);
134 virtual void StartImpl(Profile
* profile
) OVERRIDE
;
142 // Helper function to determine the appropriate insertion index of sync node
143 // |node| under the Bookmark model node |parent|, to make the positions
144 // match up between the two models. This presumes that the predecessor of the
145 // item (in the bookmark model) has already been moved into its appropriate
147 static int CalculateBookmarkModelInsertionIndex(
148 const BookmarkNode
* parent
,
149 const syncer::BaseNode
* node
,
150 BookmarkModelAssociator
* model_associator
);
152 // Helper function used to fix the position of a sync node so that it matches
153 // the position of a corresponding bookmark model node. |parent| and
154 // |index| identify the bookmark model position. |dst| is the node whose
155 // position is to be fixed. If |operation| is CREATE, treat |dst| as an
156 // uncreated node and set its position via InitByCreation(); otherwise,
157 // |dst| is treated as an existing node, and its position will be set via
158 // SetPosition(). |trans| is the transaction to which |dst| belongs. Returns
160 static bool PlaceSyncNode(MoveOrCreate operation
,
161 const BookmarkNode
* parent
,
163 syncer::WriteTransaction
* trans
,
164 syncer::WriteNode
* dst
,
165 BookmarkModelAssociator
* associator
);
167 // Copy properties (but not position) from |src| to |dst|.
168 static void UpdateSyncNodeProperties(const BookmarkNode
* src
,
169 BookmarkModel
* model
,
170 syncer::WriteNode
* dst
);
172 // Helper function to encode a bookmark's favicon into raw PNG data.
173 static void EncodeFavicon(const BookmarkNode
* src
,
174 BookmarkModel
* model
,
175 scoped_refptr
<base::RefCountedMemory
>* dst
);
177 // Remove |sync_node|. It should not have any children
178 void RemoveOneSyncNode(syncer::WriteNode
* sync_node
);
180 // Remove all sync nodes, except the permanent nodes.
181 void RemoveAllSyncNodes();
183 // Remove all children of the bookmark node with bookmark node id:
184 // |topmost_node_id|.
185 void RemoveAllChildNodes(syncer::WriteTransaction
* trans
,
186 const int64
& topmost_node_id
);
188 // Remove all the sync nodes associated with |node| and its children.
189 void RemoveSyncNodeHierarchy(const BookmarkNode
* node
);
191 // The bookmark model we are processing changes from. Non-NULL when
192 // |running_| is true.
193 BookmarkModel
* bookmark_model_
;
197 // The two models should be associated according to this ModelAssociator.
198 BookmarkModelAssociator
* model_associator_
;
200 DISALLOW_COPY_AND_ASSIGN(BookmarkChangeProcessor
);
203 } // namespace browser_sync
205 #endif // CHROME_BROWSER_SYNC_GLUE_BOOKMARK_CHANGE_PROCESSOR_H_