-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
autofill_helper.cc
466 lines (398 loc) · 17.5 KB
/
autofill_helper.cc
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
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/sync/test/integration/autofill_helper.h"
#include <stddef.h>
#include <map>
#include <utility>
#include "base/bind.h"
#include "base/guid.h"
#include "base/run_loop.h"
#include "base/synchronization/waitable_event.h"
#include "chrome/browser/autofill/personal_data_manager_factory.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sync/test/integration/sync_datatype_helper.h"
#include "chrome/browser/sync/test/integration/sync_test.h"
#include "chrome/browser/web_data_service_factory.h"
#include "components/autofill/core/browser/autofill_test_utils.h"
#include "components/autofill/core/browser/autofill_type.h"
#include "components/autofill/core/browser/data_model/autofill_profile.h"
#include "components/autofill/core/browser/personal_data_manager.h"
#include "components/autofill/core/browser/webdata/autofill_entry.h"
#include "components/autofill/core/browser/webdata/autofill_table.h"
#include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
#include "components/autofill/core/common/form_field_data.h"
#include "components/webdata/common/web_database.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
using autofill::AutofillChangeList;
using autofill::AutofillEntry;
using autofill::AutofillKey;
using autofill::AutofillProfile;
using autofill::AutofillTable;
using autofill::AutofillType;
using autofill::AutofillWebDataService;
using autofill::AutofillWebDataServiceObserverOnDBSequence;
using autofill::CreditCard;
using autofill::FormFieldData;
using autofill::PersonalDataManager;
using base::WaitableEvent;
using sync_datatype_helper::test;
namespace {
ACTION_P(SignalEvent, event) {
event->Signal();
}
class MockWebDataServiceObserver
: public AutofillWebDataServiceObserverOnDBSequence {
public:
MOCK_METHOD(void,
AutofillEntriesChanged,
(const AutofillChangeList& changes),
(override));
};
scoped_refptr<AutofillWebDataService> GetWebDataService(int index) {
return WebDataServiceFactory::GetAutofillWebDataForProfile(
test()->GetProfile(index), ServiceAccessType::EXPLICIT_ACCESS);
}
void WaitForCurrentTasksToComplete(base::SequencedTaskRunner* task_runner) {
base::WaitableEvent event(base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED);
task_runner->PostTask(FROM_HERE, base::BindOnce(&base::WaitableEvent::Signal,
base::Unretained(&event)));
event.Wait();
}
void RemoveKeyDontBlockForSync(int profile, const AutofillKey& key) {
WaitableEvent done_event(base::WaitableEvent::ResetPolicy::AUTOMATIC,
base::WaitableEvent::InitialState::NOT_SIGNALED);
MockWebDataServiceObserver mock_observer;
EXPECT_CALL(mock_observer, AutofillEntriesChanged)
.WillOnce(SignalEvent(&done_event));
scoped_refptr<AutofillWebDataService> wds = GetWebDataService(profile);
void (AutofillWebDataService::*add_observer_func)(
AutofillWebDataServiceObserverOnDBSequence*) =
&AutofillWebDataService::AddObserver;
wds->GetDBTaskRunner()->PostTask(
FROM_HERE, base::BindOnce(add_observer_func, wds, &mock_observer));
wds->RemoveFormValueForElementName(key.name(), key.value());
done_event.Wait();
void (AutofillWebDataService::*remove_observer_func)(
AutofillWebDataServiceObserverOnDBSequence*) =
&AutofillWebDataService::RemoveObserver;
wds->GetDBTaskRunner()->PostTask(
FROM_HERE, base::BindOnce(remove_observer_func, wds, &mock_observer));
}
void GetAllAutofillEntriesOnDBSequence(AutofillWebDataService* wds,
std::vector<AutofillEntry>* entries) {
DCHECK(wds->GetDBTaskRunner()->RunsTasksInCurrentSequence());
AutofillTable::FromWebDatabase(wds->GetDatabase())
->GetAllAutofillEntries(entries);
}
std::vector<AutofillEntry> GetAllAutofillEntries(AutofillWebDataService* wds) {
std::vector<AutofillEntry> entries;
wds->GetDBTaskRunner()->PostTask(
FROM_HERE, base::BindOnce(&GetAllAutofillEntriesOnDBSequence,
base::Unretained(wds), &entries));
WaitForCurrentTasksToComplete(wds->GetDBTaskRunner());
return entries;
}
bool ProfilesMatchImpl(
const absl::optional<unsigned int>& expected_count,
int profile_a,
const std::vector<AutofillProfile*>& autofill_profiles_a,
int profile_b,
const std::vector<AutofillProfile*>& autofill_profiles_b) {
if (expected_count.has_value() &&
autofill_profiles_a.size() != *expected_count) {
DVLOG(1) << "Profile " << profile_a
<< " does not have expected count of entities " << *expected_count;
return false;
}
std::map<std::string, AutofillProfile> autofill_profiles_a_map;
for (AutofillProfile* p : autofill_profiles_a) {
autofill_profiles_a_map[p->guid()] = *p;
}
// This seems to be a transient state that will eventually be rectified by
// model type logic. We don't need to check b for duplicates directly because
// after the first is erased from |autofill_profiles_a_map| the second will
// not be found.
if (autofill_profiles_a.size() != autofill_profiles_a_map.size()) {
DVLOG(1) << "Profile " << profile_a << " contains duplicate GUID(s).";
return false;
}
for (AutofillProfile* p : autofill_profiles_b) {
if (!autofill_profiles_a_map.count(p->guid())) {
DVLOG(1) << "GUID " << p->guid() << " not found in profile " << profile_b
<< ".";
return false;
}
AutofillProfile* expected_profile = &autofill_profiles_a_map[p->guid()];
expected_profile->set_guid(p->guid());
if (*expected_profile != *p) {
DVLOG(1) << "Mismatch in profile with GUID " << p->guid() << ".";
return false;
}
autofill_profiles_a_map.erase(p->guid());
}
if (!autofill_profiles_a_map.empty()) {
DVLOG(1) << "Entries present in Profile " << profile_a << " but not in "
<< profile_b << ".";
return false;
}
return true;
}
} // namespace
namespace autofill_helper {
ACTION_P(QuitMessageLoop, loop) {
loop->Quit();
}
AutofillProfile CreateAutofillProfile(ProfileType type) {
AutofillProfile profile;
switch (type) {
case PROFILE_MARION:
autofill::test::SetProfileInfoWithGuid(
&profile, "C837507A-6C3B-4872-AC14-5113F157D668", "Marion",
"Mitchell", "Morrison", "johnwayne@me.xyz", "Fox", "123 Zoo St.",
"unit 5", "Hollywood", "CA", "91601", "US", "12345678910");
break;
case PROFILE_HOMER:
autofill::test::SetProfileInfoWithGuid(
&profile, "137DE1C3-6A30-4571-AC86-109B1ECFBE7F", "Homer", "J.",
"Simpson", "homer@abc.com", "SNPP", "742 Evergreen Terrace",
"PO Box 1", "Springfield", "MA", "94101", "US", "14155551212");
break;
case PROFILE_FRASIER:
autofill::test::SetProfileInfoWithGuid(
&profile, "9A5E6872-6198-4688-BF75-0016E781BB0A", "Frasier",
"Winslow", "Crane", "", "randomness", "", "Apt. 4", "Seattle", "WA",
"99121", "US", "0000000000");
break;
case PROFILE_NULL:
autofill::test::SetProfileInfoWithGuid(
&profile, "FE461507-7E13-4198-8E66-74C7DB6D8322", "", "", "", "", "",
"", "", "", "", "", "", "");
break;
}
profile.FinalizeAfterImport();
return profile;
}
AutofillProfile CreateUniqueAutofillProfile() {
AutofillProfile profile;
autofill::test::SetProfileInfoWithGuid(
&profile, base::GenerateGUID().c_str(), "First", "Middle", "Last",
"email@domain.tld", "Company", "123 Main St", "Apt 456", "Nowhere", "OK",
"73038", "US", "12345678910");
profile.FinalizeAfterImport();
return profile;
}
PersonalDataManager* GetPersonalDataManager(int index) {
return autofill::PersonalDataManagerFactory::GetForProfile(
test()->GetProfile(index));
}
void AddKeys(int profile, const std::set<AutofillKey>& keys) {
std::vector<FormFieldData> form_fields;
for (const AutofillKey& key : keys) {
FormFieldData field;
field.name = key.name();
field.value = key.value();
form_fields.push_back(field);
}
WaitableEvent done_event(base::WaitableEvent::ResetPolicy::AUTOMATIC,
base::WaitableEvent::InitialState::NOT_SIGNALED);
MockWebDataServiceObserver mock_observer;
EXPECT_CALL(mock_observer, AutofillEntriesChanged)
.WillOnce(SignalEvent(&done_event));
scoped_refptr<AutofillWebDataService> wds = GetWebDataService(profile);
void (AutofillWebDataService::*add_observer_func)(
AutofillWebDataServiceObserverOnDBSequence*) =
&AutofillWebDataService::AddObserver;
wds->GetDBTaskRunner()->PostTask(
FROM_HERE, base::BindOnce(add_observer_func, wds, &mock_observer));
wds->AddFormFields(form_fields);
done_event.Wait();
WaitForCurrentTasksToComplete(wds->GetDBTaskRunner());
void (AutofillWebDataService::*remove_observer_func)(
AutofillWebDataServiceObserverOnDBSequence*) =
&AutofillWebDataService::RemoveObserver;
wds->GetDBTaskRunner()->PostTask(
FROM_HERE, base::BindOnce(remove_observer_func, wds, &mock_observer));
}
void RemoveKey(int profile, const AutofillKey& key) {
RemoveKeyDontBlockForSync(profile, key);
WaitForCurrentTasksToComplete(GetWebDataService(profile)->GetDBTaskRunner());
}
void RemoveKeys(int profile) {
std::set<AutofillEntry> keys = GetAllKeys(profile);
for (const AutofillEntry& entry : keys) {
RemoveKeyDontBlockForSync(profile, entry.key());
}
WaitForCurrentTasksToComplete(GetWebDataService(profile)->GetDBTaskRunner());
}
std::set<AutofillEntry> GetAllKeys(int profile) {
scoped_refptr<AutofillWebDataService> wds = GetWebDataService(profile);
std::vector<AutofillEntry> all_entries = GetAllAutofillEntries(wds.get());
return std::set<AutofillEntry>(all_entries.begin(), all_entries.end());
}
bool KeysMatch(int profile_a, int profile_b) {
return GetAllKeys(profile_a) == GetAllKeys(profile_b);
}
void SetProfiles(int profile, std::vector<AutofillProfile>* autofill_profiles) {
PersonalDataLoadedObserverMock personal_data_observer;
PersonalDataManager* pdm = GetPersonalDataManager(profile);
base::RunLoop run_loop;
pdm->AddObserver(&personal_data_observer);
EXPECT_CALL(personal_data_observer, OnPersonalDataFinishedProfileTasks())
.WillRepeatedly(QuitMessageLoop(&run_loop));
EXPECT_CALL(personal_data_observer, OnPersonalDataChanged())
.Times(testing::AnyNumber());
pdm->SetProfilesForAllSources(autofill_profiles);
run_loop.Run();
pdm->RemoveObserver(&personal_data_observer);
}
void SetCreditCards(int profile, std::vector<CreditCard>* credit_cards) {
GetPersonalDataManager(profile)->SetCreditCards(credit_cards);
}
void AddProfile(int profile, const AutofillProfile& autofill_profile) {
std::vector<AutofillProfile> autofill_profiles;
for (AutofillProfile* p : GetAllAutoFillProfiles(profile)) {
autofill_profiles.push_back(*p);
}
autofill_profiles.push_back(autofill_profile);
autofill_helper::SetProfiles(profile, &autofill_profiles);
}
void RemoveProfile(int profile, const std::string& guid) {
std::vector<AutofillProfile> autofill_profiles;
for (AutofillProfile* p : GetAllAutoFillProfiles(profile)) {
if (p->guid() != guid) {
autofill_profiles.push_back(*p);
}
}
autofill_helper::SetProfiles(profile, &autofill_profiles);
}
void UpdateProfile(int profile,
const std::string& guid,
const AutofillType& type,
const std::u16string& value,
autofill::VerificationStatus status) {
std::vector<AutofillProfile> profiles;
for (AutofillProfile* p : GetAllAutoFillProfiles(profile)) {
profiles.push_back(*p);
if (p->guid() == guid) {
profiles.back().SetRawInfoWithVerificationStatus(type.GetStorableType(),
value, status);
}
}
autofill_helper::SetProfiles(profile, &profiles);
}
std::vector<AutofillProfile*> GetAllAutoFillProfiles(int profile) {
PersonalDataLoadedObserverMock personal_data_observer;
base::RunLoop run_loop;
PersonalDataManager* pdm = GetPersonalDataManager(profile);
pdm->AddObserver(&personal_data_observer);
pdm->Refresh();
EXPECT_CALL(personal_data_observer, OnPersonalDataFinishedProfileTasks())
.WillOnce(QuitMessageLoop(&run_loop));
EXPECT_CALL(personal_data_observer, OnPersonalDataChanged()).Times(1);
// PersonalDataManager::GetProfiles() simply returns the current values that
// have been last reported to the UI sequence. PersonalDataManager::Refresh()
// will post a task to the DB sequence to read back the latest values, and we
// very much want the latest values. Unfortunately, the Refresh() call is
// asynchronous and there's no way to pass a callback that's run when our
// Refresh() call finishes. A PersonalDataManagerObserver won't completely fix
// the problem either since there could be multiple outstanding modifications
// scheduled, and we cannot ensure that we have the latest view. Instead
// explicitly wait for our Refresh to have executed. It is possible for
// another write to sneak in between our Refresh() and the task that is
// blocked for, causing the GetProfiles() read to return even more current
// data, but this shouldn't cause problems. While PersonalDataManager will
// cancel outstanding queries, this is only instigated on the UI sequence,
// which we are about to block, which means we are safe.
WaitForCurrentTasksToComplete(GetWebDataService(profile)->GetDBTaskRunner());
run_loop.Run();
pdm->RemoveObserver(&personal_data_observer);
return pdm->GetProfiles();
}
size_t GetProfileCount(int profile) {
return GetAllAutoFillProfiles(profile).size();
}
size_t GetKeyCount(int profile) {
return GetAllKeys(profile).size();
}
bool ProfilesMatch(int profile_a, int profile_b) {
const std::vector<AutofillProfile*>& autofill_profiles_a =
GetAllAutoFillProfiles(profile_a);
const std::vector<AutofillProfile*>& autofill_profiles_b =
GetAllAutoFillProfiles(profile_b);
return ProfilesMatchImpl(absl::nullopt, profile_a, autofill_profiles_a,
profile_b, autofill_profiles_b);
}
} // namespace autofill_helper
AutofillKeysChecker::AutofillKeysChecker(int profile_a, int profile_b)
: MultiClientStatusChangeChecker(
sync_datatype_helper::test()->GetSyncServices()),
profile_a_(profile_a),
profile_b_(profile_b) {}
bool AutofillKeysChecker::IsExitConditionSatisfied(std::ostream* os) {
*os << "Waiting for matching autofill keys";
return autofill_helper::KeysMatch(profile_a_, profile_b_);
}
AutofillProfileChecker::AutofillProfileChecker(
int profile_a,
int profile_b,
absl::optional<unsigned int> expected_count)
: profile_a_(profile_a),
profile_b_(profile_b),
expected_count_(expected_count) {
autofill_helper::GetPersonalDataManager(profile_a_)->AddObserver(this);
autofill_helper::GetPersonalDataManager(profile_b_)->AddObserver(this);
}
AutofillProfileChecker::~AutofillProfileChecker() {
autofill_helper::GetPersonalDataManager(profile_a_)->RemoveObserver(this);
autofill_helper::GetPersonalDataManager(profile_b_)->RemoveObserver(this);
}
bool AutofillProfileChecker::Wait() {
DLOG(WARNING) << "AutofillProfileChecker::Wait() started";
PersonalDataLoadedObserverMock personal_data_observer;
base::RunLoop run_loop_a;
base::RunLoop run_loop_b;
PersonalDataManager* pdm_a =
autofill_helper::GetPersonalDataManager(profile_a_);
PersonalDataManager* pdm_b =
autofill_helper::GetPersonalDataManager(profile_b_);
pdm_a->AddObserver(&personal_data_observer);
pdm_b->AddObserver(&personal_data_observer);
EXPECT_CALL(personal_data_observer, OnPersonalDataChanged())
.Times(testing::AnyNumber());
EXPECT_CALL(personal_data_observer, OnPersonalDataFinishedProfileTasks())
.WillRepeatedly(autofill_helper::QuitMessageLoop(&run_loop_a));
pdm_a->Refresh();
// Similar to GetAllAutoFillProfiles() we need to make sure we are not reading
// before any locally instigated async writes. This is run exactly one time
// before the first IsExitConditionSatisfied() is called.
WaitForCurrentTasksToComplete(
GetWebDataService(profile_a_)->GetDBTaskRunner());
run_loop_a.Run();
EXPECT_CALL(personal_data_observer, OnPersonalDataFinishedProfileTasks())
.WillRepeatedly(autofill_helper::QuitMessageLoop(&run_loop_b));
pdm_b->Refresh();
WaitForCurrentTasksToComplete(
GetWebDataService(profile_b_)->GetDBTaskRunner());
run_loop_b.Run();
pdm_a->RemoveObserver(&personal_data_observer);
pdm_b->RemoveObserver(&personal_data_observer);
DLOG(WARNING) << "AutofillProfileChecker::Wait() completed";
return StatusChangeChecker::Wait();
}
bool AutofillProfileChecker::IsExitConditionSatisfied(std::ostream* os) {
*os << "Waiting for matching autofill profiles";
const std::vector<AutofillProfile*>& autofill_profiles_a =
autofill_helper::GetPersonalDataManager(profile_a_)->GetProfiles();
const std::vector<AutofillProfile*>& autofill_profiles_b =
autofill_helper::GetPersonalDataManager(profile_b_)->GetProfiles();
return ProfilesMatchImpl(expected_count_, profile_a_, autofill_profiles_a,
profile_b_, autofill_profiles_b);
}
void AutofillProfileChecker::OnPersonalDataChanged() {
CheckExitCondition();
}
PersonalDataLoadedObserverMock::PersonalDataLoadedObserverMock() = default;
PersonalDataLoadedObserverMock::~PersonalDataLoadedObserverMock() = default;