Skip to content

Commit

Permalink
Crashlytics common executor (#6119)
Browse files Browse the repository at this point in the history
Cherry-pick #6019,
#6040 to main

#no-changelog
  • Loading branch information
themiswang authored Jul 24, 2024
1 parent 9a1d120 commit fd33b50
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@

import com.google.firebase.FirebaseApp;
import com.google.firebase.analytics.connector.AnalyticsConnector;
import com.google.firebase.annotations.concurrent.Background;
import com.google.firebase.annotations.concurrent.Blocking;
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.components.Qualified;
import com.google.firebase.crashlytics.internal.CrashlyticsNativeComponent;
import com.google.firebase.inject.Deferred;
import com.google.firebase.installations.FirebaseInstallationsApi;
Expand All @@ -29,10 +32,15 @@
import com.google.firebase.sessions.api.SessionSubscriber;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutorService;

/** @hide */
public class CrashlyticsRegistrar implements ComponentRegistrar {
private static final String LIBRARY_NAME = "fire-cls";
private final Qualified<ExecutorService> backgroundExecutorService =
Qualified.qualified(Background.class, ExecutorService.class);
private final Qualified<ExecutorService> blockingExecutorService =
Qualified.qualified(Blocking.class, ExecutorService.class);

static {
// Add Crashlytics as a dependency of Sessions when this class is loaded into memory.
Expand All @@ -46,6 +54,8 @@ public List<Component<?>> getComponents() {
.name(LIBRARY_NAME)
.add(Dependency.required(FirebaseApp.class))
.add(Dependency.required(FirebaseInstallationsApi.class))
.add(Dependency.required(backgroundExecutorService))
.add(Dependency.required(blockingExecutorService))
.add(Dependency.deferred(CrashlyticsNativeComponent.class))
.add(Dependency.deferred(AnalyticsConnector.class))
.add(Dependency.deferred(FirebaseRemoteConfigInterop.class))
Expand All @@ -70,6 +80,12 @@ private FirebaseCrashlytics buildCrashlytics(ComponentContainer container) {
container.getDeferred(FirebaseRemoteConfigInterop.class);

return FirebaseCrashlytics.init(
app, firebaseInstallations, nativeComponent, analyticsConnector, remoteConfigInterop);
app,
firebaseInstallations,
nativeComponent,
analyticsConnector,
remoteConfigInterop,
container.get(backgroundExecutorService),
container.get(blockingExecutorService));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
import androidx.annotation.VisibleForTesting;
import com.google.android.gms.tasks.Continuation;
import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.Tasks;
import com.google.firebase.FirebaseApp;
import com.google.firebase.analytics.connector.AnalyticsConnector;
import com.google.firebase.annotations.concurrent.Background;
import com.google.firebase.annotations.concurrent.Blocking;
import com.google.firebase.crashlytics.internal.CrashlyticsNativeComponent;
import com.google.firebase.crashlytics.internal.CrashlyticsNativeComponentDeferredProxy;
import com.google.firebase.crashlytics.internal.DevelopmentPlatformProvider;
Expand All @@ -45,7 +46,7 @@
import com.google.firebase.remoteconfig.interop.FirebaseRemoteConfigInterop;
import com.google.firebase.sessions.api.FirebaseSessionsDependencies;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;

/**
Expand All @@ -68,7 +69,9 @@ public class FirebaseCrashlytics {
@NonNull FirebaseInstallationsApi firebaseInstallationsApi,
@NonNull Deferred<CrashlyticsNativeComponent> nativeComponent,
@NonNull Deferred<AnalyticsConnector> analyticsConnector,
@NonNull Deferred<FirebaseRemoteConfigInterop> remoteConfigInteropDeferred) {
@NonNull Deferred<FirebaseRemoteConfigInterop> remoteConfigInteropDeferred,
@Background ExecutorService backgroundExecutorService,
@Blocking ExecutorService blockingExecutorService) {

Context context = app.getApplicationContext();
final String appIdentifier = context.getPackageName();
Expand Down Expand Up @@ -147,8 +150,8 @@ public class FirebaseCrashlytics {

Logger.getLogger().v("Installer package name is: " + appData.installerPackageName);

final ExecutorService threadPoolExecutor =
ExecutorUtils.buildSingleThreadExecutorService("com.google.firebase.crashlytics.startup");
final Executor threadPoolExecutor =
ExecutorUtils.buildSequentialExecutor(backgroundExecutorService);

final SettingsController settingsController =
SettingsController.create(
Expand Down Expand Up @@ -178,17 +181,9 @@ public Object then(@NonNull Task<Void> task) throws Exception {

final boolean finishCoreInBackground = core.onPreExecute(appData, settingsController);

Tasks.call(
threadPoolExecutor,
new Callable<Void>() {
@Override
public Void call() throws Exception {
if (finishCoreInBackground) {
core.doBackgroundInitializationAsync(settingsController);
}
return null;
}
});
if (finishCoreInBackground) {
core.doBackgroundInitializationAsync(settingsController);
}

return new FirebaseCrashlytics(core);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
import static java.util.concurrent.TimeUnit.SECONDS;

import android.annotation.SuppressLint;
import com.google.firebase.concurrent.FirebaseExecutors;
import com.google.firebase.crashlytics.internal.Logger;
import java.util.Locale;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
Expand All @@ -34,6 +36,10 @@ public final class ExecutorUtils {

private ExecutorUtils() {}

public static Executor buildSequentialExecutor(Executor commonExecutor) {
return FirebaseExecutors.newSequentialExecutor(commonExecutor);
}

public static ExecutorService buildSingleThreadExecutorService(String name) {
final ThreadFactory threadFactory = ExecutorUtils.getNamedThreadFactory(name);
final ExecutorService executor =
Expand Down

0 comments on commit fd33b50

Please sign in to comment.