File: authenticator_selection_dialog_view_android.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; 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 (148 lines) | stat: -rw-r--r-- 5,371 bytes parent folder | download | duplicates (4)
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
// 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/authenticator_selection_dialog_view_android.h"

#include <jni.h>
#include <stddef.h>

#include "base/android/jni_android.h"
#include "base/android/jni_string.h"
#include "base/android/scoped_java_ref.h"
#include "base/compiler_specific.h"
#include "chrome/browser/ui/autofill/payments/payments_view_factory.h"
#include "components/autofill/core/browser/payments/card_unmask_challenge_option.h"
#include "components/autofill/core/browser/ui/payments/card_unmask_authentication_selection_dialog_controller.h"
#include "components/grit/components_scaled_resources.h"
#include "content/public/browser/web_contents.h"
#include "ui/android/view_android.h"
#include "ui/android/window_android.h"
#include "ui/base/resource/resource_bundle.h"

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

using base::android::ConvertUTF16ToJavaString;
using base::android::ConvertUTF8ToJavaString;

namespace autofill {

AuthenticatorSelectionDialogViewAndroid::
    AuthenticatorSelectionDialogViewAndroid(
        CardUnmaskAuthenticationSelectionDialogController* controller)
    : controller_(controller) {
  DCHECK(controller);
}

AuthenticatorSelectionDialogViewAndroid::
    ~AuthenticatorSelectionDialogViewAndroid() = default;

void AuthenticatorSelectionDialogViewAndroid::Dismiss(bool user_closed_dialog,
                                                      bool server_success) {
  if (controller_) {
    controller_->OnDialogClosed(user_closed_dialog, server_success);
    controller_ = nullptr;
  }
  JNIEnv* env = base::android::AttachCurrentThread();
  if (java_object_) {
    Java_AuthenticatorSelectionDialogBridge_dismiss(env, java_object_);
  } else {
    delete this;
  }
}

void AuthenticatorSelectionDialogViewAndroid::UpdateContent() {}

void AuthenticatorSelectionDialogViewAndroid::OnOptionSelected(
    JNIEnv* env,
    const base::android::JavaParamRef<jstring>&
        authenticator_option_identifier) {
  std::string card_unmask_challenge_option_id =
      base::android::ConvertJavaStringToUTF8(env,
                                             authenticator_option_identifier);
  controller_->SetSelectedChallengeOptionId(
      CardUnmaskChallengeOption::ChallengeOptionId(
          card_unmask_challenge_option_id));
  controller_->OnOkButtonClicked();
}

void AuthenticatorSelectionDialogViewAndroid::OnDismissed(JNIEnv* env) {
  // If |controller_| is not nullptr, it means the dismissal was triggered by
  // user cancellation.
  if (controller_) {
    controller_->OnDialogClosed(/*user_closed_dialog=*/true,
                                /*server_success=*/false);
    controller_ = nullptr;
  }
  delete this;
}

bool AuthenticatorSelectionDialogViewAndroid::ShowDialog(
    ui::WindowAndroid* window_android) {
  JNIEnv* env = base::android::AttachCurrentThread();
  DCHECK(window_android);

  std::vector<CardUnmaskChallengeOption> options =
      controller_->GetChallengeOptions();
  base::android::ScopedJavaLocalRef<jobject> authOptions =
      CreateJavaAuthenticatorOptions(env, options);

  java_object_.Reset(Java_AuthenticatorSelectionDialogBridge_create(
      env, reinterpret_cast<intptr_t>(this), window_android->GetJavaObject()));

  if (java_object_.is_null()) {
    return false;
  }

  Java_AuthenticatorSelectionDialogBridge_show(env, java_object_, authOptions);
  return true;
}

base::android::ScopedJavaLocalRef<jobject>
AuthenticatorSelectionDialogViewAndroid::CreateJavaAuthenticatorOptions(
    JNIEnv* env,
    const std::vector<CardUnmaskChallengeOption>& options) {
  base::android::ScopedJavaLocalRef<jobject> jlist =
      Java_AuthenticatorSelectionDialogBridge_createAuthenticatorOptionList(
          env);

  for (const auto& option : options) {
    CreateJavaAuthenticatorOptionAndAddToList(env, jlist, option);
  }

  return jlist;
}

void AuthenticatorSelectionDialogViewAndroid::
    CreateJavaAuthenticatorOptionAndAddToList(
        JNIEnv* env,
        base::android::ScopedJavaLocalRef<jobject> jlist,
        const CardUnmaskChallengeOption& option) {
  std::u16string title = controller_->GetAuthenticationModeLabel(option);
  Java_AuthenticatorSelectionDialogBridge_createAuthenticatorOptionAndAddToList(
      env, jlist, ConvertUTF16ToJavaString(env, title),
      ConvertUTF8ToJavaString(env, option.id.value()),
      ConvertUTF16ToJavaString(env, option.challenge_info),
      static_cast<int>(option.type));
}

CardUnmaskAuthenticationSelectionDialog*
CreateAndShowCardUnmaskAuthenticationSelectionDialog(
    content::WebContents* web_contents,
    CardUnmaskAuthenticationSelectionDialogController* controller) {
  ui::ViewAndroid* view_android = web_contents->GetNativeView();
  ui::WindowAndroid* window_android = view_android->GetWindowAndroid();
  if (!window_android) {
    return nullptr;
  }
  AuthenticatorSelectionDialogViewAndroid* dialog_view =
      new AuthenticatorSelectionDialogViewAndroid(controller);
  if (!dialog_view->ShowDialog(window_android)) {
    delete dialog_view;
    return nullptr;
  }
  return dialog_view;
}

}  // namespace autofill