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 // This class is responsible for taking changes from the BookmarkModel
29 // and applying them to the sync API 'syncable' model, and vice versa.
30 // All operations and use of this class are from the UI thread.
31 // This is currently bookmarks specific.
32 class BookmarkChangeProcessor
: public BookmarkModelObserver
,
33 public ChangeProcessor
{
35 BookmarkChangeProcessor(BookmarkModelAssociator
* model_associator
,
36 DataTypeErrorHandler
* error_handler
);
37 virtual ~BookmarkChangeProcessor();
39 // BookmarkModelObserver implementation.
40 // BookmarkModel -> sync API model change application.
41 virtual void BookmarkModelLoaded(BookmarkModel
* model
,
42 bool ids_reassigned
) OVERRIDE
;
43 virtual void BookmarkModelBeingDeleted(BookmarkModel
* model
) OVERRIDE
;
44 virtual void BookmarkNodeMoved(BookmarkModel
* model
,
45 const BookmarkNode
* old_parent
,
47 const BookmarkNode
* new_parent
,
48 int new_index
) OVERRIDE
;
49 virtual void BookmarkNodeAdded(BookmarkModel
* model
,
50 const BookmarkNode
* parent
,
52 virtual void BookmarkNodeRemoved(BookmarkModel
* model
,
53 const BookmarkNode
* parent
,
55 const BookmarkNode
* node
) OVERRIDE
;
56 virtual void BookmarkAllNodesRemoved(BookmarkModel
* model
) OVERRIDE
;
57 virtual void BookmarkNodeChanged(BookmarkModel
* model
,
58 const BookmarkNode
* node
) OVERRIDE
;
59 virtual void BookmarkMetaInfoChanged(BookmarkModel
* model
,
60 const BookmarkNode
* node
) OVERRIDE
;
61 virtual void BookmarkNodeFaviconChanged(BookmarkModel
* model
,
62 const BookmarkNode
* node
) OVERRIDE
;
63 virtual void BookmarkNodeChildrenReordered(BookmarkModel
* model
,
64 const BookmarkNode
* node
) OVERRIDE
;
66 // The change processor implementation, responsible for applying changes from
67 // the sync model to the bookmarks model.
68 virtual void ApplyChangesFromSyncModel(
69 const syncer::BaseTransaction
* trans
,
71 const syncer::ImmutableChangeRecordList
& changes
) OVERRIDE
;
73 // The following methods are static and hence may be invoked at any time, and
74 // do not depend on having a running ChangeProcessor.
76 // Updates the title, URL, creation time and favicon of the bookmark |node|
77 // with data taken from the |sync_node| sync node.
78 static void UpdateBookmarkWithSyncData(
79 const syncer::BaseNode
& sync_node
,
81 const BookmarkNode
* node
,
84 // Creates a bookmark node under the given parent node from the given sync
85 // node. Returns the newly created node. The created node is placed at the
86 // specified index among the parent's children.
87 static const BookmarkNode
* CreateBookmarkNode(
88 syncer::BaseNode
* sync_node
,
89 const BookmarkNode
* parent
,
94 // Sets the favicon of the given bookmark node from the given sync node.
95 // Returns whether the favicon was set in the bookmark node.
96 // |profile| is the profile that contains the HistoryService and BookmarkModel
97 // for the bookmark in question.
98 static bool SetBookmarkFavicon(const syncer::BaseNode
* sync_node
,
99 const BookmarkNode
* bookmark_node
,
100 BookmarkModel
* model
,
103 // Applies the 1x favicon |bitmap_data| and |icon_url| to |bookmark_node|.
104 // |profile| is the profile that contains the HistoryService and BookmarkModel
105 // for the bookmark in question.
106 static void ApplyBookmarkFavicon(
107 const BookmarkNode
* bookmark_node
,
109 const GURL
& icon_url
,
110 const scoped_refptr
<base::RefCountedMemory
>& bitmap_data
);
112 // Sets the favicon of the given sync node from the given bookmark node.
113 static void SetSyncNodeFavicon(const BookmarkNode
* bookmark_node
,
114 BookmarkModel
* model
,
115 syncer::WriteNode
* sync_node
);
117 // Treat the |index|th child of |parent| as a newly added node, and create a
118 // corresponding node in the sync domain using |trans|. All properties
119 // will be transferred to the new node. A node corresponding to |parent|
120 // must already exist and be associated for this call to succeed. Returns
121 // the ID of the just-created node, or if creation fails, kInvalidID.
122 static int64
CreateSyncNode(const BookmarkNode
* parent
,
123 BookmarkModel
* model
,
125 syncer::WriteTransaction
* trans
,
126 BookmarkModelAssociator
* associator
,
127 DataTypeErrorHandler
* error_handler
);
129 // Update transaction version of |model| and |nodes| to |new_version| if
131 static void UpdateTransactionVersion(
133 BookmarkModel
* model
,
134 const std::vector
<const BookmarkNode
*>& nodes
);
137 virtual void StartImpl(Profile
* profile
) OVERRIDE
;
145 // Sets the meta info of the given bookmark node from the given sync node.
146 static void SetBookmarkMetaInfo(const syncer::BaseNode
* sync_node
,
147 const BookmarkNode
* bookmark_node
,
148 BookmarkModel
* bookmark_model
);
150 // Sets the meta info of the given sync node from the given bookmark node.
151 static void SetSyncNodeMetaInfo(const BookmarkNode
* node
,
152 syncer::WriteNode
* sync_node
);
154 // Helper function used to fix the position of a sync node so that it matches
155 // the position of a corresponding bookmark model node. |parent| and
156 // |index| identify the bookmark model position. |dst| is the node whose
157 // position is to be fixed. If |operation| is CREATE, treat |dst| as an
158 // uncreated node and set its position via InitByCreation(); otherwise,
159 // |dst| is treated as an existing node, and its position will be set via
160 // SetPosition(). |trans| is the transaction to which |dst| belongs. Returns
162 static bool PlaceSyncNode(MoveOrCreate operation
,
163 const BookmarkNode
* parent
,
165 syncer::WriteTransaction
* trans
,
166 syncer::WriteNode
* dst
,
167 BookmarkModelAssociator
* associator
);
169 // Copy properties (but not position) from |src| to |dst|.
170 static void UpdateSyncNodeProperties(const BookmarkNode
* src
,
171 BookmarkModel
* model
,
172 syncer::WriteNode
* dst
);
174 // Helper function to encode a bookmark's favicon into raw PNG data.
175 static void EncodeFavicon(const BookmarkNode
* src
,
176 BookmarkModel
* model
,
177 scoped_refptr
<base::RefCountedMemory
>* dst
);
179 // Remove |sync_node|. It should not have any children
180 void RemoveOneSyncNode(syncer::WriteNode
* sync_node
);
182 // Remove all sync nodes, except the permanent nodes.
183 void RemoveAllSyncNodes();
185 // Remove all children of the bookmark node with bookmark node id:
186 // |topmost_node_id|.
187 void RemoveAllChildNodes(syncer::WriteTransaction
* trans
,
188 const int64
& topmost_node_id
);
190 // Remove all the sync nodes associated with |node| and its children.
191 void RemoveSyncNodeHierarchy(const BookmarkNode
* node
);
193 // The bookmark model we are processing changes from. Non-NULL when
194 // |running_| is true.
195 BookmarkModel
* bookmark_model_
;
199 // The two models should be associated according to this ModelAssociator.
200 BookmarkModelAssociator
* model_associator_
;
202 DISALLOW_COPY_AND_ASSIGN(BookmarkChangeProcessor
);
205 } // namespace browser_sync
207 #endif // CHROME_BROWSER_SYNC_GLUE_BOOKMARK_CHANGE_PROCESSOR_H_