Commit b386af24 authored by Ioana Pandele's avatar Ioana Pandele Committed by Commit Bot

[PwdCheckAndroid] Add java bridge to talk to the C++ check logic

This CL adds a java bridge and its C++ counterpart meant to
mediate the communication between the UI displaying information
about compromised credentials and the native password check logic.

Bug: 1102025
Change-Id: I9c352e5037889f645de8fa10b624fbd2a7a61125
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2289845
Commit-Queue: Ioana Pandele <ioanap@chromium.org>
Reviewed-by: default avatarFriedrich [CET] <fhorschig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#787753}
parent 291165d2
......@@ -2894,6 +2894,7 @@ static_library("browser") {
"//chrome/browser/offline_pages/prefetch/notifications",
"//chrome/browser/optimization_guide/android:jni_headers",
"//chrome/browser/password_check/android",
"//chrome/browser/password_check/android/internal",
"//chrome/browser/payments/android:jni_headers",
"//chrome/browser/safety_check/android",
"//chrome/browser/share",
......
......@@ -45,6 +45,7 @@ android_library("internal_ui_factory_java") {
android_library("internal_java") {
deps = [
":java_resources",
"//base:jni_java",
"//chrome/android:chrome_app_java_resources",
"//chrome/android:chrome_java",
"//chrome/browser/password_check/android:public_ui_java",
......@@ -59,6 +60,7 @@ android_library("internal_java") {
_public_target,
]
sources = [
"java/src/org/chromium/chrome/browser/password_check/PasswordCheckBridge.java",
"java/src/org/chromium/chrome/browser/password_check/PasswordCheckCoordinator.java",
"java/src/org/chromium/chrome/browser/password_check/PasswordCheckImpl.java",
"java/src/org/chromium/chrome/browser/password_check/PasswordCheckMediator.java",
......@@ -66,6 +68,23 @@ android_library("internal_java") {
"java/src/org/chromium/chrome/browser/password_check/PasswordCheckViewBinder.java",
"java/src/org/chromium/chrome/browser/password_check/PasswordCheckViewHolder.java",
]
annotation_processor_deps = [ "//base/android/jni_generator:jni_processor" ]
}
generate_jni("jni_headers") {
visibility = [ ":*" ]
sources = [ "java/src/org/chromium/chrome/browser/password_check/PasswordCheckBridge.java" ]
}
source_set("internal") {
deps = [
":jni_headers",
"//base:base",
]
sources = [
"password_check_bridge.cc",
"password_check_bridge.h",
]
}
android_resources("java_resources") {
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.chrome.browser.password_check;
import org.chromium.base.annotations.NativeMethods;
/**
* Class handling the communication with the C++ part of the password check feature. It forwards
* messages to and from its C++ counterpart.
*/
class PasswordCheckBridge {
private final long mNativePasswordCheckBridge;
private final PasswordCheckObserver mPasswordCheckObserver;
/**
* Observer listening to all messages relevant to the password check.
*/
interface PasswordCheckObserver {
/**
* Called when a new compromised credential is found by the password check
* @param originUrl Origin of the compromised credential.
* @param username Username for the compromised credential.
* @param password Password of the compromised credential.
*/
void onCompromisedCredentialFound(String originUrl, String username, String password);
/**
* Called when the compromised credentials found in a previous check are read from disk.
* @param count The number of compromised credentials that were found in a previous check.
*/
void onCompromisedCredentialsFetched(int count);
}
PasswordCheckBridge(PasswordCheckObserver passwordCheckObserver) {
// Initialized its native counterpart. This will also start fetching the compromised
// credentials stored in the database by the last check.
mNativePasswordCheckBridge = PasswordCheckBridgeJni.get().create();
mPasswordCheckObserver = passwordCheckObserver;
}
// TODO(crbug.com/1102025): Add call from native.
void onCompromisedCredentialFound(String originUrl, String username, String password) {
mPasswordCheckObserver.onCompromisedCredentialFound(originUrl, username, password);
}
// TODO(crbug.com/1102025): Add call from native.
void onCompromisedCredentialsFetched(int count) {
mPasswordCheckObserver.onCompromisedCredentialsFetched(count);
}
// TODO(crbug.com/1102025): Add call from native.
private static void insertCredential(CompromisedCredential[] credentials, int index,
String originUrl, String username, String password, boolean phished) {
credentials[index] = new CompromisedCredential(originUrl, username, password, phished);
}
/**
* Starts the password check.
*/
void startCheck() {
PasswordCheckBridgeJni.get().startCheck(mNativePasswordCheckBridge);
}
/**
* Stops the password check.
*/
void stopCheck() {
PasswordCheckBridgeJni.get().stopCheck(mNativePasswordCheckBridge);
}
/**
* This can return 0 if the compromised credentials haven't been fetched from the database yet.
* @return The number of compromised credentials found in the last run password check.
*/
int getCompromisedCredentialsCount() {
return PasswordCheckBridgeJni.get().getCompromisedCredentialsCount(
mNativePasswordCheckBridge);
}
/**
* Returns the list of compromised credentials that are stored in the database.
* @param credentials array to be populated with the compromised credentials.
*/
void getCompromisedCredentials(CompromisedCredential[] credentials) {
PasswordCheckBridgeJni.get().getCompromisedCredentials(
mNativePasswordCheckBridge, credentials);
}
/**
* C++ method signatures.
*/
@NativeMethods
interface Natives {
long create();
void startCheck(long nativePasswordCheckBridge);
void stopCheck(long nativePasswordCheckBridge);
int getCompromisedCredentialsCount(long nativePasswordCheckBridge);
void getCompromisedCredentials(
long nativePasswordCheckBridge, CompromisedCredential[] credentials);
void destroy(long nativePasswordCheckBridge);
}
}
// Copyright 2020 The Chromium Authors. All rights reserved.
// 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/internal/password_check_bridge.h"
#include <jni.h>
#include "chrome/browser/password_check/android/internal/jni_headers/PasswordCheckBridge_jni.h"
static jlong JNI_PasswordCheckBridge_Create(JNIEnv* env) {
return reinterpret_cast<intptr_t>(new PasswordCheckBridge());
}
PasswordCheckBridge::PasswordCheckBridge() = default;
PasswordCheckBridge::~PasswordCheckBridge() = default;
void PasswordCheckBridge::StartCheck(JNIEnv* env) {
// TODO(crbug.com/1102025): implement this.
}
void PasswordCheckBridge::StopCheck(JNIEnv* env) {
// TODO(crbug.com/1102025): implement this.
}
jint PasswordCheckBridge::GetCompromisedCredentialsCount(JNIEnv* env) {
// TODO(crbug.com/1102025): implement this.
return 0;
}
void PasswordCheckBridge::GetCompromisedCredentials(
JNIEnv* env,
const base::android::JavaParamRef<jobjectArray>& credentials) {
// TODO(crbug.com/1102025): implement this.
}
void PasswordCheckBridge::Destroy(JNIEnv* env) {
delete this;
}
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_PASSWORD_CHECK_ANDROID_INTERNAL_PASSWORD_CHECK_BRIDGE_H_
#define CHROME_BROWSER_PASSWORD_CHECK_ANDROID_INTERNAL_PASSWORD_CHECK_BRIDGE_H_
#include <jni.h>
#include "base/android/scoped_java_ref.h"
// C++ counterpart of |PasswordCheckBridge.java|. Used to mediate the
// communication between the UI and the password check logic.
class PasswordCheckBridge {
public:
PasswordCheckBridge();
PasswordCheckBridge(const PasswordCheckBridge&) = delete;
PasswordCheckBridge& operator=(const PasswordCheckBridge&) = delete;
// Called by Java to start the password check.
void StartCheck(JNIEnv* env);
// Called by Java to stop the password check.
void StopCheck(JNIEnv* env);
// Called by Java to get the number of compromised credentials.
jint GetCompromisedCredentialsCount(JNIEnv* env);
// Called by Java to get the list of compromised credentials.
void GetCompromisedCredentials(
JNIEnv* env,
const base::android::JavaParamRef<jobjectArray>& credentials);
// Called by Java when the bridge is no longer needed. Destructs itself.
void Destroy(JNIEnv* env);
private:
~PasswordCheckBridge();
};
#endif // CHROME_BROWSER_PASSWORD_CHECK_ANDROID_INTERNAL_PASSWORD_CHECK_BRIDGE_H_
......@@ -10,23 +10,33 @@ package org.chromium.chrome.browser.password_check;
*/
public class CompromisedCredential {
private final String mUsername;
private final String mPassword;
private final String mOriginUrl;
private final boolean mPhished;
/**
* @param username Username shown to the user.
* @param originUrl Origin URL shown to the user in case this credential is a PSL match.
*/
public CompromisedCredential(String username, String originUrl) {
public CompromisedCredential(
String originUrl, String username, String password, boolean phished) {
assert originUrl != null : "Credential origin is null! Pass an empty one instead.";
mUsername = username;
mOriginUrl = originUrl;
mUsername = username;
mPassword = password;
mPhished = phished;
}
public String getOriginUrl() {
return mOriginUrl;
}
public String getUsername() {
return mUsername;
}
public String getOriginUrl() {
return mOriginUrl;
public String getPassword() {
return mPassword;
}
public boolean isPhished() {
return mPhished;
}
}
......@@ -41,7 +41,7 @@ import org.chromium.ui.modelutil.PropertyModel;
@CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE})
public class PasswordCheckViewTest {
private static final CompromisedCredential ANA =
new CompromisedCredential("Ana", "https://some-url.com");
new CompromisedCredential("https://some-url.com", "Ana", "password", false);
private PropertyModel mModel = PasswordCheckProperties.createDefaultModel();
private PasswordCheckFragmentView mPasswordCheckView;
......
......@@ -32,7 +32,7 @@ import java.util.Arrays;
@RunWith(BaseRobolectricTestRunner.class)
public class PasswordCheckControllerTest {
private static final CompromisedCredential ANA =
new CompromisedCredential("Ana", "https://m.a.xyz/");
new CompromisedCredential("https://m.a.xyz/", "Ana", "password", false);
private final PasswordCheckMediator mMediator = new PasswordCheckMediator();
private final PropertyModel mModel = PasswordCheckProperties.createDefaultModel();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment