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

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

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

Powered by Google App Engine
This is Rietveld 408576698