Skip to content

Commit

Permalink
Bugfix FXIOS-10560 - Password Generator: Keyboard Closing After Acces…
Browse files Browse the repository at this point in the history
…sing Pass Gen (backport #23305) (#23382)

Bugfix FXIOS-10560 - Password Generator: Keyboard Closing After Accessing Pass Gen (#23305)

* Keyboard dismissal bug fix

* Completed ticket

* swiftlint changes

* Addressed comments

(cherry picked from commit 4d19dac)

Co-authored-by: Daniel Dervishi <[email protected]>
  • Loading branch information
mergify[bot] and DanielDervishi authored Nov 25, 2024
1 parent bbf9121 commit 25ff72d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
3 changes: 3 additions & 0 deletions BrowserKit/Sources/Common/Logger/LoggerCategory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,7 @@ public enum LoggerCategory: String {

/// Remote settings
case remoteSettings

/// Password Generator
case passwordGenerator
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,29 @@ final class PasswordGeneratorMiddleware {

private func userTappedUsePassword(frame: WKFrameInfo, password: String) {
passwordGeneratorTelemetry.usePasswordButtonPressed()
let jsFunctionCall = "window.__firefox__.logins.fillGeneratedPassword(\"\(password)\")"
frame.webView?.evaluateJavascriptInDefaultContentWorld(jsFunctionCall, frame) { (result, error) in
if error != nil {
self.logger.log("Error filling in password info",
level: .warning,
category: .webview)
if let escapedPassword = escapeString(string: password) {
let jsFunctionCall = "window.__firefox__.logins.fillGeneratedPassword(\(escapedPassword))"
frame.webView?.evaluateJavascriptInDefaultContentWorld(jsFunctionCall, frame) { (result, error) in
if error != nil {
self.logger.log("Error filling in password info",
level: .warning,
category: .passwordGenerator)
}
}
}
}

private func escapeString(string: String) -> String? {
guard let jsonData = try? JSONEncoder().encode(string),
let jsonString = String(data: jsonData, encoding: .utf8) else {
self.logger.log("Error encoding generated password to JSON",
level: .warning,
category: .passwordGenerator)
return nil
}
return jsonString
}

private func userTappedRefreshPassword(frame: WKFrameInfo, windowUUID: WindowUUID) {
guard let origin = frame.webView?.url?.origin else {return}
generateNewPassword(frame: frame, completion: { generatedPassword in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,5 +234,7 @@ class PasswordGeneratorViewController: UIViewController, StoreSubscriber, Themea
}

extension PasswordGeneratorViewController: BottomSheetChild {
func willDismiss() { currentTab.webView?.accessoryView.reloadViewFor(.standard)}
func willDismiss() {
LoginsHelper.yieldFocusBackToField(with: currentTab)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -468,11 +468,13 @@ window.__firefox__.includeOnce("LoginsHelper", function() {
};

function fillGeneratedPassword(password) {
LoginManagerContent.fromFill = true
this.yieldFocusBackToField();
const passwordField = LoginManagerContent.activeField;
passwordField?.setUserInput(password);
const confirmationField = Logic.findConfirmationField(passwordField, LoginFormFactory);
confirmationField?.setUserInput(password);
LoginManagerContent.fromFill = false
}

function yieldFocusBackToField() {
Expand All @@ -499,7 +501,7 @@ window.__firefox__.includeOnce("LoginsHelper", function() {
const isPasswordField = field === password;
const isYieldingFocus = LoginManagerContent.activeField === field;
LoginManagerContent.activeField = field;
if (formHasNewPassword && isPasswordField && !isYieldingFocus) {
if (formHasNewPassword && isPasswordField && !LoginManagerContent.fromFill) {
webkit.messageHandlers.loginsManagerMessageHandler.postMessage({
type: "generatePassword",
});
Expand Down

0 comments on commit 25ff72d

Please sign in to comment.