File: webauthn_cred_man_delegate.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (131 lines) | stat: -rw-r--r-- 4,098 bytes parent folder | download | duplicates (3)
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
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/webauthn/android/webauthn_cred_man_delegate.h"

#include <optional>
#include <string>
#include <utility>

#include "base/android/jni_android.h"
#include "base/functional/callback.h"
#include "base/metrics/histogram_functions.h"
#include "base/notreached.h"
#include "components/webauthn/android/cred_man_support.h"
#include "content/public/browser/web_contents.h"

// Must come after all headers that specialize FromJniType() / ToJniType().
#include "components/webauthn/android/jni_headers/CredManSupportProvider_jni.h"

namespace content {
class WebContents;
}  // namespace content

namespace webauthn {

using webauthn::CredManSupport;

WebAuthnCredManDelegate::WebAuthnCredManDelegate(
    content::WebContents* web_contents) {}

WebAuthnCredManDelegate::~WebAuthnCredManDelegate() = default;

void WebAuthnCredManDelegate::OnCredManConditionalRequestPending(
    bool has_passkeys,
    base::RepeatingCallback<void(bool)> full_assertion_request) {
  has_passkeys_ = has_passkeys ? kHasPasskeys : kNoPasskeys;
  show_cred_man_ui_callback_ = std::move(full_assertion_request);

  std::vector<base::OnceClosure> notification_closures;
  if (credentials_available_closure_) {
    std::move(credentials_available_closure_).Run();
  }
}

void WebAuthnCredManDelegate::OnCredManUiClosed(bool success) {
  if (!request_completion_callback_.is_null()) {
    request_completion_callback_.Run(success);
  }
}

void WebAuthnCredManDelegate::TriggerCredManUi(
    RequestPasswords request_passwords) {
  if (!passkeys_after_fill_recorded_) {
    passkeys_after_fill_recorded_ = true;
    base::UmaHistogramBoolean(
        "PasswordManager.PasskeysArrivedAfterAutofillDisplay",
        has_passkeys_ == kNotReady);
  }

  if (show_cred_man_ui_callback_.is_null()) {
    return;
  }
  show_cred_man_ui_callback_.Run(request_passwords.value() &&
                                 !filling_callback_.is_null());
}

WebAuthnCredManDelegate::State WebAuthnCredManDelegate::HasPasskeys() const {
  return has_passkeys_;
}

void WebAuthnCredManDelegate::CleanUpConditionalRequest() {
  show_cred_man_ui_callback_.Reset();
  has_passkeys_ = kNotReady;
}

void WebAuthnCredManDelegate::SetRequestCompletionCallback(
    base::RepeatingCallback<void(bool)> callback) {
  request_completion_callback_ = std::move(callback);
}

void WebAuthnCredManDelegate::SetFillingCallback(
    base::OnceCallback<void(const std::u16string&, const std::u16string&)>
        filling_callback) {
  filling_callback_ = std::move(filling_callback);
}

void WebAuthnCredManDelegate::FillUsernameAndPassword(
    const std::u16string& username,
    const std::u16string& password) {
  std::move(filling_callback_).Run(username, password);
}

void WebAuthnCredManDelegate::RequestNotificationWhenCredentialsReady(
    base::OnceClosure closure) {
  if (has_passkeys_ != kNotReady) {
    std::move(closure).Run();
    return;
  }
  CHECK(!credentials_available_closure_);
  credentials_available_closure_ = std::move(closure);
}

base::WeakPtr<WebAuthnCredManDelegate> WebAuthnCredManDelegate::AsWeakPtr() {
  return weak_ptr_factory_.GetWeakPtr();
}

// static
WebAuthnCredManDelegate::CredManEnabledMode
WebAuthnCredManDelegate::CredManMode() {
  if (!cred_man_support_.has_value()) {
    cred_man_support_ = Java_CredManSupportProvider_getCredManSupport(
        base::android::AttachCurrentThread());
  }
  switch (cred_man_support_.value()) {
    case CredManSupport::NOT_EVALUATED:
      NOTREACHED();
    case CredManSupport::DISABLED:
    case CredManSupport::IF_REQUIRED:
      return CredManEnabledMode::kNotEnabled;
    case CredManSupport::FULL_UNLESS_INAPPLICABLE:
      return CredManEnabledMode::kAllCredMan;
    case CredManSupport::PARALLEL_WITH_FIDO_2:
      return CredManEnabledMode::kNonGpmPasskeys;
  }
  return CredManEnabledMode::kNotEnabled;
}

std::optional<int> WebAuthnCredManDelegate::cred_man_support_ = std::nullopt;

}  // namespace webauthn