OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CHROME_BROWSER_SYNC_GLUE_AUTOFILL_MODEL_ASSOCIATOR2_H_ |
| 6 #define CHROME_BROWSER_SYNC_GLUE_AUTOFILL_MODEL_ASSOCIATOR2_H_ |
| 7 #pragma once |
| 8 |
| 9 #include <map> |
| 10 #include <set> |
| 11 #include <string> |
| 12 #include <vector> |
| 13 |
| 14 #include "base/basictypes.h" |
| 15 #include "base/lock.h" |
| 16 #include "base/ref_counted.h" |
| 17 #include "chrome/browser/autofill/personal_data_manager.h" |
| 18 #include "chrome/browser/sync/engine/syncapi.h" |
| 19 #include "chrome/browser/sync/glue/model_associator.h" |
| 20 #include "chrome/browser/sync/protocol/autofill_specifics.pb.h" |
| 21 #include "chrome/browser/webdata/autofill_entry.h" |
| 22 |
| 23 class AutoFillProfile; |
| 24 |
| 25 class ProfileSyncService; |
| 26 class WebDatabase; |
| 27 |
| 28 namespace sync_api { |
| 29 class WriteTransaction; |
| 30 } |
| 31 |
| 32 namespace browser_sync { |
| 33 |
| 34 class AutofillChangeProcessor; |
| 35 class UnrecoverableErrorHandler; |
| 36 |
| 37 extern const char kAutofillTag[]; |
| 38 extern const char kAutofillProfileNamespaceTag[]; |
| 39 extern const char kAutofillEntryNamespaceTag[]; |
| 40 |
| 41 // Contains all model association related logic: |
| 42 // * Algorithm to associate autofill model and sync model. |
| 43 // We do not check if we have local data before this run; we always |
| 44 // merge and sync. |
| 45 class AutofillModelAssociator2 |
| 46 : public PerDataTypeAssociatorInterface<std::string, std::string> { |
| 47 public: |
| 48 static syncable::ModelType model_type() { return syncable::AUTOFILL; } |
| 49 AutofillModelAssociator2(ProfileSyncService* sync_service, |
| 50 WebDatabase* web_database, |
| 51 PersonalDataManager* data_manager); |
| 52 virtual ~AutofillModelAssociator2(); |
| 53 |
| 54 // A task used by this class and the change processor to inform the |
| 55 // PersonalDataManager living on the UI thread that it needs to refresh. |
| 56 class DoOptimisticRefreshTask : public Task { |
| 57 public: |
| 58 explicit DoOptimisticRefreshTask(PersonalDataManager* pdm); |
| 59 virtual ~DoOptimisticRefreshTask(); |
| 60 virtual void Run(); |
| 61 private: |
| 62 scoped_refptr<PersonalDataManager> pdm_; |
| 63 }; |
| 64 |
| 65 // PerDataTypeAssociatorInterface implementation. |
| 66 // |
| 67 // Iterates through the sync model looking for matched pairs of items. |
| 68 virtual bool AssociateModels(); |
| 69 |
| 70 // Clears all associations. |
| 71 virtual bool DisassociateModels(); |
| 72 |
| 73 // The has_nodes out param is true if the sync model has nodes other |
| 74 // than the permanent tagged nodes. |
| 75 virtual bool SyncModelHasUserCreatedNodes(bool* has_nodes); |
| 76 |
| 77 // See ModelAssociator interface. |
| 78 virtual void AbortAssociation(); |
| 79 |
| 80 // Not implemented. |
| 81 virtual const std::string* GetChromeNodeFromSyncId(int64 sync_id); |
| 82 |
| 83 // Not implemented. |
| 84 virtual bool InitSyncNodeFromChromeId(std::string node_id, |
| 85 sync_api::BaseNode* sync_node); |
| 86 |
| 87 // Returns the sync id for the given autofill name, or sync_api::kInvalidId |
| 88 // if the autofill name is not associated to any sync id. |
| 89 virtual int64 GetSyncIdFromChromeId(std::string node_id); |
| 90 |
| 91 // Associates the given autofill name with the given sync id. |
| 92 virtual void Associate(const std::string* node, int64 sync_id); |
| 93 |
| 94 // Remove the association that corresponds to the given sync id. |
| 95 virtual void Disassociate(int64 sync_id); |
| 96 |
| 97 // Returns whether a node with the given permanent tag was found and update |
| 98 // |sync_id| with that node's id. |
| 99 virtual bool GetSyncIdForTaggedNode(const std::string& tag, int64* sync_id); |
| 100 |
| 101 static std::string KeyToTag(const string16& name, const string16& value); |
| 102 static std::string ProfileLabelToTag(const string16& label); |
| 103 |
| 104 static bool MergeTimestamps(const sync_pb::AutofillSpecifics& autofill, |
| 105 const std::vector<base::Time>& timestamps, |
| 106 std::vector<base::Time>* new_timestamps); |
| 107 static bool OverwriteProfileWithServerData( |
| 108 AutoFillProfile* merge_into, |
| 109 const sync_pb::AutofillProfileSpecifics& specifics); |
| 110 |
| 111 // TODO(georgey) : add the same processing for CC info (already in protocol |
| 112 // buffers). |
| 113 |
| 114 // Returns sync service instance. |
| 115 ProfileSyncService* sync_service() { return sync_service_; } |
| 116 |
| 117 // Compute and apply suffix to a label so that the resulting label is |
| 118 // unique in the sync database. |
| 119 // |new_non_unique_label| is the colliding label which is to be uniquified. |
| 120 // |existing_unique_label| is the current label of the object, if any; this |
| 121 // is treated as a unique label even if colliding. If no such label is |
| 122 // available, |existing_unique_label| may be empty. |
| 123 static string16 MakeUniqueLabel(const string16& new_non_unique_label, |
| 124 const string16& existing_unique_label, |
| 125 sync_api::BaseTransaction* trans); |
| 126 |
| 127 private: |
| 128 typedef std::map<std::string, int64> AutofillToSyncIdMap; |
| 129 typedef std::map<int64, std::string> SyncIdToAutofillMap; |
| 130 |
| 131 // A convenience wrapper of a bunch of state we pass around while associating |
| 132 // models, and send to the WebDatabase for persistence. |
| 133 struct DataBundle; |
| 134 |
| 135 // Helper to query WebDatabase for the current autofill state. |
| 136 bool LoadAutofillData(std::vector<AutofillEntry>* entries, |
| 137 std::vector<AutoFillProfile*>* profiles); |
| 138 |
| 139 // We split up model association first by autofill sub-type (entries, and |
| 140 // profiles. There is a Traverse* method for each of these. |
| 141 bool TraverseAndAssociateChromeAutofillEntries( |
| 142 sync_api::WriteTransaction* write_trans, |
| 143 const sync_api::ReadNode& autofill_root, |
| 144 const std::vector<AutofillEntry>& all_entries_from_db, |
| 145 std::set<AutofillKey>* current_entries, |
| 146 std::vector<AutofillEntry>* new_entries); |
| 147 bool TraverseAndAssociateChromeAutoFillProfiles( |
| 148 sync_api::WriteTransaction* write_trans, |
| 149 const sync_api::ReadNode& autofill_root, |
| 150 const std::vector<AutoFillProfile*>& all_profiles_from_db, |
| 151 std::set<string16>* current_profiles, |
| 152 std::vector<AutoFillProfile*>* updated_profiles); |
| 153 |
| 154 // Once the above traversals are complete, we traverse the sync model to |
| 155 // associate all remaining nodes. |
| 156 bool TraverseAndAssociateAllSyncNodes( |
| 157 sync_api::WriteTransaction* write_trans, |
| 158 const sync_api::ReadNode& autofill_root, |
| 159 DataBundle* bundle); |
| 160 |
| 161 // Helper to persist any changes that occured during model association to |
| 162 // the WebDatabase. |
| 163 bool SaveChangesToWebData(const DataBundle& bundle); |
| 164 |
| 165 // Helper to insert an AutofillEntry into the WebDatabase (e.g. in response |
| 166 // to encountering a sync node that doesn't exist yet locally). |
| 167 void AddNativeEntryIfNeeded(const sync_pb::AutofillSpecifics& autofill, |
| 168 DataBundle* bundle, |
| 169 const sync_api::ReadNode& node); |
| 170 |
| 171 // Helper to insert an AutoFillProfile into the WebDatabase (e.g. in response |
| 172 // to encountering a sync node that doesn't exist yet locally). |
| 173 void AddNativeProfileIfNeeded( |
| 174 const sync_pb::AutofillProfileSpecifics& profile, |
| 175 DataBundle* bundle, |
| 176 const sync_api::ReadNode& node); |
| 177 |
| 178 // Helper to insert a sync node for the given AutoFillProfile (e.g. in |
| 179 // response to encountering a native profile that doesn't exist yet in the |
| 180 // cloud). |
| 181 bool MakeNewAutofillProfileSyncNode( |
| 182 sync_api::WriteTransaction* trans, |
| 183 const sync_api::BaseNode& autofill_root, |
| 184 const std::string& tag, |
| 185 const AutoFillProfile& profile, |
| 186 int64* sync_id); |
| 187 |
| 188 // Called at various points in model association to determine if the |
| 189 // user requested an abort. |
| 190 bool IsAbortPending(); |
| 191 |
| 192 ProfileSyncService* sync_service_; |
| 193 WebDatabase* web_database_; |
| 194 PersonalDataManager* personal_data_; |
| 195 int64 autofill_node_id_; |
| 196 |
| 197 AutofillToSyncIdMap id_map_; |
| 198 SyncIdToAutofillMap id_map_inverse_; |
| 199 |
| 200 // Abort association pending flag and lock. If this is set to true |
| 201 // (via the AbortAssociation method), return from the |
| 202 // AssociateModels method as soon as possible. |
| 203 Lock abort_association_pending_lock_; |
| 204 bool abort_association_pending_; |
| 205 |
| 206 DISALLOW_COPY_AND_ASSIGN(AutofillModelAssociator2); |
| 207 }; |
| 208 |
| 209 } // namespace browser_sync |
| 210 |
| 211 #endif // CHROME_BROWSER_SYNC_GLUE_AUTOFILL_MODEL_ASSOCIATOR_H_ |
OLD | NEW |