| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 |
| 5 #ifndef CHROME_BROWSER_SYNC_GLUE_AUTOFILL_PROFILE_MODEL_ASSOCIATOR_H_ | 6 #ifndef CHROME_BROWSER_SYNC_GLUE_AUTOFILL_PROFILE_MODEL_ASSOCIATOR_H_ |
| 6 #define CHROME_BROWSER_SYNC_GLUE_AUTOFILL_PROFILE_MODEL_ASSOCIATOR_H_ | 7 #define CHROME_BROWSER_SYNC_GLUE_AUTOFILL_PROFILE_MODEL_ASSOCIATOR_H_ |
| 7 #pragma once | 8 #pragma once |
| 8 | 9 |
| 9 #include <map> | 10 #include <map> |
| 10 #include <set> | 11 #include <set> |
| 11 #include <string> | 12 #include <string> |
| 12 #include <vector> | 13 #include <vector> |
| 13 | 14 |
| 14 #include "base/basictypes.h" | 15 #include "base/basictypes.h" |
| 15 #include "base/lock.h" | 16 #include "base/lock.h" |
| 16 #include "base/ref_counted.h" | 17 #include "base/ref_counted.h" |
| 17 #include "chrome/browser/autofill/personal_data_manager.h" | 18 #include "chrome/browser/autofill/personal_data_manager.h" |
| 18 #include "chrome/browser/sync/engine/syncapi.h" | 19 #include "chrome/browser/sync/engine/syncapi.h" |
| 19 #include "chrome/browser/sync/glue/model_associator.h" | 20 #include "chrome/browser/sync/glue/model_associator.h" |
| 20 #include "chrome/browser/sync/protocol/autofill_specifics.pb.h" | 21 #include "chrome/browser/sync/protocol/autofill_specifics.pb.h" |
| 21 #include "chrome/browser/webdata/autofill_entry.h" | 22 #include "chrome/browser/webdata/autofill_entry.h" |
| 22 | 23 |
| 23 class AutoFillProfile; | 24 class AutoFillProfile; |
| 24 | 25 |
| 25 class ProfileSyncService; | 26 class ProfileSyncService; |
| 26 class WebDatabase; | 27 class WebDatabase; |
| 27 | 28 |
| 28 namespace sync_api { | 29 namespace sync_api { |
| 29 class WriteTransaction; | 30 class WriteTransaction; |
| 30 } | 31 } |
| 31 | 32 |
| 32 namespace browser_sync { | 33 namespace browser_sync { |
| 33 | 34 |
| 35 extern const char kAutofillProfileTag[]; |
| 36 |
| 34 class AutofillChangeProcessor; | 37 class AutofillChangeProcessor; |
| 35 class UnrecoverableErrorHandler; | 38 class UnrecoverableErrorHandler; |
| 36 | 39 |
| 37 // Contains all model association related logic: | 40 // Contains all model association related logic: |
| 38 // * Algorithm to associate autofill model and sync model. | 41 // * Algorithm to associate autofill model and sync model. |
| 39 // We do not check if we have local data before this run; we always | 42 // We do not check if we have local data before this run; we always |
| 40 // merge and sync. | 43 // merge and sync. |
| 41 class AutofillProfileModelAssociator | 44 class AutofillProfileModelAssociator |
| 42 : public PerDataTypeAssociatorInterface<std::string, std::string> { | 45 : public PerDataTypeAssociatorInterface<std::string, std::string> { |
| 43 public: | 46 public: |
| (...skipping 19 matching lines...) Expand all Loading... |
| 63 virtual bool DisassociateModels(); | 66 virtual bool DisassociateModels(); |
| 64 | 67 |
| 65 // TODO(lipalani) Bug 64111. | 68 // TODO(lipalani) Bug 64111. |
| 66 // The has_nodes out param is true if the sync model has nodes other | 69 // The has_nodes out param is true if the sync model has nodes other |
| 67 // than the permanent tagged nodes. | 70 // than the permanent tagged nodes. |
| 68 virtual bool SyncModelHasUserCreatedNodes(bool* has_nodes); | 71 virtual bool SyncModelHasUserCreatedNodes(bool* has_nodes); |
| 69 | 72 |
| 70 // See ModelAssociator interface. | 73 // See ModelAssociator interface. |
| 71 virtual void AbortAssociation(); | 74 virtual void AbortAssociation(); |
| 72 | 75 |
| 73 virtual const std::string* GetChromeNodeFromSyncId( | 76 virtual const std::string* GetChromeNodeFromSyncId(int64 sync_id); |
| 74 int64 sync_id) { | |
| 75 return NULL; | |
| 76 } | |
| 77 | 77 |
| 78 virtual bool InitSyncNodeFromChromeId(const std::string& node_id, | 78 virtual bool InitSyncNodeFromChromeId(const std::string& node_id, |
| 79 sync_api::BaseNode* sync_node) { | 79 sync_api::BaseNode* sync_node) { |
| 80 return false; | 80 return false; |
| 81 } | 81 } |
| 82 | 82 |
| 83 // Returns the sync id for the given autofill name, or sync_api::kInvalidId | 83 // Returns the sync id for the given autofill name, or sync_api::kInvalidId |
| 84 // if the autofill name is not associated to any sync id. | 84 // if the autofill name is not associated to any sync id. |
| 85 virtual int64 GetSyncIdFromChromeId(const std::string& node_id); | 85 virtual int64 GetSyncIdFromChromeId(const std::string& node_id); |
| 86 | 86 |
| 87 // Associates the given autofill name with the given sync id. | 87 // Associates the given autofill name with the given sync id. |
| 88 virtual void Associate(const std::string* node, int64 sync_id); | 88 virtual void Associate(const std::string* node, int64 sync_id); |
| 89 | 89 |
| 90 // Remove the association that corresponds to the given sync id. | 90 // Remove the association that corresponds to the given sync id. |
| 91 virtual void Disassociate(int64 sync_id); | 91 virtual void Disassociate(int64 sync_id); |
| 92 | 92 |
| 93 // TODO(lipalani) Bug 64111. Returns whether a node with the | 93 // TODO(lipalani) Bug 64111. Returns whether a node with the |
| 94 // given permanent tag was found and update | 94 // given permanent tag was found and update |
| 95 // |sync_id| with that node's id. No current use. To Implement | 95 // |sync_id| with that node's id. No current use. To Implement |
| 96 // only for completeness. | 96 // only for completeness. |
| 97 virtual bool GetSyncIdForTaggedNode(const std::string& tag, int64* sync_id) { | 97 virtual bool GetSyncIdForTaggedNode(const std::string& tag, int64* sync_id); |
| 98 return false; | |
| 99 } | |
| 100 | 98 |
| 101 // Returns sync service instance. | 99 // Returns sync service instance. |
| 102 ProfileSyncService* sync_service() { return sync_service_; } | 100 ProfileSyncService* sync_service() { return sync_service_; } |
| 103 | 101 |
| 102 static bool OverwriteProfileWithServerData( |
| 103 AutoFillProfile* merge_into, |
| 104 const sync_pb::AutofillProfileSpecifics& specifics); |
| 105 |
| 104 protected: | 106 protected: |
| 105 AutofillProfileModelAssociator() {} | 107 AutofillProfileModelAssociator() {} |
| 106 bool TraverseAndAssociateChromeAutoFillProfiles( | 108 bool TraverseAndAssociateChromeAutoFillProfiles( |
| 107 sync_api::WriteTransaction* write_trans, | 109 sync_api::WriteTransaction* write_trans, |
| 108 const sync_api::ReadNode& autofill_root, | 110 const sync_api::ReadNode& autofill_root, |
| 109 const std::vector<AutoFillProfile*>& all_profiles_from_db, | 111 const std::vector<AutoFillProfile*>& all_profiles_from_db, |
| 110 std::set<std::string>* current_profiles, | 112 std::set<std::string>* current_profiles, |
| 111 std::vector<AutoFillProfile*>* updated_profiles, | 113 std::vector<AutoFillProfile*>* updated_profiles, |
| 112 std::vector<AutoFillProfile*>* new_profiles, | 114 std::vector<AutoFillProfile*>* new_profiles, |
| 113 std::vector<std::string>* profiles_to_delete); | 115 std::vector<std::string>* profiles_to_delete); |
| 114 | 116 |
| 117 |
| 115 // Helper to insert an AutoFillProfile into the WebDatabase (e.g. in response | 118 // Helper to insert an AutoFillProfile into the WebDatabase (e.g. in response |
| 116 // to encountering a sync node that doesn't exist yet locally). | 119 // to encountering a sync node that doesn't exist yet locally). |
| 117 virtual void AddNativeProfileIfNeeded( | 120 virtual void AddNativeProfileIfNeeded( |
| 118 const sync_pb::AutofillProfileSpecifics& profile, | 121 const sync_pb::AutofillProfileSpecifics& profile, |
| 119 DataBundle* bundle, | 122 DataBundle* bundle, |
| 120 const sync_api::ReadNode& node); | 123 const sync_api::ReadNode& node); |
| 121 | 124 |
| 122 // Helper to insert a sync node for the given AutoFillProfile (e.g. in | 125 // 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 | 126 // response to encountering a native profile that doesn't exist yet in the |
| 124 // cloud). | 127 // cloud). |
| 125 virtual bool MakeNewAutofillProfileSyncNodeIfNeeded( | 128 virtual bool MakeNewAutofillProfileSyncNodeIfNeeded( |
| 126 sync_api::WriteTransaction* trans, | 129 sync_api::WriteTransaction* trans, |
| 127 const sync_api::BaseNode& autofill_root, | 130 const sync_api::BaseNode& autofill_root, |
| 128 const AutoFillProfile& profile, | 131 const AutoFillProfile& profile, |
| 129 std::vector<AutoFillProfile*>* new_profiles, | 132 std::vector<AutoFillProfile*>* new_profiles, |
| 130 std::set<std::string>* current_profiles, | 133 std::set<std::string>* current_profiles, |
| 131 std::vector<std::string>* profiles_to_delete); | 134 std::vector<std::string>* profiles_to_delete); |
| 132 | 135 |
| 133 // Once the above traversals are complete, we traverse the sync model to | 136 // Once the above traversals are complete, we traverse the sync model to |
| 134 // associate all remaining nodes. | 137 // associate all remaining nodes. |
| 135 bool TraverseAndAssociateAllSyncNodes( | 138 bool TraverseAndAssociateAllSyncNodes( |
| 136 sync_api::WriteTransaction* write_trans, | 139 sync_api::WriteTransaction* write_trans, |
| 137 const sync_api::ReadNode& autofill_root, | 140 const sync_api::ReadNode& autofill_root, |
| 138 DataBundle* bundle); | 141 DataBundle* bundle); |
| 139 | 142 |
| 140 static bool OverwriteProfileWithServerData( | |
| 141 AutoFillProfile* merge_into, | |
| 142 const sync_pb::AutofillProfileSpecifics& specifics); | |
| 143 | |
| 144 private: | 143 private: |
| 145 typedef std::map<std::string, int64> AutofillToSyncIdMap; | 144 typedef std::map<std::string, int64> AutofillToSyncIdMap; |
| 146 typedef std::map<int64, std::string> SyncIdToAutofillMap; | 145 typedef std::map<int64, std::string> SyncIdToAutofillMap; |
| 147 | 146 |
| 148 // A convenience wrapper of a bunch of state we pass around while associating | 147 // A convenience wrapper of a bunch of state we pass around while associating |
| 149 // models, and send to the WebDatabase for persistence. | 148 // models, and send to the WebDatabase for persistence. |
| 150 // struct DataBundle; | 149 // struct DataBundle; |
| 151 | 150 |
| 152 // Helper to query WebDatabase for the current autofill state. | 151 // Helper to query WebDatabase for the current autofill state. |
| 153 bool LoadAutofillData(std::vector<AutoFillProfile*>* profiles); | 152 bool LoadAutofillData(std::vector<AutoFillProfile*>* profiles); |
| 154 | 153 |
| 155 static bool MergeField(FormGroup* f, | 154 static bool MergeField(FormGroup* f, |
| 156 AutoFillFieldType t, | 155 AutoFillFieldType t, |
| 157 const std::string& specifics_field); | 156 const std::string& specifics_field); |
| 158 | 157 |
| 159 // Helper to persist any changes that occured during model association to | 158 // Helper to persist any changes that occured during model association to |
| 160 // the WebDatabase. | 159 // the WebDatabase. |
| 161 bool SaveChangesToWebData(const DataBundle& bundle); | 160 bool SaveChangesToWebData(const DataBundle& bundle); |
| 162 | 161 |
| 163 // Called at various points in model association to determine if the | 162 // Called at various points in model association to determine if the |
| 164 // user requested an abort. | 163 // user requested an abort. |
| 165 bool IsAbortPending(); | 164 bool IsAbortPending(); |
| 166 | 165 |
| 167 int64 FindSyncNodeWithProfile(sync_api::WriteTransaction* trans, | 166 int64 FindSyncNodeWithProfile(sync_api::WriteTransaction* trans, |
| 168 const sync_api::BaseNode& autofill_root, | 167 const sync_api::BaseNode& autofill_root, |
| 169 const AutoFillProfile& profile); | 168 const AutoFillProfile& profile); |
| 170 | 169 |
| 170 bool MigrationLoggingEnabled(); |
| 171 |
| 171 ProfileSyncService* sync_service_; | 172 ProfileSyncService* sync_service_; |
| 172 WebDatabase* web_database_; | 173 WebDatabase* web_database_; |
| 173 PersonalDataManager* personal_data_; | 174 PersonalDataManager* personal_data_; |
| 174 int64 autofill_node_id_; | 175 int64 autofill_node_id_; |
| 175 | 176 |
| 176 AutofillToSyncIdMap id_map_; | 177 AutofillToSyncIdMap id_map_; |
| 177 SyncIdToAutofillMap id_map_inverse_; | 178 SyncIdToAutofillMap id_map_inverse_; |
| 178 | 179 |
| 179 // Abort association pending flag and lock. If this is set to true | 180 // Abort association pending flag and lock. If this is set to true |
| 180 // (via the AbortAssociation method), return from the | 181 // (via the AbortAssociation method), return from the |
| 181 // AssociateModels method as soon as possible. | 182 // AssociateModels method as soon as possible. |
| 182 Lock abort_association_pending_lock_; | 183 Lock abort_association_pending_lock_; |
| 183 bool abort_association_pending_; | 184 bool abort_association_pending_; |
| 184 | 185 |
| 186 int number_of_profiles_created_; |
| 187 |
| 185 DISALLOW_COPY_AND_ASSIGN(AutofillProfileModelAssociator); | 188 DISALLOW_COPY_AND_ASSIGN(AutofillProfileModelAssociator); |
| 186 }; | 189 }; |
| 187 | 190 |
| 188 struct AutofillProfileModelAssociator::DataBundle { | 191 struct AutofillProfileModelAssociator::DataBundle { |
| 189 std::set<std::string> current_profiles; | 192 std::set<std::string> current_profiles; |
| 190 std::vector<std::string> profiles_to_delete; | 193 std::vector<std::string> profiles_to_delete; |
| 191 std::vector<AutoFillProfile*> updated_profiles; | 194 std::vector<AutoFillProfile*> updated_profiles; |
| 192 std::vector<AutoFillProfile*> new_profiles; // We own these pointers. | 195 std::vector<AutoFillProfile*> new_profiles; // We own these pointers. |
| 193 ~DataBundle() { STLDeleteElements(&new_profiles); } | 196 ~DataBundle() { STLDeleteElements(&new_profiles); } |
| 194 }; | 197 }; |
| 195 | 198 |
| 196 } // namespace browser_sync | 199 } // namespace browser_sync |
| 197 | 200 |
| 198 #endif // CHROME_BROWSER_SYNC_GLUE_AUTOFILL_PROFILE_MODEL_ASSOCIATOR_H_ | 201 #endif // CHROME_BROWSER_SYNC_GLUE_AUTOFILL_PROFILE_MODEL_ASSOCIATOR_H_ |
| 202 |
| OLD | NEW |