// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
 * @fileoverview PasswordManagerProxy is an abstraction over
 * chrome.passwordsPrivate and a Mojo remote. It is intended to facilitate
 * testing. The chrome.passwordsPrivate API is being migrated to use Mojo.
 */
import { loadTimeData } from 'chrome://resources/js/load_time_data.js';
import { ExportPasswordsResult, ExportProgressStatus, PageCallbackRouter, PageHandlerFactory, PageHandlerRemote, PasswordManagerActionableError } from './password_manager.mojom-webui.js';
export { ExportPasswordsResult, ExportProgressStatus, PageCallbackRouter, PageHandlerFactory, PageHandlerRemote, PasswordManagerActionableError, };
/**
 * Represents different interactions the user can perform on the Password Check
 * page.
 *
 * These values are persisted to logs. Entries should not be renumbered and
 * numeric values should never be reused.
 *
 * Needs to stay in sync with PasswordCheckInteraction in enums.xml.
 */
export var PasswordCheckInteraction;
(function (PasswordCheckInteraction) {
    PasswordCheckInteraction[PasswordCheckInteraction["START_CHECK_AUTOMATICALLY"] = 0] = "START_CHECK_AUTOMATICALLY";
    PasswordCheckInteraction[PasswordCheckInteraction["START_CHECK_MANUALLY"] = 1] = "START_CHECK_MANUALLY";
    PasswordCheckInteraction[PasswordCheckInteraction["STOP_CHECK"] = 2] = "STOP_CHECK";
    PasswordCheckInteraction[PasswordCheckInteraction["CHANGE_PASSWORD"] = 3] = "CHANGE_PASSWORD";
    PasswordCheckInteraction[PasswordCheckInteraction["EDIT_PASSWORD"] = 4] = "EDIT_PASSWORD";
    PasswordCheckInteraction[PasswordCheckInteraction["REMOVE_PASSWORD"] = 5] = "REMOVE_PASSWORD";
    PasswordCheckInteraction[PasswordCheckInteraction["SHOW_PASSWORD"] = 6] = "SHOW_PASSWORD";
    PasswordCheckInteraction[PasswordCheckInteraction["MUTE_PASSWORD"] = 7] = "MUTE_PASSWORD";
    PasswordCheckInteraction[PasswordCheckInteraction["UNMUTE_PASSWORD"] = 8] = "UNMUTE_PASSWORD";
    PasswordCheckInteraction[PasswordCheckInteraction["CHANGE_PASSWORD_AUTOMATICALLY"] = 9] = "CHANGE_PASSWORD_AUTOMATICALLY";
    // Must be last.
    PasswordCheckInteraction[PasswordCheckInteraction["COUNT"] = 10] = "COUNT";
})(PasswordCheckInteraction || (PasswordCheckInteraction = {}));
/**
 * Should be kept in sync with PasswordViewPageInteractions in enums.xml.
 * These values are persisted to logs. Entries should not be renumbered and
 * numeric values should never be reused.
 */
export var PasswordViewPageInteractions;
(function (PasswordViewPageInteractions) {
    PasswordViewPageInteractions[PasswordViewPageInteractions["CREDENTIAL_ROW_CLICKED"] = 0] = "CREDENTIAL_ROW_CLICKED";
    PasswordViewPageInteractions[PasswordViewPageInteractions["CREDENTIAL_FOUND"] = 1] = "CREDENTIAL_FOUND";
    PasswordViewPageInteractions[PasswordViewPageInteractions["CREDENTIAL_NOT_FOUND"] = 2] = "CREDENTIAL_NOT_FOUND";
    PasswordViewPageInteractions[PasswordViewPageInteractions["USERNAME_COPY_BUTTON_CLICKED"] = 3] = "USERNAME_COPY_BUTTON_CLICKED";
    PasswordViewPageInteractions[PasswordViewPageInteractions["PASSWORD_COPY_BUTTON_CLICKED"] = 4] = "PASSWORD_COPY_BUTTON_CLICKED";
    PasswordViewPageInteractions[PasswordViewPageInteractions["PASSWORD_SHOW_BUTTON_CLICKED"] = 5] = "PASSWORD_SHOW_BUTTON_CLICKED";
    PasswordViewPageInteractions[PasswordViewPageInteractions["PASSWORD_EDIT_BUTTON_CLICKED"] = 6] = "PASSWORD_EDIT_BUTTON_CLICKED";
    PasswordViewPageInteractions[PasswordViewPageInteractions["PASSWORD_DELETE_BUTTON_CLICKED"] = 7] = "PASSWORD_DELETE_BUTTON_CLICKED";
    PasswordViewPageInteractions[PasswordViewPageInteractions["CREDENTIAL_EDITED"] = 8] = "CREDENTIAL_EDITED";
    PasswordViewPageInteractions[PasswordViewPageInteractions["TIMED_OUT_IN_EDIT_DIALOG"] = 9] = "TIMED_OUT_IN_EDIT_DIALOG";
    PasswordViewPageInteractions[PasswordViewPageInteractions["TIMED_OUT_IN_VIEW_PAGE"] = 10] = "TIMED_OUT_IN_VIEW_PAGE";
    PasswordViewPageInteractions[PasswordViewPageInteractions["CREDENTIAL_REQUESTED_BY_URL"] = 11] = "CREDENTIAL_REQUESTED_BY_URL";
    PasswordViewPageInteractions[PasswordViewPageInteractions["PASSKEY_DISPLAY_NAME_COPY_BUTTON_CLICKED"] = 12] = "PASSKEY_DISPLAY_NAME_COPY_BUTTON_CLICKED";
    PasswordViewPageInteractions[PasswordViewPageInteractions["PASSKEY_DELETE_BUTTON_CLICKED"] = 13] = "PASSKEY_DELETE_BUTTON_CLICKED";
    PasswordViewPageInteractions[PasswordViewPageInteractions["PASSKEY_EDIT_BUTTON_CLICKED"] = 14] = "PASSKEY_EDIT_BUTTON_CLICKED";
    // Must be last.
    PasswordViewPageInteractions[PasswordViewPageInteractions["COUNT"] = 15] = "COUNT";
})(PasswordViewPageInteractions || (PasswordViewPageInteractions = {}));
/**
 * Maps chrome.passwordsPrivate.ExportProgressStatus to
 * password_manager.mojom.ExportProgressStatus.
 */
export function toMojoExportProgressStatus(status) {
    const PrivateStatus = chrome.passwordsPrivate.ExportProgressStatus;
    switch (status) {
        case PrivateStatus.NOT_STARTED:
            return ExportProgressStatus.kNotStarted;
        case PrivateStatus.IN_PROGRESS:
            return ExportProgressStatus.kInProgress;
        case PrivateStatus.SUCCEEDED:
            return ExportProgressStatus.kSucceeded;
        case PrivateStatus.FAILED_CANCELLED:
            return ExportProgressStatus.kFailed;
        case PrivateStatus.FAILED_WRITE_FAILED:
            return ExportProgressStatus.kFailedWrite;
        default:
            return ExportProgressStatus.kNotStarted;
    }
}
/**
 * Maps chrome.passwordsPrivate.PasswordManagerActionableError to
 * password_manager.mojom.PasswordManagerActionableError.
 */
export function toMojoActionableError(error) {
    const PrivateError = chrome.passwordsPrivate.PasswordManagerActionableError;
    switch (error) {
        case PrivateError.NO_ERROR:
            return PasswordManagerActionableError.kNoError;
        case PrivateError.INACTIONABLE:
            return PasswordManagerActionableError.kInactionable;
        case PrivateError.INACTIONABLE_TEMPORARY_ERROR:
            return PasswordManagerActionableError.kInactionableTemporaryError;
        case PrivateError.SIGN_IN_NEEDED:
            return PasswordManagerActionableError.kSignInNeeded;
        case PrivateError.KEYCHAIN_ERROR:
            return PasswordManagerActionableError.kKeychainError;
        case PrivateError.TRUSTED_VAULT_KEY_NEEDED:
            return PasswordManagerActionableError.kTrustedVaultKeyNeeded;
        case PrivateError.NEEDS_PASSPHRASE:
            return PasswordManagerActionableError.kNeedsPassphrase;
        default:
            return PasswordManagerActionableError.kNoError;
    }
}
/**
 * Implementation that accesses the private API.
 */
export class PasswordManagerImpl {
    callbackRouter = new PageCallbackRouter();
    handler = new PageHandlerRemote();
    constructor() {
        const factory = PageHandlerFactory.getRemote();
        factory.createPageHandler(this.callbackRouter.$.bindNewPipeAndPassRemote(), this.handler.$.bindNewPipeAndPassReceiver());
    }
    addSavedPasswordListChangedListener(listener) {
        chrome.passwordsPrivate.onSavedPasswordsListChanged.addListener(listener);
    }
    removeSavedPasswordListChangedListener(listener) {
        chrome.passwordsPrivate.onSavedPasswordsListChanged.removeListener(listener);
    }
    addBlockedSitesListChangedListener(listener) {
        chrome.passwordsPrivate.onPasswordExceptionsListChanged.addListener(listener);
    }
    removeBlockedSitesListChangedListener(listener) {
        chrome.passwordsPrivate.onPasswordExceptionsListChanged.removeListener(listener);
    }
    addPasswordCheckStatusListener(listener) {
        chrome.passwordsPrivate.onPasswordCheckStatusChanged.addListener(listener);
    }
    removePasswordCheckStatusListener(listener) {
        chrome.passwordsPrivate.onPasswordCheckStatusChanged.removeListener(listener);
    }
    addInsecureCredentialsListener(listener) {
        chrome.passwordsPrivate.onInsecureCredentialsChanged.addListener(listener);
    }
    removeInsecureCredentialsListener(listener) {
        chrome.passwordsPrivate.onInsecureCredentialsChanged.removeListener(listener);
    }
    getSavedPasswordList() {
        return chrome.passwordsPrivate.getSavedPasswordList().catch(() => []);
    }
    getCredentialGroups() {
        return chrome.passwordsPrivate.getCredentialGroups();
    }
    getBlockedSitesList() {
        return chrome.passwordsPrivate.getPasswordExceptionList().catch(() => []);
    }
    getPasswordCheckStatus() {
        return chrome.passwordsPrivate.getPasswordCheckStatus();
    }
    getInsecureCredentials() {
        return chrome.passwordsPrivate.getInsecureCredentials();
    }
    getCredentialsWithReusedPassword() {
        return chrome.passwordsPrivate.getCredentialsWithReusedPassword();
    }
    startBulkPasswordCheck() {
        if (!loadTimeData.getBoolean('enablePasswordManagerMojoApi')) {
            chrome.passwordsPrivate.startPasswordCheck().catch(() => { });
            return;
        }
        this.handler.startBulkPasswordCheck();
    }
    recordPasswordCheckInteraction(interaction) {
        chrome.metricsPrivate.recordEnumerationValue('PasswordManager.BulkCheck.UserAction', interaction, PasswordCheckInteraction.COUNT);
    }
    recordPasswordViewInteraction(interaction) {
        chrome.metricsPrivate.recordEnumerationValue('PasswordManager.PasswordViewPage.UserActions', interaction, PasswordViewPageInteractions.COUNT);
    }
    showAddShortcutDialog() {
        this.handler.showAddShortcutDialog();
    }
    requestCredentialsDetails(ids) {
        return chrome.passwordsPrivate.requestCredentialsDetails(ids);
    }
    requestPlaintextPassword(id, reason) {
        return chrome.passwordsPrivate.requestPlaintextPassword(id, reason);
    }
    copyPlaintextBackupPassword(id) {
        return this.handler.copyPlaintextBackupPassword(id).then(result => result.success);
    }
    addPassword(options) {
        return chrome.passwordsPrivate.addPassword(options);
    }
    changeCredential(credential) {
        return chrome.passwordsPrivate.changeCredential(credential);
    }
    removeCredential(id, fromStores) {
        chrome.passwordsPrivate.removeCredential(id, fromStores);
    }
    removeBackupPassword(id) {
        this.handler.removeBackupPassword(id);
    }
    removeBlockedSite(id) {
        if (!loadTimeData.getBoolean('enablePasswordManagerMojoApi')) {
            chrome.passwordsPrivate.removePasswordException(id);
            return;
        }
        this.handler.removePasswordException(id);
    }
    muteInsecureCredential(insecureCredential) {
        chrome.passwordsPrivate.muteInsecureCredential(insecureCredential);
    }
    unmuteInsecureCredential(insecureCredential) {
        chrome.passwordsPrivate.unmuteInsecureCredential(insecureCredential);
    }
    undoRemoveSavedPasswordOrException() {
        if (!loadTimeData.getBoolean('enablePasswordManagerMojoApi')) {
            chrome.passwordsPrivate.undoRemoveSavedPasswordOrException();
            return;
        }
        this.handler.undoRemoveSavedPasswordOrException();
    }
    fetchFamilyMembers() {
        return chrome.passwordsPrivate.fetchFamilyMembers();
    }
    sharePassword(id, recipients) {
        chrome.passwordsPrivate.sharePassword(id, recipients);
    }
    importPasswords(toStore) {
        return chrome.passwordsPrivate.importPasswords(toStore);
    }
    continueImport(selectedIds) {
        return chrome.passwordsPrivate.continueImport(selectedIds);
    }
    resetImporter(deleteFile) {
        if (!loadTimeData.getBoolean('enablePasswordManagerMojoApi')) {
            return chrome.passwordsPrivate.resetImporter(deleteFile);
        }
        return this.handler.resetImporter(deleteFile).then(() => { });
    }
    requestExportProgressStatus() {
        if (!loadTimeData.getBoolean('enablePasswordManagerMojoApi')) {
            return chrome.passwordsPrivate.requestExportProgressStatus().then(status => toMojoExportProgressStatus(status));
        }
        return this.handler.getPasswordsExportProgress().then(response => response.status);
    }
    exportPasswords() {
        if (!loadTimeData.getBoolean('enablePasswordManagerMojoApi')) {
            return chrome.passwordsPrivate.exportPasswords()
                .then(() => ExportPasswordsResult.kSuccess)
                .catch((error) => {
                const errorMessage = error instanceof Error ? error.message : error;
                if (errorMessage === 'in-progress') {
                    return ExportPasswordsResult.kInProgress;
                }
                if (errorMessage === 'reauth-failed') {
                    return ExportPasswordsResult.kReauthFailed;
                }
                throw error;
            });
        }
        return this.handler.requestPasswordsExport().then(({ result }) => result);
    }
    addPasswordsFileExportProgressListener(listener) {
        chrome.passwordsPrivate.onPasswordsFileExportProgress.addListener(listener);
    }
    removePasswordsFileExportProgressListener(listener) {
        chrome.passwordsPrivate.onPasswordsFileExportProgress.removeListener(listener);
    }
    switchBiometricAuthBeforeFillingState() {
        return this.handler.switchBiometricAuthBeforeFillingState().then(result => result.success);
    }
    showLastExportedFileInShell() {
        this.handler.showLastExportedFileInShell();
    }
    getUrlCollection(url) {
        return chrome.passwordsPrivate.getUrlCollection(url);
    }
    addPasswordManagerAuthTimeoutListener(listener) {
        chrome.passwordsPrivate.onPasswordManagerAuthTimeout.addListener(listener);
    }
    removePasswordManagerAuthTimeoutListener(listener) {
        chrome.passwordsPrivate.onPasswordManagerAuthTimeout.removeListener(listener);
    }
    addPasswordManagerActionableErrorChangedListener(listener) {
        chrome.passwordsPrivate.onPasswordManagerActionableErrorChanged.addListener(listener);
    }
    removePasswordManagerActionableErrorChangedListener(listener) {
        chrome.passwordsPrivate.onPasswordManagerActionableErrorChanged
            .removeListener(listener);
    }
    extendAuthValidity() {
        this.handler.extendAuthValidity();
    }
    addAccountStorageEnabledStateListener(listener) {
        chrome.passwordsPrivate.onAccountStorageActiveStateChanged.addListener(listener);
    }
    removeAccountStorageEnabledStateListener(listener) {
        chrome.passwordsPrivate.onAccountStorageActiveStateChanged.removeListener(listener);
    }
    addShouldShowAccountStorageSettingToggleListener(listener) {
        chrome.passwordsPrivate.onShouldShowAccountStorageSettingToggleChanged
            .addListener(listener);
    }
    removeShouldShowAccountStorageSettingToggleListener(listener) {
        chrome.passwordsPrivate.onShouldShowAccountStorageSettingToggleChanged
            .removeListener(listener);
    }
    isAccountStorageActive() {
        return this.handler.isAccountStorageActive().then(result => result.active);
    }
    setAccountStorageEnabled(enabled) {
        this.handler.setAccountStorageEnabled(enabled);
    }
    shouldShowAccountStorageSettingToggle() {
        return this.handler.shouldShowAccountStorageSettingToggle().then(result => result.shouldShow);
    }
    movePasswordsToAccount(ids) {
        if (!loadTimeData.getBoolean('enablePasswordManagerMojoApi')) {
            chrome.passwordsPrivate.movePasswordsToAccount(ids);
            return;
        }
        this.handler.movePasswordsToAccount(ids);
    }
    dismissSafetyHubPasswordMenuNotification() {
        chrome.send('dismissSafetyHubPasswordMenuNotification');
    }
    changePasswordManagerPin() {
        return this.handler.changePasswordManagerPin().then(result => result.success);
    }
    isPasswordManagerPinAvailable() {
        return this.handler.isPasswordManagerPinAvailable().then(result => result.isAvailable);
    }
    disconnectCloudAuthenticator() {
        if (!loadTimeData.getBoolean('enablePasswordManagerMojoApi')) {
            return chrome.passwordsPrivate.disconnectCloudAuthenticator();
        }
        return this.handler.disconnectCloudAuthenticator().then(result => result.success);
    }
    isConnectedToCloudAuthenticator() {
        if (!loadTimeData.getBoolean('enablePasswordManagerMojoApi')) {
            return chrome.passwordsPrivate.isConnectedToCloudAuthenticator();
        }
        return this.handler.isConnectedToCloudAuthenticator().then(result => result.connected);
    }
    deleteAllPasswordManagerData() {
        return this.handler.deleteAllPasswordManagerData().then(result => result.success);
    }
    getActorLoginPermissions() {
        return this.handler.getActorLoginPermissions().then(result => result.sites);
    }
    revokeActorLoginPermission(site) {
        this.handler.revokeActorLoginPermission(site);
    }
    requestChangePassword(credentialId) {
        this.handler.startPasswordChange(credentialId);
    }
    getPasswordManagerActionableError() {
        return this.handler.getPasswordManagerActionableError().then(result => result.error);
    }
    static getInstance() {
        return instance || (instance = new PasswordManagerImpl());
    }
    static setInstance(proxy) {
        instance = proxy;
    }
}
let instance = null;
