Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / sync / glue / bookmark_change_processor.h
blob7945b5b2934370fcfbb30b7e46eac59e166460ec
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/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"
17 namespace base {
18 class RefCountedMemory;
21 namespace syncer {
22 class WriteNode;
23 class WriteTransaction;
24 } // namespace syncer
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 {
34 public:
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,
46 int old_index,
47 const BookmarkNode* new_parent,
48 int new_index) OVERRIDE;
49 virtual void BookmarkNodeAdded(BookmarkModel* model,
50 const BookmarkNode* parent,
51 int index) OVERRIDE;
52 virtual void BookmarkNodeRemoved(BookmarkModel* model,
53 const BookmarkNode* parent,
54 int index,
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,
70 int64 model_version,
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,
80 BookmarkModel* model,
81 const BookmarkNode* node,
82 Profile* profile);
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,
90 BookmarkModel* model,
91 Profile* profile,
92 int index);
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,
101 Profile* profile);
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,
108 Profile* profile,
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,
124 int index,
125 syncer::WriteTransaction* trans,
126 BookmarkModelAssociator* associator,
127 DataTypeErrorHandler* error_handler);
129 // Update transaction version of |model| and |nodes| to |new_version| if
130 // it's valid.
131 static void UpdateTransactionVersion(
132 int64 new_version,
133 BookmarkModel* model,
134 const std::vector<const BookmarkNode*>& nodes);
136 protected:
137 virtual void StartImpl(Profile* profile) OVERRIDE;
139 private:
140 enum MoveOrCreate {
141 MOVE,
142 CREATE,
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
161 // false on failure.
162 static bool PlaceSyncNode(MoveOrCreate operation,
163 const BookmarkNode* parent,
164 int index,
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_;
197 Profile* profile_;
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_