| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2006-2009 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 #ifdef CHROME_PERSONALIZATION | |
| 6 | |
| 7 #ifndef CHROME_BROWSER_SYNC_GLUE_MODEL_ASSOCATOR_H_ | |
| 8 #define CHROME_BROWSER_SYNC_GLUE_MODEL_ASSOCATOR_H_ | |
| 9 | |
| 10 #include <map> | |
| 11 #include <set> | |
| 12 #include <string> | |
| 13 | |
| 14 #include "base/basictypes.h" | |
| 15 #include "base/ref_counted.h" | |
| 16 #include "base/scoped_ptr.h" | |
| 17 #include "base/string16.h" | |
| 18 | |
| 19 class BookmarkNode; | |
| 20 | |
| 21 namespace sync_api { | |
| 22 class BaseNode; | |
| 23 class BaseTransaction; | |
| 24 class ReadNode; | |
| 25 }; | |
| 26 | |
| 27 class ProfileSyncService; | |
| 28 | |
| 29 namespace browser_sync { | |
| 30 | |
| 31 // Contains all model assocation related logic: | |
| 32 // * Algorithm to associate bookmark model and sync model. | |
| 33 // * Methods to get a bookmark node for a given sync node and vice versa. | |
| 34 // * Persisting model assocations and loading them back. | |
| 35 class ModelAssociator | |
| 36 : public base::RefCountedThreadSafe<ModelAssociator> { | |
| 37 public: | |
| 38 explicit ModelAssociator(ProfileSyncService* sync_service); | |
| 39 virtual ~ModelAssociator() { } | |
| 40 | |
| 41 // Clears all assocations . | |
| 42 void ClearAll(); | |
| 43 | |
| 44 // Returns sync id for the given bookmark node id. | |
| 45 // Returns sync_api::kInvalidId if the sync node is not found for the given | |
| 46 // bookmark node id. | |
| 47 int64 GetSyncIdFromBookmarkId(int64 node_id) const; | |
| 48 | |
| 49 // Stores bookmark node id for the given sync id in bookmark_id. Returns true | |
| 50 // if the bookmark id was successfully found; false otherwise. | |
| 51 bool GetBookmarkIdFromSyncId(int64 sync_id, int64* bookmark_id) const; | |
| 52 | |
| 53 // Initializes the given sync node from the given bookmark node id. | |
| 54 // Returns false if no sync node was found for the given bookmark node id or | |
| 55 // if the initialization of sync node fails. | |
| 56 bool InitSyncNodeFromBookmarkId(int64 node_id, sync_api::BaseNode* sync_node); | |
| 57 | |
| 58 // Returns the bookmark node for the given sync id. | |
| 59 // Returns NULL if no bookmark node is found for the given sync id. | |
| 60 const BookmarkNode* GetBookmarkNodeFromSyncId(int64 sync_id); | |
| 61 | |
| 62 // Associates the given bookmark node id with the given sync id. | |
| 63 void AssociateIds(int64 node_id, int64 sync_id); | |
| 64 // Disassociate the ids that correspond to the given sync id. | |
| 65 void DisassociateIds(int64 sync_id); | |
| 66 | |
| 67 // Returns whether the bookmark model has user created nodes or not. That is, | |
| 68 // whether there are nodes in the bookmark model except the bookmark bar and | |
| 69 // other bookmarks. | |
| 70 bool BookmarkModelHasUserCreatedNodes() const; | |
| 71 | |
| 72 // Returns whether the sync model has nodes other than the permanent tagged | |
| 73 // nodes. | |
| 74 bool SyncModelHasUserCreatedNodes(); | |
| 75 | |
| 76 // AssociateModels iterates through both the sync and the browser | |
| 77 // bookmark model, looking for matched pairs of items. For any pairs it | |
| 78 // finds, it will call AssociateSyncID. For any unmatched items, | |
| 79 // MergeAndAssociateModels will try to repair the match, e.g. by adding a new | |
| 80 // node. After successful completion, the models should be identical and | |
| 81 // corresponding. Returns true on success. On failure of this step, we | |
| 82 // should abort the sync operation and report an error to the user. | |
| 83 bool AssociateModels(); | |
| 84 | |
| 85 protected: | |
| 86 // Stores the id of the node with the given tag in |sync_id|. | |
| 87 // Returns of that node was found successfully. | |
| 88 // Tests override this. | |
| 89 virtual bool GetSyncIdForTaggedNode(const string16& tag, int64* sync_id); | |
| 90 | |
| 91 // Returns sync service instance. | |
| 92 ProfileSyncService* sync_service() { return sync_service_; } | |
| 93 | |
| 94 private: | |
| 95 typedef std::map<int64, int64> BookmarkIdToSyncIdMap; | |
| 96 typedef std::map<int64, int64> SyncIdToBookmarkIdMap; | |
| 97 typedef std::set<int64> DirtyAssocationsSyncIds; | |
| 98 | |
| 99 // Posts a task to persist dirty assocations. | |
| 100 void PostPersistAssociationsTask(); | |
| 101 // Persists all dirty assocations. | |
| 102 void PersistAssociations(); | |
| 103 | |
| 104 // Loads the persisted assocations into in-memory maps. | |
| 105 // If the persisted associations are out-of-date due to some reason, returns | |
| 106 // false; otehrwise returns true. | |
| 107 bool LoadAssociations(); | |
| 108 | |
| 109 // Matches up the bookmark model and the sync model to build model | |
| 110 // assocations. | |
| 111 bool BuildAssocations(); | |
| 112 | |
| 113 // Associate a top-level node of the bookmark model with a permanent node in | |
| 114 // the sync domain. Such permanent nodes are identified by a tag that is | |
| 115 // well known to the server and the client, and is unique within a particular | |
| 116 // user's share. For example, "other_bookmarks" is the tag for the Other | |
| 117 // Bookmarks folder. The sync nodes are server-created. | |
| 118 bool AssociateTaggedPermanentNode(const BookmarkNode* permanent_node, | |
| 119 const string16& tag); | |
| 120 | |
| 121 // Compare the properties of a pair of nodes from either domain. | |
| 122 bool NodesMatch(const BookmarkNode* bookmark, | |
| 123 const sync_api::BaseNode* sync_node) const; | |
| 124 | |
| 125 ProfileSyncService* sync_service_; | |
| 126 BookmarkIdToSyncIdMap id_map_; | |
| 127 SyncIdToBookmarkIdMap id_map_inverse_; | |
| 128 // Stores sync ids for dirty associations. | |
| 129 DirtyAssocationsSyncIds dirty_assocations_sync_ids_; | |
| 130 | |
| 131 // Indicates whether there is already a pending task to persist dirty model | |
| 132 // associations. | |
| 133 bool task_pending_; | |
| 134 | |
| 135 DISALLOW_COPY_AND_ASSIGN(ModelAssociator); | |
| 136 }; | |
| 137 | |
| 138 } // namespace browser_sync | |
| 139 | |
| 140 #endif // CHROME_BROWSER_SYNC_GLUE_MODEL_ASSOCATOR_H_ | |
| 141 #endif // CHROME_PERSONALIZATION | |
| OLD | NEW |