| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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_MODEL_ASSOCIATOR_H_ | 5 #ifndef CHROME_BROWSER_SYNC_GLUE_BOOKMARK_MODEL_ASSOCIATOR_H_ |
| 6 #define CHROME_BROWSER_SYNC_GLUE_BOOKMARK_MODEL_ASSOCIATOR_H_ | 6 #define CHROME_BROWSER_SYNC_GLUE_BOOKMARK_MODEL_ASSOCIATOR_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <string> | 10 #include <string> |
| 11 | 11 |
| 12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 #include "base/task.h" | 13 #include "base/task.h" |
| 14 #include "chrome/browser/sync/unrecoverable_error_handler.h" |
| 14 #include "chrome/browser/sync/glue/model_associator.h" | 15 #include "chrome/browser/sync/glue/model_associator.h" |
| 15 | 16 |
| 16 class BookmarkNode; | 17 class BookmarkNode; |
| 17 | 18 |
| 18 namespace sync_api { | 19 namespace sync_api { |
| 19 class BaseNode; | 20 class BaseNode; |
| 20 class BaseTransaction; | 21 class BaseTransaction; |
| 21 class ReadNode; | 22 class ReadNode; |
| 22 } | 23 } |
| 23 | 24 |
| 24 class ProfileSyncService; | 25 class ProfileSyncService; |
| 25 | 26 |
| 26 namespace browser_sync { | 27 namespace browser_sync { |
| 27 | 28 |
| 28 class BookmarkChangeProcessor; | 29 class BookmarkChangeProcessor; |
| 29 class UnrecoverableErrorHandler; | |
| 30 | 30 |
| 31 // Contains all model association related logic: | 31 // Contains all model association related logic: |
| 32 // * Algorithm to associate bookmark model and sync model. | 32 // * Algorithm to associate bookmark model and sync model. |
| 33 // * Methods to get a bookmark node for a given sync node and vice versa. | 33 // * Methods to get a bookmark node for a given sync node and vice versa. |
| 34 // * Persisting model associations and loading them back. | 34 // * Persisting model associations and loading them back. |
| 35 class BookmarkModelAssociator | 35 class BookmarkModelAssociator |
| 36 : public PerDataTypeAssociatorInterface<BookmarkNode, int64> { | 36 : public PerDataTypeAssociatorInterface<BookmarkNode, int64> { |
| 37 public: | 37 public: |
| 38 static syncable::ModelType model_type() { return syncable::BOOKMARKS; } | 38 static syncable::ModelType model_type() { return syncable::BOOKMARKS; } |
| 39 BookmarkModelAssociator(ProfileSyncService* sync_service, | 39 BookmarkModelAssociator(ProfileSyncService* sync_service, |
| 40 UnrecoverableErrorHandler* error_handler); | 40 UnrecoverableErrorHandler* persist_ids_error_handler); |
| 41 virtual ~BookmarkModelAssociator(); | 41 virtual ~BookmarkModelAssociator(); |
| 42 | 42 |
| 43 // AssociatorInterface implementation. | 43 // AssociatorInterface implementation. |
| 44 // | 44 // |
| 45 // AssociateModels iterates through both the sync and the browser | 45 // AssociateModels iterates through both the sync and the browser |
| 46 // bookmark model, looking for matched pairs of items. For any pairs it | 46 // bookmark model, looking for matched pairs of items. For any pairs it |
| 47 // finds, it will call AssociateSyncID. For any unmatched items, | 47 // finds, it will call AssociateSyncID. For any unmatched items, |
| 48 // MergeAndAssociateModels will try to repair the match, e.g. by adding a new | 48 // MergeAndAssociateModels will try to repair the match, e.g. by adding a new |
| 49 // node. After successful completion, the models should be identical and | 49 // node. After successful completion, the models should be identical and |
| 50 // corresponding. Returns true on success. On failure of this step, we | 50 // corresponding. Returns true on success. On failure of this step, we |
| (...skipping 29 matching lines...) Expand all Loading... |
| 80 // Associates the given bookmark node with the given sync id. | 80 // Associates the given bookmark node with the given sync id. |
| 81 virtual void Associate(const BookmarkNode* node, int64 sync_id); | 81 virtual void Associate(const BookmarkNode* node, int64 sync_id); |
| 82 // Remove the association that corresponds to the given sync id. | 82 // Remove the association that corresponds to the given sync id. |
| 83 virtual void Disassociate(int64 sync_id); | 83 virtual void Disassociate(int64 sync_id); |
| 84 | 84 |
| 85 virtual void AbortAssociation() { | 85 virtual void AbortAssociation() { |
| 86 // No implementation needed, this associator runs on the main | 86 // No implementation needed, this associator runs on the main |
| 87 // thread. | 87 // thread. |
| 88 } | 88 } |
| 89 | 89 |
| 90 // Returns sync service instance. |
| 91 ProfileSyncService* sync_service() { return sync_service_; } |
| 92 |
| 90 protected: | 93 protected: |
| 91 // Stores the id of the node with the given tag in |sync_id|. | 94 // Stores the id of the node with the given tag in |sync_id|. |
| 92 // Returns of that node was found successfully. | 95 // Returns of that node was found successfully. |
| 93 // Tests override this. | 96 // Tests override this. |
| 94 virtual bool GetSyncIdForTaggedNode(const std::string& tag, int64* sync_id); | 97 virtual bool GetSyncIdForTaggedNode(const std::string& tag, int64* sync_id); |
| 95 | 98 |
| 96 // Returns sync service instance. | |
| 97 ProfileSyncService* sync_service() { return sync_service_; } | |
| 98 | |
| 99 private: | 99 private: |
| 100 typedef std::map<int64, int64> BookmarkIdToSyncIdMap; | 100 typedef std::map<int64, int64> BookmarkIdToSyncIdMap; |
| 101 typedef std::map<int64, const BookmarkNode*> SyncIdToBookmarkNodeMap; | 101 typedef std::map<int64, const BookmarkNode*> SyncIdToBookmarkNodeMap; |
| 102 typedef std::set<int64> DirtyAssociationsSyncIds; | 102 typedef std::set<int64> DirtyAssociationsSyncIds; |
| 103 | 103 |
| 104 // Posts a task to persist dirty associations. | 104 // Posts a task to persist dirty associations. |
| 105 void PostPersistAssociationsTask(); | 105 void PostPersistAssociationsTask(); |
| 106 // Persists all dirty associations. | 106 // Persists all dirty associations. |
| 107 void PersistAssociations(); | 107 void PersistAssociations(); |
| 108 | 108 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 121 // user's share. For example, "other_bookmarks" is the tag for the Other | 121 // user's share. For example, "other_bookmarks" is the tag for the Other |
| 122 // Bookmarks folder. The sync nodes are server-created. | 122 // Bookmarks folder. The sync nodes are server-created. |
| 123 bool AssociateTaggedPermanentNode(const BookmarkNode* permanent_node, | 123 bool AssociateTaggedPermanentNode(const BookmarkNode* permanent_node, |
| 124 const std::string& tag); | 124 const std::string& tag); |
| 125 | 125 |
| 126 // Compare the properties of a pair of nodes from either domain. | 126 // Compare the properties of a pair of nodes from either domain. |
| 127 bool NodesMatch(const BookmarkNode* bookmark, | 127 bool NodesMatch(const BookmarkNode* bookmark, |
| 128 const sync_api::BaseNode* sync_node) const; | 128 const sync_api::BaseNode* sync_node) const; |
| 129 | 129 |
| 130 ProfileSyncService* sync_service_; | 130 ProfileSyncService* sync_service_; |
| 131 UnrecoverableErrorHandler* error_handler_; | 131 UnrecoverableErrorHandler* persist_ids_error_handler_; |
| 132 BookmarkIdToSyncIdMap id_map_; | 132 BookmarkIdToSyncIdMap id_map_; |
| 133 SyncIdToBookmarkNodeMap id_map_inverse_; | 133 SyncIdToBookmarkNodeMap id_map_inverse_; |
| 134 // Stores sync ids for dirty associations. | 134 // Stores sync ids for dirty associations. |
| 135 DirtyAssociationsSyncIds dirty_associations_sync_ids_; | 135 DirtyAssociationsSyncIds dirty_associations_sync_ids_; |
| 136 | 136 |
| 137 // Used to post PersistAssociation tasks to the current message loop and | 137 // Used to post PersistAssociation tasks to the current message loop and |
| 138 // guarantees no invocations can occur if |this| has been deleted. (This | 138 // guarantees no invocations can occur if |this| has been deleted. (This |
| 139 // allows this class to be non-refcounted). | 139 // allows this class to be non-refcounted). |
| 140 ScopedRunnableMethodFactory<BookmarkModelAssociator> persist_associations_; | 140 ScopedRunnableMethodFactory<BookmarkModelAssociator> persist_associations_; |
| 141 | 141 |
| 142 DISALLOW_COPY_AND_ASSIGN(BookmarkModelAssociator); | 142 DISALLOW_COPY_AND_ASSIGN(BookmarkModelAssociator); |
| 143 }; | 143 }; |
| 144 | 144 |
| 145 } // namespace browser_sync | 145 } // namespace browser_sync |
| 146 | 146 |
| 147 #endif // CHROME_BROWSER_SYNC_GLUE_BOOKMARK_MODEL_ASSOCIATOR_H_ | 147 #endif // CHROME_BROWSER_SYNC_GLUE_BOOKMARK_MODEL_ASSOCIATOR_H_ |
| OLD | NEW |