Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(115)

Side by Side Diff: chrome/browser/webdata/autofill_profile_syncable_service.cc

Issue 10662035: [Sync] Put everything in sync/api into csync namespace (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix comments Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/guid.h" 7 #include "base/guid.h"
8 #include "base/location.h" 8 #include "base/location.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/utf_string_conversions.h" 10 #include "base/utf_string_conversions.h"
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
50 50
51 AutofillProfileSyncableService::~AutofillProfileSyncableService() { 51 AutofillProfileSyncableService::~AutofillProfileSyncableService() {
52 DCHECK(CalledOnValidThread()); 52 DCHECK(CalledOnValidThread());
53 } 53 }
54 54
55 AutofillProfileSyncableService::AutofillProfileSyncableService() 55 AutofillProfileSyncableService::AutofillProfileSyncableService()
56 : web_data_service_(NULL) { 56 : web_data_service_(NULL) {
57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); 57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
58 } 58 }
59 59
60 SyncError AutofillProfileSyncableService::MergeDataAndStartSyncing( 60 csync::SyncError AutofillProfileSyncableService::MergeDataAndStartSyncing(
61 syncable::ModelType type, 61 syncable::ModelType type,
62 const SyncDataList& initial_sync_data, 62 const csync::SyncDataList& initial_sync_data,
63 scoped_ptr<SyncChangeProcessor> sync_processor, 63 scoped_ptr<csync::SyncChangeProcessor> sync_processor,
64 scoped_ptr<SyncErrorFactory> sync_error_factory) { 64 scoped_ptr<csync::SyncErrorFactory> sync_error_factory) {
65 DCHECK(CalledOnValidThread()); 65 DCHECK(CalledOnValidThread());
66 DCHECK(!sync_processor_.get()); 66 DCHECK(!sync_processor_.get());
67 DCHECK(sync_processor.get()); 67 DCHECK(sync_processor.get());
68 DCHECK(sync_error_factory.get()); 68 DCHECK(sync_error_factory.get());
69 DVLOG(1) << "Associating Autofill: MergeDataAndStartSyncing"; 69 DVLOG(1) << "Associating Autofill: MergeDataAndStartSyncing";
70 70
71 sync_error_factory_ = sync_error_factory.Pass(); 71 sync_error_factory_ = sync_error_factory.Pass();
72 if (!LoadAutofillData(&profiles_.get())) { 72 if (!LoadAutofillData(&profiles_.get())) {
73 return sync_error_factory_->CreateAndUploadError( 73 return sync_error_factory_->CreateAndUploadError(
74 FROM_HERE, "Could not get the autofill data from WebDatabase."); 74 FROM_HERE, "Could not get the autofill data from WebDatabase.");
(...skipping 13 matching lines...) Expand all
88 } 88 }
89 } 89 }
90 90
91 sync_processor_ = sync_processor.Pass(); 91 sync_processor_ = sync_processor.Pass();
92 92
93 GUIDToProfileMap remaining_profiles; 93 GUIDToProfileMap remaining_profiles;
94 CreateGUIDToProfileMap(profiles_.get(), &remaining_profiles); 94 CreateGUIDToProfileMap(profiles_.get(), &remaining_profiles);
95 95
96 DataBundle bundle; 96 DataBundle bundle;
97 // Go through and check for all the profiles that sync already knows about. 97 // Go through and check for all the profiles that sync already knows about.
98 for (SyncDataList::const_iterator sync_iter = initial_sync_data.begin(); 98 for (csync::SyncDataList::const_iterator sync_iter =
99 initial_sync_data.begin();
99 sync_iter != initial_sync_data.end(); 100 sync_iter != initial_sync_data.end();
100 ++sync_iter) { 101 ++sync_iter) {
101 GUIDToProfileMap::iterator it = 102 GUIDToProfileMap::iterator it =
102 CreateOrUpdateProfile(*sync_iter, &remaining_profiles, &bundle); 103 CreateOrUpdateProfile(*sync_iter, &remaining_profiles, &bundle);
103 // |it| points to created/updated profile. Add it to the |profiles_map_| and 104 // |it| points to created/updated profile. Add it to the |profiles_map_| and
104 // then remove it from |remaining_profiles|. After this loop is completed 105 // then remove it from |remaining_profiles|. After this loop is completed
105 // |remaining_profiles| will have only those profiles that are not in the 106 // |remaining_profiles| will have only those profiles that are not in the
106 // sync. 107 // sync.
107 profiles_map_[it->first] = it->second; 108 profiles_map_[it->first] = it->second;
108 remaining_profiles.erase(it); 109 remaining_profiles.erase(it);
(...skipping 19 matching lines...) Expand all
128 remaining_profiles.erase(profile_to_merge); 129 remaining_profiles.erase(profile_to_merge);
129 } 130 }
130 } 131 }
131 132
132 if (!SaveChangesToWebData(bundle)) { 133 if (!SaveChangesToWebData(bundle)) {
133 return sync_error_factory_->CreateAndUploadError( 134 return sync_error_factory_->CreateAndUploadError(
134 FROM_HERE, 135 FROM_HERE,
135 "Failed to update webdata."); 136 "Failed to update webdata.");
136 } 137 }
137 138
138 SyncChangeList new_changes; 139 csync::SyncChangeList new_changes;
139 for (GUIDToProfileMap::iterator i = remaining_profiles.begin(); 140 for (GUIDToProfileMap::iterator i = remaining_profiles.begin();
140 i != remaining_profiles.end(); ++i) { 141 i != remaining_profiles.end(); ++i) {
141 new_changes.push_back( 142 new_changes.push_back(
142 SyncChange(SyncChange::ACTION_ADD, CreateData(*(i->second)))); 143 csync::SyncChange(
144 csync::SyncChange::ACTION_ADD, CreateData(*(i->second))));
143 profiles_map_[i->first] = i->second; 145 profiles_map_[i->first] = i->second;
144 } 146 }
145 147
146 for (size_t i = 0; i < bundle.profiles_to_sync_back.size(); ++i) { 148 for (size_t i = 0; i < bundle.profiles_to_sync_back.size(); ++i) {
147 new_changes.push_back( 149 new_changes.push_back(
148 SyncChange(SyncChange::ACTION_UPDATE, 150 csync::SyncChange(csync::SyncChange::ACTION_UPDATE,
149 CreateData(*(bundle.profiles_to_sync_back[i])))); 151 CreateData(*(bundle.profiles_to_sync_back[i]))));
150 } 152 }
151 153
152 SyncError error; 154 csync::SyncError error;
153 if (!new_changes.empty()) 155 if (!new_changes.empty())
154 error = sync_processor_->ProcessSyncChanges(FROM_HERE, new_changes); 156 error = sync_processor_->ProcessSyncChanges(FROM_HERE, new_changes);
155 157
156 WebDataService::NotifyOfMultipleAutofillChanges(web_data_service_); 158 WebDataService::NotifyOfMultipleAutofillChanges(web_data_service_);
157 159
158 return error; 160 return error;
159 } 161 }
160 162
161 void AutofillProfileSyncableService::StopSyncing(syncable::ModelType type) { 163 void AutofillProfileSyncableService::StopSyncing(syncable::ModelType type) {
162 DCHECK(CalledOnValidThread()); 164 DCHECK(CalledOnValidThread());
163 DCHECK_EQ(type, syncable::AUTOFILL_PROFILE); 165 DCHECK_EQ(type, syncable::AUTOFILL_PROFILE);
164 166
165 sync_processor_.reset(); 167 sync_processor_.reset();
166 sync_error_factory_.reset(); 168 sync_error_factory_.reset();
167 profiles_.reset(); 169 profiles_.reset();
168 profiles_map_.clear(); 170 profiles_map_.clear();
169 } 171 }
170 172
171 SyncDataList AutofillProfileSyncableService::GetAllSyncData( 173 csync::SyncDataList AutofillProfileSyncableService::GetAllSyncData(
172 syncable::ModelType type) const { 174 syncable::ModelType type) const {
173 DCHECK(CalledOnValidThread()); 175 DCHECK(CalledOnValidThread());
174 DCHECK(sync_processor_.get()); 176 DCHECK(sync_processor_.get());
175 DCHECK_EQ(type, syncable::AUTOFILL_PROFILE); 177 DCHECK_EQ(type, syncable::AUTOFILL_PROFILE);
176 178
177 SyncDataList current_data; 179 csync::SyncDataList current_data;
178 180
179 for (GUIDToProfileMap::const_iterator i = profiles_map_.begin(); 181 for (GUIDToProfileMap::const_iterator i = profiles_map_.begin();
180 i != profiles_map_.end(); ++i) { 182 i != profiles_map_.end(); ++i) {
181 current_data.push_back(CreateData(*(i->second))); 183 current_data.push_back(CreateData(*(i->second)));
182 } 184 }
183 return current_data; 185 return current_data;
184 } 186 }
185 187
186 SyncError AutofillProfileSyncableService::ProcessSyncChanges( 188 csync::SyncError AutofillProfileSyncableService::ProcessSyncChanges(
187 const tracked_objects::Location& from_here, 189 const tracked_objects::Location& from_here,
188 const SyncChangeList& change_list) { 190 const csync::SyncChangeList& change_list) {
189 DCHECK(CalledOnValidThread()); 191 DCHECK(CalledOnValidThread());
190 if (!sync_processor_.get()) { 192 if (!sync_processor_.get()) {
191 SyncError error(FROM_HERE, "Models not yet associated.", 193 csync::SyncError error(FROM_HERE, "Models not yet associated.",
192 syncable::AUTOFILL_PROFILE); 194 syncable::AUTOFILL_PROFILE);
193 return error; 195 return error;
194 } 196 }
195 197
196 DataBundle bundle; 198 DataBundle bundle;
197 199
198 for (SyncChangeList::const_iterator i = change_list.begin(); 200 for (csync::SyncChangeList::const_iterator i = change_list.begin();
199 i != change_list.end(); ++i) { 201 i != change_list.end(); ++i) {
200 DCHECK(i->IsValid()); 202 DCHECK(i->IsValid());
201 switch (i->change_type()) { 203 switch (i->change_type()) {
202 case SyncChange::ACTION_ADD: 204 case csync::SyncChange::ACTION_ADD:
203 case SyncChange::ACTION_UPDATE: 205 case csync::SyncChange::ACTION_UPDATE:
204 CreateOrUpdateProfile(i->sync_data(), &profiles_map_, &bundle); 206 CreateOrUpdateProfile(i->sync_data(), &profiles_map_, &bundle);
205 break; 207 break;
206 case SyncChange::ACTION_DELETE: { 208 case csync::SyncChange::ACTION_DELETE: {
207 std::string guid = i->sync_data().GetSpecifics(). 209 std::string guid = i->sync_data().GetSpecifics().
208 autofill_profile().guid(); 210 autofill_profile().guid();
209 bundle.profiles_to_delete.push_back(guid); 211 bundle.profiles_to_delete.push_back(guid);
210 profiles_map_.erase(guid); 212 profiles_map_.erase(guid);
211 } break; 213 } break;
212 default: 214 default:
213 NOTREACHED() << "Unexpected sync change state."; 215 NOTREACHED() << "Unexpected sync change state.";
214 return sync_error_factory_->CreateAndUploadError( 216 return sync_error_factory_->CreateAndUploadError(
215 FROM_HERE, 217 FROM_HERE,
216 "ProcessSyncChanges failed on ChangeType " + 218 "ProcessSyncChanges failed on ChangeType " +
217 SyncChange::ChangeTypeToString(i->change_type())); 219 csync::SyncChange::ChangeTypeToString(i->change_type()));
218 } 220 }
219 } 221 }
220 222
221 if (!SaveChangesToWebData(bundle)) { 223 if (!SaveChangesToWebData(bundle)) {
222 return sync_error_factory_->CreateAndUploadError( 224 return sync_error_factory_->CreateAndUploadError(
223 FROM_HERE, 225 FROM_HERE,
224 "Failed to update webdata."); 226 "Failed to update webdata.");
225 } 227 }
226 228
227 WebDataService::NotifyOfMultipleAutofillChanges(web_data_service_); 229 WebDataService::NotifyOfMultipleAutofillChanges(web_data_service_);
228 230
229 return SyncError(); 231 return csync::SyncError();
230 } 232 }
231 233
232 void AutofillProfileSyncableService::Observe(int type, 234 void AutofillProfileSyncableService::Observe(int type,
233 const content::NotificationSource& source, 235 const content::NotificationSource& source,
234 const content::NotificationDetails& details) { 236 const content::NotificationDetails& details) {
235 DCHECK_EQ(type, chrome::NOTIFICATION_AUTOFILL_PROFILE_CHANGED); 237 DCHECK_EQ(type, chrome::NOTIFICATION_AUTOFILL_PROFILE_CHANGED);
236 DCHECK_EQ(web_data_service_, content::Source<WebDataService>(source).ptr()); 238 DCHECK_EQ(web_data_service_, content::Source<WebDataService>(source).ptr());
237 // Check if sync is on. If we receive notification prior to the sync being set 239 // Check if sync is on. If we receive notification prior to the sync being set
238 // up we are going to process all when MergeData..() is called. If we receive 240 // up we are going to process all when MergeData..() is called. If we receive
239 // notification after the sync exited, it will be sinced next time Chrome 241 // notification after the sync exited, it will be sinced next time Chrome
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
361 const std::vector<AutofillProfile*>& profiles, 363 const std::vector<AutofillProfile*>& profiles,
362 GUIDToProfileMap* profile_map) { 364 GUIDToProfileMap* profile_map) {
363 DCHECK(profile_map); 365 DCHECK(profile_map);
364 profile_map->clear(); 366 profile_map->clear();
365 for (size_t i = 0; i < profiles.size(); ++i) 367 for (size_t i = 0; i < profiles.size(); ++i)
366 (*profile_map)[profiles[i]->guid()] = profiles[i]; 368 (*profile_map)[profiles[i]->guid()] = profiles[i];
367 } 369 }
368 370
369 AutofillProfileSyncableService::GUIDToProfileMap::iterator 371 AutofillProfileSyncableService::GUIDToProfileMap::iterator
370 AutofillProfileSyncableService::CreateOrUpdateProfile( 372 AutofillProfileSyncableService::CreateOrUpdateProfile(
371 const SyncData& data, GUIDToProfileMap* profile_map, DataBundle* bundle) { 373 const csync::SyncData& data,
374 GUIDToProfileMap* profile_map,
375 DataBundle* bundle) {
372 DCHECK(profile_map); 376 DCHECK(profile_map);
373 DCHECK(bundle); 377 DCHECK(bundle);
374 378
375 DCHECK_EQ(syncable::AUTOFILL_PROFILE, data.GetDataType()); 379 DCHECK_EQ(syncable::AUTOFILL_PROFILE, data.GetDataType());
376 380
377 const sync_pb::EntitySpecifics& specifics = data.GetSpecifics(); 381 const sync_pb::EntitySpecifics& specifics = data.GetSpecifics();
378 const sync_pb::AutofillProfileSpecifics& autofill_specifics( 382 const sync_pb::AutofillProfileSpecifics& autofill_specifics(
379 specifics.autofill_profile()); 383 specifics.autofill_profile());
380 384
381 GUIDToProfileMap::iterator it = profile_map->find( 385 GUIDToProfileMap::iterator it = profile_map->find(
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
418 } 422 }
419 return it; 423 return it;
420 } 424 }
421 425
422 void AutofillProfileSyncableService::ActOnChange( 426 void AutofillProfileSyncableService::ActOnChange(
423 const AutofillProfileChange& change) { 427 const AutofillProfileChange& change) {
424 DCHECK((change.type() == AutofillProfileChange::REMOVE && 428 DCHECK((change.type() == AutofillProfileChange::REMOVE &&
425 !change.profile()) || 429 !change.profile()) ||
426 (change.type() != AutofillProfileChange::REMOVE && change.profile())); 430 (change.type() != AutofillProfileChange::REMOVE && change.profile()));
427 DCHECK(sync_processor_.get()); 431 DCHECK(sync_processor_.get());
428 SyncChangeList new_changes; 432 csync::SyncChangeList new_changes;
429 DataBundle bundle; 433 DataBundle bundle;
430 switch (change.type()) { 434 switch (change.type()) {
431 case AutofillProfileChange::ADD: 435 case AutofillProfileChange::ADD:
432 new_changes.push_back( 436 new_changes.push_back(
433 SyncChange(SyncChange::ACTION_ADD, CreateData(*(change.profile())))); 437 csync::SyncChange(
438 csync::SyncChange::ACTION_ADD, CreateData(*(change.profile()))));
434 DCHECK(profiles_map_.find(change.profile()->guid()) == 439 DCHECK(profiles_map_.find(change.profile()->guid()) ==
435 profiles_map_.end()); 440 profiles_map_.end());
436 profiles_.push_back(new AutofillProfile(*(change.profile()))); 441 profiles_.push_back(new AutofillProfile(*(change.profile())));
437 profiles_map_[change.profile()->guid()] = profiles_.get().back(); 442 profiles_map_[change.profile()->guid()] = profiles_.get().back();
438 break; 443 break;
439 case AutofillProfileChange::UPDATE: { 444 case AutofillProfileChange::UPDATE: {
440 GUIDToProfileMap::iterator it = profiles_map_.find( 445 GUIDToProfileMap::iterator it = profiles_map_.find(
441 change.profile()->guid()); 446 change.profile()->guid());
442 DCHECK(it != profiles_map_.end()); 447 DCHECK(it != profiles_map_.end());
443 *(it->second) = *(change.profile()); 448 *(it->second) = *(change.profile());
444 new_changes.push_back( 449 new_changes.push_back(
445 SyncChange(SyncChange::ACTION_UPDATE, 450 csync::SyncChange(csync::SyncChange::ACTION_UPDATE,
446 CreateData(*(change.profile())))); 451 CreateData(*(change.profile()))));
447 break; 452 break;
448 } 453 }
449 case AutofillProfileChange::REMOVE: { 454 case AutofillProfileChange::REMOVE: {
450 AutofillProfile empty_profile(change.key()); 455 AutofillProfile empty_profile(change.key());
451 new_changes.push_back(SyncChange(SyncChange::ACTION_DELETE, 456 new_changes.push_back(csync::SyncChange(csync::SyncChange::ACTION_DELETE,
452 CreateData(empty_profile))); 457 CreateData(empty_profile)));
453 profiles_map_.erase(change.key()); 458 profiles_map_.erase(change.key());
454 break; 459 break;
455 } 460 }
456 default: 461 default:
457 NOTREACHED(); 462 NOTREACHED();
458 } 463 }
459 SyncError error = sync_processor_->ProcessSyncChanges(FROM_HERE, new_changes); 464 csync::SyncError error =
465 sync_processor_->ProcessSyncChanges(FROM_HERE, new_changes);
460 if (error.IsSet()) { 466 if (error.IsSet()) {
461 // TODO(isherman): Investigating http://crbug.com/121592 467 // TODO(isherman): Investigating http://crbug.com/121592
462 VLOG(1) << "[AUTOFILL SYNC] " 468 VLOG(1) << "[AUTOFILL SYNC] "
463 << "Failed processing change:\n" 469 << "Failed processing change:\n"
464 << " Error: " << error.message() << "\n" 470 << " Error: " << error.message() << "\n"
465 << " Guid: " << change.key(); 471 << " Guid: " << change.key();
466 } 472 }
467 } 473 }
468 474
469 SyncData AutofillProfileSyncableService::CreateData( 475 csync::SyncData AutofillProfileSyncableService::CreateData(
470 const AutofillProfile& profile) { 476 const AutofillProfile& profile) {
471 sync_pb::EntitySpecifics specifics; 477 sync_pb::EntitySpecifics specifics;
472 WriteAutofillProfile(profile, &specifics); 478 WriteAutofillProfile(profile, &specifics);
473 return SyncData::CreateLocalData(profile.guid(), profile.guid(), specifics); 479 return
480 csync::SyncData::CreateLocalData(
481 profile.guid(), profile.guid(), specifics);
474 } 482 }
475 483
476 bool AutofillProfileSyncableService::UpdateField( 484 bool AutofillProfileSyncableService::UpdateField(
477 AutofillFieldType field_type, 485 AutofillFieldType field_type,
478 const std::string& new_value, 486 const std::string& new_value,
479 AutofillProfile* autofill_profile) { 487 AutofillProfile* autofill_profile) {
480 if (UTF16ToUTF8(autofill_profile->GetInfo(field_type)) == new_value) 488 if (UTF16ToUTF8(autofill_profile->GetInfo(field_type)) == new_value)
481 return false; 489 return false;
482 autofill_profile->SetInfo(field_type, UTF8ToUTF16(new_value)); 490 autofill_profile->SetInfo(field_type, UTF8ToUTF16(new_value));
483 return true; 491 return true;
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
516 } 524 }
517 525
518 AutofillTable* AutofillProfileSyncableService::GetAutofillTable() const { 526 AutofillTable* AutofillProfileSyncableService::GetAutofillTable() const {
519 return web_data_service_->GetDatabase()->GetAutofillTable(); 527 return web_data_service_->GetDatabase()->GetAutofillTable();
520 } 528 }
521 529
522 AutofillProfileSyncableService::DataBundle::DataBundle() {} 530 AutofillProfileSyncableService::DataBundle::DataBundle() {}
523 531
524 AutofillProfileSyncableService::DataBundle::~DataBundle() { 532 AutofillProfileSyncableService::DataBundle::~DataBundle() {
525 } 533 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698