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