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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
|
// Copyright 2012 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.
#import "ios/chrome/browser/passwords/password_controller.h"
#include <stddef.h>
#include <algorithm>
#include <utility>
#include <vector>
#import "base/ios/weak_nsobject.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/mac/foundation_util.h"
#include "base/mac/scoped_nsobject.h"
#include "base/memory/scoped_ptr.h"
#include "base/strings/string16.h"
#include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "components/autofill/core/common/password_form.h"
#include "components/autofill/core/common/password_form_fill_data.h"
#include "components/browser_sync/browser/profile_sync_service.h"
#include "components/infobars/core/infobar_manager.h"
#include "components/password_manager/core/browser/password_bubble_experiment.h"
#include "components/password_manager/core/browser/password_generation_manager.h"
#include "components/password_manager/core/browser/password_manager.h"
#include "components/password_manager/core/browser/password_manager_client.h"
#include "components/password_manager/core/browser/password_manager_driver.h"
#include "components/sync_driver/sync_service.h"
#include "ios/chrome/browser/browser_state/chrome_browser_state.h"
#include "ios/chrome/browser/experimental_flags.h"
#include "ios/chrome/browser/infobars/infobar_manager_impl.h"
#import "ios/chrome/browser/passwords/ios_chrome_save_password_infobar_delegate.h"
#import "ios/chrome/browser/passwords/js_password_manager.h"
#import "ios/chrome/browser/passwords/password_generation_agent.h"
#include "ios/chrome/browser/sync/ios_chrome_profile_sync_service_factory.h"
#include "ios/web/public/url_scheme_util.h"
#import "ios/web/public/web_state/js/crw_js_injection_receiver.h"
#import "ios/web/public/web_state/web_state.h"
#import "ios/web/public/web_state/web_state_observer_bridge.h"
#include "url/gurl.h"
using password_manager::PasswordFormManager;
using password_manager::PasswordGenerationManager;
using password_manager::PasswordManager;
using password_manager::PasswordManagerClient;
using password_manager::PasswordManagerDriver;
@interface PasswordController ()<CRWWebStateObserver, FormSuggestionProvider>
// Parses the |jsonString| which contatins the password forms found on a web
// page to populate the |forms| vector.
- (void)getPasswordForms:(std::vector<autofill::PasswordForm>*)forms
fromFormsJSON:(NSString*)jsonString
pageURL:(const GURL&)pageURL;
// Processes the JSON string returned as a result of extracting the submitted
// form data and populates |form|. Returns YES on success. NO otherwise.
// |form| cannot be nil.
- (BOOL)getPasswordForm:(autofill::PasswordForm*)form
fromPasswordFormJSON:(NSString*)jsonString
pageURL:(const GURL&)pageURL;
// Informs the |passwordManager_| of the password forms (if any were present)
// that have been found on the page.
- (void)didFinishPasswordFormExtraction:
(const std::vector<autofill::PasswordForm>&)forms;
// Autofills |username| and |password| into the form specified by |formData|,
// invoking |completionHandler| when finished with YES if successful and
// NO otherwise. |completionHandler| may be nil.
- (void)fillPasswordForm:(const autofill::PasswordFormFillData&)formData
withUsername:(const base::string16&)username
password:(const base::string16&)password
completionHandler:(void (^)(BOOL))completionHandler;
// Uses JavaScript to find password forms. Calls |completionHandler| with the
// extracted information used for matching and saving passwords. Calls
// |completionHandler| with an empty vector if no password forms are found.
// |completionHandler| cannot be nil.
- (void)findPasswordFormsWithCompletionHandler:
(void (^)(const std::vector<autofill::PasswordForm>&))completionHandler;
// Finds the currently submitted password form and calls |completionHandler|
// with the populated data structure. |found| is YES if the current form was
// found successfully, NO otherwise. |completionHandler| cannot be nil.
- (void)extractSubmittedPasswordForm:(const std::string&)formName
completionHandler:
(void (^)(BOOL found,
const autofill::PasswordForm& form))
completionHandler;
// Takes values from a JSON |dictionary| and populates the |form|.
// The |pageLocation| is the URL of the current page.
// Returns YES if the form was correctly populated, NO otherwise.
- (BOOL)getPasswordForm:(autofill::PasswordForm*)form
fromDictionary:(const base::DictionaryValue*)dictionary
pageURL:(const GURL&)pageLocation;
@end
namespace {
// Constructs an array of FormSuggestions, each corresponding to a username/
// password pair in |formFillData|, such that |prefix| is a prefix of the
// username of each suggestion.
NSArray* BuildSuggestions(const autofill::PasswordFormFillData& formFillData,
NSString* prefix) {
NSMutableArray* suggestions = [NSMutableArray array];
// Add the default credentials.
NSString* defaultUsername =
base::SysUTF16ToNSString(formFillData.username_field.value);
if ([prefix length] == 0 ||
[defaultUsername rangeOfString:prefix].location == 0) {
NSString* origin =
formFillData.preferred_realm.empty()
? nil
: base::SysUTF8ToNSString(formFillData.preferred_realm);
[suggestions addObject:[FormSuggestion suggestionWithValue:defaultUsername
displayDescription:origin
icon:nil
identifier:0]];
}
// Add the additional credentials.
for (const auto& it : formFillData.additional_logins) {
NSString* additionalUsername = base::SysUTF16ToNSString(it.first);
NSString* additionalOrigin = it.second.realm.empty()
? nil
: base::SysUTF8ToNSString(it.second.realm);
if ([prefix length] == 0 ||
[additionalUsername rangeOfString:prefix].location == 0) {
[suggestions
addObject:[FormSuggestion suggestionWithValue:additionalUsername
displayDescription:additionalOrigin
icon:nil
identifier:0]];
}
}
return suggestions;
}
// Looks for a credential pair in |formData| for with |username|. If such a pair
// exists, returns true and |matchingPassword|; returns false otherwise.
bool FindMatchingUsername(const autofill::PasswordFormFillData& formData,
const base::string16& username,
base::string16* matchingPassword) {
base::string16 defaultUsername = formData.username_field.value;
if (defaultUsername == username) {
*matchingPassword = formData.password_field.value;
return true;
}
// Check whether the user has finished typing an alternate username.
for (const auto& it : formData.additional_logins) {
if (it.first == username) {
*matchingPassword = it.second.password;
return true;
}
}
return false;
}
// Removes URL components not essential for matching the URL to
// saved password databases entries. The stripped components are:
// user, password, query and ref.
GURL stripURL(GURL& url) {
url::Replacements<char> replacements;
replacements.ClearUsername();
replacements.ClearPassword();
replacements.ClearQuery();
replacements.ClearRef();
return url.ReplaceComponents(replacements);
}
// Serializes |formData| into a JSON string that can be used by the JS side
// of PasswordController.
NSString* SerializePasswordFormFillData(
const autofill::PasswordFormFillData& formData) {
// Repackage PasswordFormFillData as a JSON object.
base::DictionaryValue rootDict;
// The normalized URL of the web page.
rootDict.SetString("origin", formData.origin.spec());
// The normalized URL from the "action" attribute of the <form> tag.
rootDict.SetString("action", formData.action.spec());
// Input elements in the form. The list does not necessarily contain
// all elements from the form, but all elements listed here are required
// to identify the right form to fill.
auto fieldList = make_scoped_ptr(new base::ListValue());
auto usernameField = make_scoped_ptr(new base::DictionaryValue());
usernameField->SetString("name", formData.username_field.name);
usernameField->SetString("value", formData.username_field.value);
fieldList->Append(usernameField.release());
auto passwordField = make_scoped_ptr(new base::DictionaryValue());
passwordField->SetString("name", formData.password_field.name);
passwordField->SetString("value", formData.password_field.value);
fieldList->Append(passwordField.release());
rootDict.Set("fields", fieldList.release());
std::string jsonString;
base::JSONWriter::Write(rootDict, &jsonString);
return base::SysUTF8ToNSString(jsonString);
}
// Returns true if the trust level for the current page URL of |web_state| is
// kAbsolute. If |page_url| is not null, fills it with the current page URL.
bool GetPageURLAndCheckTrustLevel(web::WebState* web_state, GURL* page_url) {
auto trustLevel = web::URLVerificationTrustLevel::kNone;
GURL dummy;
if (!page_url)
page_url = &dummy;
*page_url = web_state->GetCurrentURL(&trustLevel);
return trustLevel == web::URLVerificationTrustLevel::kAbsolute;
}
} // namespace
@implementation PasswordController {
scoped_ptr<PasswordManager> passwordManager_;
scoped_ptr<PasswordGenerationManager> passwordGenerationManager_;
scoped_ptr<PasswordManagerClient> passwordManagerClient_;
scoped_ptr<PasswordManagerDriver> passwordManagerDriver_;
base::scoped_nsobject<PasswordGenerationAgent> passwordGenerationAgent_;
JsPasswordManager* passwordJsManager_; // weak
// The pending form data.
scoped_ptr<autofill::PasswordFormFillData> formData_;
// Bridge to observe WebState from Objective-C.
scoped_ptr<web::WebStateObserverBridge> webStateObserverBridge_;
}
- (instancetype)initWithWebState:(web::WebState*)webState
passwordsUiDelegate:(id<PasswordsUiDelegate>)UIDelegate {
DCHECK(webState);
self = [super init];
if (self) {
webStateObserverBridge_.reset(
new web::WebStateObserverBridge(webState, self));
passwordManagerClient_.reset(new IOSChromePasswordManagerClient(self));
passwordManager_.reset(new PasswordManager(passwordManagerClient_.get()));
passwordManagerDriver_.reset(new IOSChromePasswordManagerDriver(self));
if (experimental_flags::IsPasswordGenerationEnabled() &&
!passwordManagerClient_->IsOffTheRecord()) {
passwordGenerationManager_.reset(new PasswordGenerationManager(
passwordManagerClient_.get(), passwordManagerDriver_.get()));
passwordGenerationAgent_.reset([[PasswordGenerationAgent alloc]
initWithWebState:webState
passwordManager:passwordManager_.get()
passwordManagerDriver:passwordManagerDriver_.get()
passwordsUiDelegate:UIDelegate]);
}
passwordJsManager_ = base::mac::ObjCCastStrict<JsPasswordManager>(
[webState->GetJSInjectionReceiver()
instanceOfClass:[JsPasswordManager class]]);
}
return self;
}
- (instancetype)init {
NOTREACHED();
return nil;
}
- (void)dealloc {
[self detach];
[super dealloc];
}
- (ios::ChromeBrowserState*)browserState {
return webStateObserverBridge_ && webStateObserverBridge_->web_state()
? ios::ChromeBrowserState::FromBrowserState(
webStateObserverBridge_->web_state()->GetBrowserState())
: nullptr;
}
- (const GURL&)lastCommittedURL {
if (!webStateObserverBridge_ || !webStateObserverBridge_->web_state())
return GURL::EmptyGURL();
return webStateObserverBridge_->web_state()->GetLastCommittedURL();
}
- (void)detach {
webStateObserverBridge_.reset();
passwordGenerationAgent_.reset();
passwordGenerationManager_.reset();
passwordManagerDriver_.reset();
passwordManager_.reset();
passwordManagerClient_.reset();
}
- (void)findAndFillPasswordForms:(NSString*)username
password:(NSString*)password
completionHandler:(void (^)(BOOL))completionHandler {
[self findPasswordFormsWithCompletionHandler:^(
const std::vector<autofill::PasswordForm>& forms) {
for (const auto& form : forms) {
autofill::PasswordFormFillData formData;
autofill::PasswordFormMap matches;
// Initialize |matches| to satisfy the expectation from
// InitPasswordFormFillData() that the preferred match (3rd parameter)
// should be one of the |matches|.
auto scoped_form = make_scoped_ptr(new autofill::PasswordForm(form));
matches.insert(
std::make_pair(form.username_value, std::move(scoped_form)));
autofill::InitPasswordFormFillData(form, matches, &form, false, false,
&formData);
[self fillPasswordForm:formData
withUsername:base::SysNSStringToUTF16(username)
password:base::SysNSStringToUTF16(password)
completionHandler:completionHandler];
}
}];
}
#pragma mark -
#pragma mark CRWWebStateObserver
- (void)webStateDidLoadPage:(web::WebState*)webState {
// Clear per-page state.
formData_.reset();
// Retrieve the identity of the page. In case the page might be malicous,
// returns early.
GURL pageURL;
if (!GetPageURLAndCheckTrustLevel(webState, &pageURL))
return;
if (!web::UrlHasWebScheme(pageURL))
return;
// Notify the password manager that the page loaded so it can clear its own
// per-page state.
passwordManager_->DidNavigateMainFrame();
if (!webState->ContentIsHTML()) {
// If the current page is not HTML, it does not contain any HTML forms.
[self
didFinishPasswordFormExtraction:std::vector<autofill::PasswordForm>()];
}
// Read all password forms from the page and send them to the password
// manager.
base::WeakNSObject<PasswordController> weakSelf(self);
[self findPasswordFormsWithCompletionHandler:^(
const std::vector<autofill::PasswordForm>& forms) {
[weakSelf didFinishPasswordFormExtraction:forms];
}];
}
- (void)webState:(web::WebState*)webState
didSubmitDocumentWithFormNamed:(const std::string&)formName
userInitiated:(BOOL)userInitiated {
base::WeakNSObject<PasswordController> weakSelf(self);
// This code is racing against the new page loading and will not get the
// password form data if the page has changed. In most cases this code wins
// the race.
// TODO(crbug.com/418827): Fix this by passing in more data from the JS side.
id completionHandler = ^(BOOL found, const autofill::PasswordForm& form) {
if (weakSelf) {
weakSelf.get()->passwordManager_->OnPasswordFormSubmitted(
weakSelf.get()->passwordManagerDriver_.get(), form);
}
};
[self extractSubmittedPasswordForm:formName
completionHandler:completionHandler];
}
- (void)webStateDestroyed:(web::WebState*)webState {
[self detach];
}
- (void)findPasswordFormsWithCompletionHandler:
(void (^)(const std::vector<autofill::PasswordForm>&))completionHandler {
DCHECK(completionHandler);
if (!webStateObserverBridge_ || !webStateObserverBridge_->web_state())
return;
GURL pageURL;
if (!GetPageURLAndCheckTrustLevel(webStateObserverBridge_->web_state(),
&pageURL)) {
return;
}
base::WeakNSObject<PasswordController> weakSelf(self);
[passwordJsManager_ findPasswordFormsWithCompletionHandler:^(
NSString* jsonString) {
std::vector<autofill::PasswordForm> forms;
[weakSelf getPasswordForms:&forms fromFormsJSON:jsonString pageURL:pageURL];
completionHandler(forms);
}];
}
- (void)getPasswordForms:(std::vector<autofill::PasswordForm>*)forms
fromFormsJSON:(NSString*)jsonString
pageURL:(const GURL&)pageURL {
DCHECK(forms);
if (![jsonString length]) {
VLOG(1) << "Error in password controller javascript.";
return;
}
int errorCode = 0;
std::string errorMessage;
scoped_ptr<base::Value> jsonData(base::JSONReader::ReadAndReturnError(
std::string([jsonString UTF8String]), false, &errorCode, &errorMessage));
if (errorCode || !jsonData || !jsonData->IsType(base::Value::TYPE_LIST)) {
VLOG(1) << "JSON parse error " << errorMessage
<< " JSON string: " << [jsonString UTF8String];
return;
}
const base::ListValue* formDataList;
if (!jsonData->GetAsList(&formDataList))
return;
for (size_t i = 0; i != formDataList->GetSize(); ++i) {
const base::DictionaryValue* formData;
if (formDataList->GetDictionary(i, &formData)) {
autofill::PasswordForm form;
if ([self getPasswordForm:&form
fromDictionary:formData
pageURL:pageURL]) {
forms->push_back(form);
}
}
}
}
- (void)extractSubmittedPasswordForm:(const std::string&)formName
completionHandler:
(void (^)(BOOL found,
const autofill::PasswordForm& form))
completionHandler {
DCHECK(completionHandler);
if (!webStateObserverBridge_ || !webStateObserverBridge_->web_state())
return;
GURL pageURL;
if (!GetPageURLAndCheckTrustLevel(webStateObserverBridge_->web_state(),
&pageURL)) {
completionHandler(NO, autofill::PasswordForm());
return;
}
base::WeakNSObject<PasswordController> weakSelf(self);
id extractSubmittedFormCompletionHandler = ^(NSString* jsonString) {
autofill::PasswordForm form;
BOOL found = [weakSelf getPasswordForm:&form
fromPasswordFormJSON:jsonString
pageURL:pageURL];
completionHandler(found, form);
};
[passwordJsManager_ extractForm:base::SysUTF8ToNSString(formName)
completionHandler:extractSubmittedFormCompletionHandler];
}
- (BOOL)getPasswordForm:(autofill::PasswordForm*)form
fromPasswordFormJSON:(NSString*)jsonString
pageURL:(const GURL&)pageURL {
DCHECK(form);
// There is no identifiable password form on the page.
if ([jsonString isEqualToString:@"noPasswordsFound"])
return NO;
int errorCode = 0;
std::string errorMessage;
scoped_ptr<const base::Value> jsonData(base::JSONReader::ReadAndReturnError(
std::string([jsonString UTF8String]), false, &errorCode, &errorMessage));
// If the the JSON string contains null, there is no identifiable password
// form on the page.
if (!errorCode && !jsonData) {
return NO;
}
if (errorCode || !jsonData->IsType(base::Value::TYPE_DICTIONARY)) {
VLOG(1) << "JSON parse error " << errorMessage
<< " JSON string: " << [jsonString UTF8String];
return NO;
}
const base::DictionaryValue* passwordJsonData;
return jsonData->GetAsDictionary(&passwordJsonData) &&
[self getPasswordForm:form
fromDictionary:passwordJsonData
pageURL:pageURL];
}
- (void)didFinishPasswordFormExtraction:
(const std::vector<autofill::PasswordForm>&)forms {
// Do nothing if |self| has been detached.
if (!passwordManager_)
return;
if (!forms.empty()) {
// Invoke the password manager callback to autofill password forms
// on the loaded page.
passwordManager_->OnPasswordFormsParsed(passwordManagerDriver_.get(),
forms);
// Pass the forms to PasswordGenerationAgent to look for account creation
// forms with local heuristics.
[passwordGenerationAgent_ processParsedPasswordForms:forms];
}
// Invoke the password manager callback to check if password was
// accepted or rejected. If accepted, infobar is presented. If
// rejected, the provisionally saved password is deleted. On Chrome
// w/ a renderer, it is the renderer who calls OnPasswordFormsParsed()
// and OnPasswordFormsRendered(). Bling has to improvised a bit on the
// ordering of these two calls.
passwordManager_->OnPasswordFormsRendered(passwordManagerDriver_.get(), forms,
true);
}
- (id<FormInputAccessoryViewProvider>)accessoryViewProvider {
return [passwordGenerationAgent_ accessoryViewProvider];
}
#pragma mark -
#pragma mark FormSuggestionProvider
- (id<FormSuggestionProvider>)suggestionProvider {
return self;
}
- (void)checkIfSuggestionsAvailableForForm:(NSString*)formName
field:(NSString*)fieldName
type:(NSString*)type
typedValue:(NSString*)typedValue
webState:(web::WebState*)webState
completionHandler:
(SuggestionsAvailableCompletion)completion {
if (!formData_ || !GetPageURLAndCheckTrustLevel(webState, nullptr)) {
completion(NO);
return;
}
// Suggestions are available for the username field of the password form.
const base::string16& pendingFormName = formData_->name;
const base::string16& pendingFieldName = formData_->username_field.name;
completion(base::SysNSStringToUTF16(formName) == pendingFormName &&
base::SysNSStringToUTF16(fieldName) == pendingFieldName);
}
- (void)retrieveSuggestionsForForm:(NSString*)formName
field:(NSString*)fieldName
type:(NSString*)type
typedValue:(NSString*)typedValue
webState:(web::WebState*)webState
completionHandler:(SuggestionsReadyCompletion)completion {
DCHECK(GetPageURLAndCheckTrustLevel(webState, nullptr));
if (!formData_) {
completion(@[], nil);
return;
}
completion(BuildSuggestions(*formData_, typedValue), self);
}
- (void)didSelectSuggestion:(FormSuggestion*)suggestion
forField:(NSString*)fieldName
form:(NSString*)formName
completionHandler:(SuggestionHandledCompletion)completion {
const base::string16 username = base::SysNSStringToUTF16(suggestion.value);
base::string16 password;
if (FindMatchingUsername(*formData_, username, &password)) {
[self fillPasswordForm:*formData_
withUsername:username
password:password
completionHandler:^(BOOL success) {
completion();
}];
} else {
completion();
}
}
#pragma mark -
#pragma mark WebPasswordFormData Adaptation
- (BOOL)getPasswordForm:(autofill::PasswordForm*)form
fromDictionary:(const base::DictionaryValue*)dictionary
pageURL:(const GURL&)pageLocation {
DCHECK(form);
DCHECK(dictionary);
DCHECK(pageLocation.is_valid());
base::string16 formOrigin;
if (!(dictionary->GetString("origin", &formOrigin)) ||
(GURL(formOrigin).GetOrigin() != pageLocation.GetOrigin())) {
return NO;
}
std::string origin = pageLocation.spec();
GURL originUrl(origin);
if (!originUrl.is_valid()) {
return NO;
}
form->origin = stripURL(originUrl);
url::Replacements<char> remove_path;
remove_path.ClearPath();
form->signon_realm = form->origin.ReplaceComponents(remove_path).spec();
std::string action;
// Not checking the return value, as empty action is valid.
dictionary->GetString("action", &action);
GURL actionUrl = action.empty() ? originUrl : originUrl.Resolve(action);
if (!actionUrl.is_valid())
return NO;
form->action = stripURL(actionUrl);
if (!dictionary->GetString("usernameElement", &form->username_element) ||
!dictionary->GetString("usernameValue", &form->username_value) ||
!dictionary->GetString("name", &form->form_data.name)) {
return NO;
}
const base::ListValue* passwordDataList;
if (!dictionary->GetList("passwords", &passwordDataList))
return NO;
size_t passwordCount = passwordDataList->GetSize();
if (passwordCount == 0)
return NO;
const base::DictionaryValue* passwordData;
// The "passwords" list contains element/value pairs for
// password inputs found in the form. We recognize
// up to three passwords in any given form and ignore the rest.
const size_t kMaxPasswordsConsidered = 3;
base::string16 elements[kMaxPasswordsConsidered];
base::string16 values[kMaxPasswordsConsidered];
for (size_t i = 0; i < std::min(passwordCount, kMaxPasswordsConsidered);
++i) {
if (!passwordDataList->GetDictionary(i, &passwordData) ||
!passwordData->GetString("element", &elements[i]) ||
!passwordData->GetString("value", &values[i])) {
return NO;
}
}
// Determine which password is the main one, and which is
// an old password (e.g on a "make new password" form), if any.
// TODO(crbug.com/564578): Make this compatible with other platforms.
switch (passwordCount) {
case 1:
form->password_element = elements[0];
form->password_value = values[0];
break;
case 2: {
if (values[0] == values[1]) {
// Treat two identical passwords as a single password.
form->password_element = elements[0];
form->password_value = values[0];
} else {
// Assume first is old password, second is new (no choice but to guess).
form->new_password_element = elements[0];
form->new_password_value = values[0];
form->password_element = elements[1];
form->password_value = values[1];
}
break;
default:
if (values[0] == values[1] && values[0] == values[2]) {
// All three passwords the same? Just treat as one and hope.
form->password_element = elements[0];
form->password_value = values[0];
} else if (values[0] == values[1]) {
// Two the same and one different -> old password is the duplicated
// one.
form->new_password_element = elements[0];
form->new_password_value = values[0];
form->password_element = elements[2];
form->password_value = values[2];
} else if (values[1] == values[2]) {
// Two the same and one different -> new password is the duplicated
// one.
form->new_password_element = elements[0];
form->new_password_value = values[0];
form->password_element = elements[1];
form->password_value = values[1];
} else {
// Three different passwords, or first and last match with middle
// different. No idea which is which, so no luck.
}
break;
}
}
// Fill in as much data about the fields as is required for password
// generation.
const base::ListValue* fieldList = nullptr;
if (!dictionary->GetList("fields", &fieldList))
return NO;
for (size_t i = 0; i < fieldList->GetSize(); ++i) {
const base::DictionaryValue* fieldDictionary = nullptr;
if (!fieldList->GetDictionary(i, &fieldDictionary))
return NO;
base::string16 element;
base::string16 type;
if (!fieldDictionary->GetString("element", &element) ||
!fieldDictionary->GetString("type", &type)) {
return NO;
}
autofill::FormFieldData field;
field.name = std::move(element);
field.form_control_type = base::UTF16ToUTF8(type);
form->form_data.fields.push_back(field);
}
return YES;
}
- (void)showSavePasswordInfoBar:(scoped_ptr<PasswordFormManager>)formToSave {
if (!webStateObserverBridge_ || !webStateObserverBridge_->web_state())
return;
bool isSmartLockBrandingEnabled = false;
if (self.browserState) {
sync_driver::SyncService* sync_service =
IOSChromeProfileSyncServiceFactory::GetForBrowserState(
self.browserState);
isSmartLockBrandingEnabled =
password_bubble_experiment::IsSmartLockBrandingSavePromptEnabled(
sync_service);
}
infobars::InfoBarManager* infoBarManager =
InfoBarManagerImpl::FromWebState(webStateObserverBridge_->web_state());
IOSChromeSavePasswordInfoBarDelegate::Create(
isSmartLockBrandingEnabled, infoBarManager, std::move(formToSave));
}
- (void)fillPasswordForm:(const autofill::PasswordFormFillData&)formData
withUsername:(const base::string16&)username
password:(const base::string16&)password
completionHandler:(void (^)(BOOL))completionHandler {
// Send JSON over to the web view.
[passwordJsManager_ fillPasswordForm:SerializePasswordFormFillData(formData)
withUsername:base::SysUTF16ToNSString(username)
password:base::SysUTF16ToNSString(password)
completionHandler:^(BOOL result) {
if (completionHandler)
completionHandler(result);
}];
}
- (void)fillPasswordForm:(const autofill::PasswordFormFillData&)formData
completionHandler:(void (^)(BOOL))completionHandler {
formData_.reset(new autofill::PasswordFormFillData(formData));
// Don't fill immediately if waiting for the user to type a username.
if (formData_->wait_for_username) {
if (completionHandler)
completionHandler(NO);
return;
}
[self fillPasswordForm:*formData_
withUsername:formData_->username_field.value
password:formData_->password_field.value
completionHandler:completionHandler];
}
- (PasswordGenerationAgent*)passwordGenerationAgent {
return passwordGenerationAgent_.get();
}
- (PasswordGenerationManager*)passwordGenerationManager {
return passwordGenerationManager_.get();
}
- (PasswordManagerClient*)passwordManagerClient {
return passwordManagerClient_.get();
}
- (PasswordManagerDriver*)passwordManagerDriver {
return passwordManagerDriver_.get();
}
- (PasswordManager*)passwordManager {
return passwordManager_.get();
}
- (JsPasswordManager*)passwordJsManager {
return passwordJsManager_;
}
@end
|