| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #include "chrome/browser/sync/glue/autofill_model_associator.h" | 5 #include "chrome/browser/sync/glue/autofill_model_associator.h" |
| 6 | 6 |
| 7 #include <functional> | 7 #include <functional> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
| 11 #include "base/task.h" | 11 #include "base/task.h" |
| 12 #include "base/time.h" | 12 #include "base/time.h" |
| 13 #include "base/tracked.h" | 13 #include "base/tracked.h" |
| 14 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
| 15 #include "chrome/browser/autofill/autofill_profile.h" | 15 #include "chrome/browser/autofill/autofill_profile.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/browser/sync/api/sync_error.h" |
| 17 #include "chrome/browser/sync/engine/syncapi.h" | 18 #include "chrome/browser/sync/engine/syncapi.h" |
| 18 #include "chrome/browser/sync/glue/autofill_change_processor.h" | 19 #include "chrome/browser/sync/glue/autofill_change_processor.h" |
| 19 #include "chrome/browser/sync/glue/autofill_profile_model_associator.h" | 20 #include "chrome/browser/sync/glue/autofill_profile_model_associator.h" |
| 20 #include "chrome/browser/sync/glue/do_optimistic_refresh_task.h" | 21 #include "chrome/browser/sync/glue/do_optimistic_refresh_task.h" |
| 21 #include "chrome/browser/sync/profile_sync_service.h" | 22 #include "chrome/browser/sync/profile_sync_service.h" |
| 22 #include "chrome/browser/sync/protocol/autofill_specifics.pb.h" | 23 #include "chrome/browser/sync/protocol/autofill_specifics.pb.h" |
| 23 #include "chrome/browser/webdata/web_database.h" | 24 #include "chrome/browser/webdata/web_database.h" |
| 24 #include "chrome/common/guid.h" | 25 #include "chrome/common/guid.h" |
| 25 #include "content/browser/browser_thread.h" | 26 #include "content/browser/browser_thread.h" |
| 26 #include "net/base/escape.h" | 27 #include "net/base/escape.h" |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 return false; | 130 return false; |
| 130 | 131 |
| 131 if (IsAbortPending()) | 132 if (IsAbortPending()) |
| 132 return false; | 133 return false; |
| 133 if (!web_database_->GetAutofillTable()->GetAutofillProfiles(profiles)) | 134 if (!web_database_->GetAutofillTable()->GetAutofillProfiles(profiles)) |
| 134 return false; | 135 return false; |
| 135 | 136 |
| 136 return true; | 137 return true; |
| 137 } | 138 } |
| 138 | 139 |
| 139 bool AutofillModelAssociator::AssociateModels() { | 140 bool AutofillModelAssociator::AssociateModels(SyncError* error) { |
| 140 VLOG(1) << "Associating Autofill Models"; | 141 VLOG(1) << "Associating Autofill Models"; |
| 141 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 142 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 142 { | 143 { |
| 143 base::AutoLock lock(abort_association_pending_lock_); | 144 base::AutoLock lock(abort_association_pending_lock_); |
| 144 abort_association_pending_ = false; | 145 abort_association_pending_ = false; |
| 145 } | 146 } |
| 146 | 147 |
| 147 // TODO(zork): Attempt to load the model association from storage. | 148 // TODO(zork): Attempt to load the model association from storage. |
| 148 std::vector<AutofillEntry> entries; | 149 std::vector<AutofillEntry> entries; |
| 149 ScopedVector<AutofillProfile> profiles; | 150 ScopedVector<AutofillProfile> profiles; |
| 150 | 151 |
| 151 if (!LoadAutofillData(&entries, &profiles.get())) { | 152 if (!LoadAutofillData(&entries, &profiles.get())) { |
| 152 LOG(ERROR) << "Could not get the autofill data from WebDatabase."; | 153 error->Reset(FROM_HERE, |
| 154 "Could not get the autofill data from WebDatabase.", |
| 155 model_type()); |
| 153 return false; | 156 return false; |
| 154 } | 157 } |
| 155 | 158 |
| 156 DataBundle bundle; | 159 DataBundle bundle; |
| 157 { | 160 { |
| 158 sync_api::WriteTransaction trans(FROM_HERE, sync_service_->GetUserShare()); | 161 sync_api::WriteTransaction trans(FROM_HERE, sync_service_->GetUserShare()); |
| 159 | 162 |
| 160 sync_api::ReadNode autofill_root(&trans); | 163 sync_api::ReadNode autofill_root(&trans); |
| 161 if (!autofill_root.InitByTagLookup(kAutofillTag)) { | 164 if (!autofill_root.InitByTagLookup(kAutofillTag)) { |
| 162 LOG(ERROR) << "Server did not create the top-level autofill node. We " | 165 error->Reset(FROM_HERE, |
| 163 << "might be running against an out-of-date server."; | 166 "Server did not create the top-level autofill node. We " |
| 167 "might be running against an out-of-date server.", |
| 168 model_type()); |
| 164 return false; | 169 return false; |
| 165 } | 170 } |
| 166 | 171 |
| 167 if (!TraverseAndAssociateChromeAutofillEntries(&trans, autofill_root, | 172 if (!TraverseAndAssociateChromeAutofillEntries(&trans, autofill_root, |
| 168 entries, &bundle.current_entries, &bundle.new_entries)) { | 173 entries, &bundle.current_entries, &bundle.new_entries)) { |
| 174 error->Reset(FROM_HERE, "Failed to associate entries.", model_type()); |
| 169 return false; | 175 return false; |
| 170 } | 176 } |
| 171 | 177 |
| 172 if (!TraverseAndAssociateAllSyncNodes( | 178 if (!TraverseAndAssociateAllSyncNodes( |
| 173 &trans, | 179 &trans, |
| 174 autofill_root, | 180 autofill_root, |
| 175 &bundle, | 181 &bundle, |
| 176 profiles.get())) { | 182 profiles.get())) { |
| 183 error->Reset(FROM_HERE, "Failed to associate sync nodes.", model_type()); |
| 177 return false; | 184 return false; |
| 178 } | 185 } |
| 179 } | 186 } |
| 180 | 187 |
| 181 // Since we're on the DB thread, we don't have to worry about updating | 188 // Since we're on the DB thread, we don't have to worry about updating |
| 182 // the autofill database after closing the write transaction, since | 189 // the autofill database after closing the write transaction, since |
| 183 // this is the only thread that writes to the database. We also don't have | 190 // this is the only thread that writes to the database. We also don't have |
| 184 // to worry about the sync model getting out of sync, because changes are | 191 // to worry about the sync model getting out of sync, because changes are |
| 185 // propagated to the ChangeProcessor on this thread. | 192 // propagated to the ChangeProcessor on this thread. |
| 186 if (!SaveChangesToWebData(bundle)) { | 193 if (!SaveChangesToWebData(bundle)) { |
| 187 LOG(ERROR) << "Failed to update autofill entries."; | 194 error->Reset(FROM_HERE, "Failed to update webdata.", model_type()); |
| 188 return false; | 195 return false; |
| 189 } | 196 } |
| 190 | 197 |
| 191 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 198 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 192 new DoOptimisticRefreshForAutofill(personal_data_)); | 199 new DoOptimisticRefreshForAutofill(personal_data_)); |
| 193 return true; | 200 return true; |
| 194 } | 201 } |
| 195 | 202 |
| 196 bool AutofillModelAssociator::SaveChangesToWebData(const DataBundle& bundle) { | 203 bool AutofillModelAssociator::SaveChangesToWebData(const DataBundle& bundle) { |
| 197 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 204 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 338 DCHECK(false) << "Guid generated is invalid " << guid; | 345 DCHECK(false) << "Guid generated is invalid " << guid; |
| 339 return; | 346 return; |
| 340 } | 347 } |
| 341 Associate(&guid, node.GetId()); | 348 Associate(&guid, node.GetId()); |
| 342 AutofillProfile* p = new AutofillProfile(guid); | 349 AutofillProfile* p = new AutofillProfile(guid); |
| 343 FillProfileWithServerData(p, profile); | 350 FillProfileWithServerData(p, profile); |
| 344 bundle->new_profiles.push_back(p); | 351 bundle->new_profiles.push_back(p); |
| 345 } | 352 } |
| 346 } | 353 } |
| 347 | 354 |
| 348 bool AutofillModelAssociator::DisassociateModels() { | 355 bool AutofillModelAssociator::DisassociateModels(SyncError* error) { |
| 349 id_map_.clear(); | 356 id_map_.clear(); |
| 350 id_map_inverse_.clear(); | 357 id_map_inverse_.clear(); |
| 351 return true; | 358 return true; |
| 352 } | 359 } |
| 353 | 360 |
| 354 bool AutofillModelAssociator::SyncModelHasUserCreatedNodes(bool* has_nodes) { | 361 bool AutofillModelAssociator::SyncModelHasUserCreatedNodes(bool* has_nodes) { |
| 355 DCHECK(has_nodes); | 362 DCHECK(has_nodes); |
| 356 *has_nodes = false; | 363 *has_nodes = false; |
| 357 int64 autofill_sync_id; | 364 int64 autofill_sync_id; |
| 358 if (!GetSyncIdForTaggedNode(kAutofillTag, &autofill_sync_id)) { | 365 if (!GetSyncIdForTaggedNode(kAutofillTag, &autofill_sync_id)) { |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 506 bool AutofillModelAssociator::CryptoReadyIfNecessary() { | 513 bool AutofillModelAssociator::CryptoReadyIfNecessary() { |
| 507 // We only access the cryptographer while holding a transaction. | 514 // We only access the cryptographer while holding a transaction. |
| 508 sync_api::ReadTransaction trans(FROM_HERE, sync_service_->GetUserShare()); | 515 sync_api::ReadTransaction trans(FROM_HERE, sync_service_->GetUserShare()); |
| 509 syncable::ModelTypeSet encrypted_types; | 516 syncable::ModelTypeSet encrypted_types; |
| 510 encrypted_types = sync_api::GetEncryptedTypes(&trans); | 517 encrypted_types = sync_api::GetEncryptedTypes(&trans); |
| 511 return encrypted_types.count(syncable::AUTOFILL) == 0 || | 518 return encrypted_types.count(syncable::AUTOFILL) == 0 || |
| 512 sync_service_->IsCryptographerReady(&trans); | 519 sync_service_->IsCryptographerReady(&trans); |
| 513 } | 520 } |
| 514 | 521 |
| 515 } // namespace browser_sync | 522 } // namespace browser_sync |
| OLD | NEW |