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_PROFILE_MODEL_ASSOCIATOR_H_ |
| 6 #define CHROME_BROWSER_SYNC_GLUE_AUTOFILL_PROFILE_MODEL_ASSOCIATOR_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 // Contains all model association related logic: |
| 38 // * Algorithm to associate autofill model and sync model. |
| 39 // We do not check if we have local data before this run; we always |
| 40 // merge and sync. |
| 41 class AutofillProfileModelAssociator |
| 42 : public PerDataTypeAssociatorInterface<std::string, std::string> { |
| 43 public: |
| 44 AutofillProfileModelAssociator(ProfileSyncService* sync_service, |
| 45 WebDatabase* web_database, |
| 46 PersonalDataManager* data_manager); |
| 47 virtual ~AutofillProfileModelAssociator(); |
| 48 |
| 49 // A convenience wrapper of a bunch of state we pass around while |
| 50 // associating models, and send to the WebDatabase for persistence. |
| 51 // We do this so we hold the write lock for only a small period. |
| 52 // When storing the web db we are out of the write lock. |
| 53 struct DataBundle; |
| 54 |
| 55 static syncable::ModelType model_type() { return syncable::AUTOFILL_PROFILE; } |
| 56 |
| 57 // PerDataTypeAssociatorInterface implementation. |
| 58 // |
| 59 // Iterates through the sync model looking for matched pairs of items. |
| 60 virtual bool AssociateModels(); |
| 61 |
| 62 // Clears all associations. |
| 63 virtual bool DisassociateModels(); |
| 64 |
| 65 // [TODO] The has_nodes out param is true if the sync model has nodes other |
| 66 // than the permanent tagged nodes. |
| 67 virtual bool SyncModelHasUserCreatedNodes(bool* has_nodes); |
| 68 |
| 69 // See ModelAssociator interface. |
| 70 virtual void AbortAssociation(); |
| 71 |
| 72 // [TODO]Not implemented. Does not seemt to be necessary. |
| 73 virtual const std::string* GetChromeNodeFromSyncId( |
| 74 int64 sync_id) { |
| 75 return NULL; |
| 76 } |
| 77 |
| 78 // [TODO]Not implemented. Does not seem to be necessary. |
| 79 virtual bool InitSyncNodeFromChromeId(std::string node_id, |
| 80 sync_api::BaseNode* sync_node) { |
| 81 return false; |
| 82 } |
| 83 |
| 84 // Returns the sync id for the given autofill name, or sync_api::kInvalidId |
| 85 // if the autofill name is not associated to any sync id. |
| 86 virtual int64 GetSyncIdFromChromeId(std::string node_id); |
| 87 |
| 88 // Associates the given autofill name with the given sync id. |
| 89 virtual void Associate(const std::string* node, int64 sync_id); |
| 90 |
| 91 // Remove the association that corresponds to the given sync id. |
| 92 virtual void Disassociate(int64 sync_id); |
| 93 |
| 94 // [TODO] Returns whether a node with the |
| 95 // given permanent tag was found and update |
| 96 // |sync_id| with that node's id. No current use. To Implement |
| 97 // only for completeness. |
| 98 virtual bool GetSyncIdForTaggedNode(const std::string& tag, int64* sync_id) { |
| 99 return false; |
| 100 } |
| 101 |
| 102 // Returns sync service instance. |
| 103 ProfileSyncService* sync_service() { return sync_service_; } |
| 104 |
| 105 protected: |
| 106 AutofillProfileModelAssociator() {} |
| 107 virtual sync_api::ReadNode* GetReadNode(sync_api::WriteTransaction *trans); |
| 108 bool TraverseAndAssociateChromeAutoFillProfiles( |
| 109 sync_api::WriteTransaction* write_trans, |
| 110 const sync_api::ReadNode& autofill_root, |
| 111 const std::vector<AutoFillProfile*>& all_profiles_from_db, |
| 112 std::set<std::string>* current_profiles, |
| 113 std::vector<AutoFillProfile*>* updated_profiles); |
| 114 |
| 115 // Helper to insert an AutoFillProfile into the WebDatabase (e.g. in response |
| 116 // to encountering a sync node that doesn't exist yet locally). |
| 117 virtual void AddNativeProfileIfNeeded( |
| 118 const sync_pb::AutofillProfile2Specifics& profile, |
| 119 DataBundle* bundle, |
| 120 const sync_api::ReadNode& node); |
| 121 |
| 122 // Helper to insert a sync node for the given AutoFillProfile (e.g. in |
| 123 // response to encountering a native profile that doesn't exist yet in the |
| 124 // cloud). |
| 125 virtual bool MakeNewAutofillProfileSyncNode( |
| 126 sync_api::WriteTransaction* trans, |
| 127 const sync_api::BaseNode& autofill_root, |
| 128 const AutoFillProfile& profile, |
| 129 int64* sync_id); |
| 130 |
| 131 // Once the above traversals are complete, we traverse the sync model to |
| 132 // associate all remaining nodes. |
| 133 bool TraverseAndAssociateAllSyncNodes( |
| 134 sync_api::WriteTransaction* write_trans, |
| 135 const sync_api::ReadNode& autofill_root, |
| 136 DataBundle* bundle); |
| 137 |
| 138 static bool OverwriteProfileWithServerData( |
| 139 AutoFillProfile* merge_into, |
| 140 const sync_pb::AutofillProfile2Specifics& specifics); |
| 141 |
| 142 virtual bool EnsureOnThread(BrowserThread::ID); |
| 143 |
| 144 // This is used by test cases that test this object. |
| 145 // Since destructor cannot really call any overloaded methods |
| 146 // it makes it impossible |
| 147 // For us to override the ::currentlyon function. So setting this boolean |
| 148 // indicates we should not perform the check. |
| 149 bool dontCheckThreadOnExit_; |
| 150 private: |
| 151 typedef std::map<std::string, int64> AutofillToSyncIdMap; |
| 152 typedef std::map<int64, std::string> SyncIdToAutofillMap; |
| 153 |
| 154 // A convenience wrapper of a bunch of state we pass around while associating |
| 155 // models, and send to the WebDatabase for persistence. |
| 156 // struct DataBundle; |
| 157 |
| 158 // Helper to query WebDatabase for the current autofill state. |
| 159 bool LoadAutofillData(std::vector<AutoFillProfile*>* profiles); |
| 160 |
| 161 static bool MergeField(FormGroup* f, |
| 162 AutoFillFieldType t, |
| 163 const std::string& specifics_field); |
| 164 |
| 165 // Helper to persist any changes that occured during model association to |
| 166 // the WebDatabase. |
| 167 bool SaveChangesToWebData(const DataBundle& bundle); |
| 168 |
| 169 // Called at various points in model association to determine if the |
| 170 // user requested an abort. |
| 171 bool IsAbortPending(); |
| 172 |
| 173 ProfileSyncService* sync_service_; |
| 174 WebDatabase* web_database_; |
| 175 PersonalDataManager* personal_data_; |
| 176 int64 autofill_node_id_; |
| 177 |
| 178 AutofillToSyncIdMap id_map_; |
| 179 SyncIdToAutofillMap id_map_inverse_; |
| 180 |
| 181 // Abort association pending flag and lock. If this is set to true |
| 182 // (via the AbortAssociation method), return from the |
| 183 // AssociateModels method as soon as possible. |
| 184 Lock abort_association_pending_lock_; |
| 185 bool abort_association_pending_; |
| 186 |
| 187 DISALLOW_COPY_AND_ASSIGN(AutofillProfileModelAssociator); |
| 188 }; |
| 189 |
| 190 struct AutofillProfileModelAssociator::DataBundle { |
| 191 std::set<std::string> current_profiles; |
| 192 std::vector<AutoFillProfile*> updated_profiles; |
| 193 std::vector<AutoFillProfile*> new_profiles; // We own these pointers. |
| 194 ~DataBundle() { STLDeleteElements(&new_profiles); } |
| 195 }; |
| 196 |
| 197 } // namespace browser_sync |
| 198 |
| 199 #endif // CHROME_BROWSER_SYNC_GLUE_AUTOFILL_PROFILE_MODEL_ASSOCIATOR_H_ |
OLD | NEW |