Update To 11.40.268.0
[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/sync/glue/bookmark_model_associator.h"
12 #include "chrome/browser/sync/glue/sync_backend_host.h"
13 #include "components/bookmarks/browser/bookmark_model_observer.h"
14 #include "components/bookmarks/browser/bookmark_node.h"
15 #include "components/sync_driver/change_processor.h"
16 #include "components/sync_driver/data_type_error_handler.h"
17
18 class Profile;
19
20 namespace base {
21 class RefCountedMemory;
22 }
23
24 namespace syncer {
25 class WriteNode;
26 class WriteTransaction;
27 }  // namespace syncer
28
29 namespace browser_sync {
30
31 // This class is responsible for taking changes from the BookmarkModel
32 // and applying them to the sync API 'syncable' model, and vice versa.
33 // All operations and use of this class are from the UI thread.
34 // This is currently bookmarks specific.
35 class BookmarkChangeProcessor : public BookmarkModelObserver,
36                                 public sync_driver::ChangeProcessor {
37  public:
38   BookmarkChangeProcessor(Profile* profile,
39                           BookmarkModelAssociator* model_associator,
40                           sync_driver::DataTypeErrorHandler* error_handler);
41   ~BookmarkChangeProcessor() override;
42
43   // BookmarkModelObserver implementation.
44   // BookmarkModel -> sync API model change application.
45   void BookmarkModelLoaded(BookmarkModel* model, bool ids_reassigned) override;
46   void BookmarkModelBeingDeleted(BookmarkModel* model) override;
47   void BookmarkNodeMoved(BookmarkModel* model,
48                          const BookmarkNode* old_parent,
49                          int old_index,
50                          const BookmarkNode* new_parent,
51                          int new_index) override;
52   void BookmarkNodeAdded(BookmarkModel* model,
53                          const BookmarkNode* parent,
54                          int index) override;
55   void BookmarkNodeRemoved(BookmarkModel* model,
56                            const BookmarkNode* parent,
57                            int index,
58                            const BookmarkNode* node,
59                            const std::set<GURL>& removed_urls) override;
60   void BookmarkAllUserNodesRemoved(BookmarkModel* model,
61                                    const std::set<GURL>& removed_urls) override;
62   void BookmarkNodeChanged(BookmarkModel* model,
63                            const BookmarkNode* node) override;
64   void BookmarkMetaInfoChanged(BookmarkModel* model,
65                                const BookmarkNode* node) override;
66   void BookmarkNodeFaviconChanged(BookmarkModel* model,
67                                   const BookmarkNode* node) override;
68   void BookmarkNodeChildrenReordered(BookmarkModel* model,
69                                      const BookmarkNode* node) override;
70
71   // The change processor implementation, responsible for applying changes from
72   // the sync model to the bookmarks model.
73   void ApplyChangesFromSyncModel(
74       const syncer::BaseTransaction* trans,
75       int64 model_version,
76       const syncer::ImmutableChangeRecordList& changes) override;
77
78   // The following methods are static and hence may be invoked at any time, and
79   // do not depend on having a running ChangeProcessor.
80
81   // Updates the title, URL, creation time and favicon of the bookmark |node|
82   // with data taken from the |sync_node| sync node.
83   static void UpdateBookmarkWithSyncData(
84       const syncer::BaseNode& sync_node,
85       BookmarkModel* model,
86       const BookmarkNode* node,
87       Profile* profile);
88
89   // Creates a bookmark node under the given parent node from the given sync
90   // node. Returns the newly created node.  The created node is placed at the
91   // specified index among the parent's children.
92   static const BookmarkNode* CreateBookmarkNode(
93       syncer::BaseNode* sync_node,
94       const BookmarkNode* parent,
95       BookmarkModel* model,
96       Profile* profile,
97       int index);
98
99   // Sets the favicon of the given bookmark node from the given sync node.
100   // Returns whether the favicon was set in the bookmark node.
101   // |profile| is the profile that contains the HistoryService and BookmarkModel
102   // for the bookmark in question.
103   static bool SetBookmarkFavicon(const syncer::BaseNode* sync_node,
104                                  const BookmarkNode* bookmark_node,
105                                  BookmarkModel* model,
106                                  Profile* profile);
107
108   // Applies the 1x favicon |bitmap_data| and |icon_url| to |bookmark_node|.
109   // |profile| is the profile that contains the HistoryService and BookmarkModel
110   // for the bookmark in question.
111   static void ApplyBookmarkFavicon(
112       const BookmarkNode* bookmark_node,
113       Profile* profile,
114       const GURL& icon_url,
115       const scoped_refptr<base::RefCountedMemory>& bitmap_data);
116
117   // Sets the favicon of the given sync node from the given bookmark node.
118   static void SetSyncNodeFavicon(const BookmarkNode* bookmark_node,
119                                  BookmarkModel* model,
120                                  syncer::WriteNode* sync_node);
121
122   // Treat the |index|th child of |parent| as a newly added node, and create a
123   // corresponding node in the sync domain using |trans|.  All properties
124   // will be transferred to the new node.  A node corresponding to |parent|
125   // must already exist and be associated for this call to succeed.  Returns
126   // the ID of the just-created node, or if creation fails, kInvalidID.
127   static int64 CreateSyncNode(const BookmarkNode* parent,
128                               BookmarkModel* model,
129                               int index,
130                               syncer::WriteTransaction* trans,
131                               BookmarkModelAssociator* associator,
132                               sync_driver::DataTypeErrorHandler* error_handler);
133
134   // Update |bookmark_node|'s sync node.
135   static int64 UpdateSyncNode(const BookmarkNode* bookmark_node,
136                               BookmarkModel* model,
137                               syncer::WriteTransaction* trans,
138                               BookmarkModelAssociator* associator,
139                               sync_driver::DataTypeErrorHandler* error_handler);
140
141   // Update transaction version of |model| and |nodes| to |new_version| if
142   // it's valid.
143   static void UpdateTransactionVersion(
144       int64 new_version,
145       BookmarkModel* model,
146       const std::vector<const BookmarkNode*>& nodes);
147
148  protected:
149   void StartImpl() override;
150
151  private:
152   enum MoveOrCreate {
153     MOVE,
154     CREATE,
155   };
156
157   // Retrieves the meta info from the given sync node.
158   static scoped_ptr<BookmarkNode::MetaInfoMap> GetBookmarkMetaInfo(
159       const syncer::BaseNode* sync_node);
160
161   // Sets the meta info of the given sync node from the given bookmark node.
162   static void SetSyncNodeMetaInfo(const BookmarkNode* node,
163                                   syncer::WriteNode* sync_node);
164
165   // Helper function used to fix the position of a sync node so that it matches
166   // the position of a corresponding bookmark model node. |parent| and
167   // |index| identify the bookmark model position.  |dst| is the node whose
168   // position is to be fixed.  If |operation| is CREATE, treat |dst| as an
169   // uncreated node and set its position via InitByCreation(); otherwise,
170   // |dst| is treated as an existing node, and its position will be set via
171   // SetPosition().  |trans| is the transaction to which |dst| belongs. Returns
172   // false on failure.
173   static bool PlaceSyncNode(MoveOrCreate operation,
174                             const BookmarkNode* parent,
175                             int index,
176                             syncer::WriteTransaction* trans,
177                             syncer::WriteNode* dst,
178                             BookmarkModelAssociator* associator);
179
180   // Copy properties (but not position) from |src| to |dst|.
181   static void UpdateSyncNodeProperties(const BookmarkNode* src,
182                                        BookmarkModel* model,
183                                        syncer::WriteNode* dst);
184
185   // Helper function to encode a bookmark's favicon into raw PNG data.
186   static void EncodeFavicon(const BookmarkNode* src,
187                             BookmarkModel* model,
188                             scoped_refptr<base::RefCountedMemory>* dst);
189
190   // Remove |sync_node|. It should not have any children
191   void RemoveOneSyncNode(syncer::WriteNode* sync_node);
192
193   // Remove all sync nodes, except the permanent nodes.
194   void RemoveAllSyncNodes();
195
196   // Remove all children of the bookmark node with bookmark node id:
197   // |topmost_node_id|.
198   void RemoveAllChildNodes(syncer::WriteTransaction* trans,
199                            const int64& topmost_node_id);
200
201   // Remove all the sync nodes associated with |node| and its children.
202   void RemoveSyncNodeHierarchy(const BookmarkNode* node);
203
204   // Creates or updates a sync node associated with |node|.
205   void CreateOrUpdateSyncNode(const BookmarkNode* node);
206
207   // Returns false if |node| should not be synced.
208   bool CanSyncNode(const BookmarkNode* node);
209
210   // The bookmark model we are processing changes from.  Non-NULL when
211   // |running_| is true.
212   BookmarkModel* bookmark_model_;
213
214   Profile* profile_;
215
216   // The two models should be associated according to this ModelAssociator.
217   BookmarkModelAssociator* model_associator_;
218
219   DISALLOW_COPY_AND_ASSIGN(BookmarkChangeProcessor);
220 };
221
222 }  // namespace browser_sync
223
224 #endif  // CHROME_BROWSER_SYNC_GLUE_BOOKMARK_CHANGE_PROCESSOR_H_