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 // The following methods are static and hence may be invoked at any time, and
73 // do not depend on having a running ChangeProcessor.
75 // Updates the title, URL, creation time and favicon of the bookmark |node|
76 // with data taken from the |sync_node| sync node.
77 static void UpdateBookmarkWithSyncData(
78 const syncer::BaseNode& sync_node,
80 const BookmarkNode* node,
83 // Creates a bookmark node under the given parent node from the given sync
84 // node. Returns the newly created node. The created node is placed at the
85 // specified index among the parent's children.
86 static const BookmarkNode* CreateBookmarkNode(
87 syncer::BaseNode* sync_node,
88 const BookmarkNode* parent,
93 // Sets the favicon of the given bookmark node from the given sync node.
94 // Returns whether the favicon was set in the bookmark node.
95 // |profile| is the profile that contains the HistoryService and BookmarkModel
96 // for the bookmark in question.
97 static bool SetBookmarkFavicon(const syncer::BaseNode* sync_node,
98 const BookmarkNode* bookmark_node,
102 // Applies the 1x favicon |bitmap_data| and |icon_url| to |bookmark_node|.
103 // |profile| is the profile that contains the HistoryService and BookmarkModel
104 // for the bookmark in question.
105 static void ApplyBookmarkFavicon(
106 const BookmarkNode* bookmark_node,
108 const GURL& icon_url,
109 const scoped_refptr<base::RefCountedMemory>& bitmap_data);
111 // Sets the favicon of the given sync node from the given bookmark node.
112 static void SetSyncNodeFavicon(const BookmarkNode* bookmark_node,
113 BookmarkModel* model,
114 syncer::WriteNode* sync_node);
116 // Treat the |index|th child of |parent| as a newly added node, and create a
117 // corresponding node in the sync domain using |trans|. All properties
118 // will be transferred to the new node. A node corresponding to |parent|
119 // must already exist and be associated for this call to succeed. Returns
120 // the ID of the just-created node, or if creation fails, kInvalidID.
121 static int64 CreateSyncNode(const BookmarkNode* parent,
122 BookmarkModel* model,
124 syncer::WriteTransaction* trans,
125 BookmarkModelAssociator* associator,
126 DataTypeErrorHandler* error_handler);
128 // Update transaction version of |model| and |nodes| to |new_version| if
130 static void UpdateTransactionVersion(
132 BookmarkModel* model,
133 const std::vector<const BookmarkNode*>& nodes);
136 virtual void StartImpl(Profile* profile) OVERRIDE;
144 // Helper function used to fix the position of a sync node so that it matches
145 // the position of a corresponding bookmark model node. |parent| and
146 // |index| identify the bookmark model position. |dst| is the node whose
147 // position is to be fixed. If |operation| is CREATE, treat |dst| as an
148 // uncreated node and set its position via InitByCreation(); otherwise,
149 // |dst| is treated as an existing node, and its position will be set via
150 // SetPosition(). |trans| is the transaction to which |dst| belongs. Returns
152 static bool PlaceSyncNode(MoveOrCreate operation,
153 const BookmarkNode* parent,
155 syncer::WriteTransaction* trans,
156 syncer::WriteNode* dst,
157 BookmarkModelAssociator* associator);
159 // Copy properties (but not position) from |src| to |dst|.
160 static void UpdateSyncNodeProperties(const BookmarkNode* src,
161 BookmarkModel* model,
162 syncer::WriteNode* dst);
164 // Helper function to encode a bookmark's favicon into raw PNG data.
165 static void EncodeFavicon(const BookmarkNode* src,
166 BookmarkModel* model,
167 scoped_refptr<base::RefCountedMemory>* dst);
169 // Remove |sync_node|. It should not have any children
170 void RemoveOneSyncNode(syncer::WriteNode* sync_node);
172 // Remove all sync nodes, except the permanent nodes.
173 void RemoveAllSyncNodes();
175 // Remove all children of the bookmark node with bookmark node id:
176 // |topmost_node_id|.
177 void RemoveAllChildNodes(syncer::WriteTransaction* trans,
178 const int64& topmost_node_id);
180 // Remove all the sync nodes associated with |node| and its children.
181 void RemoveSyncNodeHierarchy(const BookmarkNode* node);
183 // The bookmark model we are processing changes from. Non-NULL when
184 // |running_| is true.
185 BookmarkModel* bookmark_model_;
189 // The two models should be associated according to this ModelAssociator.
190 BookmarkModelAssociator* model_associator_;
192 DISALLOW_COPY_AND_ASSIGN(BookmarkChangeProcessor);
195 } // namespace browser_sync
197 #endif // CHROME_BROWSER_SYNC_GLUE_BOOKMARK_CHANGE_PROCESSOR_H_