Commit b85cc232 authored by Manas Verma's avatar Manas Verma Committed by Commit Bot

[AF Auth] Fix failing opt-in flow

This CL creates the JavaRef for AuthenticatorImpl when request is made,
rather than in constructor.

Bug: 949269
Change-Id: I43244cafde4f2e98d69caa7adfe347ee1334af5e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2314030
Commit-Queue: Manas Verma <manasverma@google.com>
Reviewed-by: default avatarAndrew Grieve <agrieve@chromium.org>
Reviewed-by: default avatarTommy Martino <tmartino@chromium.org>
Reviewed-by: default avatarJared Saul <jsaul@google.com>
Cr-Commit-Position: refs/heads/master@{#792818}
parent 6db73ca9
......@@ -29,12 +29,10 @@ InternalAuthenticatorAndroid::InternalAuthenticatorAndroid(
content::RenderFrameHost* render_frame_host)
: render_frame_host_(render_frame_host) {
DCHECK(render_frame_host_);
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> local_ref = Java_AuthenticatorImpl_create(
java_authenticator_impl_ref_ = Java_AuthenticatorImpl_create(
env, reinterpret_cast<intptr_t>(this),
render_frame_host_->GetJavaRenderFrameHost());
java_authenticator_impl_ref_ = JavaObjectWeakGlobalRef(env, local_ref);
}
InternalAuthenticatorAndroid::~InternalAuthenticatorAndroid() {
......@@ -47,9 +45,8 @@ InternalAuthenticatorAndroid::~InternalAuthenticatorAndroid() {
void InternalAuthenticatorAndroid::SetEffectiveOrigin(
const url::Origin& origin) {
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> obj = java_authenticator_impl_ref_.get(env);
if (obj.is_null())
return;
JavaRef<jobject>& obj = GetJavaObject();
DCHECK(!obj.is_null());
Java_AuthenticatorImpl_setEffectiveOrigin(env, obj,
origin.CreateJavaObject());
......@@ -59,18 +56,15 @@ void InternalAuthenticatorAndroid::MakeCredential(
blink::mojom::PublicKeyCredentialCreationOptionsPtr options,
blink::mojom::Authenticator::MakeCredentialCallback callback) {
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> obj = java_authenticator_impl_ref_.get(env);
if (obj.is_null())
return;
JavaRef<jobject>& obj = GetJavaObject();
DCHECK(!obj.is_null());
make_credential_response_callback_ = std::move(callback);
std::vector<uint8_t> byte_vector =
blink::mojom::PublicKeyCredentialCreationOptions::Serialize(&options);
base::android::ScopedJavaLocalRef<jobject> byte_buffer =
base::android::ScopedJavaLocalRef<jobject>(
env,
env->NewDirectByteBuffer(byte_vector.data(), byte_vector.size()));
ScopedJavaLocalRef<jobject> byte_buffer = ScopedJavaLocalRef<jobject>(
env, env->NewDirectByteBuffer(byte_vector.data(), byte_vector.size()));
Java_AuthenticatorImpl_makeCredentialBridge(env, obj, byte_buffer);
}
......@@ -79,18 +73,15 @@ void InternalAuthenticatorAndroid::GetAssertion(
blink::mojom::PublicKeyCredentialRequestOptionsPtr options,
blink::mojom::Authenticator::GetAssertionCallback callback) {
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> obj = java_authenticator_impl_ref_.get(env);
if (obj.is_null())
return;
JavaRef<jobject>& obj = GetJavaObject();
DCHECK(!obj.is_null());
get_assertion_response_callback_ = std::move(callback);
std::vector<uint8_t> byte_vector =
blink::mojom::PublicKeyCredentialRequestOptions::Serialize(&options);
base::android::ScopedJavaLocalRef<jobject> byte_buffer =
base::android::ScopedJavaLocalRef<jobject>(
env,
env->NewDirectByteBuffer(byte_vector.data(), byte_vector.size()));
ScopedJavaLocalRef<jobject> byte_buffer = ScopedJavaLocalRef<jobject>(
env, env->NewDirectByteBuffer(byte_vector.data(), byte_vector.size()));
Java_AuthenticatorImpl_getAssertionBridge(env, obj, byte_buffer);
}
......@@ -100,9 +91,8 @@ void InternalAuthenticatorAndroid::
blink::mojom::Authenticator::
IsUserVerifyingPlatformAuthenticatorAvailableCallback callback) {
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> obj = java_authenticator_impl_ref_.get(env);
if (obj.is_null())
return;
JavaRef<jobject>& obj = GetJavaObject();
DCHECK(!obj.is_null());
is_uvpaa_callback_ = std::move(callback);
Java_AuthenticatorImpl_isUserVerifyingPlatformAuthenticatorAvailableBridge(
......@@ -111,9 +101,8 @@ void InternalAuthenticatorAndroid::
void InternalAuthenticatorAndroid::Cancel() {
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> obj = java_authenticator_impl_ref_.get(env);
if (obj.is_null())
return;
JavaRef<jobject>& obj = GetJavaObject();
DCHECK(!obj.is_null());
Java_AuthenticatorImpl_cancel(env, obj);
}
......@@ -162,3 +151,13 @@ void InternalAuthenticatorAndroid::
jboolean is_uvpaa) {
std::move(is_uvpaa_callback_).Run(static_cast<bool>(is_uvpaa));
}
JavaRef<jobject>& InternalAuthenticatorAndroid::GetJavaObject() {
if (java_authenticator_impl_ref_.is_null()) {
JNIEnv* env = AttachCurrentThread();
java_authenticator_impl_ref_ = Java_AuthenticatorImpl_create(
env, reinterpret_cast<intptr_t>(this),
render_frame_host_->GetJavaRenderFrameHost());
}
return java_authenticator_impl_ref_;
}
......@@ -61,7 +61,12 @@ class InternalAuthenticatorAndroid : public autofill::InternalAuthenticator {
jboolean is_uvpaa);
private:
JavaObjectWeakGlobalRef java_authenticator_impl_ref_;
// Returns the associated AuthenticatorImpl Java object. Initializes new
// instance if not done so already in order to avoid possibility of any null
// pointer issues.
base::android::JavaRef<jobject>& GetJavaObject();
base::android::ScopedJavaGlobalRef<jobject> java_authenticator_impl_ref_;
content::RenderFrameHost* render_frame_host_;
blink::mojom::Authenticator::MakeCredentialCallback
make_credential_response_callback_;
......
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