Skip to content

Commit

Permalink
Modify autofill update credential pixel to include backfilled parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
CDRussell committed Dec 13, 2024
1 parent 969ed0e commit 0223949
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,7 @@ class BrowserTabFragment :
override suspend fun onCredentialsAvailableToSave(
currentUrl: String,
credentials: LoginCredentials,
wasUsernameBackfilled: Boolean,
) {
val username = credentials.username
val password = credentials.password
Expand All @@ -707,8 +708,8 @@ class BrowserTabFragment :
withContext(dispatchers.main()) {
when (matchType) {
ExactMatch -> Timber.w("Credentials already exist for %s", currentUrl)
UsernameMatch -> showAutofillDialogUpdatePassword(currentUrl, credentials)
UsernameMissing -> showAutofillDialogUpdateUsername(currentUrl, credentials)
UsernameMatch -> showAutofillDialogUpdatePassword(currentUrl, credentials, wasUsernameBackfilled)
UsernameMissing -> showAutofillDialogUpdateUsername(currentUrl, credentials, wasUsernameBackfilled)
NoMatch -> showAutofillDialogSaveCredentials(currentUrl, credentials)
UrlOnlyMatch -> showAutofillDialogSaveCredentials(currentUrl, credentials)
}
Expand Down Expand Up @@ -2778,22 +2779,24 @@ class BrowserTabFragment :
private fun showAutofillDialogUpdatePassword(
currentUrl: String,
credentials: LoginCredentials,
wasUsernameBackfilled: Boolean,
) {
val url = webView?.url ?: return
if (url != currentUrl) return

val dialog = credentialAutofillDialogFactory.autofillSavingUpdatePasswordDialog(url, credentials, tabId)
val dialog = credentialAutofillDialogFactory.autofillSavingUpdatePasswordDialog(url, credentials, tabId, wasUsernameBackfilled)
showDialogHidingPrevious(dialog, CredentialUpdateExistingCredentialsDialog.TAG)
}

private fun showAutofillDialogUpdateUsername(
currentUrl: String,
credentials: LoginCredentials,
wasUsernameBackfilled: Boolean,
) {
val url = webView?.url ?: return
if (url != currentUrl) return

val dialog = credentialAutofillDialogFactory.autofillSavingUpdateUsernameDialog(url, credentials, tabId)
val dialog = credentialAutofillDialogFactory.autofillSavingUpdateUsernameDialog(url, credentials, tabId, wasUsernameBackfilled)
showDialogHidingPrevious(dialog, CredentialUpdateExistingCredentialsDialog.TAG)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ interface CredentialAutofillDialogFactory {
url: String,
credentials: LoginCredentials,
tabId: String,
wasUsernameBackfilled: Boolean,
): DialogFragment

/**
Expand All @@ -224,6 +225,7 @@ interface CredentialAutofillDialogFactory {
url: String,
credentials: LoginCredentials,
tabId: String,
wasUsernameBackfilled: Boolean,
): DialogFragment

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,15 @@ interface Callback {
/**
* Called when there are login credentials available to be saved.
* When this is called, we'd typically want to prompt the user if they want to save the credentials.
*
* @param currentUrl The current URL of the webpage
* @param credentials The login credentials that can be saved
* @param wasUsernameBackfilled Whether the username was backfilled from a previous partial save
*/
suspend fun onCredentialsAvailableToSave(
currentUrl: String,
credentials: LoginCredentials,
wasUsernameBackfilled: Boolean,
)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ class AutofillStoredBackJavascriptInterface @Inject constructor(
}

is PromptToSave -> {
callback?.onCredentialsAvailableToSave(currentUrl, credentials)
callback?.onCredentialsAvailableToSave(currentUrl, credentials, wasUsernameBackfilled)
}

is UpdateSavedAutoLogin -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ interface NoOpAutofillCallback : Callback {
override suspend fun onCredentialsAvailableToSave(
currentUrl: String,
credentials: LoginCredentials,
wasUsernameBackfilled: Boolean,
) {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,29 @@ class CredentialAutofillDialogAndroidFactory @Inject constructor() : CredentialA
url: String,
credentials: LoginCredentials,
tabId: String,
wasUsernameBackfilled: Boolean,
): DialogFragment {
return AutofillUpdatingExistingCredentialsDialogFragment.instance(
url,
credentials,
tabId,
CredentialUpdateType.Password,
wasUsernameBackfilled = wasUsernameBackfilled,
)
}

override fun autofillSavingUpdateUsernameDialog(
url: String,
credentials: LoginCredentials,
tabId: String,
wasUsernameBackfilled: Boolean,
): DialogFragment {
return AutofillUpdatingExistingCredentialsDialogFragment.instance(
url,
credentials,
tabId,
CredentialUpdateType.Username,
wasUsernameBackfilled = wasUsernameBackfilled,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class AutofillUpdatingExistingCredentialsDialogFragment : BottomSheetDialogFragm
container: ViewGroup?,
savedInstanceState: Bundle?,
): View {
pixelNameDialogEvent(Shown)?.let { pixel.fire(it) }
pixelNameDialogEvent(Shown)?.let { pixel.fire(it, paramsForUpdateLoginPixel()) }

autofillFireproofDialogSuppressor.autofillSaveOrUpdateDialogVisibilityChanged(visible = true)

Expand Down Expand Up @@ -140,7 +140,7 @@ class AutofillUpdatingExistingCredentialsDialogFragment : BottomSheetDialogFragm
}

binding.updateCredentialsButton.setOnClickListener {
pixelNameDialogEvent(Updated)?.let { pixel.fire(it) }
pixelNameDialogEvent(Updated)?.let { pixel.fire(it, paramsForUpdateLoginPixel()) }

val result = Bundle().also {
it.putString(CredentialUpdateExistingCredentialsDialog.KEY_URL, originalUrl)
Expand Down Expand Up @@ -182,7 +182,7 @@ class AutofillUpdatingExistingCredentialsDialogFragment : BottomSheetDialogFragm

Timber.v("onCancel: AutofillUpdatingExistingCredentialsDialogFragment. User declined to update credentials")
autofillFireproofDialogSuppressor.autofillSaveOrUpdateDialogVisibilityChanged(visible = false)
pixelNameDialogEvent(Dismissed)?.let { pixel.fire(it) }
pixelNameDialogEvent(Dismissed)?.let { pixel.fire(it, paramsForUpdateLoginPixel()) }
}

private fun pixelNameDialogEvent(dialogEvent: DialogEvent): AutofillPixelNames? {
Expand All @@ -194,6 +194,10 @@ class AutofillUpdatingExistingCredentialsDialogFragment : BottomSheetDialogFragm
}
}

private fun paramsForUpdateLoginPixel(): Map<String, String> {
return mapOf(PIXEL_PARAM_WAS_USERNAME_BACKFILLED to getWasUsernameBackfilled().toString())
}

private interface DialogEvent {
object Shown : DialogEvent
object Dismissed : DialogEvent
Expand All @@ -205,6 +209,7 @@ class AutofillUpdatingExistingCredentialsDialogFragment : BottomSheetDialogFragm
private fun getOriginalUrl() = arguments?.getString(CredentialUpdateExistingCredentialsDialog.KEY_URL)!!
private fun getUpdateType() =
arguments?.getParcelable<CredentialUpdateType>(CredentialUpdateExistingCredentialsDialog.KEY_CREDENTIAL_UPDATE_TYPE)!!
private fun getWasUsernameBackfilled() = arguments?.getBoolean(KEY_WAS_USERNAME_BACKFILLED)!!

companion object {

Expand All @@ -213,6 +218,7 @@ class AutofillUpdatingExistingCredentialsDialogFragment : BottomSheetDialogFragm
credentials: LoginCredentials,
tabId: String,
credentialUpdateType: CredentialUpdateType,
wasUsernameBackfilled: Boolean,
): AutofillUpdatingExistingCredentialsDialogFragment {
val fragment = AutofillUpdatingExistingCredentialsDialogFragment()
fragment.arguments =
Expand All @@ -221,8 +227,12 @@ class AutofillUpdatingExistingCredentialsDialogFragment : BottomSheetDialogFragm
it.putParcelable(CredentialUpdateExistingCredentialsDialog.KEY_CREDENTIALS, credentials)
it.putString(CredentialUpdateExistingCredentialsDialog.KEY_TAB_ID, tabId)
it.putParcelable(CredentialUpdateExistingCredentialsDialog.KEY_CREDENTIAL_UPDATE_TYPE, credentialUpdateType)
it.putBoolean(KEY_WAS_USERNAME_BACKFILLED, wasUsernameBackfilled)
}
return fragment
}

private const val PIXEL_PARAM_WAS_USERNAME_BACKFILLED = "backfilled"
private const val KEY_WAS_USERNAME_BACKFILLED = "wasUsernameBackfilled"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ class AutofillStoredBackJavascriptInterfaceTest {
override suspend fun onCredentialsAvailableToSave(
currentUrl: String,
credentials: LoginCredentials,
wasUsernameBackfilled: Boolean,
) {
credentialsToSave = credentials
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class InlineBrowserAutofillTest {
override suspend fun onCredentialsAvailableToSave(
currentUrl: String,
credentials: LoginCredentials,
wasUsernameBackfilled: Boolean,
) {
}

Expand Down

0 comments on commit 0223949

Please sign in to comment.