Chromium Code Reviews| 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/autocomplete_syncable_service.h" | 5 #include "chrome/browser/webdata/autocomplete_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/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 100 | 100 |
| 101 std::vector<AutofillEntry> new_synced_entries; | 101 std::vector<AutofillEntry> new_synced_entries; |
| 102 // Go through and check for all the entries that sync already knows about. | 102 // Go through and check for all the entries that sync already knows about. |
| 103 // CreateOrUpdateEntry() will remove entries that are same with the synced | 103 // CreateOrUpdateEntry() will remove entries that are same with the synced |
| 104 // ones from |new_db_entries|. | 104 // ones from |new_db_entries|. |
| 105 for (SyncDataList::const_iterator sync_iter = initial_sync_data.begin(); | 105 for (SyncDataList::const_iterator sync_iter = initial_sync_data.begin(); |
| 106 sync_iter != initial_sync_data.end(); ++sync_iter) { | 106 sync_iter != initial_sync_data.end(); ++sync_iter) { |
| 107 CreateOrUpdateEntry(*sync_iter, &new_db_entries, &new_synced_entries); | 107 CreateOrUpdateEntry(*sync_iter, &new_db_entries, &new_synced_entries); |
| 108 } | 108 } |
| 109 | 109 |
| 110 // Check if newly received items need culling. | |
| 111 bool need_to_cull_data = false; | |
| 112 base::Time culling_time = base::Time::Now(); | |
| 113 culling_time -= | |
| 114 base::TimeDelta::FromDays(AutofillEntry::kExpirationPeriodInDays); | |
|
Ilya Sherman
2012/03/06 08:48:35
nit: might as well write these three lines as two
GeorgeY
2012/03/09 20:14:24
Done.
| |
| 115 for (size_t i = 0; i < new_synced_entries.size() && !need_to_cull_data; ++i) | |
|
Ilya Sherman
2012/03/06 08:48:35
nit: The loop body is multiple lines, so please ad
GeorgeY
2012/03/09 20:14:24
Done.
| |
| 116 if (new_synced_entries[i].HasTimestampsOlder(culling_time)) | |
| 117 need_to_cull_data = true; | |
| 118 | |
| 110 if (!SaveChangesToWebData(new_synced_entries)) | 119 if (!SaveChangesToWebData(new_synced_entries)) |
| 111 return SyncError(FROM_HERE, "Failed to update webdata.", model_type()); | 120 return SyncError(FROM_HERE, "Failed to update webdata.", model_type()); |
| 112 | 121 |
| 113 WebDataService::NotifyOfMultipleAutofillChanges(web_data_service_); | 122 WebDataService::NotifyOfMultipleAutofillChanges(web_data_service_); |
| 123 keys_to_ignore_.clear(); | |
| 114 | 124 |
| 115 SyncChangeList new_changes; | 125 SyncChangeList new_changes; |
| 116 for (AutocompleteEntryMap::iterator i = new_db_entries.begin(); | 126 for (AutocompleteEntryMap::iterator i = new_db_entries.begin(); |
| 117 i != new_db_entries.end(); ++i) { | 127 i != new_db_entries.end(); ++i) { |
| 118 new_changes.push_back( | 128 if (i->second.second->CullExpiredTimeStamps(culling_time)) |
| 119 SyncChange(i->second.first, CreateSyncData(*(i->second.second)))); | 129 need_to_cull_data = true; |
| 130 // Sync back only the data that appeared after |culling_time|. | |
| 131 if (!i->second.second->timestamps().empty()) { | |
| 132 new_changes.push_back( | |
| 133 SyncChange(i->second.first, CreateSyncData(*(i->second.second)))); | |
| 134 } else { | |
| 135 // Key is not on the server and is too old, it will not ever be synced - | |
| 136 // delete it locally. | |
|
Ilya Sherman
2012/03/06 08:48:35
I don't follow how this tells us that the key is n
GeorgeY
2012/03/09 20:14:24
Then it would be SyncChange::ACTION_UPDATE
Ilya Sherman
2012/03/09 23:15:22
Ah, makes sense -- thanks. I think it would help
| |
| 137 if (SyncChange::ACTION_ADD == i->second.first) | |
| 138 keys_to_ignore_.insert(i->first); | |
| 139 } | |
| 140 } | |
| 141 | |
| 142 if (need_to_cull_data) { | |
|
Ilya Sherman
2012/03/06 08:48:35
Why not just always make this call? It should be
GeorgeY
2012/03/09 20:14:24
Yes, but the call *always* schedules extra task on
| |
| 143 // This will schedule deletion operation later on DB thread and we will | |
| 144 // be notified on the results of the deletion and deletes will be synced to | |
| 145 // the sync. | |
| 146 web_data_service_->RemoveFormElementsAddedBetween(base::Time(), | |
| 147 culling_time); | |
|
Ilya Sherman
2012/03/06 08:48:35
nit: WDS::RemoveFormElementsAddedBefore(culling_ti
GeorgeY
2012/03/09 20:14:24
Why create a trivial interface (it would call Remo
| |
| 120 } | 148 } |
| 121 | 149 |
| 122 SyncError error = sync_processor_->ProcessSyncChanges(FROM_HERE, new_changes); | 150 SyncError error = sync_processor_->ProcessSyncChanges(FROM_HERE, new_changes); |
| 123 | 151 |
| 124 return error; | 152 return error; |
| 125 } | 153 } |
| 126 | 154 |
| 127 void AutocompleteSyncableService::StopSyncing(syncable::ModelType type) { | 155 void AutocompleteSyncableService::StopSyncing(syncable::ModelType type) { |
| 128 DCHECK(CalledOnValidThread()); | 156 DCHECK(CalledOnValidThread()); |
| 129 DCHECK_EQ(syncable::AUTOFILL, type); | 157 DCHECK_EQ(syncable::AUTOFILL, type); |
| (...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 349 } | 377 } |
| 350 AutofillEntry entry(change->key(), timestamps); | 378 AutofillEntry entry(change->key(), timestamps); |
| 351 SyncChange::SyncChangeType change_type = | 379 SyncChange::SyncChangeType change_type = |
| 352 (change->type() == AutofillChange::ADD) ? | 380 (change->type() == AutofillChange::ADD) ? |
| 353 SyncChange::ACTION_ADD : SyncChange::ACTION_UPDATE; | 381 SyncChange::ACTION_ADD : SyncChange::ACTION_UPDATE; |
| 354 new_changes.push_back(SyncChange(change_type, | 382 new_changes.push_back(SyncChange(change_type, |
| 355 CreateSyncData(entry))); | 383 CreateSyncData(entry))); |
| 356 break; | 384 break; |
| 357 } | 385 } |
| 358 case AutofillChange::REMOVE: { | 386 case AutofillChange::REMOVE: { |
| 359 std::vector<base::Time> timestamps; | 387 if (keys_to_ignore_.find(change->key()) == keys_to_ignore_.end()) { |
| 360 AutofillEntry entry(change->key(), timestamps); | 388 std::vector<base::Time> timestamps; |
| 361 new_changes.push_back(SyncChange(SyncChange::ACTION_DELETE, | 389 AutofillEntry entry(change->key(), timestamps); |
| 362 CreateSyncData(entry))); | 390 new_changes.push_back(SyncChange(SyncChange::ACTION_DELETE, |
| 391 CreateSyncData(entry))); | |
| 392 } | |
| 363 break; | 393 break; |
| 364 } | 394 } |
| 365 default: | 395 default: |
| 366 NOTREACHED(); | 396 NOTREACHED(); |
| 367 break; | 397 break; |
| 368 } | 398 } |
| 369 } | 399 } |
| 370 SyncError error = sync_processor_->ProcessSyncChanges(FROM_HERE, new_changes); | 400 SyncError error = sync_processor_->ProcessSyncChanges(FROM_HERE, new_changes); |
| 371 if (error.IsSet()) { | 401 if (error.IsSet()) { |
| 372 DLOG(WARNING) << "[AUTOCOMPLETE SYNC]" | 402 DLOG(WARNING) << "[AUTOCOMPLETE SYNC]" |
| 373 << " Failed processing change:" | 403 << " Failed processing change:" |
| 374 << " Error:" << error.message(); | 404 << " Error:" << error.message(); |
| 375 } | 405 } |
| 406 // |keys_to_ignore_| are only needed for the very first notification. | |
| 407 keys_to_ignore_.clear(); | |
| 376 } | 408 } |
| 377 | 409 |
| 378 SyncData AutocompleteSyncableService::CreateSyncData( | 410 SyncData AutocompleteSyncableService::CreateSyncData( |
| 379 const AutofillEntry& entry) const { | 411 const AutofillEntry& entry) const { |
| 380 sync_pb::EntitySpecifics autofill_specifics; | 412 sync_pb::EntitySpecifics autofill_specifics; |
| 381 WriteAutofillEntry(entry, &autofill_specifics); | 413 WriteAutofillEntry(entry, &autofill_specifics); |
| 382 std::string tag(KeyToTag(UTF16ToUTF8(entry.key().name()), | 414 std::string tag(KeyToTag(UTF16ToUTF8(entry.key().name()), |
| 383 UTF16ToUTF8(entry.key().value()))); | 415 UTF16ToUTF8(entry.key().value()))); |
| 384 return SyncData::CreateLocalData(tag, tag, autofill_specifics); | 416 return SyncData::CreateLocalData(tag, tag, autofill_specifics); |
| 385 } | 417 } |
| 386 | 418 |
| 387 // static | 419 // static |
| 388 std::string AutocompleteSyncableService::KeyToTag(const std::string& name, | 420 std::string AutocompleteSyncableService::KeyToTag(const std::string& name, |
| 389 const std::string& value) { | 421 const std::string& value) { |
| 390 std::string ns(kAutofillEntryNamespaceTag); | 422 std::string ns(kAutofillEntryNamespaceTag); |
| 391 return ns + net::EscapePath(name) + "|" + net::EscapePath(value); | 423 return ns + net::EscapePath(name) + "|" + net::EscapePath(value); |
| 392 } | 424 } |
| OLD | NEW |