| 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 COMPONENTS_SYNC_BOOKMARKS_BOOKMARK_MODEL_ASSOCIATOR_H_ | 5 #ifndef COMPONENTS_SYNC_BOOKMARKS_BOOKMARK_MODEL_ASSOCIATOR_H_ |
| 6 #define COMPONENTS_SYNC_BOOKMARKS_BOOKMARK_MODEL_ASSOCIATOR_H_ | 6 #define COMPONENTS_SYNC_BOOKMARKS_BOOKMARK_MODEL_ASSOCIATOR_H_ |
| 7 | 7 |
| 8 #include <stddef.h> |
| 9 #include <stdint.h> |
| 10 |
| 8 #include <map> | 11 #include <map> |
| 9 #include <set> | 12 #include <set> |
| 10 #include <stack> | 13 #include <stack> |
| 11 #include <string> | 14 #include <string> |
| 12 #include <vector> | 15 #include <vector> |
| 13 | 16 |
| 14 #include "base/basictypes.h" | |
| 15 #include "base/compiler_specific.h" | 17 #include "base/compiler_specific.h" |
| 16 #include "base/hash.h" | 18 #include "base/hash.h" |
| 19 #include "base/macros.h" |
| 17 #include "base/memory/weak_ptr.h" | 20 #include "base/memory/weak_ptr.h" |
| 18 #include "base/threading/thread_checker.h" | 21 #include "base/threading/thread_checker.h" |
| 19 #include "components/sync_driver/data_type_error_handler.h" | 22 #include "components/sync_driver/data_type_error_handler.h" |
| 20 #include "components/sync_driver/model_associator.h" | 23 #include "components/sync_driver/model_associator.h" |
| 21 #include "sync/internal_api/public/util/unrecoverable_error_handler.h" | 24 #include "sync/internal_api/public/util/unrecoverable_error_handler.h" |
| 22 | 25 |
| 23 class GURL; | 26 class GURL; |
| 24 | 27 |
| 25 namespace bookmarks { | 28 namespace bookmarks { |
| 26 class BookmarkModel; | 29 class BookmarkModel; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 39 } | 42 } |
| 40 | 43 |
| 41 namespace browser_sync { | 44 namespace browser_sync { |
| 42 | 45 |
| 43 // Contains all model association related logic: | 46 // Contains all model association related logic: |
| 44 // * Algorithm to associate bookmark model and sync model. | 47 // * Algorithm to associate bookmark model and sync model. |
| 45 // * Methods to get a bookmark node for a given sync node and vice versa. | 48 // * Methods to get a bookmark node for a given sync node and vice versa. |
| 46 // * Persisting model associations and loading them back. | 49 // * Persisting model associations and loading them back. |
| 47 class BookmarkModelAssociator | 50 class BookmarkModelAssociator |
| 48 : public sync_driver:: | 51 : public sync_driver:: |
| 49 PerDataTypeAssociatorInterface<bookmarks::BookmarkNode, int64> { | 52 PerDataTypeAssociatorInterface<bookmarks::BookmarkNode, int64_t> { |
| 50 public: | 53 public: |
| 51 static syncer::ModelType model_type() { return syncer::BOOKMARKS; } | 54 static syncer::ModelType model_type() { return syncer::BOOKMARKS; } |
| 52 // |expect_mobile_bookmarks_folder| controls whether or not we | 55 // |expect_mobile_bookmarks_folder| controls whether or not we |
| 53 // expect the mobile bookmarks permanent folder to be created. | 56 // expect the mobile bookmarks permanent folder to be created. |
| 54 // Should be set to true only by mobile clients. | 57 // Should be set to true only by mobile clients. |
| 55 BookmarkModelAssociator( | 58 BookmarkModelAssociator( |
| 56 bookmarks::BookmarkModel* bookmark_model, | 59 bookmarks::BookmarkModel* bookmark_model, |
| 57 sync_driver::SyncClient* sync_client, | 60 sync_driver::SyncClient* sync_client, |
| 58 syncer::UserShare* user_share, | 61 syncer::UserShare* user_share, |
| 59 sync_driver::DataTypeErrorHandler* unrecoverable_error_handler, | 62 sync_driver::DataTypeErrorHandler* unrecoverable_error_handler, |
| (...skipping 15 matching lines...) Expand all Loading... |
| 75 | 78 |
| 76 syncer::SyncError DisassociateModels() override; | 79 syncer::SyncError DisassociateModels() override; |
| 77 | 80 |
| 78 // The has_nodes out param is true if the sync model has nodes other | 81 // The has_nodes out param is true if the sync model has nodes other |
| 79 // than the permanent tagged nodes. | 82 // than the permanent tagged nodes. |
| 80 bool SyncModelHasUserCreatedNodes(bool* has_nodes) override; | 83 bool SyncModelHasUserCreatedNodes(bool* has_nodes) override; |
| 81 | 84 |
| 82 // Returns sync id for the given bookmark node id. | 85 // Returns sync id for the given bookmark node id. |
| 83 // Returns syncer::kInvalidId if the sync node is not found for the given | 86 // Returns syncer::kInvalidId if the sync node is not found for the given |
| 84 // bookmark node id. | 87 // bookmark node id. |
| 85 int64 GetSyncIdFromChromeId(const int64& node_id) override; | 88 int64_t GetSyncIdFromChromeId(const int64_t& node_id) override; |
| 86 | 89 |
| 87 // Returns the bookmark node for the given sync id. | 90 // Returns the bookmark node for the given sync id. |
| 88 // Returns NULL if no bookmark node is found for the given sync id. | 91 // Returns NULL if no bookmark node is found for the given sync id. |
| 89 const bookmarks::BookmarkNode* GetChromeNodeFromSyncId( | 92 const bookmarks::BookmarkNode* GetChromeNodeFromSyncId( |
| 90 int64 sync_id) override; | 93 int64_t sync_id) override; |
| 91 | 94 |
| 92 // Initializes the given sync node from the given bookmark node id. | 95 // Initializes the given sync node from the given bookmark node id. |
| 93 // Returns false if no sync node was found for the given bookmark node id or | 96 // Returns false if no sync node was found for the given bookmark node id or |
| 94 // if the initialization of sync node fails. | 97 // if the initialization of sync node fails. |
| 95 bool InitSyncNodeFromChromeId(const int64& node_id, | 98 bool InitSyncNodeFromChromeId(const int64_t& node_id, |
| 96 syncer::BaseNode* sync_node) override; | 99 syncer::BaseNode* sync_node) override; |
| 97 | 100 |
| 98 // Associates the given bookmark node with the given sync node. | 101 // Associates the given bookmark node with the given sync node. |
| 99 void Associate(const bookmarks::BookmarkNode* node, | 102 void Associate(const bookmarks::BookmarkNode* node, |
| 100 const syncer::BaseNode& sync_node) override; | 103 const syncer::BaseNode& sync_node) override; |
| 101 // Remove the association that corresponds to the given sync id. | 104 // Remove the association that corresponds to the given sync id. |
| 102 void Disassociate(int64 sync_id) override; | 105 void Disassociate(int64_t sync_id) override; |
| 103 | 106 |
| 104 void AbortAssociation() override { | 107 void AbortAssociation() override { |
| 105 // No implementation needed, this associator runs on the main | 108 // No implementation needed, this associator runs on the main |
| 106 // thread. | 109 // thread. |
| 107 } | 110 } |
| 108 | 111 |
| 109 // See ModelAssociator interface. | 112 // See ModelAssociator interface. |
| 110 bool CryptoReadyIfNecessary() override; | 113 bool CryptoReadyIfNecessary() override; |
| 111 | 114 |
| 112 private: | 115 private: |
| 113 typedef std::map<int64, int64> BookmarkIdToSyncIdMap; | 116 typedef std::map<int64_t, int64_t> BookmarkIdToSyncIdMap; |
| 114 typedef std::map<int64, const bookmarks::BookmarkNode*> | 117 typedef std::map<int64_t, const bookmarks::BookmarkNode*> |
| 115 SyncIdToBookmarkNodeMap; | 118 SyncIdToBookmarkNodeMap; |
| 116 typedef std::set<int64> DirtyAssociationsSyncIds; | 119 typedef std::set<int64_t> DirtyAssociationsSyncIds; |
| 117 typedef std::vector<const bookmarks::BookmarkNode*> BookmarkList; | 120 typedef std::vector<const bookmarks::BookmarkNode*> BookmarkList; |
| 118 typedef std::stack<const bookmarks::BookmarkNode*> BookmarkStack; | 121 typedef std::stack<const bookmarks::BookmarkNode*> BookmarkStack; |
| 119 | 122 |
| 120 // Add association between native node and sync node to the maps. | 123 // Add association between native node and sync node to the maps. |
| 121 void AddAssociation(const bookmarks::BookmarkNode* node, int64 sync_id); | 124 void AddAssociation(const bookmarks::BookmarkNode* node, int64_t sync_id); |
| 122 | 125 |
| 123 // Posts a task to persist dirty associations. | 126 // Posts a task to persist dirty associations. |
| 124 void PostPersistAssociationsTask(); | 127 void PostPersistAssociationsTask(); |
| 125 // Persists all dirty associations. | 128 // Persists all dirty associations. |
| 126 void PersistAssociations(); | 129 void PersistAssociations(); |
| 127 | 130 |
| 128 // Result of the native model version check against the sync | 131 // Result of the native model version check against the sync |
| 129 // version performed by CheckModelSyncState. | 132 // version performed by CheckModelSyncState. |
| 130 enum NativeModelSyncState { | 133 enum NativeModelSyncState { |
| 131 // The native version is syncer::syncable::kInvalidTransactionVersion, | 134 // The native version is syncer::syncable::kInvalidTransactionVersion, |
| (...skipping 15 matching lines...) Expand all Loading... |
| 147 | 150 |
| 148 // Helper class used within AssociateModels to simplify the logic and | 151 // Helper class used within AssociateModels to simplify the logic and |
| 149 // minimize the number of arguments passed between private functions. | 152 // minimize the number of arguments passed between private functions. |
| 150 class Context { | 153 class Context { |
| 151 public: | 154 public: |
| 152 Context(syncer::SyncMergeResult* local_merge_result, | 155 Context(syncer::SyncMergeResult* local_merge_result, |
| 153 syncer::SyncMergeResult* syncer_merge_result); | 156 syncer::SyncMergeResult* syncer_merge_result); |
| 154 ~Context(); | 157 ~Context(); |
| 155 | 158 |
| 156 // Push a sync node to the DFS stack. | 159 // Push a sync node to the DFS stack. |
| 157 void PushNode(int64 sync_id); | 160 void PushNode(int64_t sync_id); |
| 158 // Pops a sync node from the DFS stack. Returns false if the stack | 161 // Pops a sync node from the DFS stack. Returns false if the stack |
| 159 // is empty. | 162 // is empty. |
| 160 bool PopNode(int64* sync_id); | 163 bool PopNode(int64_t* sync_id); |
| 161 | 164 |
| 162 // The following methods are used to update |local_merge_result_| and | 165 // The following methods are used to update |local_merge_result_| and |
| 163 // |syncer_merge_result_|. | 166 // |syncer_merge_result_|. |
| 164 void SetPreAssociationVersions(int64 native_version, int64 sync_version); | 167 void SetPreAssociationVersions(int64_t native_version, |
| 168 int64_t sync_version); |
| 165 void SetNumItemsBeforeAssociation(int local_num, int sync_num); | 169 void SetNumItemsBeforeAssociation(int local_num, int sync_num); |
| 166 void SetNumItemsAfterAssociation(int local_num, int sync_num); | 170 void SetNumItemsAfterAssociation(int local_num, int sync_num); |
| 167 void IncrementLocalItemsDeleted(); | 171 void IncrementLocalItemsDeleted(); |
| 168 void IncrementLocalItemsAdded(); | 172 void IncrementLocalItemsAdded(); |
| 169 void IncrementLocalItemsModified(); | 173 void IncrementLocalItemsModified(); |
| 170 void IncrementSyncItemsAdded(); | 174 void IncrementSyncItemsAdded(); |
| 171 void IncrementSyncItemsDeleted(int count); | 175 void IncrementSyncItemsDeleted(int count); |
| 172 | 176 |
| 173 void UpdateDuplicateCount(const base::string16& title, const GURL& url); | 177 void UpdateDuplicateCount(const base::string16& title, const GURL& url); |
| 174 | 178 |
| 175 int duplicate_count() const { return duplicate_count_; } | 179 int duplicate_count() const { return duplicate_count_; } |
| 176 | 180 |
| 177 NativeModelSyncState native_model_sync_state() const { | 181 NativeModelSyncState native_model_sync_state() const { |
| 178 return native_model_sync_state_; | 182 return native_model_sync_state_; |
| 179 } | 183 } |
| 180 void set_native_model_sync_state(NativeModelSyncState state) { | 184 void set_native_model_sync_state(NativeModelSyncState state) { |
| 181 native_model_sync_state_ = state; | 185 native_model_sync_state_ = state; |
| 182 } | 186 } |
| 183 | 187 |
| 184 // Bookmark roots participating in the sync. | 188 // Bookmark roots participating in the sync. |
| 185 void AddBookmarkRoot(const bookmarks::BookmarkNode* root); | 189 void AddBookmarkRoot(const bookmarks::BookmarkNode* root); |
| 186 const BookmarkList& bookmark_roots() const { return bookmark_roots_; } | 190 const BookmarkList& bookmark_roots() const { return bookmark_roots_; } |
| 187 | 191 |
| 188 // Gets pre-association sync version for Bookmarks datatype. | 192 // Gets pre-association sync version for Bookmarks datatype. |
| 189 int64 GetSyncPreAssociationVersion() const; | 193 int64_t GetSyncPreAssociationVersion() const; |
| 190 | 194 |
| 191 void MarkForVersionUpdate(const bookmarks::BookmarkNode* node); | 195 void MarkForVersionUpdate(const bookmarks::BookmarkNode* node); |
| 192 const BookmarkList& bookmarks_for_version_update() const { | 196 const BookmarkList& bookmarks_for_version_update() const { |
| 193 return bookmarks_for_version_update_; | 197 return bookmarks_for_version_update_; |
| 194 } | 198 } |
| 195 | 199 |
| 196 private: | 200 private: |
| 197 // DFS stack of sync nodes traversed during association. | 201 // DFS stack of sync nodes traversed during association. |
| 198 std::stack<int64> dfs_stack_; | 202 std::stack<int64_t> dfs_stack_; |
| 199 // Local and merge results are not owned. | 203 // Local and merge results are not owned. |
| 200 syncer::SyncMergeResult* local_merge_result_; | 204 syncer::SyncMergeResult* local_merge_result_; |
| 201 syncer::SyncMergeResult* syncer_merge_result_; | 205 syncer::SyncMergeResult* syncer_merge_result_; |
| 202 // |hashes_| contains hash codes of all native bookmarks | 206 // |hashes_| contains hash codes of all native bookmarks |
| 203 // for the purpose of detecting duplicates. A small number of | 207 // for the purpose of detecting duplicates. A small number of |
| 204 // false positives due to hash collisions is OK because this | 208 // false positives due to hash collisions is OK because this |
| 205 // data is used for reporting purposes only. | 209 // data is used for reporting purposes only. |
| 206 base::hash_set<size_t> hashes_; | 210 base::hash_set<size_t> hashes_; |
| 207 // Overall number of bookmark collisions from RecordDuplicates call. | 211 // Overall number of bookmark collisions from RecordDuplicates call. |
| 208 int duplicate_count_; | 212 int duplicate_count_; |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 255 // according to sync delete journals. | 259 // according to sync delete journals. |
| 256 void ApplyDeletesFromSyncJournal(syncer::BaseTransaction* trans, | 260 void ApplyDeletesFromSyncJournal(syncer::BaseTransaction* trans, |
| 257 Context* context); | 261 Context* context); |
| 258 | 262 |
| 259 // The main part of the association process that associatiates | 263 // The main part of the association process that associatiates |
| 260 // native nodes that are children of |parent_node| with sync nodes with IDs | 264 // native nodes that are children of |parent_node| with sync nodes with IDs |
| 261 // from |sync_ids|. | 265 // from |sync_ids|. |
| 262 syncer::SyncError BuildAssociations( | 266 syncer::SyncError BuildAssociations( |
| 263 syncer::WriteTransaction* trans, | 267 syncer::WriteTransaction* trans, |
| 264 const bookmarks::BookmarkNode* parent_node, | 268 const bookmarks::BookmarkNode* parent_node, |
| 265 const std::vector<int64>& sync_ids, | 269 const std::vector<int64_t>& sync_ids, |
| 266 Context* context); | 270 Context* context); |
| 267 | 271 |
| 268 // Helper method for creating a new native bookmark node. | 272 // Helper method for creating a new native bookmark node. |
| 269 const bookmarks::BookmarkNode* CreateBookmarkNode( | 273 const bookmarks::BookmarkNode* CreateBookmarkNode( |
| 270 const bookmarks::BookmarkNode* parent_node, | 274 const bookmarks::BookmarkNode* parent_node, |
| 271 int bookmark_index, | 275 int bookmark_index, |
| 272 const syncer::BaseNode* sync_child_node, | 276 const syncer::BaseNode* sync_child_node, |
| 273 const GURL& url, | 277 const GURL& url, |
| 274 Context* context, | 278 Context* context, |
| 275 syncer::SyncError* error); | 279 syncer::SyncError* error); |
| 276 | 280 |
| 277 // Helper method for deleting a sync node and all its children. | 281 // Helper method for deleting a sync node and all its children. |
| 278 // Returns the number of sync nodes deleted. | 282 // Returns the number of sync nodes deleted. |
| 279 int RemoveSyncNodeHierarchy(syncer::WriteTransaction* trans, int64 sync_id); | 283 int RemoveSyncNodeHierarchy(syncer::WriteTransaction* trans, int64_t sync_id); |
| 280 | 284 |
| 281 // Check whether bookmark model and sync model are synced by comparing | 285 // Check whether bookmark model and sync model are synced by comparing |
| 282 // their transaction versions. | 286 // their transaction versions. |
| 283 // Returns a PERSISTENCE_ERROR if a transaction mismatch was detected where | 287 // Returns a PERSISTENCE_ERROR if a transaction mismatch was detected where |
| 284 // the native model has a newer transaction verison. | 288 // the native model has a newer transaction verison. |
| 285 syncer::SyncError CheckModelSyncState(Context* context) const; | 289 syncer::SyncError CheckModelSyncState(Context* context) const; |
| 286 | 290 |
| 287 base::ThreadChecker thread_checker_; | 291 base::ThreadChecker thread_checker_; |
| 288 bookmarks::BookmarkModel* bookmark_model_; | 292 bookmarks::BookmarkModel* bookmark_model_; |
| 289 sync_driver::SyncClient* sync_client_; | 293 sync_driver::SyncClient* sync_client_; |
| 290 syncer::UserShare* user_share_; | 294 syncer::UserShare* user_share_; |
| 291 sync_driver::DataTypeErrorHandler* unrecoverable_error_handler_; | 295 sync_driver::DataTypeErrorHandler* unrecoverable_error_handler_; |
| 292 const bool expect_mobile_bookmarks_folder_; | 296 const bool expect_mobile_bookmarks_folder_; |
| 293 BookmarkIdToSyncIdMap id_map_; | 297 BookmarkIdToSyncIdMap id_map_; |
| 294 SyncIdToBookmarkNodeMap id_map_inverse_; | 298 SyncIdToBookmarkNodeMap id_map_inverse_; |
| 295 // Stores sync ids for dirty associations. | 299 // Stores sync ids for dirty associations. |
| 296 DirtyAssociationsSyncIds dirty_associations_sync_ids_; | 300 DirtyAssociationsSyncIds dirty_associations_sync_ids_; |
| 297 | 301 |
| 298 // Used to post PersistAssociation tasks to the current message loop and | 302 // Used to post PersistAssociation tasks to the current message loop and |
| 299 // guarantees no invocations can occur if |this| has been deleted. (This | 303 // guarantees no invocations can occur if |this| has been deleted. (This |
| 300 // allows this class to be non-refcounted). | 304 // allows this class to be non-refcounted). |
| 301 base::WeakPtrFactory<BookmarkModelAssociator> weak_factory_; | 305 base::WeakPtrFactory<BookmarkModelAssociator> weak_factory_; |
| 302 | 306 |
| 303 DISALLOW_COPY_AND_ASSIGN(BookmarkModelAssociator); | 307 DISALLOW_COPY_AND_ASSIGN(BookmarkModelAssociator); |
| 304 }; | 308 }; |
| 305 | 309 |
| 306 } // namespace browser_sync | 310 } // namespace browser_sync |
| 307 | 311 |
| 308 #endif // COMPONENTS_SYNC_BOOKMARKS_BOOKMARK_MODEL_ASSOCIATOR_H_ | 312 #endif // COMPONENTS_SYNC_BOOKMARKS_BOOKMARK_MODEL_ASSOCIATOR_H_ |
| OLD | NEW |