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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
// 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/views/autofill/payments/webauthn_dialog_view.h"
#include "chrome/browser/ui/autofill/payments/webauthn_dialog_controller.h"
#include "chrome/browser/ui/autofill/payments/webauthn_dialog_model.h"
#include "chrome/browser/ui/autofill/payments/webauthn_dialog_state.h"
#include "chrome/browser/ui/tabs/public/tab_dialog_manager.h"
#include "chrome/browser/ui/tabs/public/tab_features.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "chrome/browser/ui/views/webauthn/authenticator_request_sheet_view.h"
#include "chrome/browser/ui/views/webauthn/sheet_view_factory.h"
#include "chrome/browser/ui/webauthn/authenticator_request_sheet_model.h"
#include "components/constrained_window/constrained_window_views.h"
#include "components/tabs/public/tab_interface.h"
#include "components/web_modal/web_contents_modal_dialog_host.h"
#include "components/web_modal/web_contents_modal_dialog_manager.h"
#include "components/web_modal/web_contents_modal_dialog_manager_delegate.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/mojom/dialog_button.mojom.h"
#include "ui/base/mojom/ui_base_types.mojom-shared.h"
#include "ui/views/controls/button/label_button.h"
namespace autofill {
using AcceptButtonState = AuthenticatorRequestSheetModel::AcceptButtonState;
WebauthnDialogView::WebauthnDialogView(WebauthnDialogController* controller,
WebauthnDialogState dialog_state)
: controller_(controller) {
SetShowTitle(false);
SetUseDefaultFillLayout(true);
std::unique_ptr<WebauthnDialogModel> model =
std::make_unique<WebauthnDialogModel>(dialog_state);
model_ = model.get();
model_->AddObserver(this);
sheet_view_ =
AddChildView(CreateSheetViewForAutofillWebAuthn(std::move(model)));
sheet_view_->ReInitChildViews();
SetModalType(ui::mojom::ModalType::kChild);
SetShowCloseButton(false);
set_fixed_width(views::LayoutProvider::Get()->GetDistanceMetric(
views::DISTANCE_MODAL_DIALOG_PREFERRED_WIDTH));
SetButtonLabel(ui::mojom::DialogButton::kOk, model_->GetAcceptButtonLabel());
SetButtonLabel(ui::mojom::DialogButton::kCancel,
model_->GetCancelButtonLabel());
SetButtons(model_->GetAcceptButtonState() != AcceptButtonState::kNotVisible
? static_cast<int>(ui::mojom::DialogButton::kOk) |
static_cast<int>(ui::mojom::DialogButton::kCancel)
: static_cast<int>(ui::mojom::DialogButton::kCancel));
}
WebauthnDialogView::~WebauthnDialogView() {
model_->RemoveObserver(this);
if (controller_) {
controller_->OnDialogClosed();
controller_ = nullptr;
}
}
// static
WebauthnDialog* WebauthnDialog::CreateAndShow(
WebauthnDialogController* controller,
WebauthnDialogState dialog_state) {
auto* dialog_delegate = new WebauthnDialogView(controller, dialog_state);
tabs::TabInterface* tab_interface =
tabs::TabInterface::GetFromContents(controller->GetWebContents());
tab_interface->GetTabFeatures()
->tab_dialog_manager()
->CreateShowDialogAndBlockTabInteraction(dialog_delegate)
.release();
return dialog_delegate;
}
WebauthnDialogModel* WebauthnDialogView::GetDialogModel() const {
return model_;
}
void WebauthnDialogView::OnDialogStateChanged() {
switch (model_->dialog_state()) {
case WebauthnDialogState::kInactive:
Hide();
break;
case WebauthnDialogState::kOfferPending:
case WebauthnDialogState::kOfferError:
case WebauthnDialogState::kVerifyPending:
RefreshContent();
break;
case WebauthnDialogState::kUnknown:
case WebauthnDialogState::kOffer:
NOTREACHED();
}
}
bool WebauthnDialogView::Accept() {
DCHECK_EQ(model_->dialog_state(), WebauthnDialogState::kOffer);
controller_->OnOkButtonClicked();
return false;
}
bool WebauthnDialogView::Cancel() {
if (model_->dialog_state() == WebauthnDialogState::kOffer ||
model_->dialog_state() == WebauthnDialogState::kOfferPending ||
model_->dialog_state() == WebauthnDialogState::kVerifyPending) {
controller_->OnCancelButtonClicked();
}
return true;
}
bool WebauthnDialogView::IsDialogButtonEnabled(
ui::mojom::DialogButton button) const {
return button == ui::mojom::DialogButton::kOk
? model_->GetAcceptButtonState() == AcceptButtonState::kEnabled
: true;
}
std::u16string WebauthnDialogView::GetWindowTitle() const {
return model_->GetStepTitle();
}
void WebauthnDialogView::Hide() {
// Reset controller reference if the controller has been destroyed before the
// view being destroyed. This happens if browser window is closed when the
// dialog is visible.
if (controller_) {
controller_->OnDialogClosed();
controller_ = nullptr;
}
GetWidget()->Close();
}
void WebauthnDialogView::RefreshContent() {
sheet_view_->ReInitChildViews();
sheet_view_->InvalidateLayout();
SetButtonLabel(ui::mojom::DialogButton::kOk, model_->GetAcceptButtonLabel());
SetButtonLabel(ui::mojom::DialogButton::kCancel,
model_->GetCancelButtonLabel());
DCHECK(model_->IsCancelButtonVisible());
SetButtons(model_->GetAcceptButtonState() != AcceptButtonState::kNotVisible
? static_cast<int>(ui::mojom::DialogButton::kOk) |
static_cast<int>(ui::mojom::DialogButton::kCancel)
: static_cast<int>(ui::mojom::DialogButton::kCancel));
DialogModelChanged();
DeprecatedLayoutImmediately();
// Update the dialog's size.
if (GetWidget() && controller_->GetWebContents()) {
constrained_window::UpdateWebContentsModalDialogPosition(
GetWidget(), web_modal::WebContentsModalDialogManager::FromWebContents(
controller_->GetWebContents())
->delegate()
->GetWebContentsModalDialogHost());
}
}
BEGIN_METADATA(WebauthnDialogView)
END_METADATA
} // namespace autofill
|