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

Side by Side Diff: components/autofill/core/browser/webdata/autocomplete_sync_bridge.cc

Issue 2620783002: [sync] Handle local changes in AutocompleteSyncBridge (Closed)
Patch Set: rebase Created 3 years, 11 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
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "components/autofill/core/browser/webdata/autocomplete_sync_bridge.h" 5 #include "components/autofill/core/browser/webdata/autocomplete_sync_bridge.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <set> 8 #include <set>
9 #include <unordered_set> 9 #include <unordered_set>
10 #include <utility> 10 #include <utility>
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after
286 286
287 AutocompleteSyncBridge::AutocompleteSyncBridge( 287 AutocompleteSyncBridge::AutocompleteSyncBridge(
288 AutofillWebDataBackend* backend, 288 AutofillWebDataBackend* backend,
289 const ChangeProcessorFactory& change_processor_factory) 289 const ChangeProcessorFactory& change_processor_factory)
290 : ModelTypeSyncBridge(change_processor_factory, syncer::AUTOFILL), 290 : ModelTypeSyncBridge(change_processor_factory, syncer::AUTOFILL),
291 web_data_backend_(backend), 291 web_data_backend_(backend),
292 scoped_observer_(this) { 292 scoped_observer_(this) {
293 DCHECK(web_data_backend_); 293 DCHECK(web_data_backend_);
294 294
295 scoped_observer_.Add(web_data_backend_); 295 scoped_observer_.Add(web_data_backend_);
296
297 LoadMetadata();
296 } 298 }
297 299
298 AutocompleteSyncBridge::~AutocompleteSyncBridge() { 300 AutocompleteSyncBridge::~AutocompleteSyncBridge() {
299 DCHECK(thread_checker_.CalledOnValidThread()); 301 DCHECK(thread_checker_.CalledOnValidThread());
300 } 302 }
301 303
302 std::unique_ptr<MetadataChangeList> 304 std::unique_ptr<MetadataChangeList>
303 AutocompleteSyncBridge::CreateMetadataChangeList() { 305 AutocompleteSyncBridge::CreateMetadataChangeList() {
304 DCHECK(thread_checker_.CalledOnValidThread()); 306 DCHECK(thread_checker_.CalledOnValidThread());
305 return base::MakeUnique<AutofillMetadataChangeList>(GetAutofillTable(), 307 return base::MakeUnique<AutofillMetadataChangeList>(GetAutofillTable(),
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
383 return; 385 return;
384 } 386 }
385 387
386 auto batch = base::MakeUnique<MutableDataBatch>(); 388 auto batch = base::MakeUnique<MutableDataBatch>();
387 for (const AutofillEntry& entry : entries) { 389 for (const AutofillEntry& entry : entries) {
388 batch->Put(GetStorageKeyFromModel(entry.key()), CreateEntityData(entry)); 390 batch->Put(GetStorageKeyFromModel(entry.key()), CreateEntityData(entry));
389 } 391 }
390 callback.Run(std::move(batch)); 392 callback.Run(std::move(batch));
391 } 393 }
392 394
395 void AutocompleteSyncBridge::ActOnLocalChanges(
396 const AutofillChangeList& changes) {
397 if (!change_processor()->IsTrackingMetadata()) {
398 return;
399 }
400
401 auto metadata_change_list = base::MakeUnique<AutofillMetadataChangeList>(
402 GetAutofillTable(), syncer::AUTOFILL);
403 for (const auto& change : changes) {
404 const std::string storage_key = GetStorageKeyFromModel(change.key());
405 switch (change.type()) {
406 case AutofillChange::ADD:
407 case AutofillChange::UPDATE: {
408 base::Time date_created, date_last_used;
409 bool success = GetAutofillTable()->GetAutofillTimestamps(
410 change.key().name(), change.key().value(), &date_created,
411 &date_last_used);
412 if (!success) {
413 change_processor()->ReportError(
414 FROM_HERE, "Failed reading autofill entry from WebDatabase.");
415 return;
416 }
417
418 const AutofillEntry entry(change.key(), date_created, date_last_used);
419 change_processor()->Put(storage_key, CreateEntityData(entry),
420 metadata_change_list.get());
421 break;
422 }
423 case AutofillChange::REMOVE: {
424 change_processor()->Delete(storage_key, metadata_change_list.get());
425 break;
426 }
427 }
428 }
429
430 if (Optional<ModelError> error = metadata_change_list->TakeError())
431 change_processor()->ReportError(error.value());
432 }
433
434 void AutocompleteSyncBridge::LoadMetadata() {
435 auto batch = base::MakeUnique<syncer::MetadataBatch>();
436 if (!GetAutofillTable()->GetAllSyncMetadata(syncer::AUTOFILL, batch.get())) {
437 change_processor()->ReportError(
438 FROM_HERE, "Failed reading autofill metadata from WebDatabase.");
439 return;
440 }
441 change_processor()->ModelReadyToSync(std::move(batch));
442 }
443
393 std::string AutocompleteSyncBridge::GetClientTag( 444 std::string AutocompleteSyncBridge::GetClientTag(
394 const EntityData& entity_data) { 445 const EntityData& entity_data) {
395 DCHECK(entity_data.specifics.has_autofill()); 446 DCHECK(entity_data.specifics.has_autofill());
396 const AutofillSpecifics specifics = entity_data.specifics.autofill(); 447 const AutofillSpecifics specifics = entity_data.specifics.autofill();
397 return std::string(kAutocompleteEntryNamespaceTag) + 448 return std::string(kAutocompleteEntryNamespaceTag) +
398 net::EscapePath(specifics.name()) + 449 net::EscapePath(specifics.name()) +
399 std::string(kAutocompleteTagDelimiter) + 450 std::string(kAutocompleteTagDelimiter) +
400 net::EscapePath(specifics.value()); 451 net::EscapePath(specifics.value());
401 } 452 }
402 453
403 std::string AutocompleteSyncBridge::GetStorageKey( 454 std::string AutocompleteSyncBridge::GetStorageKey(
404 const EntityData& entity_data) { 455 const EntityData& entity_data) {
405 DCHECK(entity_data.specifics.has_autofill()); 456 DCHECK(entity_data.specifics.has_autofill());
406 const AutofillSpecifics specifics = entity_data.specifics.autofill(); 457 const AutofillSpecifics specifics = entity_data.specifics.autofill();
407 return BuildSerializedStorageKey(specifics.name(), specifics.value()); 458 return BuildSerializedStorageKey(specifics.name(), specifics.value());
408 } 459 }
409 460
410 // AutofillWebDataServiceObserverOnDBThread implementation. 461 // AutofillWebDataServiceObserverOnDBThread implementation.
411 void AutocompleteSyncBridge::AutofillEntriesChanged( 462 void AutocompleteSyncBridge::AutofillEntriesChanged(
412 const AutofillChangeList& changes) { 463 const AutofillChangeList& changes) {
413 DCHECK(thread_checker_.CalledOnValidThread()); 464 DCHECK(thread_checker_.CalledOnValidThread());
465 ActOnLocalChanges(changes);
414 } 466 }
415 467
416 AutofillTable* AutocompleteSyncBridge::GetAutofillTable() const { 468 AutofillTable* AutocompleteSyncBridge::GetAutofillTable() const {
417 return AutofillTable::FromWebDatabase(web_data_backend_->GetDatabase()); 469 return AutofillTable::FromWebDatabase(web_data_backend_->GetDatabase());
418 } 470 }
419 471
420 } // namespace autofill 472 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698