Skip to content

Commit f30a68e

Browse files
Resolves comments
1 parent 874ef41 commit f30a68e

File tree

5 files changed

+18
-30
lines changed

5 files changed

+18
-30
lines changed

appcheck/firebase-appcheck-recaptchaenterprise/firebase-appcheck-recaptchaenterprise.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,5 @@ dependencies {
6060
testImplementation libs.junit
6161
testImplementation libs.mockito.core
6262
testImplementation libs.robolectric
63+
testImplementation libs.org.json
6364
}

appcheck/firebase-appcheck-recaptchaenterprise/src/main/java/com/google/firebase/appcheck/recaptchaenterprise/FirebaseAppCheckRecaptchaEnterpriseRegistrar.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414

1515
package com.google.firebase.appcheck.recaptchaenterprise;
1616

17-
import android.app.Application;
1817
import com.google.android.gms.common.annotation.KeepForSdk;
19-
import com.google.firebase.FirebaseApp;
2018
import com.google.firebase.annotations.concurrent.Blocking;
2119
import com.google.firebase.annotations.concurrent.Lightweight;
2220
import com.google.firebase.appcheck.recaptchaenterprise.internal.FirebaseExecutors;
@@ -45,15 +43,6 @@ public List<Component<?>> getComponents() {
4543
Qualified<Executor> blockingExecutor = Qualified.qualified(Blocking.class, Executor.class);
4644

4745
return Arrays.asList(
48-
Component.builder(Application.class)
49-
.name(LIBRARY_NAME)
50-
.add(Dependency.required(FirebaseApp.class))
51-
.factory(
52-
container -> {
53-
FirebaseApp firebaseApp = container.get(FirebaseApp.class);
54-
return (Application) firebaseApp.getApplicationContext();
55-
})
56-
.build(),
5746
Component.builder(FirebaseExecutors.class)
5847
.name(LIBRARY_NAME)
5948
.add(Dependency.required(liteExecutor))

appcheck/firebase-appcheck-recaptchaenterprise/src/main/java/com/google/firebase/appcheck/recaptchaenterprise/RecaptchaEnterpriseAppCheckProviderFactory.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,8 @@ private RecaptchaEnterpriseAppCheckProviderFactory(@NonNull String siteKey) {
4444
/** Gets an instance of this class for installation into a {@link FirebaseAppCheck} instance. */
4545
@NonNull
4646
public static RecaptchaEnterpriseAppCheckProviderFactory getInstance(@NonNull String siteKey) {
47-
RecaptchaEnterpriseAppCheckProviderFactory factory = factoryInstances.get(siteKey);
48-
if (factory == null) {
49-
synchronized (factoryInstances) {
50-
factory = factoryInstances.get(siteKey);
51-
if (factory == null) {
52-
factory = new RecaptchaEnterpriseAppCheckProviderFactory(siteKey);
53-
factoryInstances.put(siteKey, factory);
54-
}
55-
}
56-
}
57-
return factory;
47+
return factoryInstances.computeIfAbsent(
48+
siteKey, RecaptchaEnterpriseAppCheckProviderFactory::new);
5849
}
5950

6051
@NonNull
@@ -67,7 +58,7 @@ public AppCheckProvider create(@NonNull FirebaseApp firebaseApp) {
6758
if (RecaptchaEnterpriseAppCheckProviderFactory.firebaseExecutors == null) {
6859
firebaseExecutors = firebaseApp.get(FirebaseExecutors.class);
6960
}
70-
Application application = firebaseApp.get(Application.class);
61+
Application application = (Application) firebaseApp.getApplicationContext();
7162

7263
provider =
7364
new RecaptchaEnterpriseAppCheckProvider(

appcheck/firebase-appcheck-recaptchaenterprise/src/main/java/com/google/firebase/appcheck/recaptchaenterprise/internal/RecaptchaEnterpriseAppCheckProvider.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,20 @@
4343
*
4444
* <ol>
4545
* <li>Obtain a reCAPTCHA token via {@code RecaptchaTasksClient}.
46-
* <li>Exchange the reCAPTCHA token with the Firebase App Check backend using {@link
47-
* NetworkClient} to receive a Firebase App Check token.
46+
* <li>Exchange the reCAPTCHA token with the Firebase App Check backend to receive a Firebase App
47+
* Check token.
4848
* </ol>
4949
*/
5050
public class RecaptchaEnterpriseAppCheckProvider implements AppCheckProvider {
5151

5252
private final RecaptchaAction recaptchaAction = RecaptchaAction.custom("fire_app_check");
53-
private final Task<RecaptchaTasksClient> recaptchaTasksClientTask;
53+
private volatile Task<RecaptchaTasksClient> recaptchaTasksClientTask;
5454
private final Executor liteExecutor;
5555
private final Executor blockingExecutor;
5656
private final RetryManager retryManager;
5757
private final NetworkClient networkClient;
58+
private String siteKey;
59+
private Application application;
5860
private static final String TAG = "rCEAppCheckProvider";
5961

6062
public RecaptchaEnterpriseAppCheckProvider(
@@ -63,11 +65,12 @@ public RecaptchaEnterpriseAppCheckProvider(
6365
@NonNull String siteKey,
6466
@Lightweight Executor liteExecutor,
6567
@Blocking Executor blockingExecutor) {
68+
this.application = application;
69+
this.siteKey = siteKey;
6670
this.liteExecutor = liteExecutor;
6771
this.blockingExecutor = blockingExecutor;
6872
this.retryManager = new RetryManager();
6973
this.networkClient = new NetworkClient(firebaseApp);
70-
recaptchaTasksClientTask = Recaptcha.fetchTaskClient(application, siteKey);
7174
}
7275

7376
@VisibleForTesting
@@ -111,6 +114,13 @@ public Task<AppCheckToken> getToken() {
111114

112115
@NonNull
113116
private Task<String> getRecaptchaEnterpriseAttestation() {
117+
if (recaptchaTasksClientTask == null) {
118+
synchronized (this) {
119+
if (recaptchaTasksClientTask == null) {
120+
recaptchaTasksClientTask = Recaptcha.fetchTaskClient(application, siteKey);
121+
}
122+
}
123+
}
114124
return recaptchaTasksClientTask.continueWithTask(
115125
blockingExecutor,
116126
task -> {

appcheck/firebase-appcheck-recaptchaenterprise/src/test/java/com/google/firebase/appcheck/recaptchaenterprise/internal/ExchangeRecaptchaEnterpriseTokenRequestTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,8 @@
1818

1919
import org.json.JSONObject;
2020
import org.junit.Test;
21-
import org.junit.runner.RunWith;
22-
import org.robolectric.RobolectricTestRunner;
2321

2422
/** Tests for {@link ExchangeRecaptchaEnterpriseTokenRequest}. */
25-
@RunWith(RobolectricTestRunner.class)
2623
public class ExchangeRecaptchaEnterpriseTokenRequestTest {
2724
private static final String RECAPTCHA_ENTERPRISE_TOKEN = "recaptchaEnterpriseToken";
2825

0 commit comments

Comments
 (0)