Commit f53e5e91 authored by David Jean's avatar David Jean Committed by Commit Bot

[ios] add presaving calls to password generation

Bug: 886583
Change-Id: I28e56d75def275ec05b5425be85282d1f11fffe2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1480458
Commit-Queue: David Jean <djean@chromium.org>
Reviewed-by: default avatarVadym Doroshenko <dvadym@chromium.org>
Cr-Commit-Position: refs/heads/master@{#637647}
parent c36e5134
...@@ -93,7 +93,7 @@ class WebState; ...@@ -93,7 +93,7 @@ class WebState;
// Finds the password form named |formName| and calls // Finds the password form named |formName| and calls
// |completionHandler| with the populated |FormData| data structure. |found| is // |completionHandler| with the populated |FormData| data structure. |found| is
// YES if the current form was found successfully, NO otherwise. // YES if the current form was found successfully, NO otherwise.
- (void)extractPasswordFormData:(const std::string&)formName - (void)extractPasswordFormData:(NSString*)formName
completionHandler: completionHandler:
(void (^)(BOOL found, (void (^)(BOOL found,
const autofill::FormData& form))completionHandler; const autofill::FormData& form))completionHandler;
......
...@@ -427,7 +427,7 @@ constexpr char kCommandPrefix[] = "passwordForm"; ...@@ -427,7 +427,7 @@ constexpr char kCommandPrefix[] = "passwordForm";
// Finds the password form named |formName| and calls // Finds the password form named |formName| and calls
// |completionHandler| with the populated |FormData| data structure. |found| is // |completionHandler| with the populated |FormData| data structure. |found| is
// YES if the current form was found successfully, NO otherwise. // YES if the current form was found successfully, NO otherwise.
- (void)extractPasswordFormData:(const std::string&)formName - (void)extractPasswordFormData:(NSString*)formName
completionHandler:(void (^)(BOOL found, const FormData& form)) completionHandler:(void (^)(BOOL found, const FormData& form))
completionHandler { completionHandler {
DCHECK(completionHandler); DCHECK(completionHandler);
...@@ -459,7 +459,7 @@ constexpr char kCommandPrefix[] = "passwordForm"; ...@@ -459,7 +459,7 @@ constexpr char kCommandPrefix[] = "passwordForm";
completionHandler(YES, formData); completionHandler(YES, formData);
}; };
[self.jsPasswordManager extractForm:base::SysUTF8ToNSString(formName) [self.jsPasswordManager extractForm:formName
completionHandler:extractFormDataCompletionHandler]; completionHandler:extractFormDataCompletionHandler];
} }
......
...@@ -750,7 +750,7 @@ TEST_F(PasswordFormHelperTest, ExtractPasswordFormData) { ...@@ -750,7 +750,7 @@ TEST_F(PasswordFormHelperTest, ExtractPasswordFormData) {
__block int call_counter = 0; __block int call_counter = 0;
__block int success_counter = 0; __block int success_counter = 0;
__block FormData result = FormData(); __block FormData result = FormData();
[helper_ extractPasswordFormData:GetFormId(1) [helper_ extractPasswordFormData:base::SysUTF8ToNSString(GetFormId(1))
completionHandler:^(BOOL complete, const FormData& form) { completionHandler:^(BOOL complete, const FormData& form) {
++call_counter; ++call_counter;
if (complete) { if (complete) {
...@@ -768,7 +768,7 @@ TEST_F(PasswordFormHelperTest, ExtractPasswordFormData) { ...@@ -768,7 +768,7 @@ TEST_F(PasswordFormHelperTest, ExtractPasswordFormData) {
success_counter = 0; success_counter = 0;
result = FormData(); result = FormData();
[helper_ extractPasswordFormData:"unknown" [helper_ extractPasswordFormData:@"unknown"
completionHandler:^(BOOL complete, const FormData& form) { completionHandler:^(BOOL complete, const FormData& form) {
++call_counter; ++call_counter;
if (complete) { if (complete) {
......
...@@ -141,6 +141,9 @@ void LogSuggestionShown(PasswordSuggestionType type) { ...@@ -141,6 +141,9 @@ void LogSuggestionShown(PasswordSuggestionType type) {
// Tracks if current password is generated. // Tracks if current password is generated.
@property(nonatomic, assign) BOOL isPasswordGenerated; @property(nonatomic, assign) BOOL isPasswordGenerated;
// Tracks field when current password was generated.
@property(nonatomic, copy) NSString* passwordGeneratedIdentifier;
@end @end
@interface PasswordController ()<FormSuggestionProvider, PasswordFormFiller> @interface PasswordController ()<FormSuggestionProvider, PasswordFormFiller>
...@@ -360,20 +363,31 @@ void LogSuggestionShown(PasswordSuggestionType type) { ...@@ -360,20 +363,31 @@ void LogSuggestionShown(PasswordSuggestionType type) {
}]; }];
if (self.isPasswordGenerated && if (self.isPasswordGenerated &&
[self canGeneratePasswordForForm:formName [fieldIdentifier isEqualToString:self.passwordGeneratedIdentifier]) {
fieldIdentifier:fieldIdentifier // TODO(crbug.com/886583): On other platforms, when the user clicks on
fieldType:fieldType]) { // generation field, we show password in clear text. And the user has
// the possibility to edit it. On iOS, it's harder to do (it's probably
// bad idea to change field type from password to text).
// We probably need to show some additionaly UI. Waiting on UX.
if (typedValue.length < kMinimumLengthForEditedPassword) { if (typedValue.length < kMinimumLengthForEditedPassword) {
self.isPasswordGenerated = NO; self.isPasswordGenerated = NO;
// TODO(crbug.com/886583): call self.passwordGeneratedIdentifier = nil;
// passwordManager->OnPasswordNoLongerGenerated, but how to get self.passwordManager->OnPasswordNoLongerGenerated(
// PasswordForm? self.passwordManagerDriver);
} else { } else {
// Inject updated value to possibly update confirmation field.
[self injectGeneratedPasswordForFormName:formName [self injectGeneratedPasswordForFormName:formName
generatedPassword:typedValue generatedPassword:typedValue
completionHandler:nil]; completionHandler:nil];
} }
} }
if (self.isPasswordGenerated) {
// Always update, in case, for example, that username has been edited.
self.passwordManager->UpdateGeneratedPasswordOnUserInput(
self.passwordManagerDriver, SysNSStringToUTF16(formName),
SysNSStringToUTF16(fieldIdentifier), SysNSStringToUTF16(typedValue));
}
} }
- (void)retrieveSuggestionsForForm:(NSString*)formName - (void)retrieveSuggestionsForForm:(NSString*)formName
...@@ -794,10 +808,24 @@ void LogSuggestionShown(PasswordSuggestionType type) { ...@@ -794,10 +808,24 @@ void LogSuggestionShown(PasswordSuggestionType type) {
generatedPassword:(NSString*)generatedPassword generatedPassword:(NSString*)generatedPassword
completionHandler:(void (^)(BOOL))completionHandler { completionHandler:(void (^)(BOOL))completionHandler {
auto generatedPasswordInjected = ^(BOOL success) { auto generatedPasswordInjected = ^(BOOL success) {
auto passwordPresaved = ^(BOOL found, const autofill::FormData& form) {
if (found) {
// TODO(crbug.com/886583): set is_manually_triggered when we show a
// prompt.
self.passwordManager->PresaveGeneratedPassword(
self.passwordManagerDriver, form,
SysNSStringToUTF16(generatedPassword),
SysNSStringToUTF16(newPasswordIdentifier),
/* is_manually_triggered */ false);
}
// If the form isn't found, it disappeared between fillPasswordForm below
// and here. There isn't much that can be done.
};
if (success) { if (success) {
// TODO(crbug.com/886583) call _pM::OnPresaveGeneratedPassword once it has [self.formHelper extractPasswordFormData:formName
// been refactored not to need a full form. completionHandler:passwordPresaved];
self.isPasswordGenerated = YES; self.isPasswordGenerated = YES;
self.passwordGeneratedIdentifier = newPasswordIdentifier;
} }
if (completionHandler) if (completionHandler)
completionHandler(YES); completionHandler(YES);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment