File: password_check_bridge.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 (186 lines) | stat: -rw-r--r-- 7,151 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright 2020 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/password_check/android/password_check_bridge.h"

#include <jni.h>
#include <string>

#include "base/android/jni_string.h"
#include "chrome/browser/password_manager/android/password_checkup_launcher_helper.h"
#include "chrome/browser/password_manager/android/password_checkup_launcher_helper_impl.h"
#include "components/affiliations/core/browser/affiliation_utils.h"
#include "components/password_manager/core/browser/password_manager_util.h"
#include "components/password_manager/core/browser/ui/insecure_credentials_manager.h"
#include "url/android/gurl_android.h"

// Must come after all headers that specialize FromJniType() / ToJniType().
#include "chrome/browser/password_check/android/internal/internal_jni/PasswordCheckBridge_jni.h"
#include "chrome/browser/password_check/android/jni_headers/CompromisedCredential_jni.h"

namespace {

using affiliations::FacetURI;

password_manager::CredentialUIEntry ConvertJavaObjectToCredential(
    JNIEnv* env,
    const base::android::JavaParamRef<jobject>& credential) {
  std::string signon_realm =
      Java_CompromisedCredential_getSignonRealm(env, credential);
  FacetURI facet = FacetURI::FromPotentiallyInvalidSpec(signon_realm);
  // For the UI, Android credentials store the affiliated realm in the
  // url field, however the saved credential should contains the signon realm
  // instead.
  GURL url = facet.IsValidAndroidFacetURI()
                 ? GURL(signon_realm)
                 : url::GURLAndroid::ToNativeGURL(
                       env, Java_CompromisedCredential_getAssociatedUrl(
                                env, credential));
  password_manager::CredentialUIEntry entry;

  password_manager::CredentialFacet credential_facet;
  credential_facet.url = std::move(url);
  credential_facet.signon_realm = std::move(signon_realm);
  entry.facets.push_back(std::move(credential_facet));

  entry.username = Java_CompromisedCredential_getUsername(env, credential);
  entry.password = Java_CompromisedCredential_getPassword(env, credential);
  entry.last_used_time = base::Time::FromMillisecondsSinceUnixEpoch(
      Java_CompromisedCredential_getLastUsedTime(env, credential));
  entry.stored_in.insert(password_manager::PasswordForm::Store::kProfileStore);
  return entry;
}

// Checks whether the credential is leaked but not phished. Other compromising
// states are ignored (e.g. weak or reused).
constexpr bool IsOnlyLeaked(
    const PasswordCheckManager::CompromisedCredentialForUI& credential) {
  return credential.IsLeaked() && !credential.IsPhished();
}

// Checks whether the credential is phished but not leaked. Other compromising
// states are ignored (e.g. weak or reused).
constexpr bool IsOnlyPhished(
    const PasswordCheckManager::CompromisedCredentialForUI& credential) {
  return credential.IsPhished() && !credential.IsLeaked();
}

}  // namespace

static jlong JNI_PasswordCheckBridge_Create(
    JNIEnv* env,
    const base::android::JavaParamRef<jobject>& java_bridge) {
  return reinterpret_cast<intptr_t>(new PasswordCheckBridge(java_bridge));
}

PasswordCheckBridge::PasswordCheckBridge(
    const base::android::JavaParamRef<jobject>& java_bridge)
    : java_bridge_(java_bridge) {}

PasswordCheckBridge::~PasswordCheckBridge() = default;

void PasswordCheckBridge::StartCheck(JNIEnv* env) {
  check_manager_.StartCheck();
}

void PasswordCheckBridge::StopCheck(JNIEnv* env) {
  check_manager_.StopCheck();
}

int64_t PasswordCheckBridge::GetLastCheckTimestamp(JNIEnv* env) {
  return check_manager_.GetLastCheckTimestamp().InMillisecondsSinceUnixEpoch();
}

jint PasswordCheckBridge::GetCompromisedCredentialsCount(JNIEnv* env) {
  return check_manager_.GetCompromisedCredentialsCount();
}

jint PasswordCheckBridge::GetSavedPasswordsCount(JNIEnv* env) {
  return check_manager_.GetSavedPasswordsCount();
}

void PasswordCheckBridge::GetCompromisedCredentials(
    JNIEnv* env,
    const base::android::JavaParamRef<jobjectArray>& java_credentials) {
  std::vector<PasswordCheckManager::CompromisedCredentialForUI> credentials =
      check_manager_.GetCompromisedCredentials();

  for (size_t i = 0; i < credentials.size(); ++i) {
    const auto& credential = credentials[i];
    Java_PasswordCheckBridge_insertCredential(
        env, java_credentials, i, credential.GetFirstSignonRealm(),
        url::GURLAndroid::FromNativeGURL(env, credential.GetURL()),
        credential.username, credential.display_origin,
        credential.display_username, credential.password,
        credential.change_password_url, credential.package_name,
        credential.GetLastLeakedOrPhishedTime().InMillisecondsSinceUnixEpoch(),
        credential.last_used_time.InMillisecondsSinceUnixEpoch(),
        IsOnlyLeaked(credential), IsOnlyPhished(credential));
  }
}

void PasswordCheckBridge::LaunchCheckupInAccount(
    JNIEnv* env,
    const base::android::JavaParamRef<jobject>& activity) {
  PasswordCheckupLauncherHelperImpl checkup_launcher;
  std::string spec = password_manager::GetPasswordCheckupURL().spec();
  checkup_launcher.LaunchCheckupOnlineWithActivity(env, spec, activity);
}

void PasswordCheckBridge::UpdateCredential(
    JNIEnv* env,
    const base::android::JavaParamRef<jobject>& credential,
    std::string& new_password) {
  check_manager_.UpdateCredential(
      ConvertJavaObjectToCredential(env, credential), new_password);
}

void PasswordCheckBridge::OnEditCredential(
    JNIEnv* env,
    const base::android::JavaParamRef<jobject>& credential,
    const base::android::JavaParamRef<jobject>& context) {
  check_manager_.OnEditCredential(
      ConvertJavaObjectToCredential(env, credential), context);
}

void PasswordCheckBridge::RemoveCredential(
    JNIEnv* env,
    const base::android::JavaParamRef<jobject>& credential) {
  check_manager_.RemoveCredential(
      ConvertJavaObjectToCredential(env, credential));
}

bool PasswordCheckBridge::HasAccountForRequest(JNIEnv* env) {
  return check_manager_.HasAccountForRequest();
}

void PasswordCheckBridge::Destroy(JNIEnv* env) {
  check_manager_.StopCheck();
  delete this;
}

void PasswordCheckBridge::OnSavedPasswordsFetched(int count) {
  Java_PasswordCheckBridge_onSavedPasswordsFetched(
      base::android::AttachCurrentThread(), java_bridge_, count);
}

void PasswordCheckBridge::OnCompromisedCredentialsChanged(int count) {
  Java_PasswordCheckBridge_onCompromisedCredentialsFetched(
      base::android::AttachCurrentThread(), java_bridge_, count);
}

void PasswordCheckBridge::OnPasswordCheckStatusChanged(
    password_manager::PasswordCheckUIStatus status) {
  Java_PasswordCheckBridge_onPasswordCheckStatusChanged(
      base::android::AttachCurrentThread(), java_bridge_,
      static_cast<int>(status));
}

void PasswordCheckBridge::OnPasswordCheckProgressChanged(
    int already_processed,
    int remaining_in_queue) {
  Java_PasswordCheckBridge_onPasswordCheckProgressChanged(
      base::android::AttachCurrentThread(), java_bridge_, already_processed,
      remaining_in_queue);
}