Skip to content

Commit 4027c4d

Browse files
Resolves comments
1 parent 24056ce commit 4027c4d

File tree

7 files changed

+95
-45
lines changed

7 files changed

+95
-45
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,15 @@ android {
4646
}
4747

4848
dependencies {
49+
implementation(libs.dagger.dagger)
50+
4951
api project(':appcheck:firebase-appcheck')
5052
api 'com.google.firebase:firebase-common'
5153
api 'com.google.firebase:firebase-components'
5254
api 'com.google.android.recaptcha:recaptcha:18.7.1'
5355

56+
annotationProcessor(libs.dagger.compiler)
57+
5458
testImplementation(project(":integ-testing")) {
5559
exclude group: 'com.google.firebase', module: 'firebase-common'
5660
exclude group: 'com.google.firebase', module: 'firebase-components'

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515
package com.google.firebase.appcheck.recaptchaenterprise;
1616

1717
import com.google.android.gms.common.annotation.KeepForSdk;
18+
import com.google.firebase.FirebaseApp;
1819
import com.google.firebase.annotations.concurrent.Blocking;
1920
import com.google.firebase.annotations.concurrent.Lightweight;
20-
import com.google.firebase.appcheck.recaptchaenterprise.internal.FirebaseExecutors;
21+
import com.google.firebase.appcheck.recaptchaenterprise.internal.ProviderMultiResourceComponent;
2122
import com.google.firebase.components.Component;
2223
import com.google.firebase.components.ComponentRegistrar;
2324
import com.google.firebase.components.Dependency;
2425
import com.google.firebase.components.Qualified;
26+
import com.google.firebase.appcheck.recaptchaenterprise.internal.DaggerProviderComponent;
2527
import com.google.firebase.platforminfo.LibraryVersionComponent;
2628
import java.util.Arrays;
2729
import java.util.List;
@@ -43,14 +45,19 @@ public List<Component<?>> getComponents() {
4345
Qualified<Executor> blockingExecutor = Qualified.qualified(Blocking.class, Executor.class);
4446

4547
return Arrays.asList(
46-
Component.builder(FirebaseExecutors.class)
48+
Component.builder(ProviderMultiResourceComponent.class)
4749
.name(LIBRARY_NAME)
50+
.add(Dependency.required(FirebaseApp.class))
4851
.add(Dependency.required(liteExecutor))
4952
.add(Dependency.required(blockingExecutor))
5053
.factory(
5154
container ->
52-
new FirebaseExecutors(
53-
container.get(liteExecutor), container.get(blockingExecutor)))
55+
DaggerProviderComponent.builder()
56+
.setFirebaseApp(container.get(FirebaseApp.class))
57+
.setLiteExecutor(container.get(liteExecutor))
58+
.setBlockingExecutor(container.get(blockingExecutor))
59+
.build()
60+
.getMultiResourceComponent())
5461
.build(),
5562
LibraryVersionComponent.create(LIBRARY_NAME, BuildConfig.VERSION_NAME));
5663
}

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

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@
1414

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

17-
import android.app.Application;
1817
import androidx.annotation.NonNull;
1918
import com.google.firebase.FirebaseApp;
2019
import com.google.firebase.appcheck.AppCheckProvider;
2120
import com.google.firebase.appcheck.AppCheckProviderFactory;
2221
import com.google.firebase.appcheck.FirebaseAppCheck;
23-
import com.google.firebase.appcheck.recaptchaenterprise.internal.FirebaseExecutors;
22+
import com.google.firebase.appcheck.recaptchaenterprise.internal.ProviderMultiResourceComponent;
2423
import com.google.firebase.appcheck.recaptchaenterprise.internal.RecaptchaEnterpriseAppCheckProvider;
2524
import java.util.Map;
2625
import java.util.concurrent.ConcurrentHashMap;
@@ -31,7 +30,6 @@
3130
*/
3231
public class RecaptchaEnterpriseAppCheckProviderFactory implements AppCheckProviderFactory {
3332

34-
private static FirebaseExecutors firebaseExecutors;
3533
private static final Map<String, RecaptchaEnterpriseAppCheckProviderFactory> factoryInstances =
3634
new ConcurrentHashMap<>();
3735
private final String siteKey;
@@ -55,18 +53,9 @@ public AppCheckProvider create(@NonNull FirebaseApp firebaseApp) {
5553
if (provider == null) {
5654
synchronized (this) {
5755
if (provider == null) {
58-
if (RecaptchaEnterpriseAppCheckProviderFactory.firebaseExecutors == null) {
59-
firebaseExecutors = firebaseApp.get(FirebaseExecutors.class);
60-
}
61-
Application application = (Application) firebaseApp.getApplicationContext();
62-
63-
provider =
64-
new RecaptchaEnterpriseAppCheckProvider(
65-
firebaseApp,
66-
application,
67-
siteKey,
68-
firebaseExecutors.getLiteExecutor(),
69-
firebaseExecutors.getBlockingExecutor());
56+
ProviderMultiResourceComponent component =
57+
firebaseApp.get(ProviderMultiResourceComponent.class);
58+
provider = component.get(siteKey);
7059
}
7160
}
7261
}
Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,34 @@
1414

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

17+
import com.google.firebase.FirebaseApp;
1718
import com.google.firebase.annotations.concurrent.Blocking;
1819
import com.google.firebase.annotations.concurrent.Lightweight;
1920
import java.util.concurrent.Executor;
21+
import javax.inject.Singleton;
22+
import dagger.BindsInstance;
23+
import dagger.Component;
24+
import dagger.Module;
2025

21-
/**
22-
* This class encapsulates a {@link com.google.firebase.annotations.concurrent.Lightweight} executor
23-
* and a {@link com.google.firebase.annotations.concurrent.Blocking} executor, making them available
24-
* for various asynchronous operations related to reCAPTCHA Enterprise App Check.
25-
*/
26-
public class FirebaseExecutors {
27-
private final Executor liteExecutor;
28-
private final Executor blockingExecutor;
29-
30-
public FirebaseExecutors(
31-
@Lightweight Executor liteExecutor, @Blocking Executor blockingExecutor) {
32-
this.liteExecutor = liteExecutor;
33-
this.blockingExecutor = blockingExecutor;
34-
}
26+
@Singleton
27+
@Component(modules = ProviderComponent.MainModule.class)
28+
public interface ProviderComponent {
29+
ProviderMultiResourceComponent getMultiResourceComponent();
3530

36-
public Executor getLiteExecutor() {
37-
return liteExecutor;
38-
}
31+
@Component.Builder
32+
interface Builder {
33+
@BindsInstance
34+
Builder setFirebaseApp(FirebaseApp firebaseApp);
35+
36+
@BindsInstance
37+
Builder setLiteExecutor(@Lightweight Executor liteExecutor);
3938

40-
public Executor getBlockingExecutor() {
41-
return blockingExecutor;
39+
@BindsInstance
40+
Builder setBlockingExecutor(@Blocking Executor blockingExecutor);
41+
42+
ProviderComponent build();
4243
}
44+
45+
@Module
46+
abstract class MainModule {}
4347
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.firebase.appcheck.recaptchaenterprise.internal;
16+
17+
import androidx.annotation.NonNull;
18+
19+
import java.util.Map;
20+
import java.util.concurrent.ConcurrentHashMap;
21+
import javax.inject.Inject;
22+
import javax.inject.Singleton;
23+
import dagger.assisted.Assisted;
24+
import dagger.assisted.AssistedFactory;
25+
26+
/** Multi-resource container for RecaptchaEnterpriseAppCheckProvider */
27+
@Singleton
28+
public final class ProviderMultiResourceComponent {
29+
private final RecaptchaEnterpriseAppCheckProviderFactory providerFactory;
30+
31+
private final Map<String, RecaptchaEnterpriseAppCheckProvider> instances =
32+
new ConcurrentHashMap<>();
33+
34+
@Inject
35+
ProviderMultiResourceComponent(RecaptchaEnterpriseAppCheckProviderFactory providerFactory) {
36+
this.providerFactory = providerFactory;
37+
}
38+
39+
@NonNull
40+
public RecaptchaEnterpriseAppCheckProvider get(@NonNull String siteKey) {
41+
return instances.computeIfAbsent(siteKey, providerFactory::create);
42+
}
43+
44+
@AssistedFactory
45+
interface RecaptchaEnterpriseAppCheckProviderFactory {
46+
RecaptchaEnterpriseAppCheckProvider create(@Assisted String siteKey);
47+
}
48+
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import java.nio.charset.StandardCharsets;
3535
import java.util.Objects;
3636
import java.util.concurrent.Executor;
37+
import dagger.assisted.Assisted;
38+
import dagger.assisted.AssistedInject;
3739

3840
/**
3941
* An implementation of {@link AppCheckProvider} that uses reCAPTCHA Enterprise for device
@@ -59,13 +61,13 @@ public class RecaptchaEnterpriseAppCheckProvider implements AppCheckProvider {
5961
private Application application;
6062
private static final String TAG = "rCEAppCheckProvider";
6163

64+
@AssistedInject
6265
public RecaptchaEnterpriseAppCheckProvider(
6366
@NonNull FirebaseApp firebaseApp,
64-
@NonNull Application application,
65-
@NonNull String siteKey,
67+
@Assisted @NonNull String siteKey,
6668
@Lightweight Executor liteExecutor,
6769
@Blocking Executor blockingExecutor) {
68-
this.application = application;
70+
this.application = (Application) firebaseApp.getApplicationContext();
6971
this.siteKey = siteKey;
7072
this.liteExecutor = liteExecutor;
7173
this.blockingExecutor = blockingExecutor;

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import static org.mockito.Mockito.verify;
2222
import static org.mockito.Mockito.when;
2323

24-
import android.app.Application;
2524
import com.google.android.gms.tasks.Task;
2625
import com.google.android.gms.tasks.Tasks;
2726
import com.google.android.recaptcha.RecaptchaAction;
@@ -62,7 +61,6 @@ public class RecaptchaEnterpriseAppCheckProviderTest {
6261
private final String siteKey = "siteKey";
6362

6463
@Mock private NetworkClient mockNetworkClient;
65-
@Mock private Application mockApplication;
6664
@Mock FirebaseApp mockFirebaseApp;
6765
@Mock RecaptchaTasksClient mockRecaptchaTasksClient;
6866
@Mock RetryManager mockRetryManager;
@@ -82,7 +80,6 @@ public void testPublicConstructor_nullFirebaseApp_expectThrows() {
8280
() ->
8381
new RecaptchaEnterpriseAppCheckProvider(
8482
null,
85-
mockApplication,
8683
siteKey,
8784
TestOnlyExecutors.lite(),
8885
TestOnlyExecutors.blocking()));
@@ -95,7 +92,6 @@ public void testPublicConstructor_nullSiteKey_expectThrows() {
9592
() ->
9693
new RecaptchaEnterpriseAppCheckProvider(
9794
mockFirebaseApp,
98-
mockApplication,
9995
null,
10096
TestOnlyExecutors.lite(),
10197
TestOnlyExecutors.blocking()));

0 commit comments

Comments
 (0)