Skip to content

Commit 92cbd0d

Browse files
Resolves comments
1 parent dde05ae commit 92cbd0d

File tree

11 files changed

+105
-113
lines changed

11 files changed

+105
-113
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ dependencies {
3636
javadocClasspath libs.autovalue.annotations
3737

3838
api project(':appcheck:firebase-appcheck')
39-
api 'com.google.firebase:firebase-annotations:16.2.0'
40-
api 'com.google.firebase:firebase-common:21.0.0'
41-
api 'com.google.firebase:firebase-common-ktx:21.0.0'
42-
api 'com.google.firebase:firebase-components:18.0.0'
43-
api 'com.google.android.recaptcha:recaptcha:18.8.0-beta01'
39+
api libs.firebase.annotations
40+
api libs.firebase.common.v2200
41+
api libs.firebase.common.ktx
42+
api libs.firebase.components.v1800
43+
api libs.recaptcha.v1871
4444

4545
testImplementation(project(":integ-testing")) {
4646
exclude group: 'com.google.firebase', module: 'firebase-common'

appcheck/firebase-appcheck-recaptchaenterprise/src/main/AndroidManifest.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22

33
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
4-
<!--Although the *SdkVersion is captured in gradle build files, this is required for non gradle builds-->
5-
<!--<uses-sdk android:minSdkVersion="21"/>-->
4+
65
<application>
76
<service
87
android:name="com.google.firebase.components.ComponentDiscoveryService"

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

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44
import android.content.Context;
55

66
import com.google.android.gms.common.annotation.KeepForSdk;
7-
import com.google.firebase.FirebaseApp;
87
import com.google.firebase.annotations.concurrent.Blocking;
98
import com.google.firebase.annotations.concurrent.Lightweight;
10-
import com.google.firebase.appcheck.recaptchaenterprise.internal.RecaptchaEnterpriseAppCheckProvider;
11-
import com.google.firebase.appcheck.recaptchaenterprise.internal.SiteKey;
9+
import com.google.firebase.appcheck.recaptchaenterprise.internal.FirebaseExecutors;
1210
import com.google.firebase.components.Component;
1311
import com.google.firebase.components.ComponentRegistrar;
1412
import com.google.firebase.components.Dependency;
@@ -44,26 +42,14 @@ public List<Component<?>> getComponents() {
4442
return (Application) context.getApplicationContext();
4543
})
4644
.build(),
47-
Component.builder(SiteKey.class)
45+
Component.builder(FirebaseExecutors.class)
4846
.name(LIBRARY_NAME)
49-
.factory(
50-
container -> new SiteKey(RecaptchaEnterpriseAppCheckProviderFactory.getSiteKey()))
51-
.build(),
52-
Component.builder(RecaptchaEnterpriseAppCheckProvider.class)
53-
.name(LIBRARY_NAME)
54-
.add(Dependency.required(FirebaseApp.class))
55-
.add(Dependency.required(Application.class))
56-
.add(Dependency.required(SiteKey.class))
5747
.add(Dependency.required(liteExecutor))
5848
.add(Dependency.required(blockingExecutor))
5949
.factory(
60-
(container ->
61-
new RecaptchaEnterpriseAppCheckProvider(
62-
container.get(FirebaseApp.class),
63-
container.get(Application.class),
64-
container.get(SiteKey.class),
65-
container.get(liteExecutor),
66-
container.get(blockingExecutor))))
50+
container ->
51+
new FirebaseExecutors(
52+
container.get(liteExecutor), container.get(blockingExecutor)))
6753
.build(),
6854
LibraryVersionComponent.create(LIBRARY_NAME, BuildConfig.VERSION_NAME));
6955
}
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,72 @@
11
package com.google.firebase.appcheck.recaptchaenterprise;
22

3+
import android.app.Application;
4+
35
import androidx.annotation.NonNull;
46
import com.google.firebase.FirebaseApp;
57
import com.google.firebase.appcheck.AppCheckProvider;
68
import com.google.firebase.appcheck.AppCheckProviderFactory;
79
import com.google.firebase.appcheck.FirebaseAppCheck;
10+
import com.google.firebase.appcheck.recaptchaenterprise.internal.FirebaseExecutors;
811
import com.google.firebase.appcheck.recaptchaenterprise.internal.RecaptchaEnterpriseAppCheckProvider;
912

13+
import java.util.Map;
14+
import java.util.concurrent.ConcurrentHashMap;
15+
1016
/**
1117
* Implementation of an {@link AppCheckProviderFactory} that builds <br>
1218
* {@link RecaptchaEnterpriseAppCheckProvider}s. This is the default implementation.
1319
*/
1420
public class RecaptchaEnterpriseAppCheckProviderFactory implements AppCheckProviderFactory {
1521

16-
private static volatile RecaptchaEnterpriseAppCheckProviderFactory instance;
17-
private static String siteKey;
22+
private static FirebaseExecutors firebaseExecutors;
23+
private static final Map<String, RecaptchaEnterpriseAppCheckProviderFactory> factoryInstances =
24+
new ConcurrentHashMap<>();
25+
private final String siteKey;
26+
private volatile RecaptchaEnterpriseAppCheckProvider provider;
27+
28+
private RecaptchaEnterpriseAppCheckProviderFactory(@NonNull String siteKey) {
29+
this.siteKey = siteKey;
30+
}
1831

1932
/** Gets an instance of this class for installation into a {@link FirebaseAppCheck} instance. */
2033
@NonNull
2134
public static RecaptchaEnterpriseAppCheckProviderFactory getInstance(@NonNull String siteKey) {
22-
if (instance == null) {
23-
synchronized (RecaptchaEnterpriseAppCheckProviderFactory.class) {
24-
if (instance == null) {
25-
instance = new RecaptchaEnterpriseAppCheckProviderFactory();
26-
RecaptchaEnterpriseAppCheckProviderFactory.siteKey = siteKey;
35+
RecaptchaEnterpriseAppCheckProviderFactory factory = factoryInstances.get(siteKey);
36+
if (factory == null) {
37+
synchronized (factoryInstances) {
38+
factory = factoryInstances.get(siteKey);
39+
if (factory == null) {
40+
factory = new RecaptchaEnterpriseAppCheckProviderFactory(siteKey);
41+
factoryInstances.put(siteKey, factory);
2742
}
2843
}
2944
}
30-
return instance;
31-
}
32-
33-
@NonNull
34-
public static String getSiteKey() {
35-
return siteKey;
45+
return factory;
3646
}
3747

3848
@NonNull
3949
@Override
4050
@SuppressWarnings("FirebaseUseExplicitDependencies")
4151
public AppCheckProvider create(@NonNull FirebaseApp firebaseApp) {
42-
return firebaseApp.get(RecaptchaEnterpriseAppCheckProvider.class);
52+
if (provider == null) {
53+
synchronized (this) {
54+
if (provider == null) {
55+
if (RecaptchaEnterpriseAppCheckProviderFactory.firebaseExecutors == null) {
56+
firebaseExecutors = firebaseApp.get(FirebaseExecutors.class);
57+
}
58+
Application application = firebaseApp.get(Application.class);
59+
60+
provider =
61+
new RecaptchaEnterpriseAppCheckProvider(
62+
firebaseApp,
63+
application,
64+
siteKey,
65+
firebaseExecutors.getLiteExecutor(),
66+
firebaseExecutors.getBlockingExecutor());
67+
}
68+
}
69+
}
70+
return provider;
4371
}
4472
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.google.firebase.appcheck.recaptchaenterprise.internal;
2+
3+
import com.google.firebase.annotations.concurrent.Blocking;
4+
import com.google.firebase.annotations.concurrent.Lightweight;
5+
6+
import java.util.concurrent.Executor;
7+
8+
/**
9+
* This class encapsulates a {@link com.google.firebase.annotations.concurrent.Lightweight}
10+
* executor and a {@link com.google.firebase.annotations.concurrent.Blocking} executor,
11+
* making them available for various asynchronous operations related to reCAPTCHA Enterprise
12+
* App Check.
13+
**/
14+
public class FirebaseExecutors {
15+
private final Executor liteExecutor;
16+
private final Executor blockingExecutor;
17+
18+
public FirebaseExecutors(@Lightweight Executor liteExecutor, @Blocking Executor blockingExecutor) {
19+
this.liteExecutor = liteExecutor;
20+
this.blockingExecutor = blockingExecutor;
21+
}
22+
23+
public Executor getLiteExecutor() {
24+
return liteExecutor;
25+
}
26+
27+
public Executor getBlockingExecutor() {
28+
return blockingExecutor;
29+
}
30+
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.google.firebase.appcheck.recaptchaenterprise.internal;
22

33
import android.app.Application;
4+
import android.util.Log;
45

56
import androidx.annotation.NonNull;
67
import androidx.annotation.VisibleForTesting;
@@ -31,18 +32,19 @@ public class RecaptchaEnterpriseAppCheckProvider implements AppCheckProvider {
3132
private final Executor blockingExecutor;
3233
private final RetryManager retryManager;
3334
private final NetworkClient networkClient;
35+
private static final String TAG = "rCEAppCheckProvider";
3436

3537
public RecaptchaEnterpriseAppCheckProvider(
3638
@NonNull FirebaseApp firebaseApp,
3739
@NonNull Application application,
38-
@NonNull SiteKey siteKey,
40+
@NonNull String siteKey,
3941
@Lightweight Executor liteExecutor,
4042
@Blocking Executor blockingExecutor) {
4143
this.liteExecutor = liteExecutor;
4244
this.blockingExecutor = blockingExecutor;
4345
this.retryManager = new RetryManager();
4446
this.networkClient = new NetworkClient(firebaseApp);
45-
recaptchaTasksClientTask = Recaptcha.fetchTaskClient(application, siteKey.value());
47+
recaptchaTasksClientTask = Recaptcha.fetchTaskClient(application, siteKey);
4648
}
4749

4850
@VisibleForTesting
@@ -93,6 +95,7 @@ private Task<String> getRecaptchaEnterpriseAttestation() {
9395
RecaptchaTasksClient client = task.getResult();
9496
return client.executeTask(recaptchaAction);
9597
} else {
98+
Log.w(TAG, "Recaptcha task failed", task.getException());
9699
throw Objects.requireNonNull(task.getException());
97100
}
98101
});

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

Lines changed: 0 additions & 17 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,15 @@
11
package com.google.firebase.appcheck.recaptchaenterprise;
22

33
import static com.google.common.truth.Truth.assertThat;
4-
5-
import android.app.Application;
64
import android.content.Context;
7-
8-
import com.google.firebase.FirebaseApp;
9-
import com.google.firebase.annotations.concurrent.Blocking;
10-
import com.google.firebase.annotations.concurrent.Lightweight;
11-
import com.google.firebase.appcheck.recaptchaenterprise.internal.SiteKey;
125
import com.google.firebase.components.Component;
136
import com.google.firebase.components.Dependency;
14-
import com.google.firebase.components.Qualified;
157

168
import org.junit.Test;
179
import org.junit.runner.RunWith;
1810
import org.robolectric.RobolectricTestRunner;
1911

2012
import java.util.List;
21-
import java.util.concurrent.Executor;
2213

2314
/** Tests for {@link FirebaseAppCheckRecaptchaEnterpriseRegistrar}. */
2415
@RunWith(RobolectricTestRunner.class)
@@ -29,21 +20,10 @@ public void testGetComponents() {
2920
new FirebaseAppCheckRecaptchaEnterpriseRegistrar();
3021
List<Component<?>> components = registrar.getComponents();
3122
assertThat(components).isNotEmpty();
32-
assertThat(components).hasSize(4);
23+
assertThat(components).hasSize(3);
3324
Component<?> applicationComponent = components.get(0);
3425
assertThat(applicationComponent.getDependencies())
3526
.containsExactly(Dependency.required(Context.class));
3627
assertThat(applicationComponent.isLazy()).isTrue();
37-
Component<?> siteKeyComponent = components.get(1);
38-
assertThat(siteKeyComponent.isLazy()).isTrue();
39-
Component<?> appCheckRecaptchaEnterpriseComponent = components.get(2);
40-
assertThat(appCheckRecaptchaEnterpriseComponent.getDependencies())
41-
.containsExactly(
42-
Dependency.required(FirebaseApp.class),
43-
Dependency.required(Application.class),
44-
Dependency.required(SiteKey.class),
45-
Dependency.required(Qualified.qualified(Lightweight.class, Executor.class)),
46-
Dependency.required(Qualified.qualified(Blocking.class, Executor.class)));
47-
assertThat(appCheckRecaptchaEnterpriseComponent.isLazy()).isTrue();
4828
}
4929
}

appcheck/firebase-appcheck-recaptchaenterprise/src/test/java/com/google/firebase/appcheck/recaptchaenterprise/RecaptchaEnterpriseAppCheckProviderFactoryTest.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,26 @@
1111
@RunWith(RobolectricTestRunner.class)
1212
@Config(manifest = Config.NONE)
1313
public class RecaptchaEnterpriseAppCheckProviderFactoryTest {
14-
static final String SITE_KEY = "siteKey";
14+
static final String SITE_KEY_1 = "siteKey1";
15+
static final String SITE_KEY_2 = "siteKey2";
1516

1617
@Test
17-
public void testGetInstance_callTwice_sameInstance() {
18+
public void testGetInstance_callTwiceSameSiteKey_sameInstance() {
1819
RecaptchaEnterpriseAppCheckProviderFactory firstInstance =
19-
RecaptchaEnterpriseAppCheckProviderFactory.getInstance(SITE_KEY);
20+
RecaptchaEnterpriseAppCheckProviderFactory.getInstance(SITE_KEY_1);
2021
RecaptchaEnterpriseAppCheckProviderFactory secondInstance =
21-
RecaptchaEnterpriseAppCheckProviderFactory.getInstance(SITE_KEY);
22+
RecaptchaEnterpriseAppCheckProviderFactory.getInstance(SITE_KEY_1);
2223

2324
assertThat(firstInstance).isEqualTo(secondInstance);
2425
}
26+
27+
@Test
28+
public void testGetInstance_callTwiceDifferentSiteKey_differentInstance() {
29+
RecaptchaEnterpriseAppCheckProviderFactory firstInstance =
30+
RecaptchaEnterpriseAppCheckProviderFactory.getInstance(SITE_KEY_1);
31+
RecaptchaEnterpriseAppCheckProviderFactory secondInstance =
32+
RecaptchaEnterpriseAppCheckProviderFactory.getInstance(SITE_KEY_2);
33+
34+
assertThat(firstInstance).isNotEqualTo(secondInstance);
35+
}
2536
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public class RecaptchaEnterpriseAppCheckProviderTest {
4747
private static final String RECAPTCHA_ENTERPRISE_TOKEN = "recaptchaEnterpriseToken";
4848
private final Executor liteExecutor = MoreExecutors.directExecutor();
4949
private final Executor blockingExecutor = MoreExecutors.directExecutor();
50-
private final SiteKey siteKey = new SiteKey("siteKey");
50+
private final String siteKey = "siteKey";
5151

5252
@Mock private NetworkClient mockNetworkClient;
5353
@Mock private Application mockApplication;

0 commit comments

Comments
 (0)