OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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_change_processor.h" | 5 #include "chrome/browser/sync/glue/autofill_change_processor.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
12 #include "chrome/browser/guid.h" | 12 #include "chrome/browser/guid.h" |
13 #include "chrome/browser/profile.h" | 13 #include "chrome/browser/profile.h" |
14 #include "chrome/browser/autofill/personal_data_manager.h" | 14 #include "chrome/browser/autofill/personal_data_manager.h" |
| 15 #include "chrome/browser/prefs/pref_service.h" |
15 #include "chrome/browser/sync/glue/autofill_change_processor2.h" | 16 #include "chrome/browser/sync/glue/autofill_change_processor2.h" |
16 #include "chrome/browser/sync/glue/autofill_model_associator.h" | 17 #include "chrome/browser/sync/glue/autofill_model_associator.h" |
| 18 #include "chrome/browser/sync/glue/autofill_profile_model_associator.h" |
| 19 #include "chrome/browser/sync/glue/do_optimistic_refresh_Task.h" |
17 #include "chrome/browser/sync/profile_sync_service.h" | 20 #include "chrome/browser/sync/profile_sync_service.h" |
18 #include "chrome/browser/webdata/autofill_change.h" | 21 #include "chrome/browser/webdata/autofill_change.h" |
19 #include "chrome/browser/webdata/web_data_service.h" | 22 #include "chrome/browser/webdata/web_data_service.h" |
20 #include "chrome/browser/webdata/web_database.h" | 23 #include "chrome/browser/webdata/web_database.h" |
21 #include "chrome/common/notification_service.h" | 24 #include "chrome/common/notification_service.h" |
| 25 #include "chrome/common/pref_names.h" |
22 | 26 |
23 namespace browser_sync { | 27 namespace browser_sync { |
24 | 28 |
25 struct AutofillChangeProcessor::AutofillChangeRecord { | 29 struct AutofillChangeProcessor::AutofillChangeRecord { |
26 sync_api::SyncManager::ChangeRecord::Action action_; | 30 sync_api::SyncManager::ChangeRecord::Action action_; |
27 int64 id_; | 31 int64 id_; |
28 sync_pb::AutofillSpecifics autofill_; | 32 sync_pb::AutofillSpecifics autofill_; |
29 AutofillChangeRecord(sync_api::SyncManager::ChangeRecord::Action action, | 33 AutofillChangeRecord(sync_api::SyncManager::ChangeRecord::Action action, |
30 int64 id, const sync_pb::AutofillSpecifics& autofill) | 34 int64 id, const sync_pb::AutofillSpecifics& autofill) |
31 : action_(action), | 35 : action_(action), |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 } | 82 } |
79 | 83 |
80 DCHECK(type.value == NotificationType::AUTOFILL_ENTRIES_CHANGED); | 84 DCHECK(type.value == NotificationType::AUTOFILL_ENTRIES_CHANGED); |
81 | 85 |
82 AutofillChangeList* changes = Details<AutofillChangeList>(details).ptr(); | 86 AutofillChangeList* changes = Details<AutofillChangeList>(details).ptr(); |
83 ObserveAutofillEntriesChanged(changes, &trans, autofill_root); | 87 ObserveAutofillEntriesChanged(changes, &trans, autofill_root); |
84 } | 88 } |
85 | 89 |
86 void AutofillChangeProcessor::PostOptimisticRefreshTask() { | 90 void AutofillChangeProcessor::PostOptimisticRefreshTask() { |
87 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 91 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
88 new AutofillModelAssociator::DoOptimisticRefreshTask( | 92 new DoOptimisticRefreshTask( |
89 personal_data_)); | 93 personal_data_)); |
90 } | 94 } |
91 | 95 |
92 void AutofillChangeProcessor::ObserveAutofillEntriesChanged( | 96 void AutofillChangeProcessor::ObserveAutofillEntriesChanged( |
93 AutofillChangeList* changes, sync_api::WriteTransaction* trans, | 97 AutofillChangeList* changes, sync_api::WriteTransaction* trans, |
94 const sync_api::ReadNode& autofill_root) { | 98 const sync_api::ReadNode& autofill_root) { |
95 for (AutofillChangeList::iterator change = changes->begin(); | 99 for (AutofillChangeList::iterator change = changes->begin(); |
96 change != changes->end(); ++change) { | 100 change != changes->end(); ++change) { |
97 switch (change->type()) { | 101 switch (change->type()) { |
98 case AutofillChange::ADD: | 102 case AutofillChange::ADD: |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 } | 209 } |
206 | 210 |
207 for (int i = 0; i < change_count; ++i) { | 211 for (int i = 0; i < change_count; ++i) { |
208 sync_api::SyncManager::ChangeRecord::Action action(changes[i].action); | 212 sync_api::SyncManager::ChangeRecord::Action action(changes[i].action); |
209 if (sync_api::SyncManager::ChangeRecord::ACTION_DELETE == action) { | 213 if (sync_api::SyncManager::ChangeRecord::ACTION_DELETE == action) { |
210 DCHECK(changes[i].specifics.HasExtension(sync_pb::autofill)) | 214 DCHECK(changes[i].specifics.HasExtension(sync_pb::autofill)) |
211 << "Autofill specifics data not present on delete!"; | 215 << "Autofill specifics data not present on delete!"; |
212 const sync_pb::AutofillSpecifics& autofill = | 216 const sync_pb::AutofillSpecifics& autofill = |
213 changes[i].specifics.GetExtension(sync_pb::autofill); | 217 changes[i].specifics.GetExtension(sync_pb::autofill); |
214 if (autofill.has_value() || | 218 if (autofill.has_value() || |
215 (IsUpgrading() && autofill.has_profile())) { | 219 (IsUpgrading(trans) && autofill.has_profile())) { |
216 autofill_changes_.push_back(AutofillChangeRecord(changes[i].action, | 220 autofill_changes_.push_back(AutofillChangeRecord(changes[i].action, |
217 changes[i].id, | 221 changes[i].id, |
218 autofill)); | 222 autofill)); |
219 } else { | 223 } else { |
220 NOTREACHED() << "Autofill specifics has no data!"; | 224 NOTREACHED() << "Autofill specifics has no data!"; |
221 } | 225 } |
222 continue; | 226 continue; |
223 } | 227 } |
224 | 228 |
225 // Handle an update or add. | 229 // Handle an update or add. |
226 sync_api::ReadNode sync_node(trans); | 230 sync_api::ReadNode sync_node(trans); |
227 if (!sync_node.InitByIdLookup(changes[i].id)) { | 231 if (!sync_node.InitByIdLookup(changes[i].id)) { |
228 error_handler()->OnUnrecoverableError(FROM_HERE, | 232 error_handler()->OnUnrecoverableError(FROM_HERE, |
229 "Autofill node lookup failed."); | 233 "Autofill node lookup failed."); |
230 return; | 234 return; |
231 } | 235 } |
232 | 236 |
233 // Check that the changed node is a child of the autofills folder. | 237 // Check that the changed node is a child of the autofills folder. |
234 DCHECK(autofill_root.GetId() == sync_node.GetParentId()); | 238 DCHECK(autofill_root.GetId() == sync_node.GetParentId()); |
235 DCHECK(syncable::AUTOFILL == sync_node.GetModelType()); | 239 DCHECK(syncable::AUTOFILL == sync_node.GetModelType()); |
236 | 240 |
237 const sync_pb::AutofillSpecifics& autofill( | 241 const sync_pb::AutofillSpecifics& autofill( |
238 sync_node.GetAutofillSpecifics()); | 242 sync_node.GetAutofillSpecifics()); |
239 int64 sync_id = sync_node.GetId(); | 243 int64 sync_id = sync_node.GetId(); |
240 if (autofill.has_value() || (IsUpgrading() && autofill.has_profile())) { | 244 if (autofill.has_value() || (IsUpgrading(trans) && autofill.has_profile()))
{ |
241 autofill_changes_.push_back(AutofillChangeRecord(changes[i].action, | 245 autofill_changes_.push_back(AutofillChangeRecord(changes[i].action, |
242 sync_id, autofill)); | 246 sync_id, autofill)); |
243 } else { | 247 } else { |
244 NOTREACHED() << "Autofill specifics has no data!"; | 248 NOTREACHED() << "Autofill specifics has no data!"; |
245 } | 249 } |
246 } | 250 } |
247 | 251 |
248 StartObserving(); | 252 StartObserving(); |
249 } | 253 } |
250 | 254 |
251 void AutofillChangeProcessor::CommitChangesFromSyncModel() { | 255 void AutofillChangeProcessor::CommitChangesFromSyncModel() { |
252 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 256 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
253 if (!running()) | 257 if (!running()) |
254 return; | 258 return; |
255 StopObserving(); | 259 StopObserving(); |
256 | 260 |
257 std::vector<AutofillEntry> new_entries; | 261 std::vector<AutofillEntry> new_entries; |
258 for (unsigned int i = 0; i < autofill_changes_.size(); i++) { | 262 for (unsigned int i = 0; i < autofill_changes_.size(); i++) { |
259 // Handle deletions. | 263 // Handle deletions. |
260 if (sync_api::SyncManager::ChangeRecord::ACTION_DELETE == | 264 if (sync_api::SyncManager::ChangeRecord::ACTION_DELETE == |
261 autofill_changes_[i].action_) { | 265 autofill_changes_[i].action_) { |
262 if (autofill_changes_[i].autofill_.has_value()) { | 266 if (autofill_changes_[i].autofill_.has_value()) { |
263 ApplySyncAutofillEntryDelete(autofill_changes_[i].autofill_); | 267 ApplySyncAutofillEntryDelete(autofill_changes_[i].autofill_); |
264 } else if (autofill_changes_[i].autofill_.has_profile()) { | 268 } else if (autofill_changes_[i].autofill_.has_profile()) { |
265 DCHECK(IsUpgrading()); | |
266 ApplySyncAutofillProfileDelete(autofill_changes_[i].id_); | 269 ApplySyncAutofillProfileDelete(autofill_changes_[i].id_); |
267 } else { | 270 } else { |
268 NOTREACHED() << "Autofill's CommitChanges received change with no" | 271 NOTREACHED() << "Autofill's CommitChanges received change with no" |
269 " data!"; | 272 " data!"; |
270 } | 273 } |
271 continue; | 274 continue; |
272 } | 275 } |
273 | 276 |
274 // Handle update/adds. | 277 // Handle update/adds. |
275 if (autofill_changes_[i].autofill_.has_value()) { | 278 if (autofill_changes_[i].autofill_.has_value()) { |
276 ApplySyncAutofillEntryChange(autofill_changes_[i].action_, | 279 ApplySyncAutofillEntryChange(autofill_changes_[i].action_, |
277 autofill_changes_[i].autofill_, &new_entries, | 280 autofill_changes_[i].autofill_, &new_entries, |
278 autofill_changes_[i].id_); | 281 autofill_changes_[i].id_); |
279 } else if (autofill_changes_[i].autofill_.has_profile()) { | 282 } else if (autofill_changes_[i].autofill_.has_profile()) { |
280 DCHECK(IsUpgrading()); | |
281 ApplySyncAutofillProfileChange(autofill_changes_[i].action_, | 283 ApplySyncAutofillProfileChange(autofill_changes_[i].action_, |
282 autofill_changes_[i].autofill_.profile(), | 284 autofill_changes_[i].autofill_.profile(), |
283 autofill_changes_[i].id_); | 285 autofill_changes_[i].id_); |
284 } else { | 286 } else { |
285 NOTREACHED() << "Autofill's CommitChanges received change with no data!"; | 287 NOTREACHED() << "Autofill's CommitChanges received change with no data!"; |
286 } | 288 } |
287 } | 289 } |
288 autofill_changes_.clear(); | 290 autofill_changes_.clear(); |
289 | 291 |
| 292 |
290 // Make changes | 293 // Make changes |
291 if (!web_database_->UpdateAutofillEntries(new_entries)) { | 294 if (!web_database_->UpdateAutofillEntries(new_entries)) { |
292 error_handler()->OnUnrecoverableError(FROM_HERE, | 295 error_handler()->OnUnrecoverableError(FROM_HERE, |
293 "Could not update autofill entries."); | 296 "Could not update autofill entries."); |
294 return; | 297 return; |
295 } | 298 } |
296 | 299 |
297 PostOptimisticRefreshTask(); | 300 PostOptimisticRefreshTask(); |
298 | 301 |
299 StartObserving(); | 302 StartObserving(); |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
428 autofill.set_name(UTF16ToUTF8(entry.key().name())); | 431 autofill.set_name(UTF16ToUTF8(entry.key().name())); |
429 autofill.set_value(UTF16ToUTF8(entry.key().value())); | 432 autofill.set_value(UTF16ToUTF8(entry.key().value())); |
430 const std::vector<base::Time>& ts(entry.timestamps()); | 433 const std::vector<base::Time>& ts(entry.timestamps()); |
431 for (std::vector<base::Time>::const_iterator timestamp = ts.begin(); | 434 for (std::vector<base::Time>::const_iterator timestamp = ts.begin(); |
432 timestamp != ts.end(); ++timestamp) { | 435 timestamp != ts.end(); ++timestamp) { |
433 autofill.add_usage_timestamp(timestamp->ToInternalValue()); | 436 autofill.add_usage_timestamp(timestamp->ToInternalValue()); |
434 } | 437 } |
435 node->SetAutofillSpecifics(autofill); | 438 node->SetAutofillSpecifics(autofill); |
436 } | 439 } |
437 | 440 |
438 // static | 441 bool AutofillChangeProcessor::IsUpgrading( |
439 void AutofillChangeProcessor::WriteAutofillProfile( | 442 const sync_api::BaseTransaction* trans) { |
440 const AutoFillProfile& profile, sync_api::WriteNode* node) { | 443 sync_api::ReadNode node(trans); |
441 sync_pb::AutofillSpecifics autofill; | 444 return !node.InitByClientTagLookup(syncable::AUTOFILL_PROFILE, |
442 sync_pb::AutofillProfileSpecifics* s(autofill.mutable_profile()); | 445 kAutofillProfileTag); |
443 s->set_name_first(UTF16ToUTF8( | |
444 profile.GetFieldText(AutoFillType(NAME_FIRST)))); | |
445 s->set_name_middle(UTF16ToUTF8( | |
446 profile.GetFieldText(AutoFillType(NAME_MIDDLE)))); | |
447 s->set_name_last(UTF16ToUTF8(profile.GetFieldText(AutoFillType(NAME_LAST)))); | |
448 s->set_address_home_line1( | |
449 UTF16ToUTF8(profile.GetFieldText(AutoFillType(ADDRESS_HOME_LINE1)))); | |
450 s->set_address_home_line2( | |
451 UTF16ToUTF8(profile.GetFieldText(AutoFillType(ADDRESS_HOME_LINE2)))); | |
452 s->set_address_home_city(UTF16ToUTF8(profile.GetFieldText( | |
453 AutoFillType(ADDRESS_HOME_CITY)))); | |
454 s->set_address_home_state(UTF16ToUTF8(profile.GetFieldText( | |
455 AutoFillType(ADDRESS_HOME_STATE)))); | |
456 s->set_address_home_country(UTF16ToUTF8(profile.GetFieldText( | |
457 AutoFillType(ADDRESS_HOME_COUNTRY)))); | |
458 s->set_address_home_zip(UTF16ToUTF8(profile.GetFieldText( | |
459 AutoFillType(ADDRESS_HOME_ZIP)))); | |
460 s->set_email_address(UTF16ToUTF8(profile.GetFieldText( | |
461 AutoFillType(EMAIL_ADDRESS)))); | |
462 s->set_company_name(UTF16ToUTF8(profile.GetFieldText( | |
463 AutoFillType(COMPANY_NAME)))); | |
464 s->set_phone_fax_whole_number(UTF16ToUTF8(profile.GetFieldText( | |
465 AutoFillType(PHONE_FAX_WHOLE_NUMBER)))); | |
466 s->set_phone_home_whole_number(UTF16ToUTF8(profile.GetFieldText( | |
467 AutoFillType(PHONE_HOME_WHOLE_NUMBER)))); | |
468 node->SetAutofillSpecifics(autofill); | |
469 } | |
470 bool AutofillChangeProcessor::IsUpgrading() { | |
471 return true; | |
472 } | 446 } |
473 | 447 |
474 } // namespace browser_sync | 448 } // namespace browser_sync |
OLD | NEW |