1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/autofill/payments/webauthn_dialog_controller_impl.h"
#include "chrome/browser/ui/autofill/payments/webauthn_dialog.h"
#include "chrome/browser/ui/autofill/payments/webauthn_dialog_model.h"
#include "chrome/browser/ui/autofill/payments/webauthn_dialog_state.h"
#include "components/autofill/core/browser/payments/webauthn_callback_types.h"
#include "content/public/browser/web_contents.h"
namespace autofill {
WebauthnDialogControllerImpl::WebauthnDialogControllerImpl(content::Page& page)
: content::PageUserData<WebauthnDialogControllerImpl>(page) {
// WebauthnDialogControllerImpl is only for the outermost primary page.
DCHECK(page.IsPrimary());
}
WebauthnDialogControllerImpl::~WebauthnDialogControllerImpl() {
// This part of code is executed only if browser window is closed when the
// dialog is visible. In this case the controller is destroyed before
// WebauthnDialogView::dtor() being called, but the reference to
// controller is not reset. Need to reset via WebauthnDialogView::Hide()
// to avoid crash.
if (dialog_model_) {
dialog_model_->SetDialogState(WebauthnDialogState::kInactive);
}
}
void WebauthnDialogControllerImpl::ShowOfferDialog(
payments::PaymentsAutofillClient::WebauthnDialogCallback
offer_dialog_callback) {
DCHECK(!dialog_model_);
callback_ = std::move(offer_dialog_callback);
dialog_ = WebauthnDialog::CreateAndShow(this, WebauthnDialogState::kOffer);
dialog_model_ = dialog_->GetDialogModel();
}
void WebauthnDialogControllerImpl::ShowVerifyPendingDialog(
payments::PaymentsAutofillClient::WebauthnDialogCallback
verify_pending_dialog_callback) {
DCHECK(!dialog_model_);
callback_ = std::move(verify_pending_dialog_callback);
dialog_ =
WebauthnDialog::CreateAndShow(this, WebauthnDialogState::kVerifyPending);
dialog_model_ = dialog_->GetDialogModel();
}
bool WebauthnDialogControllerImpl::CloseDialog() {
if (!dialog_model_) {
return false;
}
dialog_model_->SetDialogState(WebauthnDialogState::kInactive);
return true;
}
void WebauthnDialogControllerImpl::UpdateDialog(
WebauthnDialogState dialog_state) {
dialog_model_->SetDialogState(dialog_state);
// TODO(crbug.com/40639086): Handle callback resetting for verify pending
// dialog. Right now this function should only be passed in
// WebauthnDialogState::kOfferError.
DCHECK_EQ(dialog_state, WebauthnDialogState::kOfferError);
callback_.Reset();
}
void WebauthnDialogControllerImpl::OnDialogClosed() {
dialog_model_ = nullptr;
dialog_ = nullptr;
callback_.Reset();
}
content::WebContents* WebauthnDialogControllerImpl::GetWebContents() {
return content::WebContents::FromRenderFrameHost(&page().GetMainDocument());
}
void WebauthnDialogControllerImpl::OnOkButtonClicked() {
// The OK button is available only when the dialog is in
// WebauthnDialogState::kOffer state.
DCHECK(callback_);
callback_.Run(WebauthnDialogCallbackType::kOfferAccepted);
dialog_model_->SetDialogState(WebauthnDialogState::kOfferPending);
}
void WebauthnDialogControllerImpl::OnCancelButtonClicked() {
switch (dialog_model_->dialog_state()) {
case WebauthnDialogState::kOffer:
case WebauthnDialogState::kOfferPending:
DCHECK(callback_);
callback_.Run(WebauthnDialogCallbackType::kOfferCancelled);
return;
case WebauthnDialogState::kVerifyPending:
DCHECK(callback_);
callback_.Run(WebauthnDialogCallbackType::kVerificationCancelled);
return;
case WebauthnDialogState::kUnknown:
case WebauthnDialogState::kInactive:
case WebauthnDialogState::kOfferError:
NOTREACHED();
}
}
PAGE_USER_DATA_KEY_IMPL(WebauthnDialogControllerImpl);
} // namespace autofill
|