1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/sync/glue/autofill_profile_change_processor.h"
#include <string>
#include <vector>
#include "base/string_util.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/autofill/autofill_profile.h"
#include "chrome/browser/autofill/personal_data_manager.h"
#include "chrome/browser/sync/engine/syncapi.h"
#include "chrome/browser/sync/glue/autofill_profile_model_associator.h"
#include "chrome/browser/sync/glue/change_processor.h"
#include "chrome/browser/sync/glue/do_optimistic_refresh_task.h"
#include "chrome/browser/sync/unrecoverable_error_handler.h"
#include "chrome/browser/webdata/autofill_change.h"
#include "chrome/browser/webdata/web_database.h"
#include "chrome/common/guid.h"
#include "chrome/common/notification_registrar.h"
#include "chrome/common/notification_service.h"
#include "chrome/common/notification_type.h"
namespace browser_sync {
AutofillProfileChangeProcessor::AutofillProfileChangeProcessor(
AutofillProfileModelAssociator *model_associator,
WebDatabase* web_database,
PersonalDataManager* personal_data_manager,
UnrecoverableErrorHandler* error_handler)
: ChangeProcessor(error_handler),
model_associator_(model_associator),
observing_(false),
web_database_(web_database),
personal_data_(personal_data_manager) {
DCHECK(model_associator);
DCHECK(web_database);
DCHECK(error_handler);
DCHECK(personal_data_manager);
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
StartObserving();
}
AutofillProfileChangeProcessor::~AutofillProfileChangeProcessor() {}
AutofillProfileChangeProcessor::ScopedStopObserving::ScopedStopObserving(
AutofillProfileChangeProcessor* processor) {
processor_ = processor;
processor_->StopObserving();
}
AutofillProfileChangeProcessor::ScopedStopObserving::~ScopedStopObserving() {
processor_->StartObserving();
}
void AutofillProfileChangeProcessor::ApplyChangesFromSyncModel(
const sync_api::BaseTransaction *write_trans,
const sync_api::SyncManager::ChangeRecord* changes,
int change_count) {
ScopedStopObserving observer(this);
sync_api::ReadNode autofill_profile_root(write_trans);
if (!autofill_profile_root.InitByTagLookup(kAutofillProfileTag)) {
error_handler()->OnUnrecoverableError(FROM_HERE,
"Autofill Profile root node lookup failed");
return;
}
for (int i = 0; i < change_count; ++i) {
if (sync_api::SyncManager::ChangeRecord::ACTION_DELETE ==
changes[i].action) {
DCHECK(changes[i].specifics.HasExtension(
sync_pb::autofill_profile));
const sync_pb::AutofillProfileSpecifics& specifics =
changes[i].specifics.GetExtension(sync_pb::autofill_profile);
autofill_changes_.push_back(AutofillProfileChangeRecord(changes[i].action,
changes[i].id,
specifics));
continue;
}
// If it is not a delete.
sync_api::ReadNode sync_node(write_trans);
if (!sync_node.InitByIdLookup(changes[i].id)) {
LOG(ERROR) << "Could not find the id in sync db " << changes[i].id;
continue;
}
const sync_pb::AutofillProfileSpecifics& autofill(
sync_node.GetAutofillProfileSpecifics());
autofill_changes_.push_back(AutofillProfileChangeRecord(changes[i].action,
changes[i].id,
autofill));
}
}
void AutofillProfileChangeProcessor::Observe(NotificationType type,
const NotificationSource& source,
const NotificationDetails& details) {
DCHECK_EQ(type.value, NotificationType::AUTOFILL_PROFILE_CHANGED);
WebDataService* wds = Source<WebDataService>(source).ptr();
if (!wds || wds->GetDatabase() != web_database_)
return;
sync_api::WriteTransaction trans(share_handle());
sync_api::ReadNode autofill_root(&trans);
if (!autofill_root.InitByTagLookup(kAutofillProfileTag)) {
error_handler()->OnUnrecoverableError(FROM_HERE,
"Server did not create a tolp level node");
return;
}
AutofillProfileChange* change = Details<AutofillProfileChange>(details).ptr();
ActOnChange(change, &trans, autofill_root);
}
void AutofillProfileChangeProcessor::ActOnChange(
AutofillProfileChange* change,
sync_api::WriteTransaction* trans,
sync_api::ReadNode& autofill_root) {
DCHECK(change->type() == AutofillProfileChange::REMOVE || change->profile());
switch (change->type()) {
case AutofillProfileChange::ADD: {
AddAutofillProfileSyncNode(trans, autofill_root, *(change->profile()));
break;
}
case AutofillProfileChange::UPDATE: {
int64 sync_id = model_associator_->GetSyncIdFromChromeId(change->key());
if (sync_api::kInvalidId == sync_id) {
LOG(ERROR) << "Sync id is not found for " << change->key();
break;
}
sync_api::WriteNode node(trans);
if (!node.InitByIdLookup(sync_id)) {
LOG(ERROR) << "Could not find sync node for id " << sync_id;
break;
}
WriteAutofillProfile(*(change->profile()), &node);
break;
}
case AutofillProfileChange::REMOVE: {
int64 sync_id = model_associator_->GetSyncIdFromChromeId(change->key());
if (sync_api::kInvalidId == sync_id) {
LOG(ERROR) << "Sync id is not found for " << change->key();
break;
}
sync_api::WriteNode node(trans);
if (!node.InitByIdLookup(sync_id)) {
LOG(ERROR) << "Could not find sync node for id " << sync_id;
break;
}
node.Remove();
model_associator_->Disassociate(sync_id);
break;
}
default:
NOTREACHED();
}
}
void AutofillProfileChangeProcessor::CommitChangesFromSyncModel() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
if (!running())
return;
ScopedStopObserving observer(this);
for (unsigned int i = 0;i < autofill_changes_.size(); ++i) {
if (sync_api::SyncManager::ChangeRecord::ACTION_DELETE ==
autofill_changes_[i].action_) {
if (!web_database_->RemoveAutoFillProfile(
autofill_changes_[i].profile_specifics_.guid())) {
LOG(ERROR) << "could not delete the profile " <<
autofill_changes_[i].profile_specifics_.guid();
continue;
}
continue;
}
// Now for updates and adds.
ApplyAutofillProfileChange(autofill_changes_[i].action_,
autofill_changes_[i].profile_specifics_,
autofill_changes_[i].id_);
}
autofill_changes_.clear();
PostOptimisticRefreshTask();
}
void AutofillProfileChangeProcessor::PostOptimisticRefreshTask() {
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
new DoOptimisticRefreshForAutofill(
personal_data_));
}
void AutofillProfileChangeProcessor::ApplyAutofillProfileChange(
sync_api::SyncManager::ChangeRecord::Action action,
const sync_pb::AutofillProfileSpecifics& profile_specifics,
int64 sync_id) {
DCHECK_NE(sync_api::SyncManager::ChangeRecord::ACTION_DELETE, action);
switch (action) {
case sync_api::SyncManager::ChangeRecord::ACTION_ADD: {
if (guid::IsValidGUID(profile_specifics.guid()) == false) {
NOTREACHED() << "Guid from the server is invalid " <<
profile_specifics.guid();
return;
}
AutoFillProfile p(profile_specifics.guid());
AutofillProfileModelAssociator::OverwriteProfileWithServerData(&p,
profile_specifics);
if (!web_database_->AddAutoFillProfile(p)) {
LOG(ERROR) << "could not add autofill profile for guid " << p.guid();
break;
}
// Now that the node has been succesfully created we can associate it.
std::string guid = p.guid();
model_associator_->Associate(&guid, sync_id);
break;
}
case sync_api::SyncManager::ChangeRecord::ACTION_UPDATE: {
AutoFillProfile *p;
if (!web_database_->GetAutoFillProfile(profile_specifics.guid(), &p)) {
LOG(ERROR) << "Could not find the autofill profile to update for " <<
profile_specifics.guid();
break;
}
scoped_ptr<AutoFillProfile> autofill_pointer(p);
AutofillProfileModelAssociator::OverwriteProfileWithServerData(
autofill_pointer.get(),
profile_specifics);
if (!web_database_->UpdateAutoFillProfile(*(autofill_pointer.get()))) {
LOG(ERROR) << "Could not update autofill profile for " <<
profile_specifics.guid();
break;
}
break;
}
default: {
NOTREACHED();
break;
}
}
}
void AutofillProfileChangeProcessor::RemoveSyncNode(const std::string& guid,
sync_api::WriteTransaction* trans) {
sync_api::WriteNode node(trans);
int64 sync_id = model_associator_->GetSyncIdFromChromeId(guid);
if (sync_api::kInvalidId == sync_id) {
LOG(ERROR) << "Could not find the node in associator " << guid;
return;
}
if (!node.InitByIdLookup(sync_id)) {
LOG(ERROR) << "Could not find the sync node for " << guid;
return;
}
model_associator_->Disassociate(sync_id);
node.Remove();
}
void AutofillProfileChangeProcessor::AddAutofillProfileSyncNode(
sync_api::WriteTransaction* trans,
sync_api::BaseNode& autofill_profile_root,
const AutoFillProfile& profile) {
std::string guid = profile.guid();
if (guid::IsValidGUID(guid) == false) {
DCHECK(false) << "Guid set on the profile is invalid " << guid;
return;
}
sync_api::WriteNode node(trans);
if (!node.InitUniqueByCreation(syncable::AUTOFILL_PROFILE,
autofill_profile_root,
profile.guid())) {
LOG(ERROR) << "could not create a sync node ";
return;
}
node.SetTitle(UTF8ToWide(profile.guid()));
WriteAutofillProfile(profile, &node);
model_associator_->Associate(&guid, node.GetId());
}
void AutofillProfileChangeProcessor::StartObserving() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
notification_registrar_.Add(this,
NotificationType::AUTOFILL_PROFILE_CHANGED,
NotificationService::AllSources());
}
void AutofillProfileChangeProcessor::StopObserving() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
notification_registrar_.RemoveAll();
}
void AutofillProfileChangeProcessor::WriteAutofillProfile(
const AutoFillProfile& profile,
sync_api::WriteNode* node) {
sync_pb::AutofillProfileSpecifics specifics;
// This would get compiled out in official builds. The caller is expected to
// pass in a valid profile object with valid guid.(i.e., the caller might
// have to a DCHECK and log before calling. Having to check in 2 places is
// not optimal.)
DCHECK(guid::IsValidGUID(profile.guid()));
specifics.set_guid(profile.guid());
specifics.set_name_first(UTF16ToUTF8(
profile.GetFieldText(AutofillType(NAME_FIRST))));
specifics.set_name_middle(UTF16ToUTF8(
profile.GetFieldText(AutofillType(NAME_MIDDLE))));
specifics.set_name_last(
UTF16ToUTF8(profile.GetFieldText(AutofillType(NAME_LAST))));
specifics.set_address_home_line1(
UTF16ToUTF8(profile.GetFieldText(AutofillType(ADDRESS_HOME_LINE1))));
specifics.set_address_home_line2(
UTF16ToUTF8(profile.GetFieldText(AutofillType(ADDRESS_HOME_LINE2))));
specifics.set_address_home_city(UTF16ToUTF8(profile.GetFieldText(
AutofillType(ADDRESS_HOME_CITY))));
specifics.set_address_home_state(UTF16ToUTF8(profile.GetFieldText(
AutofillType(ADDRESS_HOME_STATE))));
specifics.set_address_home_country(UTF16ToUTF8(profile.GetFieldText(
AutofillType(ADDRESS_HOME_COUNTRY))));
specifics.set_address_home_zip(UTF16ToUTF8(profile.GetFieldText(
AutofillType(ADDRESS_HOME_ZIP))));
specifics.set_email_address(UTF16ToUTF8(profile.GetFieldText(
AutofillType(EMAIL_ADDRESS))));
specifics.set_company_name(UTF16ToUTF8(profile.GetFieldText(
AutofillType(COMPANY_NAME))));
specifics.set_phone_fax_whole_number(UTF16ToUTF8(profile.GetFieldText(
AutofillType(PHONE_FAX_WHOLE_NUMBER))));
specifics.set_phone_home_whole_number(UTF16ToUTF8(profile.GetFieldText(
AutofillType(PHONE_HOME_WHOLE_NUMBER))));
node->SetAutofillProfileSpecifics(specifics);
}
} // namespace browser_sync
|