Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New autofill save & update password prompt pixels for alignment with iOS #2801

Merged
merged 5 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions DuckDuckGo/SecureVault/View/SaveCredentialsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@
@IBOutlet var fireproofCheck: NSButton!
@IBOutlet weak var fireproofCheckDescription: NSTextFieldCell!

private enum Action {
case displayed
case confirmed
case dismissed
}

weak var delegate: SaveCredentialsDelegate?

private var credentials: SecureVaultModels.WebsiteCredentials?
Expand Down Expand Up @@ -139,6 +145,9 @@
// Only use the non-editable state if a credential was automatically saved and it didn't already exist.
let condition = credentials.account.id != nil && !(credentials.account.username?.isEmpty ?? true) && automaticallySaved
updateViewState(editable: !condition)

let existingCredentials = getExistingCredentialsFrom(credentials)
firePixels(for: .displayed, credentials: existingCredentials)
amddg44 marked this conversation as resolved.
Show resolved Hide resolved
}

private func updateViewState(editable: Bool) {
Expand Down Expand Up @@ -208,6 +217,7 @@
domain: domainLabel.stringValue)
account.id = credentials?.account.id
let credentials = SecureVaultModels.WebsiteCredentials(account: account, password: passwordData)
var existingCredentials = getExistingCredentialsFrom(credentials)

Check warning on line 220 in DuckDuckGo/SecureVault/View/SaveCredentialsViewController.swift

View workflow job for this annotation

GitHub Actions / Make Release Build

variable 'existingCredentials' was never mutated; consider changing to 'let' constant

Check warning on line 220 in DuckDuckGo/SecureVault/View/SaveCredentialsViewController.swift

View workflow job for this annotation

GitHub Actions / Make Release Build

variable 'existingCredentials' was never mutated; consider changing to 'let' constant

Check warning on line 220 in DuckDuckGo/SecureVault/View/SaveCredentialsViewController.swift

View workflow job for this annotation

GitHub Actions / Test (Sandbox)

variable 'existingCredentials' was never mutated; consider changing to 'let' constant

Check warning on line 220 in DuckDuckGo/SecureVault/View/SaveCredentialsViewController.swift

View workflow job for this annotation

GitHub Actions / Test (Sandbox)

variable 'existingCredentials' was never mutated; consider changing to 'let' constant

Check warning on line 220 in DuckDuckGo/SecureVault/View/SaveCredentialsViewController.swift

View workflow job for this annotation

GitHub Actions / Test (Non-Sandbox)

variable 'existingCredentials' was never mutated; consider changing to 'let' constant
amddg44 marked this conversation as resolved.
Show resolved Hide resolved

do {
if passwordManagerCoordinator.isEnabled {
Expand All @@ -231,6 +241,8 @@
PixelKit.fire(DebugEvent(GeneralPixel.secureVaultError(error: error)))
}

firePixels(for: .confirmed, credentials: existingCredentials)

PixelKit.fire(GeneralPixel.autofillItemSaved(kind: .password))

if passwordManagerCoordinator.isEnabled {
Expand All @@ -250,6 +262,9 @@

@IBAction func onDontUpdateClicked(_ sender: Any) {
delegate?.shouldCloseSaveCredentialsViewController(self)

let existingCredentials = getExistingCredentialsFrom(credentials)
firePixels(for: .dismissed, credentials: existingCredentials)
}

@IBAction func onNotNowSegmentedControlClicked(_ sender: Any) {
Expand Down Expand Up @@ -280,6 +295,9 @@
delegate?.shouldCloseSaveCredentialsViewController(self)
}

let existingCredentials = getExistingCredentialsFrom(credentials)
firePixels(for: .dismissed, credentials: existingCredentials)

guard DataClearingPreferences.shared.isLoginDetectionEnabled else {
notifyDelegate()
return
Expand Down Expand Up @@ -365,4 +383,90 @@
}
}

private func getExistingCredentialsFrom(_ credentials: SecureVaultModels.WebsiteCredentials?) -> SecureVaultModels.WebsiteCredentials? {
guard let credentials = credentials, let id = credentials.account.id else {
return nil
}

var existingCredentials: SecureVaultModels.WebsiteCredentials?

if passwordManagerCoordinator.isEnabled {
guard !passwordManagerCoordinator.isLocked else {
os_log("Failed to access credentials: Password manager is locked")
return existingCredentials
}

passwordManagerCoordinator.websiteCredentialsFor(accountId: id) { credentials, _ in
existingCredentials = credentials
}
} else {
if let idInt = Int64(id) {
existingCredentials = try? AutofillSecureVaultFactory.makeVault(reporter: SecureVaultReporter.shared).websiteCredentialsFor(accountId: idInt)
}
}

return existingCredentials
}

private func isUsernameUpdated(credentials: SecureVaultModels.WebsiteCredentials) -> Bool {
if credentials.account.username != self.usernameField.stringValue.trimmingWhitespace() {
return true
}
return false
}

private func isPasswordUpdated(credentials: SecureVaultModels.WebsiteCredentials) -> Bool {
if credentials.password != self.passwordData {
return true
}
return false
}

private func firePixels(for action: Action, credentials: SecureVaultModels.WebsiteCredentials?) {
switch action {
case .displayed:
if let credentials = credentials {
if isPasswordUpdated(credentials: credentials) {
PixelKit.fire(GeneralPixel.autofillLoginsUpdatePasswordInlineDisplayed)
} else {
PixelKit.fire(GeneralPixel.autofillLoginsUpdateUsernameInlineDisplayed)
}
} else {
if usernameField.stringValue.trimmingWhitespace().isEmpty {
PixelKit.fire(GeneralPixel.autofillLoginsSavePasswordInlineDisplayed)
} else {
PixelKit.fire(GeneralPixel.autofillLoginsSaveLoginInlineDisplayed)
}
}
case .confirmed, .dismissed:
if let credentials = credentials {
if isUsernameUpdated(credentials: credentials) {
firePixel(for: action,
confirmedPixel: GeneralPixel.autofillLoginsUpdateUsernameInlineConfirmed,
dismissedPixel: GeneralPixel.autofillLoginsUpdateUsernameInlineDismissed)
}
if isPasswordUpdated(credentials: credentials) {
firePixel(for: action,
confirmedPixel: GeneralPixel.autofillLoginsUpdatePasswordInlineConfirmed,
dismissedPixel: GeneralPixel.autofillLoginsUpdatePasswordInlineDismissed)
}
} else {
if usernameField.stringValue.trimmingWhitespace().isEmpty {
firePixel(for: action,
confirmedPixel: GeneralPixel.autofillLoginsSavePasswordInlineConfirmed,
dismissedPixel: GeneralPixel.autofillLoginsSavePasswordInlineDismissed)
} else {
firePixel(for: action,
confirmedPixel: GeneralPixel.autofillLoginsSaveLoginInlineConfirmed,
dismissedPixel: GeneralPixel.autofillLoginsSaveLoginInlineDismissed)
}
}
}
}

private func firePixel(for action: Action, confirmedPixel: PixelKitEventV2, dismissedPixel: PixelKitEventV2) {
let pixel = action == .confirmed ? confirmedPixel : dismissedPixel
PixelKit.fire(pixel)
}

}
44 changes: 44 additions & 0 deletions DuckDuckGo/Statistics/GeneralPixel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,27 @@ enum GeneralPixel: PixelKitEventV2 {
case formAutofilled(kind: FormAutofillKind)
case autofillItemSaved(kind: FormAutofillKind)

case autofillLoginsSaveLoginInlineDisplayed
case autofillLoginsSaveLoginInlineConfirmed
case autofillLoginsSaveLoginInlineDismissed

case autofillLoginsSavePasswordInlineDisplayed
case autofillLoginsSavePasswordInlineConfirmed
case autofillLoginsSavePasswordInlineDismissed

case autofillLoginsSaveLoginModalExcludeSiteConfirmed
case autofillLoginsSettingsResetExcludedDisplayed
case autofillLoginsSettingsResetExcludedConfirmed
case autofillLoginsSettingsResetExcludedDismissed

case autofillLoginsUpdatePasswordInlineDisplayed
case autofillLoginsUpdatePasswordInlineConfirmed
case autofillLoginsUpdatePasswordInlineDismissed

case autofillLoginsUpdateUsernameInlineDisplayed
case autofillLoginsUpdateUsernameInlineConfirmed
case autofillLoginsUpdateUsernameInlineDismissed

case bitwardenPasswordAutofilled
case bitwardenPasswordSaved

Expand Down Expand Up @@ -361,6 +377,20 @@ enum GeneralPixel: PixelKitEventV2 {
case .autofillItemSaved(kind: let kind):
return "m_mac_save_\(kind)"

case .autofillLoginsSaveLoginInlineDisplayed:
return "m_mac_autofill_logins_save_login_inline_displayed"
case .autofillLoginsSaveLoginInlineConfirmed:
return "m_mac_autofill_logins_save_login_inline_confirmed"
case .autofillLoginsSaveLoginInlineDismissed:
return "m_mac_autofill_logins_save_login_inline_dismissed"

case .autofillLoginsSavePasswordInlineDisplayed:
return "m_mac_autofill_logins_save_password_inline_displayed"
case .autofillLoginsSavePasswordInlineConfirmed:
return "m_mac_autofill_logins_save_password_inline_confirmed"
case .autofillLoginsSavePasswordInlineDismissed:
return "m_mac_autofill_logins_save_password_inline_dismissed"

case .autofillLoginsSaveLoginModalExcludeSiteConfirmed:
return "m_mac_autofill_logins_save_login_exclude_site_confirmed"
case .autofillLoginsSettingsResetExcludedDisplayed:
Expand All @@ -370,6 +400,20 @@ enum GeneralPixel: PixelKitEventV2 {
case .autofillLoginsSettingsResetExcludedDismissed:
return "m_mac_autofill_settings_reset_excluded_dismissed"

case .autofillLoginsUpdatePasswordInlineDisplayed:
return "m_mac_autofill_logins_update_password_inline_displayed"
case .autofillLoginsUpdatePasswordInlineConfirmed:
return "m_mac_autofill_logins_update_password_inline_confirmed"
case .autofillLoginsUpdatePasswordInlineDismissed:
return "m_mac_autofill_logins_update_password_inline_dismissed"

case .autofillLoginsUpdateUsernameInlineDisplayed:
return "m_mac_autofill_logins_update_username_inline_displayed"
case .autofillLoginsUpdateUsernameInlineConfirmed:
return "m_mac_autofill_logins_update_username_inline_confirmed"
case .autofillLoginsUpdateUsernameInlineDismissed:
return "m_mac_autofill_logins_update_username_inline_dismissed"

case .bitwardenPasswordAutofilled:
return "m_mac_bitwarden_autofill_password"

Expand Down
Loading