Skip to content

Add DI to FirebasePerfRegistrar.java #2406

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Feb 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions firebase-perf/firebase-perf.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ dependencies {
// 3rd Party Deps
implementation 'com.squareup.okhttp3:okhttp:3.12.1'
compileOnly group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.6'
implementation 'com.google.dagger:dagger:2.27'
annotationProcessor 'com.google.dagger:dagger-compiler:2.27'

// Unit Test Deps
testImplementation 'junit:junit:4.12'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@
import com.google.android.datatransport.TransportFactory;
import com.google.firebase.FirebaseApp;
import com.google.firebase.components.Component;
import com.google.firebase.components.ComponentContainer;
import com.google.firebase.components.ComponentRegistrar;
import com.google.firebase.components.Dependency;
import com.google.firebase.installations.FirebaseInstallationsApi;
import com.google.firebase.perf.injection.components.DaggerFirebasePerformanceComponent;
import com.google.firebase.perf.injection.components.FirebasePerformanceComponent;
import com.google.firebase.perf.injection.modules.FirebasePerformanceModule;
import com.google.firebase.platforminfo.LibraryVersionComponent;
import com.google.firebase.remoteconfig.RemoteConfigComponent;
import java.util.Arrays;
Expand All @@ -47,13 +51,7 @@ public List<Component<?>> getComponents() {
.add(Dependency.requiredProvider(RemoteConfigComponent.class))
.add(Dependency.required(FirebaseInstallationsApi.class))
.add(Dependency.requiredProvider(TransportFactory.class))
.factory(
container ->
new FirebasePerformance(
container.get(FirebaseApp.class),
container.getProvider(RemoteConfigComponent.class),
container.get(FirebaseInstallationsApi.class),
container.getProvider(TransportFactory.class)))
.factory(FirebasePerfRegistrar::providesFirebasePerformance)
// Since the SDK is eager(auto starts at app start), we use "lazy" dependency for some
// components that are not required during initialization so as not to force initialize
// them at app startup (refer
Expand All @@ -62,4 +60,18 @@ public List<Component<?>> getComponents() {
.build(),
LibraryVersionComponent.create("fire-perf", BuildConfig.VERSION_NAME));
}

private static FirebasePerformance providesFirebasePerformance(ComponentContainer container) {
FirebasePerformanceComponent component =
DaggerFirebasePerformanceComponent.builder()
.firebasePerformanceModule(
new FirebasePerformanceModule(
container.get(FirebaseApp.class),
container.get(FirebaseInstallationsApi.class),
container.getProvider(RemoteConfigComponent.class),
container.getProvider(TransportFactory.class)))
.build();

return component.getFirebasePerformance();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.inject.Inject;

/**
* The Firebase Performance Monitoring API.
Expand Down Expand Up @@ -132,22 +133,10 @@ public static FirebasePerformance getInstance() {
// to false if it's been force disabled or it is set to null if neither.
@Nullable private Boolean mPerformanceCollectionForceEnabledState = null;

/** This constructor is invoked from {@link com.google.firebase.perf.FirebasePerfRegistrar}. */
FirebasePerformance(
FirebaseApp firebaseApp,
Provider<RemoteConfigComponent> firebaseRemoteConfigProvider,
FirebaseInstallationsApi firebaseInstallationsApi,
Provider<TransportFactory> transportFactoryProvider) {

this(
firebaseApp,
firebaseRemoteConfigProvider,
firebaseInstallationsApi,
transportFactoryProvider,
RemoteConfigManager.getInstance(),
ConfigResolver.getInstance(),
GaugeManager.getInstance());
}
private final FirebaseApp firebaseApp;
private final Provider<RemoteConfigComponent> firebaseRemoteConfigProvider;
private final FirebaseInstallationsApi firebaseInstallationsApi;
private final Provider<TransportFactory> transportFactoryProvider;

/**
* Constructs the FirebasePerformance class and allows injecting dependencies.
Expand All @@ -164,6 +153,7 @@ public static FirebasePerformance getInstance() {
* @param gaugeManager The GaugeManager instance.
*/
@VisibleForTesting
@Inject
FirebasePerformance(
FirebaseApp firebaseApp,
Provider<RemoteConfigComponent> firebaseRemoteConfigProvider,
Expand All @@ -173,6 +163,11 @@ public static FirebasePerformance getInstance() {
ConfigResolver configResolver,
GaugeManager gaugeManager) {

this.firebaseApp = firebaseApp;
this.firebaseRemoteConfigProvider = firebaseRemoteConfigProvider;
this.firebaseInstallationsApi = firebaseInstallationsApi;
this.transportFactoryProvider = transportFactoryProvider;

if (firebaseApp == null) {
this.mPerformanceCollectionForceEnabledState = false;
this.configResolver = configResolver;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.firebase.perf.injection.components;

import androidx.annotation.NonNull;
import com.google.firebase.perf.FirebasePerformance;
import com.google.firebase.perf.injection.modules.FirebasePerformanceModule;
import dagger.Component;

/**
* Dagger component to create FirebasePerformanceComponent Objects.
*
* @hide
*/
@Component(modules = {FirebasePerformanceModule.class})
public interface FirebasePerformanceComponent {
@NonNull
FirebasePerformance getFirebasePerformance();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.firebase.perf.injection.modules;

import androidx.annotation.NonNull;
import com.google.android.datatransport.TransportFactory;
import com.google.firebase.FirebaseApp;
import com.google.firebase.inject.Provider;
import com.google.firebase.installations.FirebaseInstallationsApi;
import com.google.firebase.perf.FirebasePerformance;
import com.google.firebase.perf.config.ConfigResolver;
import com.google.firebase.perf.internal.GaugeManager;
import com.google.firebase.perf.internal.RemoteConfigManager;
import com.google.firebase.remoteconfig.RemoteConfigComponent;
import dagger.Module;
import dagger.Provides;

/**
* Provider for {@link FirebasePerformance}.
*
* @hide
*/
@Module
public class FirebasePerformanceModule {
private final FirebaseApp firebaseApp;
private final FirebaseInstallationsApi firebaseInstallations;
private final Provider<RemoteConfigComponent> remoteConfigComponentProvider;
private final Provider<TransportFactory> transportFactoryProvider;

public FirebasePerformanceModule(
@NonNull FirebaseApp firebaseApp,
@NonNull FirebaseInstallationsApi firebaseInstallations,
@NonNull Provider<RemoteConfigComponent> remoteConfigComponentProvider,
@NonNull Provider<TransportFactory> transportFactoryProvider) {
this.firebaseApp = firebaseApp;
this.firebaseInstallations = firebaseInstallations;
this.remoteConfigComponentProvider = remoteConfigComponentProvider;
this.transportFactoryProvider = transportFactoryProvider;
}

@Provides
FirebaseApp providesFirebaseApp() {
return firebaseApp;
}

@Provides
FirebaseInstallationsApi providesFirebaseInstallations() {
return firebaseInstallations;
}

@Provides
Provider<RemoteConfigComponent> providesRemoteConfigComponent() {
return remoteConfigComponentProvider;
}

@Provides
Provider<TransportFactory> providesTransportFactoryProvider() {
return transportFactoryProvider;
}

@Provides
RemoteConfigManager providesRemoteConfigManager() {
return RemoteConfigManager.getInstance();
}

@Provides
ConfigResolver providesConfigResolver() {
return ConfigResolver.getInstance();
}

@Provides
GaugeManager providesGaugeManager() {
return GaugeManager.getInstance();
}
}