- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / sync / glue / bookmark_change_processor.h
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.
4
5 #ifndef CHROME_BROWSER_SYNC_GLUE_BOOKMARK_CHANGE_PROCESSOR_H_
6 #define CHROME_BROWSER_SYNC_GLUE_BOOKMARK_CHANGE_PROCESSOR_H_
7
8 #include <vector>
9
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"
16
17 namespace base {
18 class RefCountedMemory;
19 }
20
21 namespace syncer {
22 class WriteNode;
23 class WriteTransaction;
24 }  // namespace syncer
25
26 namespace browser_sync {
27
28 extern const char kBookmarkTransactionVersionKey[];
29
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 {
36  public:
37   BookmarkChangeProcessor(BookmarkModelAssociator* model_associator,
38                           DataTypeErrorHandler* error_handler);
39   virtual ~BookmarkChangeProcessor();
40
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,
47                                  int old_index,
48                                  const BookmarkNode* new_parent,
49                                  int new_index) OVERRIDE;
50   virtual void BookmarkNodeAdded(BookmarkModel* model,
51                                  const BookmarkNode* parent,
52                                  int index) OVERRIDE;
53   virtual void BookmarkNodeRemoved(BookmarkModel* model,
54                                    const BookmarkNode* parent,
55                                    int index,
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;
64
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,
69       int64 model_version,
70       const syncer::ImmutableChangeRecordList& changes) OVERRIDE;
71
72   // The following methods are static and hence may be invoked at any time, and
73   // do not depend on having a running ChangeProcessor.
74
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,
79       BookmarkModel* model,
80       const BookmarkNode* node,
81       Profile* profile);
82
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,
89       BookmarkModel* model,
90       Profile* profile,
91       int index);
92
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,
99                                  BookmarkModel* model,
100                                  Profile* profile);
101
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,
107       Profile* profile,
108       const GURL& icon_url,
109       const scoped_refptr<base::RefCountedMemory>& bitmap_data);
110
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);
115
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,
123                               int index,
124                               syncer::WriteTransaction* trans,
125                               BookmarkModelAssociator* associator,
126                               DataTypeErrorHandler* error_handler);
127
128   // Update transaction version of |model| and |nodes| to |new_version| if
129   // it's valid.
130   static void UpdateTransactionVersion(
131       int64 new_version,
132       BookmarkModel* model,
133       const std::vector<const BookmarkNode*>& nodes);
134
135  protected:
136   virtual void StartImpl(Profile* profile) OVERRIDE;
137
138  private:
139   enum MoveOrCreate {
140     MOVE,
141     CREATE,
142   };
143
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
151   // false on failure.
152   static bool PlaceSyncNode(MoveOrCreate operation,
153                             const BookmarkNode* parent,
154                             int index,
155                             syncer::WriteTransaction* trans,
156                             syncer::WriteNode* dst,
157                             BookmarkModelAssociator* associator);
158
159   // Copy properties (but not position) from |src| to |dst|.
160   static void UpdateSyncNodeProperties(const BookmarkNode* src,
161                                        BookmarkModel* model,
162                                        syncer::WriteNode* dst);
163
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);
168
169   // Remove |sync_node|. It should not have any children
170   void RemoveOneSyncNode(syncer::WriteNode* sync_node);
171
172   // Remove all sync nodes, except the permanent nodes.
173   void RemoveAllSyncNodes();
174
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);
179
180   // Remove all the sync nodes associated with |node| and its children.
181   void RemoveSyncNodeHierarchy(const BookmarkNode* node);
182
183   // The bookmark model we are processing changes from.  Non-NULL when
184   // |running_| is true.
185   BookmarkModel* bookmark_model_;
186
187   Profile* profile_;
188
189   // The two models should be associated according to this ModelAssociator.
190   BookmarkModelAssociator* model_associator_;
191
192   DISALLOW_COPY_AND_ASSIGN(BookmarkChangeProcessor);
193 };
194
195 }  // namespace browser_sync
196
197 #endif  // CHROME_BROWSER_SYNC_GLUE_BOOKMARK_CHANGE_PROCESSOR_H_