| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_SYNC_GLUE_BOOKMARK_CHANGE_PROCESSOR_H_ | 5 #ifndef CHROME_BROWSER_SYNC_GLUE_BOOKMARK_CHANGE_PROCESSOR_H_ |
| 6 #define CHROME_BROWSER_SYNC_GLUE_BOOKMARK_CHANGE_PROCESSOR_H_ | 6 #define CHROME_BROWSER_SYNC_GLUE_BOOKMARK_CHANGE_PROCESSOR_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 12 #include "chrome/browser/bookmarks/bookmark_model_observer.h" | 12 #include "chrome/browser/bookmarks/bookmark_model_observer.h" |
| 13 #include "chrome/browser/sync/glue/bookmark_model_associator.h" | 13 #include "chrome/browser/sync/glue/bookmark_model_associator.h" |
| 14 #include "chrome/browser/sync/glue/change_processor.h" | 14 #include "chrome/browser/sync/glue/change_processor.h" |
| 15 #include "chrome/browser/sync/glue/data_type_error_handler.h" | 15 #include "chrome/browser/sync/glue/data_type_error_handler.h" |
| 16 #include "chrome/browser/sync/glue/sync_backend_host.h" | 16 #include "chrome/browser/sync/glue/sync_backend_host.h" |
| 17 | 17 |
| 18 namespace csync { | 18 namespace syncer { |
| 19 class WriteNode; | 19 class WriteNode; |
| 20 class WriteTransaction; | 20 class WriteTransaction; |
| 21 } // namespace csync | 21 } // namespace syncer |
| 22 | 22 |
| 23 namespace browser_sync { | 23 namespace browser_sync { |
| 24 | 24 |
| 25 // This class is responsible for taking changes from the BookmarkModel | 25 // This class is responsible for taking changes from the BookmarkModel |
| 26 // and applying them to the csync 'syncable' model, and vice versa. | 26 // and applying them to the sync API 'syncable' model, and vice versa. |
| 27 // All operations and use of this class are from the UI thread. | 27 // All operations and use of this class are from the UI thread. |
| 28 // This is currently bookmarks specific. | 28 // This is currently bookmarks specific. |
| 29 class BookmarkChangeProcessor : public BookmarkModelObserver, | 29 class BookmarkChangeProcessor : public BookmarkModelObserver, |
| 30 public ChangeProcessor { | 30 public ChangeProcessor { |
| 31 public: | 31 public: |
| 32 BookmarkChangeProcessor(BookmarkModelAssociator* model_associator, | 32 BookmarkChangeProcessor(BookmarkModelAssociator* model_associator, |
| 33 DataTypeErrorHandler* error_handler); | 33 DataTypeErrorHandler* error_handler); |
| 34 virtual ~BookmarkChangeProcessor() {} | 34 virtual ~BookmarkChangeProcessor() {} |
| 35 | 35 |
| 36 // BookmarkModelObserver implementation. | 36 // BookmarkModelObserver implementation. |
| 37 // BookmarkModel -> csync model change application. | 37 // BookmarkModel -> sync API model change application. |
| 38 virtual void Loaded(BookmarkModel* model, bool ids_reassigned) OVERRIDE; | 38 virtual void Loaded(BookmarkModel* model, bool ids_reassigned) OVERRIDE; |
| 39 virtual void BookmarkModelBeingDeleted(BookmarkModel* model) OVERRIDE; | 39 virtual void BookmarkModelBeingDeleted(BookmarkModel* model) OVERRIDE; |
| 40 virtual void BookmarkNodeMoved(BookmarkModel* model, | 40 virtual void BookmarkNodeMoved(BookmarkModel* model, |
| 41 const BookmarkNode* old_parent, | 41 const BookmarkNode* old_parent, |
| 42 int old_index, | 42 int old_index, |
| 43 const BookmarkNode* new_parent, | 43 const BookmarkNode* new_parent, |
| 44 int new_index) OVERRIDE; | 44 int new_index) OVERRIDE; |
| 45 virtual void BookmarkNodeAdded(BookmarkModel* model, | 45 virtual void BookmarkNodeAdded(BookmarkModel* model, |
| 46 const BookmarkNode* parent, | 46 const BookmarkNode* parent, |
| 47 int index) OVERRIDE; | 47 int index) OVERRIDE; |
| 48 virtual void BookmarkNodeRemoved(BookmarkModel* model, | 48 virtual void BookmarkNodeRemoved(BookmarkModel* model, |
| 49 const BookmarkNode* parent, | 49 const BookmarkNode* parent, |
| 50 int index, | 50 int index, |
| 51 const BookmarkNode* node) OVERRIDE; | 51 const BookmarkNode* node) OVERRIDE; |
| 52 virtual void BookmarkNodeChanged(BookmarkModel* model, | 52 virtual void BookmarkNodeChanged(BookmarkModel* model, |
| 53 const BookmarkNode* node) OVERRIDE; | 53 const BookmarkNode* node) OVERRIDE; |
| 54 virtual void BookmarkNodeFaviconChanged(BookmarkModel* model, | 54 virtual void BookmarkNodeFaviconChanged(BookmarkModel* model, |
| 55 const BookmarkNode* node) OVERRIDE; | 55 const BookmarkNode* node) OVERRIDE; |
| 56 virtual void BookmarkNodeChildrenReordered(BookmarkModel* model, | 56 virtual void BookmarkNodeChildrenReordered(BookmarkModel* model, |
| 57 const BookmarkNode* node) OVERRIDE; | 57 const BookmarkNode* node) OVERRIDE; |
| 58 | 58 |
| 59 // The change processor implementation, responsible for applying changes from | 59 // The change processor implementation, responsible for applying changes from |
| 60 // the sync model to the bookmarks model. | 60 // the sync model to the bookmarks model. |
| 61 virtual void ApplyChangesFromSyncModel( | 61 virtual void ApplyChangesFromSyncModel( |
| 62 const csync::BaseTransaction* trans, | 62 const syncer::BaseTransaction* trans, |
| 63 const csync::ImmutableChangeRecordList& changes) OVERRIDE; | 63 const syncer::ImmutableChangeRecordList& changes) OVERRIDE; |
| 64 | 64 |
| 65 // The following methods are static and hence may be invoked at any time, | 65 // The following methods are static and hence may be invoked at any time, |
| 66 // and do not depend on having a running ChangeProcessor. | 66 // and do not depend on having a running ChangeProcessor. |
| 67 // Creates a bookmark node under the given parent node from the given sync | 67 // Creates a bookmark node under the given parent node from the given sync |
| 68 // node. Returns the newly created node. | 68 // node. Returns the newly created node. |
| 69 static const BookmarkNode* CreateBookmarkNode( | 69 static const BookmarkNode* CreateBookmarkNode( |
| 70 csync::BaseNode* sync_node, | 70 syncer::BaseNode* sync_node, |
| 71 const BookmarkNode* parent, | 71 const BookmarkNode* parent, |
| 72 BookmarkModel* model, | 72 BookmarkModel* model, |
| 73 int index); | 73 int index); |
| 74 | 74 |
| 75 // Sets the favicon of the given bookmark node from the given sync node. | 75 // Sets the favicon of the given bookmark node from the given sync node. |
| 76 // Returns whether the favicon was set in the bookmark node. | 76 // Returns whether the favicon was set in the bookmark node. |
| 77 // |profile| is the profile that contains the HistoryService and BookmarkModel | 77 // |profile| is the profile that contains the HistoryService and BookmarkModel |
| 78 // for the bookmark in question. | 78 // for the bookmark in question. |
| 79 static bool SetBookmarkFavicon(csync::BaseNode* sync_node, | 79 static bool SetBookmarkFavicon(syncer::BaseNode* sync_node, |
| 80 const BookmarkNode* bookmark_node, | 80 const BookmarkNode* bookmark_node, |
| 81 BookmarkModel* model); | 81 BookmarkModel* model); |
| 82 | 82 |
| 83 // Applies the favicon data in |icon_bytes_vector| to |bookmark_node|. | 83 // Applies the favicon data in |icon_bytes_vector| to |bookmark_node|. |
| 84 // |profile| is the profile that contains the HistoryService and BookmarkModel | 84 // |profile| is the profile that contains the HistoryService and BookmarkModel |
| 85 // for the bookmark in question. | 85 // for the bookmark in question. |
| 86 static void ApplyBookmarkFavicon( | 86 static void ApplyBookmarkFavicon( |
| 87 const BookmarkNode* bookmark_node, | 87 const BookmarkNode* bookmark_node, |
| 88 Profile* profile, | 88 Profile* profile, |
| 89 const std::vector<unsigned char>& icon_bytes_vector); | 89 const std::vector<unsigned char>& icon_bytes_vector); |
| 90 | 90 |
| 91 // Sets the favicon of the given sync node from the given bookmark node. | 91 // Sets the favicon of the given sync node from the given bookmark node. |
| 92 static void SetSyncNodeFavicon(const BookmarkNode* bookmark_node, | 92 static void SetSyncNodeFavicon(const BookmarkNode* bookmark_node, |
| 93 BookmarkModel* model, | 93 BookmarkModel* model, |
| 94 csync::WriteNode* sync_node); | 94 syncer::WriteNode* sync_node); |
| 95 | 95 |
| 96 // Treat the |index|th child of |parent| as a newly added node, and create a | 96 // Treat the |index|th child of |parent| as a newly added node, and create a |
| 97 // corresponding node in the sync domain using |trans|. All properties | 97 // corresponding node in the sync domain using |trans|. All properties |
| 98 // will be transferred to the new node. A node corresponding to |parent| | 98 // will be transferred to the new node. A node corresponding to |parent| |
| 99 // must already exist and be associated for this call to succeed. Returns | 99 // must already exist and be associated for this call to succeed. Returns |
| 100 // the ID of the just-created node, or if creation fails, kInvalidID. | 100 // the ID of the just-created node, or if creation fails, kInvalidID. |
| 101 static int64 CreateSyncNode(const BookmarkNode* parent, | 101 static int64 CreateSyncNode(const BookmarkNode* parent, |
| 102 BookmarkModel* model, | 102 BookmarkModel* model, |
| 103 int index, | 103 int index, |
| 104 csync::WriteTransaction* trans, | 104 syncer::WriteTransaction* trans, |
| 105 BookmarkModelAssociator* associator, | 105 BookmarkModelAssociator* associator, |
| 106 DataTypeErrorHandler* error_handler); | 106 DataTypeErrorHandler* error_handler); |
| 107 | 107 |
| 108 protected: | 108 protected: |
| 109 virtual void StartImpl(Profile* profile) OVERRIDE; | 109 virtual void StartImpl(Profile* profile) OVERRIDE; |
| 110 virtual void StopImpl() OVERRIDE; | 110 virtual void StopImpl() OVERRIDE; |
| 111 | 111 |
| 112 private: | 112 private: |
| 113 enum MoveOrCreate { | 113 enum MoveOrCreate { |
| 114 MOVE, | 114 MOVE, |
| 115 CREATE, | 115 CREATE, |
| 116 }; | 116 }; |
| 117 | 117 |
| 118 // Create a bookmark node corresponding to |src| if one is not already | 118 // Create a bookmark node corresponding to |src| if one is not already |
| 119 // associated with |src|. Returns the node that was created or updated. | 119 // associated with |src|. Returns the node that was created or updated. |
| 120 const BookmarkNode* CreateOrUpdateBookmarkNode( | 120 const BookmarkNode* CreateOrUpdateBookmarkNode( |
| 121 csync::BaseNode* src, | 121 syncer::BaseNode* src, |
| 122 BookmarkModel* model); | 122 BookmarkModel* model); |
| 123 | 123 |
| 124 // Helper function to determine the appropriate insertion index of sync node | 124 // Helper function to determine the appropriate insertion index of sync node |
| 125 // |node| under the Bookmark model node |parent|, to make the positions | 125 // |node| under the Bookmark model node |parent|, to make the positions |
| 126 // match up between the two models. This presumes that the predecessor of the | 126 // match up between the two models. This presumes that the predecessor of the |
| 127 // item (in the bookmark model) has already been moved into its appropriate | 127 // item (in the bookmark model) has already been moved into its appropriate |
| 128 // position. | 128 // position. |
| 129 int CalculateBookmarkModelInsertionIndex( | 129 int CalculateBookmarkModelInsertionIndex( |
| 130 const BookmarkNode* parent, | 130 const BookmarkNode* parent, |
| 131 const csync::BaseNode* node) const; | 131 const syncer::BaseNode* node) const; |
| 132 | 132 |
| 133 // Helper function used to fix the position of a sync node so that it matches | 133 // Helper function used to fix the position of a sync node so that it matches |
| 134 // the position of a corresponding bookmark model node. |parent| and | 134 // the position of a corresponding bookmark model node. |parent| and |
| 135 // |index| identify the bookmark model position. |dst| is the node whose | 135 // |index| identify the bookmark model position. |dst| is the node whose |
| 136 // position is to be fixed. If |operation| is CREATE, treat |dst| as an | 136 // position is to be fixed. If |operation| is CREATE, treat |dst| as an |
| 137 // uncreated node and set its position via InitByCreation(); otherwise, | 137 // uncreated node and set its position via InitByCreation(); otherwise, |
| 138 // |dst| is treated as an existing node, and its position will be set via | 138 // |dst| is treated as an existing node, and its position will be set via |
| 139 // SetPosition(). |trans| is the transaction to which |dst| belongs. Returns | 139 // SetPosition(). |trans| is the transaction to which |dst| belongs. Returns |
| 140 // false on failure. | 140 // false on failure. |
| 141 static bool PlaceSyncNode(MoveOrCreate operation, | 141 static bool PlaceSyncNode(MoveOrCreate operation, |
| 142 const BookmarkNode* parent, | 142 const BookmarkNode* parent, |
| 143 int index, | 143 int index, |
| 144 csync::WriteTransaction* trans, | 144 syncer::WriteTransaction* trans, |
| 145 csync::WriteNode* dst, | 145 syncer::WriteNode* dst, |
| 146 BookmarkModelAssociator* associator); | 146 BookmarkModelAssociator* associator); |
| 147 | 147 |
| 148 // Copy properties (but not position) from |src| to |dst|. | 148 // Copy properties (but not position) from |src| to |dst|. |
| 149 static void UpdateSyncNodeProperties(const BookmarkNode* src, | 149 static void UpdateSyncNodeProperties(const BookmarkNode* src, |
| 150 BookmarkModel* model, | 150 BookmarkModel* model, |
| 151 csync::WriteNode* dst); | 151 syncer::WriteNode* dst); |
| 152 | 152 |
| 153 // Helper function to encode a bookmark's favicon into a PNG byte vector. | 153 // Helper function to encode a bookmark's favicon into a PNG byte vector. |
| 154 static void EncodeFavicon(const BookmarkNode* src, | 154 static void EncodeFavicon(const BookmarkNode* src, |
| 155 BookmarkModel* model, | 155 BookmarkModel* model, |
| 156 std::vector<unsigned char>* dst); | 156 std::vector<unsigned char>* dst); |
| 157 | 157 |
| 158 // Remove the sync node corresponding to |node|. It shouldn't have | 158 // Remove the sync node corresponding to |node|. It shouldn't have |
| 159 // any children. | 159 // any children. |
| 160 void RemoveOneSyncNode(csync::WriteTransaction* trans, | 160 void RemoveOneSyncNode(syncer::WriteTransaction* trans, |
| 161 const BookmarkNode* node); | 161 const BookmarkNode* node); |
| 162 | 162 |
| 163 // Remove all the sync nodes associated with |node| and its children. | 163 // Remove all the sync nodes associated with |node| and its children. |
| 164 void RemoveSyncNodeHierarchy(const BookmarkNode* node); | 164 void RemoveSyncNodeHierarchy(const BookmarkNode* node); |
| 165 | 165 |
| 166 // The bookmark model we are processing changes from. Non-NULL when | 166 // The bookmark model we are processing changes from. Non-NULL when |
| 167 // |running_| is true. | 167 // |running_| is true. |
| 168 BookmarkModel* bookmark_model_; | 168 BookmarkModel* bookmark_model_; |
| 169 | 169 |
| 170 // The two models should be associated according to this ModelAssociator. | 170 // The two models should be associated according to this ModelAssociator. |
| 171 BookmarkModelAssociator* model_associator_; | 171 BookmarkModelAssociator* model_associator_; |
| 172 | 172 |
| 173 DISALLOW_COPY_AND_ASSIGN(BookmarkChangeProcessor); | 173 DISALLOW_COPY_AND_ASSIGN(BookmarkChangeProcessor); |
| 174 }; | 174 }; |
| 175 | 175 |
| 176 } // namespace browser_sync | 176 } // namespace browser_sync |
| 177 | 177 |
| 178 #endif // CHROME_BROWSER_SYNC_GLUE_BOOKMARK_CHANGE_PROCESSOR_H_ | 178 #endif // CHROME_BROWSER_SYNC_GLUE_BOOKMARK_CHANGE_PROCESSOR_H_ |
| OLD | NEW |