| 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/webdata/autofill_profile_syncable_service.h" | 5 #include "chrome/browser/webdata/autofill_profile_syncable_service.h" |
| 6 | 6 |
| 7 #include "base/location.h" | 7 #include "base/location.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 10 #include "chrome/browser/autofill/autofill_profile.h" | 10 #include "chrome/browser/autofill/autofill_profile.h" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 form_group->SetInfo(field_type, UTF8ToUTF16(specifics_field)); | 31 form_group->SetInfo(field_type, UTF8ToUTF16(specifics_field)); |
| 32 return true; | 32 return true; |
| 33 } | 33 } |
| 34 | 34 |
| 35 } // namespace | 35 } // namespace |
| 36 | 36 |
| 37 const char kAutofillProfileTag[] = "google_chrome_autofill_profiles"; | 37 const char kAutofillProfileTag[] = "google_chrome_autofill_profiles"; |
| 38 | 38 |
| 39 AutofillProfileSyncableService::AutofillProfileSyncableService( | 39 AutofillProfileSyncableService::AutofillProfileSyncableService( |
| 40 WebDataService* web_data_service) | 40 WebDataService* web_data_service) |
| 41 : web_data_service_(web_data_service) { | 41 : web_data_service_(web_data_service), |
| 42 sync_processor_(NULL) { |
| 42 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 43 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 43 DCHECK(web_data_service_); | 44 DCHECK(web_data_service_); |
| 44 notification_registrar_.Add(this, | 45 notification_registrar_.Add(this, |
| 45 chrome::NOTIFICATION_AUTOFILL_PROFILE_CHANGED, | 46 chrome::NOTIFICATION_AUTOFILL_PROFILE_CHANGED, |
| 46 Source<WebDataService>(web_data_service_)); | 47 Source<WebDataService>(web_data_service_)); |
| 47 } | 48 } |
| 48 | 49 |
| 49 AutofillProfileSyncableService::~AutofillProfileSyncableService() { | 50 AutofillProfileSyncableService::~AutofillProfileSyncableService() { |
| 50 DCHECK(CalledOnValidThread()); | 51 DCHECK(CalledOnValidThread()); |
| 51 } | 52 } |
| 52 | 53 |
| 53 AutofillProfileSyncableService::AutofillProfileSyncableService() | 54 AutofillProfileSyncableService::AutofillProfileSyncableService() |
| 54 : web_data_service_(NULL) { | 55 : web_data_service_(NULL), |
| 56 sync_processor_(NULL) { |
| 55 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 56 } | 58 } |
| 57 | 59 |
| 58 SyncError AutofillProfileSyncableService::MergeDataAndStartSyncing( | 60 SyncError AutofillProfileSyncableService::MergeDataAndStartSyncing( |
| 59 syncable::ModelType type, | 61 syncable::ModelType type, |
| 60 const SyncDataList& initial_sync_data, | 62 const SyncDataList& initial_sync_data, |
| 61 SyncChangeProcessor* sync_processor) { | 63 SyncChangeProcessor* sync_processor) { |
| 62 DCHECK(CalledOnValidThread()); | 64 DCHECK(CalledOnValidThread()); |
| 63 DCHECK(!sync_processor_.get()); | 65 DCHECK(sync_processor_ == NULL); |
| 64 VLOG(1) << "Associating Autofill: MergeDataAndStartSyncing"; | 66 VLOG(1) << "Associating Autofill: MergeDataAndStartSyncing"; |
| 65 | 67 |
| 66 if (!LoadAutofillData(&profiles_.get())) { | 68 if (!LoadAutofillData(&profiles_.get())) { |
| 67 return SyncError( | 69 return SyncError( |
| 68 FROM_HERE, "Could not get the autofill data from WebDatabase.", | 70 FROM_HERE, "Could not get the autofill data from WebDatabase.", |
| 69 model_type()); | 71 model_type()); |
| 70 } | 72 } |
| 71 | 73 |
| 72 if (VLOG_IS_ON(2)) { | 74 if (VLOG_IS_ON(2)) { |
| 73 VLOG(2) << "[AUTOFILL MIGRATION]" | 75 VLOG(2) << "[AUTOFILL MIGRATION]" |
| 74 << "Printing profiles from web db"; | 76 << "Printing profiles from web db"; |
| 75 | 77 |
| 76 for (ScopedVector<AutofillProfile>::const_iterator ix = | 78 for (ScopedVector<AutofillProfile>::const_iterator ix = |
| 77 profiles_.begin(); ix != profiles_.end(); ++ix) { | 79 profiles_.begin(); ix != profiles_.end(); ++ix) { |
| 78 AutofillProfile* p = *ix; | 80 AutofillProfile* p = *ix; |
| 79 VLOG(2) << "[AUTOFILL MIGRATION] " | 81 VLOG(2) << "[AUTOFILL MIGRATION] " |
| 80 << p->GetInfo(NAME_FIRST) | 82 << p->GetInfo(NAME_FIRST) |
| 81 << p->GetInfo(NAME_LAST) | 83 << p->GetInfo(NAME_LAST) |
| 82 << p->guid(); | 84 << p->guid(); |
| 83 } | 85 } |
| 84 } | 86 } |
| 85 | 87 |
| 86 sync_processor_.reset(sync_processor); | 88 sync_processor_ = sync_processor; |
| 87 | 89 |
| 88 GUIDToProfileMap remaining_profiles; | 90 GUIDToProfileMap remaining_profiles; |
| 89 CreateGUIDToProfileMap(profiles_.get(), &remaining_profiles); | 91 CreateGUIDToProfileMap(profiles_.get(), &remaining_profiles); |
| 90 | 92 |
| 91 DataBundle bundle; | 93 DataBundle bundle; |
| 92 // Go through and check for all the profiles that sync already knows about. | 94 // Go through and check for all the profiles that sync already knows about. |
| 93 for (SyncDataList::const_iterator sync_iter = initial_sync_data.begin(); | 95 for (SyncDataList::const_iterator sync_iter = initial_sync_data.begin(); |
| 94 sync_iter != initial_sync_data.end(); | 96 sync_iter != initial_sync_data.end(); |
| 95 ++sync_iter) { | 97 ++sync_iter) { |
| 96 GUIDToProfileMap::iterator it = | 98 GUIDToProfileMap::iterator it = |
| 97 CreateOrUpdateProfile(*sync_iter, &remaining_profiles, &bundle); | 99 CreateOrUpdateProfile(*sync_iter, &remaining_profiles, &bundle); |
| 98 profiles_map_[it->first] = it->second; | 100 profiles_map_[it->first] = it->second; |
| 99 remaining_profiles.erase(it); | 101 remaining_profiles.erase(it); |
| 100 } | 102 } |
| 101 | 103 |
| 102 if (!SaveChangesToWebData(bundle)) | 104 if (!SaveChangesToWebData(bundle)) |
| 103 return SyncError(FROM_HERE, "Failed to update webdata.", model_type()); | 105 return SyncError(FROM_HERE, "Failed to update webdata.", model_type()); |
| 104 | 106 |
| 105 SyncChangeList new_changes; | 107 SyncChangeList new_changes; |
| 106 for (GUIDToProfileMap::iterator i = remaining_profiles.begin(); | 108 for (GUIDToProfileMap::iterator i = remaining_profiles.begin(); |
| 107 i != remaining_profiles.end(); ++i) { | 109 i != remaining_profiles.end(); ++i) { |
| 108 new_changes.push_back( | 110 new_changes.push_back( |
| 109 SyncChange(SyncChange::ACTION_ADD, CreateData(*(i->second)))); | 111 SyncChange(SyncChange::ACTION_ADD, CreateData(*(i->second)))); |
| 110 profiles_map_[i->first] = i->second; | 112 profiles_map_[i->first] = i->second; |
| 111 } | 113 } |
| 112 | 114 |
| 113 SyncError error; | 115 SyncError error = sync_processor_->ProcessSyncChanges(FROM_HERE, new_changes); |
| 114 if (!new_changes.empty()) | |
| 115 error = sync_processor_->ProcessSyncChanges(FROM_HERE, new_changes); | |
| 116 | 116 |
| 117 WebDataService::NotifyOfMultipleAutofillChanges(web_data_service_); | 117 WebDataService::NotifyOfMultipleAutofillChanges(web_data_service_); |
| 118 | 118 |
| 119 return error; | 119 return error; |
| 120 } | 120 } |
| 121 | 121 |
| 122 void AutofillProfileSyncableService::StopSyncing(syncable::ModelType type) { | 122 void AutofillProfileSyncableService::StopSyncing(syncable::ModelType type) { |
| 123 DCHECK(CalledOnValidThread()); | 123 DCHECK(CalledOnValidThread()); |
| 124 DCHECK(sync_processor_ != NULL); |
| 124 DCHECK_EQ(type, syncable::AUTOFILL_PROFILE); | 125 DCHECK_EQ(type, syncable::AUTOFILL_PROFILE); |
| 125 | 126 |
| 126 sync_processor_.reset(); | 127 sync_processor_ = NULL; |
| 127 profiles_.reset(); | 128 profiles_.reset(); |
| 128 profiles_map_.clear(); | 129 profiles_map_.clear(); |
| 129 } | 130 } |
| 130 | 131 |
| 131 SyncDataList AutofillProfileSyncableService::GetAllSyncData( | 132 SyncDataList AutofillProfileSyncableService::GetAllSyncData( |
| 132 syncable::ModelType type) const { | 133 syncable::ModelType type) const { |
| 133 DCHECK(CalledOnValidThread()); | 134 DCHECK(CalledOnValidThread()); |
| 134 DCHECK(sync_processor_.get()); | 135 DCHECK(sync_processor_ != NULL); |
| 135 DCHECK_EQ(type, syncable::AUTOFILL_PROFILE); | 136 DCHECK_EQ(type, syncable::AUTOFILL_PROFILE); |
| 136 | 137 |
| 137 SyncDataList current_data; | 138 SyncDataList current_data; |
| 138 | 139 |
| 139 for (GUIDToProfileMap::const_iterator i = profiles_map_.begin(); | 140 for (GUIDToProfileMap::const_iterator i = profiles_map_.begin(); |
| 140 i != profiles_map_.end(); ++i) { | 141 i != profiles_map_.end(); ++i) { |
| 141 current_data.push_back(CreateData(*(i->second))); | 142 current_data.push_back(CreateData(*(i->second))); |
| 142 } | 143 } |
| 143 return current_data; | 144 return current_data; |
| 144 } | 145 } |
| 145 | 146 |
| 146 SyncError AutofillProfileSyncableService::ProcessSyncChanges( | 147 SyncError AutofillProfileSyncableService::ProcessSyncChanges( |
| 147 const tracked_objects::Location& from_here, | 148 const tracked_objects::Location& from_here, |
| 148 const SyncChangeList& change_list) { | 149 const SyncChangeList& change_list) { |
| 149 DCHECK(CalledOnValidThread()); | 150 DCHECK(CalledOnValidThread()); |
| 150 if (!sync_processor_.get()) { | 151 DCHECK(sync_processor_ != NULL); |
| 152 if (sync_processor_ == NULL) { |
| 151 SyncError error(FROM_HERE, "Models not yet associated.", | 153 SyncError error(FROM_HERE, "Models not yet associated.", |
| 152 syncable::AUTOFILL_PROFILE); | 154 syncable::AUTOFILL_PROFILE); |
| 153 return error; | 155 return error; |
| 154 } | 156 } |
| 155 | 157 |
| 156 DataBundle bundle; | 158 DataBundle bundle; |
| 157 | 159 |
| 158 for (SyncChangeList::const_iterator i = change_list.begin(); | 160 for (SyncChangeList::const_iterator i = change_list.begin(); |
| 159 i != change_list.end(); ++i) { | 161 i != change_list.end(); ++i) { |
| 160 DCHECK(i->IsValid()); | 162 DCHECK(i->IsValid()); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 187 | 189 |
| 188 void AutofillProfileSyncableService::Observe(int type, | 190 void AutofillProfileSyncableService::Observe(int type, |
| 189 const NotificationSource& source, | 191 const NotificationSource& source, |
| 190 const NotificationDetails& details) { | 192 const NotificationDetails& details) { |
| 191 DCHECK_EQ(type, chrome::NOTIFICATION_AUTOFILL_PROFILE_CHANGED); | 193 DCHECK_EQ(type, chrome::NOTIFICATION_AUTOFILL_PROFILE_CHANGED); |
| 192 DCHECK_EQ(web_data_service_, Source<WebDataService>(source).ptr()); | 194 DCHECK_EQ(web_data_service_, Source<WebDataService>(source).ptr()); |
| 193 // Check if sync is on. If we receive notification prior to the sync being set | 195 // Check if sync is on. If we receive notification prior to the sync being set |
| 194 // up we are going to process all when MergeData..() is called. If we receive | 196 // up we are going to process all when MergeData..() is called. If we receive |
| 195 // notification after the sync exited, it will be sinced next time Chrome | 197 // notification after the sync exited, it will be sinced next time Chrome |
| 196 // starts. | 198 // starts. |
| 197 if (!sync_processor_.get()) | 199 if (!sync_processor_) |
| 198 return; | 200 return; |
| 199 | 201 |
| 200 AutofillProfileChange* change = Details<AutofillProfileChange>(details).ptr(); | 202 AutofillProfileChange* change = Details<AutofillProfileChange>(details).ptr(); |
| 201 ActOnChange(*change); | 203 ActOnChange(*change); |
| 202 } | 204 } |
| 203 | 205 |
| 204 bool AutofillProfileSyncableService::LoadAutofillData( | 206 bool AutofillProfileSyncableService::LoadAutofillData( |
| 205 std::vector<AutofillProfile*>* profiles) { | 207 std::vector<AutofillProfile*>* profiles) { |
| 206 return GetAutofillTable()->GetAutofillProfiles(profiles); | 208 return GetAutofillTable()->GetAutofillProfiles(profiles); |
| 207 } | 209 } |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 342 bundle->profiles_to_add.push_back(new_profile); | 344 bundle->profiles_to_add.push_back(new_profile); |
| 343 } | 345 } |
| 344 return it; | 346 return it; |
| 345 } | 347 } |
| 346 | 348 |
| 347 void AutofillProfileSyncableService::ActOnChange( | 349 void AutofillProfileSyncableService::ActOnChange( |
| 348 const AutofillProfileChange& change) { | 350 const AutofillProfileChange& change) { |
| 349 DCHECK((change.type() == AutofillProfileChange::REMOVE && | 351 DCHECK((change.type() == AutofillProfileChange::REMOVE && |
| 350 !change.profile()) || | 352 !change.profile()) || |
| 351 (change.type() != AutofillProfileChange::REMOVE && change.profile())); | 353 (change.type() != AutofillProfileChange::REMOVE && change.profile())); |
| 352 DCHECK(sync_processor_.get()); | 354 DCHECK(sync_processor_); |
| 353 SyncChangeList new_changes; | 355 SyncChangeList new_changes; |
| 354 DataBundle bundle; | 356 DataBundle bundle; |
| 355 switch (change.type()) { | 357 switch (change.type()) { |
| 356 case AutofillProfileChange::ADD: | 358 case AutofillProfileChange::ADD: |
| 357 new_changes.push_back( | 359 new_changes.push_back( |
| 358 SyncChange(SyncChange::ACTION_ADD, CreateData(*(change.profile())))); | 360 SyncChange(SyncChange::ACTION_ADD, CreateData(*(change.profile())))); |
| 359 DCHECK(profiles_map_.find(change.profile()->guid()) == | 361 DCHECK(profiles_map_.find(change.profile()->guid()) == |
| 360 profiles_map_.end()); | 362 profiles_map_.end()); |
| 361 profiles_.push_back(new AutofillProfile(*(change.profile()))); | 363 profiles_.push_back(new AutofillProfile(*(change.profile()))); |
| 362 profiles_map_[change.profile()->guid()] = profiles_.get().back(); | 364 profiles_map_[change.profile()->guid()] = profiles_.get().back(); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 398 } | 400 } |
| 399 | 401 |
| 400 AutofillTable* AutofillProfileSyncableService::GetAutofillTable() const { | 402 AutofillTable* AutofillProfileSyncableService::GetAutofillTable() const { |
| 401 return web_data_service_->GetDatabase()->GetAutofillTable(); | 403 return web_data_service_->GetDatabase()->GetAutofillTable(); |
| 402 } | 404 } |
| 403 | 405 |
| 404 AutofillProfileSyncableService::DataBundle::DataBundle() {} | 406 AutofillProfileSyncableService::DataBundle::DataBundle() {} |
| 405 | 407 |
| 406 AutofillProfileSyncableService::DataBundle::~DataBundle() { | 408 AutofillProfileSyncableService::DataBundle::~DataBundle() { |
| 407 } | 409 } |
| OLD | NEW |