summaryrefslogtreecommitdiff
path: root/chromium/components/autofill/core/browser/webdata/autocomplete_sync_bridge.cc
blob: 589df1628afd352b8a5380537b414b7808359d6d (plain)
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
// Copyright 2016 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 "components/autofill/core/browser/webdata/autocomplete_sync_bridge.h"

#include <algorithm>
#include <set>
#include <unordered_set>
#include <utility>
#include <vector>

#include "base/bind.h"
#include "base/debug/dump_without_crashing.h"
#include "base/memory/ptr_util.h"
#include "base/strings/utf_string_conversions.h"
#include "components/autofill/core/browser/proto/autofill_sync.pb.h"
#include "components/autofill/core/browser/webdata/autofill_metadata_change_list.h"
#include "components/autofill/core/browser/webdata/autofill_table.h"
#include "components/autofill/core/browser/webdata/autofill_webdata_backend.h"
#include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
#include "components/sync/model/entity_data.h"
#include "components/sync/model/model_type_change_processor.h"
#include "components/sync/model/mutable_data_batch.h"
#include "net/base/escape.h"

using base::Optional;
using base::Time;
using base::debug::DumpWithoutCrashing;
using sync_pb::AutofillSpecifics;
using syncer::EntityChange;
using syncer::EntityChangeList;
using syncer::EntityData;
using syncer::EntityDataMap;
using syncer::MetadataChangeList;
using syncer::ModelError;
using syncer::ModelTypeChangeProcessor;
using syncer::ModelTypeSyncBridge;
using syncer::MutableDataBatch;

namespace autofill {

namespace {

const char kAutocompleteEntryNamespaceTag[] = "autofill_entry|";
const char kAutocompleteTagDelimiter[] = "|";

// Simplify checking for optional errors and returning only when present.
#define RETURN_IF_ERROR(x)                \
  if (Optional<ModelError> ret_val = x) { \
    return ret_val;                       \
  }

void* UserDataKey() {
  // Use the address of a static that COMDAT folding won't ever collide
  // with something else.
  static int user_data_key = 0;
  return reinterpret_cast<void*>(&user_data_key);
}

std::string EscapeIdentifiers(const AutofillSpecifics& specifics) {
  return net::EscapePath(specifics.name()) +
         std::string(kAutocompleteTagDelimiter) +
         net::EscapePath(specifics.value());
}

std::unique_ptr<EntityData> CreateEntityData(const AutofillEntry& entry) {
  auto entity_data = base::MakeUnique<EntityData>();
  AutofillSpecifics* autofill = entity_data->specifics.mutable_autofill();
  autofill->set_name(base::UTF16ToUTF8(entry.key().name()));
  autofill->set_value(base::UTF16ToUTF8(entry.key().value()));
  autofill->add_usage_timestamp(entry.date_created().ToInternalValue());
  if (entry.date_created() != entry.date_last_used())
    autofill->add_usage_timestamp(entry.date_last_used().ToInternalValue());
  entity_data->non_unique_name = EscapeIdentifiers(*autofill);
  return entity_data;
}

std::string BuildSerializedStorageKey(const std::string& name,
                                      const std::string& value) {
  AutofillSyncStorageKey proto;
  proto.set_name(name);
  proto.set_value(value);
  return proto.SerializeAsString();
}

std::string GetStorageKeyFromModel(const AutofillKey& key) {
  return BuildSerializedStorageKey(base::UTF16ToUTF8(key.name()),
                                   base::UTF16ToUTF8(key.value()));
}

AutofillEntry MergeEntryDates(const AutofillEntry& entry1,
                              const AutofillEntry& entry2) {
  DCHECK(entry1.key() == entry2.key());
  return AutofillEntry(
      entry1.key(), std::min(entry1.date_created(), entry2.date_created()),
      std::max(entry1.date_last_used(), entry2.date_last_used()));
}

bool ParseStorageKey(const std::string& storage_key, AutofillKey* out_key) {
  AutofillSyncStorageKey proto;
  if (proto.ParseFromString(storage_key)) {
    *out_key = AutofillKey(base::UTF8ToUTF16(proto.name()),
                           base::UTF8ToUTF16((proto.value())));
    return true;
  } else {
    return false;
  }
}

AutofillEntry CreateAutofillEntry(const AutofillSpecifics& autofill_specifics) {
  AutofillKey key(base::UTF8ToUTF16(autofill_specifics.name()),
                  base::UTF8ToUTF16(autofill_specifics.value()));
  Time date_created, date_last_used;
  const google::protobuf::RepeatedField<int64_t>& timestamps =
      autofill_specifics.usage_timestamp();
  if (!timestamps.empty()) {
    auto iter_pair = std::minmax_element(timestamps.begin(), timestamps.end());
    date_created = Time::FromInternalValue(*iter_pair.first);
    date_last_used = Time::FromInternalValue(*iter_pair.second);
  }
  return AutofillEntry(key, date_created, date_last_used);
}

// This is used to respond to ApplySyncChanges() and MergeSyncData(). Attempts
// to lazily load local data, and then react to sync data by maintaining
// internal state until flush calls are made, at which point the applicable
// modification should be sent towards local and sync directions.
class SyncDifferenceTracker {
 public:
  explicit SyncDifferenceTracker(AutofillTable* table) : table_(table) {}

  Optional<ModelError> IncorporateRemoteSpecifics(
      const std::string& storage_key,
      const AutofillSpecifics& specifics) {
    if (!specifics.has_value()) {
      // A long time ago autofill had a different format, and it's possible we
      // could encounter some of that legacy data. It is not useful to us,
      // because an autofill entry with no value will not place any text in a
      // form for the user. So drop all of these on the floor.
      DVLOG(1) << "Dropping old-style autofill profile change.";
      return {};
    }

    const AutofillEntry remote = CreateAutofillEntry(specifics);
    DCHECK_EQ(storage_key, GetStorageKeyFromModel(remote.key()));

    Optional<AutofillEntry> local;
    if (!ReadEntry(remote.key(), &local)) {
      return ModelError(FROM_HERE, "Failed reading from WebDatabase.");
    } else if (!local) {
      save_to_local_.push_back(remote);
    } else {
      unique_to_local_.erase(local.value());
      if (remote != local.value()) {
        if (specifics.usage_timestamp().empty()) {
          // Skip merging if there are no timestamps. We don't want to wipe out
          // a local value of |date_created| if the remote copy is oddly formed.
          save_to_sync_.push_back(local.value());
        } else {
          const AutofillEntry merged = MergeEntryDates(local.value(), remote);
          save_to_local_.push_back(merged);
          save_to_sync_.push_back(merged);
        }
      }
    }
    return {};
  }

  Optional<ModelError> IncorporateRemoteDelete(const std::string& storage_key) {
    AutofillKey key;
    if (!ParseStorageKey(storage_key, &key)) {
      return ModelError(FROM_HERE, "Failed parsing storage key.");
    }
    delete_from_local_.insert(key);
    return {};
  }

  Optional<ModelError> FlushToLocal(AutofillWebDataBackend* web_data_backend) {
    for (const AutofillKey& key : delete_from_local_) {
      if (!table_->RemoveFormElement(key.name(), key.value())) {
        return ModelError(FROM_HERE, "Failed deleting from WebDatabase");
      }
    }
    if (!table_->UpdateAutofillEntries(save_to_local_)) {
      return ModelError(FROM_HERE, "Failed updating WebDatabase");
    }
    if (!delete_from_local_.empty() || !save_to_local_.empty()) {
      web_data_backend->NotifyOfMultipleAutofillChanges();
    }
    return {};
  }

  Optional<ModelError> FlushToSync(
      bool include_local_only,
      std::unique_ptr<MetadataChangeList> metadata_change_list,
      ModelTypeChangeProcessor* change_processor) {
    for (const AutofillEntry& entry : save_to_sync_) {
      change_processor->Put(GetStorageKeyFromModel(entry.key()),
                            CreateEntityData(entry),
                            metadata_change_list.get());
    }
    if (include_local_only) {
      if (!InitializeIfNeeded()) {
        return ModelError(FROM_HERE, "Failed reading from WebDatabase.");
      }
      for (const AutofillEntry& entry : unique_to_local_) {
        // This should never be true because only ApplySyncChanges should be
        // calling IncorporateRemoteDelete, while only MergeSyncData should be
        // passing in true for |include_local_only|. If this requirement
        // changes, this DCHECK can change to act as a filter.
        DCHECK(delete_from_local_.find(entry.key()) ==
               delete_from_local_.end());
        change_processor->Put(GetStorageKeyFromModel(entry.key()),
                              CreateEntityData(entry),
                              metadata_change_list.get());
      }
    }
    return static_cast<AutofillMetadataChangeList*>(metadata_change_list.get())
        ->TakeError();
  }

 private:
  // There are three major outcomes of this method.
  // 1. An error is encountered reading from the db, false is returned.
  // 2. The entry is not found, |entry| will not be touched.
  // 3. The entry is found, |entry| will be set.
  bool ReadEntry(const AutofillKey& key, Optional<AutofillEntry>* entry) {
    if (!InitializeIfNeeded()) {
      return false;
    }
    auto iter = unique_to_local_.find(AutofillEntry(key, Time(), Time()));
    if (iter != unique_to_local_.end()) {
      *entry = *iter;
    }
    return true;
  }

  bool InitializeIfNeeded() {
    if (initialized_) {
      return true;
    }

    std::vector<AutofillEntry> vector;
    if (!table_->GetAllAutofillEntries(&vector)) {
      return false;
    }

    unique_to_local_ = std::set<AutofillEntry>(vector.begin(), vector.end());
    initialized_ = true;
    return true;
  }

  AutofillTable* table_;

  // This class attempts to lazily load data from |table_|. This field tracks
  // if that has happened or not yet. To facilitate this, the first usage of
  // |unique_to_local_| should typically be done through ReadEntry().
  bool initialized_ = false;

  // Important to note that because AutofillEntry's operator < simply compares
  // contained AutofillKeys, this acts as a map<AutofillKey, AutofillEntry>.
  // Shouldn't be accessed until either ReadEntry() or InitializeIfNeeded() is
  // called, afterward it will start with all the local data. As sync data is
  // encountered entries are removed from here, leaving only entries that exist
  // solely on the local client.
  std::set<AutofillEntry> unique_to_local_;

  std::set<AutofillKey> delete_from_local_;
  std::vector<AutofillEntry> save_to_local_;

  // Contains merged data for entries that existed on both sync and local sides
  // and need to be saved back to sync.
  std::vector<AutofillEntry> save_to_sync_;

  DISALLOW_COPY_AND_ASSIGN(SyncDifferenceTracker);
};

}  // namespace

// static
void AutocompleteSyncBridge::CreateForWebDataServiceAndBackend(
    AutofillWebDataService* web_data_service,
    AutofillWebDataBackend* web_data_backend) {
  web_data_service->GetDBUserData()->SetUserData(
      UserDataKey(),
      new AutocompleteSyncBridge(
          web_data_backend,
          base::BindRepeating(
              &ModelTypeChangeProcessor::Create,
              base::BindRepeating(base::IgnoreResult(&DumpWithoutCrashing)))));
}

// static
base::WeakPtr<ModelTypeSyncBridge> AutocompleteSyncBridge::FromWebDataService(
    AutofillWebDataService* web_data_service) {
  return static_cast<AutocompleteSyncBridge*>(
             web_data_service->GetDBUserData()->GetUserData(UserDataKey()))
      ->AsWeakPtr();
}

AutocompleteSyncBridge::AutocompleteSyncBridge(
    AutofillWebDataBackend* backend,
    const ChangeProcessorFactory& change_processor_factory)
    : ModelTypeSyncBridge(change_processor_factory, syncer::AUTOFILL),
      web_data_backend_(backend),
      scoped_observer_(this) {
  DCHECK(web_data_backend_);

  scoped_observer_.Add(web_data_backend_);

  LoadMetadata();
}

AutocompleteSyncBridge::~AutocompleteSyncBridge() {
  DCHECK(thread_checker_.CalledOnValidThread());
}

std::unique_ptr<MetadataChangeList>
AutocompleteSyncBridge::CreateMetadataChangeList() {
  DCHECK(thread_checker_.CalledOnValidThread());
  return base::MakeUnique<AutofillMetadataChangeList>(GetAutofillTable(),
                                                      syncer::AUTOFILL);
}

Optional<syncer::ModelError> AutocompleteSyncBridge::MergeSyncData(
    std::unique_ptr<MetadataChangeList> metadata_change_list,
    EntityDataMap entity_data_map) {
  DCHECK(thread_checker_.CalledOnValidThread());

  SyncDifferenceTracker tracker(GetAutofillTable());
  for (auto kv : entity_data_map) {
    DCHECK(kv.second->specifics.has_autofill());
    RETURN_IF_ERROR(tracker.IncorporateRemoteSpecifics(
        kv.first, kv.second->specifics.autofill()));
  }

  RETURN_IF_ERROR(tracker.FlushToLocal(web_data_backend_));
  RETURN_IF_ERROR(tracker.FlushToSync(true, std::move(metadata_change_list),
                                change_processor()));
  web_data_backend_->RemoveExpiredFormElements();
  web_data_backend_->NotifyThatSyncHasStarted(syncer::AUTOFILL);
  return {};
}

Optional<ModelError> AutocompleteSyncBridge::ApplySyncChanges(
    std::unique_ptr<MetadataChangeList> metadata_change_list,
    EntityChangeList entity_changes) {
  DCHECK(thread_checker_.CalledOnValidThread());

  SyncDifferenceTracker tracker(GetAutofillTable());
  for (const EntityChange& change : entity_changes) {
    if (change.type() == EntityChange::ACTION_DELETE) {
      RETURN_IF_ERROR(tracker.IncorporateRemoteDelete(change.storage_key()));
    } else {
      DCHECK(change.data().specifics.has_autofill());
      RETURN_IF_ERROR(tracker.IncorporateRemoteSpecifics(
          change.storage_key(), change.data().specifics.autofill()));
    }
  }

  RETURN_IF_ERROR(tracker.FlushToLocal(web_data_backend_));
  RETURN_IF_ERROR(tracker.FlushToSync(false, std::move(metadata_change_list),
                                      change_processor()));
  web_data_backend_->RemoveExpiredFormElements();
  return {};
}

void AutocompleteSyncBridge::AutocompleteSyncBridge::GetData(
    StorageKeyList storage_keys,
    DataCallback callback) {
  DCHECK(thread_checker_.CalledOnValidThread());
  std::vector<AutofillEntry> entries;
  if (!GetAutofillTable()->GetAllAutofillEntries(&entries)) {
    change_processor()->ReportError(FROM_HERE,
                                    "Failed to load entries from table.");
    return;
  }

  std::unordered_set<std::string> keys_set(storage_keys.begin(),
                                           storage_keys.end());
  auto batch = base::MakeUnique<MutableDataBatch>();
  for (const AutofillEntry& entry : entries) {
    std::string key = GetStorageKeyFromModel(entry.key());
    if (keys_set.find(key) != keys_set.end()) {
      batch->Put(key, CreateEntityData(entry));
    }
  }
  callback.Run(std::move(batch));
}

void AutocompleteSyncBridge::GetAllData(DataCallback callback) {
  DCHECK(thread_checker_.CalledOnValidThread());

  std::vector<AutofillEntry> entries;
  if (!GetAutofillTable()->GetAllAutofillEntries(&entries)) {
    change_processor()->ReportError(FROM_HERE,
                                    "Failed to load entries from table.");
    return;
  }

  auto batch = base::MakeUnique<MutableDataBatch>();
  for (const AutofillEntry& entry : entries) {
    batch->Put(GetStorageKeyFromModel(entry.key()), CreateEntityData(entry));
  }
  callback.Run(std::move(batch));
}

void AutocompleteSyncBridge::ActOnLocalChanges(
    const AutofillChangeList& changes) {
  if (!change_processor()->IsTrackingMetadata()) {
    return;
  }

  auto metadata_change_list = base::MakeUnique<AutofillMetadataChangeList>(
      GetAutofillTable(), syncer::AUTOFILL);
  for (const auto& change : changes) {
    const std::string storage_key = GetStorageKeyFromModel(change.key());
    switch (change.type()) {
      case AutofillChange::ADD:
      case AutofillChange::UPDATE: {
        base::Time date_created, date_last_used;
        bool success = GetAutofillTable()->GetAutofillTimestamps(
            change.key().name(), change.key().value(), &date_created,
            &date_last_used);
        if (!success) {
          change_processor()->ReportError(
              FROM_HERE, "Failed reading autofill entry from WebDatabase.");
          return;
        }

        const AutofillEntry entry(change.key(), date_created, date_last_used);
        change_processor()->Put(storage_key, CreateEntityData(entry),
                                metadata_change_list.get());
        break;
      }
      case AutofillChange::REMOVE: {
        change_processor()->Delete(storage_key, metadata_change_list.get());
        break;
      }
    }
  }

  if (Optional<ModelError> error = metadata_change_list->TakeError())
    change_processor()->ReportError(error.value());
}

void AutocompleteSyncBridge::LoadMetadata() {
  auto batch = base::MakeUnique<syncer::MetadataBatch>();
  if (!GetAutofillTable()->GetAllSyncMetadata(syncer::AUTOFILL, batch.get())) {
    change_processor()->ReportError(
        FROM_HERE, "Failed reading autofill metadata from WebDatabase.");
    return;
  }
  change_processor()->ModelReadyToSync(std::move(batch));
}

std::string AutocompleteSyncBridge::GetClientTag(
    const EntityData& entity_data) {
  DCHECK(entity_data.specifics.has_autofill());
  return std::string(kAutocompleteEntryNamespaceTag) +
         EscapeIdentifiers(entity_data.specifics.autofill());
}

std::string AutocompleteSyncBridge::GetStorageKey(
    const EntityData& entity_data) {
  DCHECK(entity_data.specifics.has_autofill());
  const AutofillSpecifics specifics = entity_data.specifics.autofill();
  return BuildSerializedStorageKey(specifics.name(), specifics.value());
}

// AutofillWebDataServiceObserverOnDBThread implementation.
void AutocompleteSyncBridge::AutofillEntriesChanged(
    const AutofillChangeList& changes) {
  DCHECK(thread_checker_.CalledOnValidThread());
  ActOnLocalChanges(changes);
}

AutofillTable* AutocompleteSyncBridge::GetAutofillTable() const {
  return AutofillTable::FromWebDatabase(web_data_backend_->GetDatabase());
}

}  // namespace autofill