File: otp_verification_dialog_view_android.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (144 lines) | stat: -rw-r--r-- 5,018 bytes parent folder | download | duplicates (6)
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
// Copyright 2021 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/android/autofill/otp_verification_dialog_view_android.h"

#include "base/android/jni_android.h"
#include "base/android/jni_string.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/ui/autofill/payments/payments_view_factory.h"
#include "components/autofill/core/browser/ui/payments/card_unmask_otp_input_dialog_controller.h"
#include "content/public/browser/web_contents.h"
#include "ui/android/view_android.h"
#include "ui/android/window_android.h"

// Must come after all headers that specialize FromJniType() / ToJniType().
#include "chrome/browser/ui/android/autofill/internal/jni_headers/OtpVerificationDialogBridge_jni.h"

using base::android::ConvertJavaStringToUTF16;
using base::android::ConvertUTF16ToJavaString;

namespace autofill {

OtpVerificationDialogViewAndroid::OtpVerificationDialogViewAndroid(
    base::WeakPtr<CardUnmaskOtpInputDialogController> controller)
    : controller_(controller) {
  DCHECK(controller);
}

OtpVerificationDialogViewAndroid::~OtpVerificationDialogViewAndroid() = default;

void OtpVerificationDialogViewAndroid::ShowPendingState() {
  // For Android, the Java code takes care of showing the pending state after
  // user submits the OTP.
  NOTREACHED();
}

void OtpVerificationDialogViewAndroid::ShowInvalidState(
    const std::u16string& invalid_label_text) {
  DCHECK(java_object_);
  JNIEnv* env = base::android::AttachCurrentThread();
  Java_OtpVerificationDialogBridge_showOtpErrorMessage(
      env, java_object_, ConvertUTF16ToJavaString(env, invalid_label_text));
}

void OtpVerificationDialogViewAndroid::Dismiss(
    bool show_confirmation_before_closing,
    bool user_closed_dialog) {
  if (controller_ && show_confirmation_before_closing) {
    DCHECK(!user_closed_dialog);

    std::u16string confirmation_message = controller_->GetConfirmationMessage();
    controller_->OnDialogClosed(/*user_closed_dialog=*/false,
                                /*server_request_succeeded=*/true);
    controller_ = nullptr;
    ShowConfirmationAndDismissDialog(confirmation_message);
    return;
  }

  if (controller_) {
    controller_->OnDialogClosed(
        user_closed_dialog,
        /*server_request_succeeded=*/show_confirmation_before_closing);
    controller_ = nullptr;
  }
  JNIEnv* env = base::android::AttachCurrentThread();
  if (!java_object_.is_null()) {
    Java_OtpVerificationDialogBridge_dismissDialog(env, java_object_);
  } else {
    delete this;
  }
}

base::WeakPtr<CardUnmaskOtpInputDialogView>
OtpVerificationDialogViewAndroid::GetWeakPtr() {
  return weak_ptr_factory_.GetWeakPtr();
}

void OtpVerificationDialogViewAndroid::OnDialogDismissed(JNIEnv* env) {
  // Inform |controller_| of the dialog's destruction.
  if (controller_) {
    controller_->OnDialogClosed(/*user_closed_dialog=*/true,
                                /*server_request_succeeded=*/false);
    controller_ = nullptr;
  }
  delete this;
}

void OtpVerificationDialogViewAndroid::OnConfirm(
    JNIEnv* env,
    const JavaParamRef<jstring>& otp) {
  if (controller_) {
    controller_->OnOkButtonClicked(ConvertJavaStringToUTF16(env, otp));
  }
}

void OtpVerificationDialogViewAndroid::OnNewOtpRequested(JNIEnv* env) {
  if (controller_) {
    controller_->OnNewCodeLinkClicked();
  }
}

bool OtpVerificationDialogViewAndroid::ShowDialog(
    ui::WindowAndroid* window_android) {
  JNIEnv* env = base::android::AttachCurrentThread();
  java_object_.Reset(Java_OtpVerificationDialogBridge_create(
      env, reinterpret_cast<intptr_t>(this), window_android->GetJavaObject()));
  if (java_object_.is_null() || !controller_) {
    return false;
  }
  Java_OtpVerificationDialogBridge_showDialog(
      env, java_object_, controller_->GetExpectedOtpLength());
  return true;
}

void OtpVerificationDialogViewAndroid::ShowConfirmationAndDismissDialog(
    std::u16string confirmation_message) {
  JNIEnv* env = base::android::AttachCurrentThread();
  if (java_object_) {
    Java_OtpVerificationDialogBridge_showConfirmationAndDismissDialog(
        env, java_object_, ConvertUTF16ToJavaString(env, confirmation_message));
  }
}

base::WeakPtr<CardUnmaskOtpInputDialogView> CreateAndShowOtpInputDialog(
    base::WeakPtr<CardUnmaskOtpInputDialogController> controller,
    content::WebContents* web_contents) {
  ui::ViewAndroid* view_android = web_contents->GetNativeView();
  if (!view_android) {
    return nullptr;
  }
  ui::WindowAndroid* window_android = view_android->GetWindowAndroid();
  if (!window_android) {
    return nullptr;
  }
  std::unique_ptr<OtpVerificationDialogViewAndroid> dialog_view =
      std::make_unique<OtpVerificationDialogViewAndroid>(controller);
  // Return the dialog only if we were able to show it.
  return dialog_view->ShowDialog(window_android)
             ? dialog_view.release()->GetWeakPtr()
             : nullptr;
}

}  // namespace autofill