summaryrefslogtreecommitdiffstats
path: root/chrome/browser/sync/glue/bookmark_model_associator.h
diff options
context:
space:
mode:
authorBen Murdoch <benm@google.com>2010-07-29 17:14:53 +0100
committerBen Murdoch <benm@google.com>2010-08-04 14:29:45 +0100
commitc407dc5cd9bdc5668497f21b26b09d988ab439de (patch)
tree7eaf8707c0309516bdb042ad976feedaf72b0bb1 /chrome/browser/sync/glue/bookmark_model_associator.h
parent0998b1cdac5733f299c12d88bc31ef9c8035b8fa (diff)
downloadexternal_chromium-c407dc5cd9bdc5668497f21b26b09d988ab439de.zip
external_chromium-c407dc5cd9bdc5668497f21b26b09d988ab439de.tar.gz
external_chromium-c407dc5cd9bdc5668497f21b26b09d988ab439de.tar.bz2
Merge Chromium src@r53293
Change-Id: Ia79acf8670f385cee48c45b0a75371d8e950af34
Diffstat (limited to 'chrome/browser/sync/glue/bookmark_model_associator.h')
-rw-r--r--chrome/browser/sync/glue/bookmark_model_associator.h142
1 files changed, 142 insertions, 0 deletions
diff --git a/chrome/browser/sync/glue/bookmark_model_associator.h b/chrome/browser/sync/glue/bookmark_model_associator.h
new file mode 100644
index 0000000..5c725cc
--- /dev/null
+++ b/chrome/browser/sync/glue/bookmark_model_associator.h
@@ -0,0 +1,142 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_SYNC_GLUE_BOOKMARK_MODEL_ASSOCIATOR_H_
+#define CHROME_BROWSER_SYNC_GLUE_BOOKMARK_MODEL_ASSOCIATOR_H_
+
+#include <map>
+#include <set>
+#include <string>
+
+#include "base/basictypes.h"
+#include "base/task.h"
+#include "chrome/browser/sync/unrecoverable_error_handler.h"
+#include "chrome/browser/sync/glue/model_associator.h"
+
+class BookmarkNode;
+
+namespace sync_api {
+class BaseNode;
+class BaseTransaction;
+class ReadNode;
+}
+
+class ProfileSyncService;
+
+namespace browser_sync {
+
+class BookmarkChangeProcessor;
+
+// Contains all model association related logic:
+// * Algorithm to associate bookmark model and sync model.
+// * Methods to get a bookmark node for a given sync node and vice versa.
+// * Persisting model associations and loading them back.
+class BookmarkModelAssociator
+ : public PerDataTypeAssociatorInterface<BookmarkNode, int64> {
+ public:
+ static syncable::ModelType model_type() { return syncable::BOOKMARKS; }
+ BookmarkModelAssociator(ProfileSyncService* sync_service,
+ UnrecoverableErrorHandler* persist_ids_error_handler);
+ virtual ~BookmarkModelAssociator();
+
+ // AssociatorInterface implementation.
+ //
+ // AssociateModels iterates through both the sync and the browser
+ // bookmark model, looking for matched pairs of items. For any pairs it
+ // finds, it will call AssociateSyncID. For any unmatched items,
+ // MergeAndAssociateModels will try to repair the match, e.g. by adding a new
+ // node. After successful completion, the models should be identical and
+ // corresponding. Returns true on success. On failure of this step, we
+ // should abort the sync operation and report an error to the user.
+ virtual bool AssociateModels();
+
+ virtual bool DisassociateModels();
+
+ // The has_nodes out param is true if the sync model has nodes other
+ // than the permanent tagged nodes.
+ virtual bool SyncModelHasUserCreatedNodes(bool* has_nodes);
+
+ // Returns sync id for the given bookmark node id.
+ // Returns sync_api::kInvalidId if the sync node is not found for the given
+ // bookmark node id.
+ virtual int64 GetSyncIdFromChromeId(int64 node_id);
+
+ // Returns the bookmark node for the given sync id.
+ // Returns NULL if no bookmark node is found for the given sync id.
+ virtual const BookmarkNode* GetChromeNodeFromSyncId(int64 sync_id);
+
+ // Initializes the given sync node from the given bookmark node id.
+ // Returns false if no sync node was found for the given bookmark node id or
+ // if the initialization of sync node fails.
+ virtual bool InitSyncNodeFromChromeId(int64 node_id,
+ sync_api::BaseNode* sync_node);
+
+ // Associates the given bookmark node with the given sync id.
+ virtual void Associate(const BookmarkNode* node, int64 sync_id);
+ // Remove the association that corresponds to the given sync id.
+ virtual void Disassociate(int64 sync_id);
+
+ virtual void AbortAssociation() {
+ // No implementation needed, this associator runs on the main
+ // thread.
+ }
+
+ // Returns sync service instance.
+ ProfileSyncService* sync_service() { return sync_service_; }
+
+ protected:
+ // Stores the id of the node with the given tag in |sync_id|.
+ // Returns of that node was found successfully.
+ // Tests override this.
+ virtual bool GetSyncIdForTaggedNode(const std::string& tag, int64* sync_id);
+
+ private:
+ typedef std::map<int64, int64> BookmarkIdToSyncIdMap;
+ typedef std::map<int64, const BookmarkNode*> SyncIdToBookmarkNodeMap;
+ typedef std::set<int64> DirtyAssociationsSyncIds;
+
+ // Posts a task to persist dirty associations.
+ void PostPersistAssociationsTask();
+ // Persists all dirty associations.
+ void PersistAssociations();
+
+ // Loads the persisted associations into in-memory maps.
+ // If the persisted associations are out-of-date due to some reason, returns
+ // false; otherwise returns true.
+ bool LoadAssociations();
+
+ // Matches up the bookmark model and the sync model to build model
+ // associations.
+ bool BuildAssociations();
+
+ // Associate a top-level node of the bookmark model with a permanent node in
+ // the sync domain. Such permanent nodes are identified by a tag that is
+ // well known to the server and the client, and is unique within a particular
+ // user's share. For example, "other_bookmarks" is the tag for the Other
+ // Bookmarks folder. The sync nodes are server-created.
+ bool AssociateTaggedPermanentNode(const BookmarkNode* permanent_node,
+ const std::string& tag);
+
+ // Compare the properties of a pair of nodes from either domain.
+ bool NodesMatch(const BookmarkNode* bookmark,
+ const sync_api::BaseNode* sync_node) const;
+
+ ProfileSyncService* sync_service_;
+ UnrecoverableErrorHandler* persist_ids_error_handler_;
+ BookmarkIdToSyncIdMap id_map_;
+ SyncIdToBookmarkNodeMap id_map_inverse_;
+ // Stores sync ids for dirty associations.
+ DirtyAssociationsSyncIds dirty_associations_sync_ids_;
+
+ // Used to post PersistAssociation tasks to the current message loop and
+ // guarantees no invocations can occur if |this| has been deleted. (This
+ // allows this class to be non-refcounted).
+ ScopedRunnableMethodFactory<BookmarkModelAssociator> persist_associations_;
+
+ DISALLOW_COPY_AND_ASSIGN(BookmarkModelAssociator);
+};
+
+} // namespace browser_sync
+
+#endif // CHROME_BROWSER_SYNC_GLUE_BOOKMARK_MODEL_ASSOCIATOR_H_