Add ENABLE_MEDIA_ROUTER define to builds other than Android and iOS.
[chromium-blink-merge.git] / chrome / browser / sync / glue / bookmark_model_associator.h
blobaecdcbf20b7fa662111dba323ea646aa99ba797a
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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_SYNC_GLUE_BOOKMARK_MODEL_ASSOCIATOR_H_
6 #define CHROME_BROWSER_SYNC_GLUE_BOOKMARK_MODEL_ASSOCIATOR_H_
8 #include <map>
9 #include <set>
10 #include <string>
12 #include "base/basictypes.h"
13 #include "base/compiler_specific.h"
14 #include "base/memory/weak_ptr.h"
15 #include "components/sync_driver/data_type_controller.h"
16 #include "components/sync_driver/data_type_error_handler.h"
17 #include "components/sync_driver/model_associator.h"
18 #include "sync/internal_api/public/util/unrecoverable_error_handler.h"
20 class Profile;
22 namespace bookmarks {
23 class BookmarkModel;
24 class BookmarkNode;
27 namespace syncer {
28 class BaseNode;
29 class BaseTransaction;
30 struct UserShare;
33 namespace browser_sync {
35 // Contains all model association related logic:
36 // * Algorithm to associate bookmark model and sync model.
37 // * Methods to get a bookmark node for a given sync node and vice versa.
38 // * Persisting model associations and loading them back.
39 class BookmarkModelAssociator
40 : public sync_driver::
41 PerDataTypeAssociatorInterface<bookmarks::BookmarkNode, int64> {
42 public:
43 static syncer::ModelType model_type() { return syncer::BOOKMARKS; }
44 // |expect_mobile_bookmarks_folder| controls whether or not we
45 // expect the mobile bookmarks permanent folder to be created.
46 // Should be set to true only by mobile clients.
47 BookmarkModelAssociator(
48 bookmarks::BookmarkModel* bookmark_model,
49 Profile* profile_,
50 syncer::UserShare* user_share,
51 sync_driver::DataTypeErrorHandler* unrecoverable_error_handler,
52 bool expect_mobile_bookmarks_folder);
53 ~BookmarkModelAssociator() override;
55 // Updates the visibility of the permanents node in the BookmarkModel.
56 void UpdatePermanentNodeVisibility();
58 // AssociatorInterface implementation.
60 // AssociateModels iterates through both the sync and the browser
61 // bookmark model, looking for matched pairs of items. For any pairs it
62 // finds, it will call AssociateSyncID. For any unmatched items,
63 // MergeAndAssociateModels will try to repair the match, e.g. by adding a new
64 // node. After successful completion, the models should be identical and
65 // corresponding. Returns true on success. On failure of this step, we
66 // should abort the sync operation and report an error to the user.
67 syncer::SyncError AssociateModels(
68 syncer::SyncMergeResult* local_merge_result,
69 syncer::SyncMergeResult* syncer_merge_result) override;
71 syncer::SyncError DisassociateModels() override;
73 // The has_nodes out param is true if the sync model has nodes other
74 // than the permanent tagged nodes.
75 bool SyncModelHasUserCreatedNodes(bool* has_nodes) override;
77 // Returns sync id for the given bookmark node id.
78 // Returns syncer::kInvalidId if the sync node is not found for the given
79 // bookmark node id.
80 int64 GetSyncIdFromChromeId(const int64& node_id) override;
82 // Returns the bookmark node for the given sync id.
83 // Returns NULL if no bookmark node is found for the given sync id.
84 const bookmarks::BookmarkNode* GetChromeNodeFromSyncId(
85 int64 sync_id) override;
87 // Initializes the given sync node from the given bookmark node id.
88 // Returns false if no sync node was found for the given bookmark node id or
89 // if the initialization of sync node fails.
90 bool InitSyncNodeFromChromeId(const int64& node_id,
91 syncer::BaseNode* sync_node) override;
93 // Associates the given bookmark node with the given sync id.
94 void Associate(const bookmarks::BookmarkNode* node, int64 sync_id) override;
95 // Remove the association that corresponds to the given sync id.
96 void Disassociate(int64 sync_id) override;
98 void AbortAssociation() override {
99 // No implementation needed, this associator runs on the main
100 // thread.
103 // See ModelAssociator interface.
104 bool CryptoReadyIfNecessary() override;
106 protected:
107 // Stores the id of the node with the given tag in |sync_id|.
108 // Returns of that node was found successfully.
109 // Tests override this.
110 virtual bool GetSyncIdForTaggedNode(const std::string& tag, int64* sync_id);
112 private:
113 typedef std::map<int64, int64> BookmarkIdToSyncIdMap;
114 typedef std::map<int64, const bookmarks::BookmarkNode*>
115 SyncIdToBookmarkNodeMap;
116 typedef std::set<int64> DirtyAssociationsSyncIds;
118 // Posts a task to persist dirty associations.
119 void PostPersistAssociationsTask();
120 // Persists all dirty associations.
121 void PersistAssociations();
123 // Matches up the bookmark model and the sync model to build model
124 // associations.
125 syncer::SyncError BuildAssociations(
126 syncer::SyncMergeResult* local_merge_result,
127 syncer::SyncMergeResult* syncer_merge_result);
129 // Removes bookmark nodes whose corresponding sync nodes have been deleted
130 // according to sync delete journals. Return number of deleted bookmarks.
131 int64 ApplyDeletesFromSyncJournal(syncer::BaseTransaction* trans);
133 // Associate a top-level node of the bookmark model with a permanent node in
134 // the sync domain. Such permanent nodes are identified by a tag that is
135 // well known to the server and the client, and is unique within a particular
136 // user's share. For example, "other_bookmarks" is the tag for the Other
137 // Bookmarks folder. The sync nodes are server-created.
138 // Returns true on success, false if association failed.
139 bool AssociateTaggedPermanentNode(
140 const bookmarks::BookmarkNode* permanent_node,
141 const std::string& tag) WARN_UNUSED_RESULT;
143 // Check whether bookmark model and sync model are synced by comparing
144 // their transaction versions.
145 // Returns a PERSISTENCE_ERROR if a transaction mismatch was detected where
146 // the native model has a newer transaction verison.
147 syncer::SyncError CheckModelSyncState(
148 syncer::SyncMergeResult* local_merge_result,
149 syncer::SyncMergeResult* syncer_merge_result) const;
151 bookmarks::BookmarkModel* bookmark_model_;
152 Profile* profile_;
153 syncer::UserShare* user_share_;
154 sync_driver::DataTypeErrorHandler* unrecoverable_error_handler_;
155 const bool expect_mobile_bookmarks_folder_;
156 BookmarkIdToSyncIdMap id_map_;
157 SyncIdToBookmarkNodeMap id_map_inverse_;
158 // Stores sync ids for dirty associations.
159 DirtyAssociationsSyncIds dirty_associations_sync_ids_;
161 // Used to post PersistAssociation tasks to the current message loop and
162 // guarantees no invocations can occur if |this| has been deleted. (This
163 // allows this class to be non-refcounted).
164 base::WeakPtrFactory<BookmarkModelAssociator> weak_factory_;
166 DISALLOW_COPY_AND_ASSIGN(BookmarkModelAssociator);
169 } // namespace browser_sync
171 #endif // CHROME_BROWSER_SYNC_GLUE_BOOKMARK_MODEL_ASSOCIATOR_H_