Skip to content

Merge executors to master #4322

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 6 commits into from
Nov 15, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import static com.google.android.gms.common.internal.Preconditions.checkNotNull;

import android.annotation.SuppressLint;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand Down Expand Up @@ -45,6 +46,8 @@ public class DebugAppCheckProvider implements AppCheckProvider {
private final RetryManager retryManager;
private final Task<String> debugSecretTask;

// TODO(b/258273630): Migrate to go/firebase-android-executors
@SuppressLint("ThreadPoolCreation")
public DebugAppCheckProvider(@NonNull FirebaseApp firebaseApp, @Nullable String debugSecret) {
checkNotNull(firebaseApp);
this.networkClient = new NetworkClient(firebaseApp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package com.google.firebase.appcheck.playintegrity.internal;

import android.annotation.SuppressLint;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import com.google.android.gms.tasks.Task;
Expand Down Expand Up @@ -41,6 +42,8 @@ public class PlayIntegrityAppCheckProvider implements AppCheckProvider {
private final ExecutorService backgroundExecutor;
private final RetryManager retryManager;

// TODO(b/258273630): Migrate to go/firebase-android-executors
@SuppressLint("ThreadPoolCreation")
public PlayIntegrityAppCheckProvider(@NonNull FirebaseApp firebaseApp) {
this(
firebaseApp.getOptions().getGcmSenderId(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static com.google.android.gms.common.internal.Preconditions.checkNotEmpty;
import static com.google.android.gms.common.internal.Preconditions.checkNotNull;

import android.annotation.SuppressLint;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
Expand Down Expand Up @@ -56,6 +57,8 @@ public class SafetyNetAppCheckProvider implements AppCheckProvider {
private final String apiKey;

/** @param firebaseApp the FirebaseApp to which this Factory is tied. */
// TODO(b/258273630): Migrate to go/firebase-android-executors
@SuppressLint("ThreadPoolCreation")
public SafetyNetAppCheckProvider(@NonNull FirebaseApp firebaseApp) {
this(
firebaseApp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import static com.google.android.gms.common.internal.Preconditions.checkNotNull;

import android.annotation.SuppressLint;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
Expand Down Expand Up @@ -57,6 +58,8 @@ public class DefaultFirebaseAppCheck extends FirebaseAppCheck {
private AppCheckProvider appCheckProvider;
private AppCheckToken cachedToken;

// TODO(b/258273630): Migrate to go/firebase-android-executors
@SuppressLint("ThreadPoolCreation")
public DefaultFirebaseAppCheck(
@NonNull FirebaseApp firebaseApp,
@NonNull Provider<HeartBeatController> heartBeatController) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;

import android.annotation.SuppressLint;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import com.google.android.gms.tasks.OnFailureListener;
Expand All @@ -41,6 +42,8 @@ public class DefaultTokenRefresher {
private volatile ScheduledFuture<?> refreshFuture;
private volatile long delayAfterFailureSeconds;

// TODO(b/258273630): Migrate to go/firebase-android-executors
@SuppressLint("ThreadPoolCreation")
DefaultTokenRefresher(@NonNull DefaultFirebaseAppCheck firebaseAppCheck) {
this(checkNotNull(firebaseAppCheck), Executors.newScheduledThreadPool(/* corePoolSize= */ 1));
}
Expand Down
4 changes: 4 additions & 0 deletions firebase-annotations/firebase-annotations.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ java {
tasks.withType(JavaCompile) {
options.compilerArgs << "-Werror"
}

dependencies {
implementation 'javax.inject:javax.inject:1'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2022 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.annotations.concurrent;

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
import javax.inject.Qualifier;

/**
* An executor/coroutine dispatcher for long running tasks including disk IO, heavy CPU
* computations.
*
* <p>For operations that can block for long periods of time, like network requests, use the {@link
* Blocking} executor.
*/
@Qualifier
@Target({ElementType.PARAMETER, ElementType.METHOD, ElementType.FIELD})
public @interface Background {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2022 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.annotations.concurrent;

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
import javax.inject.Qualifier;

/**
* An executor/coroutine dispatcher for tasks that can block for long periods of time, e.g network
* IO.
*/
@Qualifier
@Target({ElementType.PARAMETER, ElementType.METHOD, ElementType.FIELD})
public @interface Blocking {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2022 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.annotations.concurrent;

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
import javax.inject.Qualifier;

/**
* An executor/coroutine dispatcher for lightweight tasks that never block (on IO or other tasks).
*/
@Qualifier
@Target({ElementType.PARAMETER, ElementType.METHOD, ElementType.FIELD})
public @interface Lightweight {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2022 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.annotations.concurrent;

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
import javax.inject.Qualifier;

/** An executor/coroutine dispatcher for work that must run on the UI thread. */
@Qualifier
@Target({ElementType.PARAMETER, ElementType.METHOD, ElementType.FIELD})
public @interface UiThread {}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.google.firebase.appdistribution.impl.TaskUtils.runAsyncInTask;
import static com.google.firebase.appdistribution.impl.TaskUtils.safeSetTaskException;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
Expand Down Expand Up @@ -52,6 +53,8 @@ class AabUpdater {
@GuardedBy("updateAabLock")
private boolean hasBeenSentToPlayForCurrentTask = false;

// TODO(b/258264924): Migrate to go/firebase-android-executors
@SuppressLint("ThreadPoolCreation")
AabUpdater() {
this(
FirebaseAppDistributionLifecycleNotifier.getInstance(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.google.firebase.appdistribution.impl.TaskUtils.safeSetTaskException;
import static com.google.firebase.appdistribution.impl.TaskUtils.safeSetTaskResult;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
Expand Down Expand Up @@ -62,6 +63,8 @@ class ApkUpdater {

private final Object updateTaskLock = new Object();

// TODO(b/258264924): Migrate to go/firebase-android-executors
@SuppressLint("ThreadPoolCreation")
public ApkUpdater(@NonNull FirebaseApp firebaseApp, @NonNull ApkInstaller apkInstaller) {
this(
Executors.newSingleThreadExecutor(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import static com.google.firebase.appdistribution.impl.TaskUtils.runAsyncInTask;

import android.annotation.SuppressLint;
import androidx.annotation.NonNull;
import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.Tasks;
Expand Down Expand Up @@ -63,6 +64,9 @@ private interface FidDependentJob<TResult> {
private final FirebaseApp firebaseApp;
private final Provider<FirebaseInstallationsApi> firebaseInstallationsApiProvider;
private final TesterApiHttpClient testerApiHttpClient;

// TODO(b/258264924): Migrate to go/firebase-android-executors
@SuppressLint("ThreadPoolCreation")
private final Executor taskExecutor = Executors.newSingleThreadExecutor();

FirebaseAppDistributionTesterApiClient(
Expand Down
3 changes: 2 additions & 1 deletion firebase-common/firebase-common.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,11 @@ android {
}

dependencies {
// TODO(vkryachko): have sdks depend on components directly once components are released.
implementation project(':firebase-annotations')
implementation project(':firebase-components')
implementation 'com.google.android.gms:play-services-basement:18.1.0'
implementation "com.google.android.gms:play-services-tasks:18.0.1"
implementation 'androidx.concurrent:concurrent-futures:1.1.0'

// FirebaseApp references storage, so storage needs to be on classpath when dokka runs.
javadocClasspath project(path: ':firebase-storage')
Expand Down
1 change: 1 addition & 0 deletions firebase-common/ktx/ktx.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ android {
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"

implementation project(':firebase-annotations')
implementation project(':firebase-common')
implementation project(':firebase-components')
implementation 'androidx.annotation:annotation:1.1.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,18 @@ import android.content.Context
import androidx.annotation.Keep
import com.google.firebase.FirebaseApp
import com.google.firebase.FirebaseOptions
import com.google.firebase.annotations.concurrent.Background
import com.google.firebase.annotations.concurrent.Blocking
import com.google.firebase.annotations.concurrent.Lightweight
import com.google.firebase.annotations.concurrent.UiThread
import com.google.firebase.components.Component
import com.google.firebase.components.ComponentRegistrar
import com.google.firebase.components.Dependency
import com.google.firebase.components.Qualified
import com.google.firebase.platforminfo.LibraryVersionComponent
import java.util.concurrent.Executor
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.asCoroutineDispatcher

/**
* Single access point to all firebase SDKs from Kotlin.
Expand Down Expand Up @@ -56,6 +65,20 @@ internal const val LIBRARY_NAME: String = "fire-core-ktx"
@Keep
class FirebaseCommonKtxRegistrar : ComponentRegistrar {
override fun getComponents(): List<Component<*>> {
return listOf(LibraryVersionComponent.create(LIBRARY_NAME, BuildConfig.VERSION_NAME))
return listOf(
LibraryVersionComponent.create(LIBRARY_NAME, BuildConfig.VERSION_NAME),
coroutineDispatcher<Background>(),
coroutineDispatcher<Lightweight>(),
coroutineDispatcher<Blocking>(),
coroutineDispatcher<UiThread>()
)
}
}

private inline fun <reified T : Annotation> coroutineDispatcher(): Component<CoroutineDispatcher> =
Component.builder(Qualified.qualified(T::class.java, CoroutineDispatcher::class.java))
.add(Dependency.required(Qualified.qualified(T::class.java, Executor::class.java)))
.factory { c ->
c.get(Qualified.qualified(T::class.java, Executor::class.java)).asCoroutineDispatcher()
}
.build()
23 changes: 3 additions & 20 deletions firebase-common/src/main/java/com/google/firebase/FirebaseApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;
import androidx.annotation.GuardedBy;
Expand All @@ -47,6 +45,7 @@
import com.google.firebase.components.ComponentRegistrar;
import com.google.firebase.components.ComponentRuntime;
import com.google.firebase.components.Lazy;
import com.google.firebase.concurrent.ExecutorsRegistrar;
import com.google.firebase.events.Publisher;
import com.google.firebase.heartbeatinfo.DefaultHeartBeatController;
import com.google.firebase.inject.Provider;
Expand All @@ -59,7 +58,6 @@
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;

Expand Down Expand Up @@ -97,16 +95,10 @@ public class FirebaseApp {

private static final Object LOCK = new Object();

private static final Executor UI_EXECUTOR = new UiExecutor();

/** A map of (name, FirebaseApp) instances. */
@GuardedBy("LOCK")
static final Map<String, FirebaseApp> INSTANCES = new ArrayMap<>();

private static final String FIREBASE_ANDROID = "fire-android";
private static final String FIREBASE_COMMON = "fire-core";
private static final String KOTLIN = "kotlin";

private final Context applicationContext;
private final String name;
private final FirebaseOptions options;
Expand Down Expand Up @@ -427,9 +419,10 @@ protected FirebaseApp(Context applicationContext, String name, FirebaseOptions o

FirebaseTrace.pushTrace("Runtime");
componentRuntime =
ComponentRuntime.builder(UI_EXECUTOR)
ComponentRuntime.builder(com.google.firebase.concurrent.UiExecutor.INSTANCE)
.addLazyComponentRegistrars(registrars)
.addComponentRegistrar(new FirebaseCommonRegistrar())
.addComponentRegistrar(new ExecutorsRegistrar())
.addComponent(Component.of(applicationContext, Context.class))
.addComponent(Component.of(this, FirebaseApp.class))
.addComponent(Component.of(options, FirebaseOptions.class))
Expand Down Expand Up @@ -712,14 +705,4 @@ public void onBackgroundStateChanged(boolean background) {
}
}
}

private static class UiExecutor implements Executor {

private static final Handler HANDLER = new Handler(Looper.getMainLooper());

@Override
public void execute(@NonNull Runnable command) {
HANDLER.post(command);
}
}
}
Loading