Skip to content

Use dagger in functions. #4366

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 2 commits into from
Nov 28, 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
1 change: 1 addition & 0 deletions firebase-functions/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Unreleased
* [changed] Avoid executing code on the UI thread as much as possible.
* [changed] Internal infrastructure improvements.

# 20.2.1
* [changed] Updated dependency of `firebase-iid` to its latest
Expand Down
7 changes: 7 additions & 0 deletions firebase-functions/firebase-functions.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

plugins {
id 'firebase-library'
id 'firebase-vendor'
}

firebaseLibrary {
Expand Down Expand Up @@ -68,6 +69,12 @@ dependencies {

implementation 'com.squareup.okhttp3:okhttp:3.12.1'

implementation 'javax.inject:javax.inject:1'
vendor ('com.google.dagger:dagger:2.43.2') {
exclude group: "javax.inject", module: "javax.inject"
}
annotationProcessor 'com.google.dagger:dagger-compiler:2.43.2'

annotationProcessor 'com.google.auto.value:auto-value:1.6.2'

javadocClasspath 'com.google.code.findbugs:jsr305:3.0.2'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ public void testToken() throws InterruptedException, ExecutionException {
// Override the normal token provider to simulate FirebaseAuth being logged in.
FirebaseFunctions functions =
new FirebaseFunctions(
app,
app.getApplicationContext(),
app.getOptions().getProjectId(),
"us-central1",
Expand All @@ -108,7 +107,6 @@ public void testInstanceId() throws InterruptedException, ExecutionException {
// Override the normal token provider to simulate FirebaseAuth being logged in.
FirebaseFunctions functions =
new FirebaseFunctions(
app,
app.getApplicationContext(),
app.getOptions().getProjectId(),
"us-central1",
Expand All @@ -131,7 +129,6 @@ public void testAppCheck() throws InterruptedException, ExecutionException {
// Override the normal token provider to simulate FirebaseAuth being logged in.
FirebaseFunctions functions =
new FirebaseFunctions(
app,
app.getApplicationContext(),
app.getOptions().getProjectId(),
"us-central1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@
import com.google.firebase.internal.api.FirebaseNoSignedInUserException;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicReference;
import javax.inject.Inject;
import javax.inject.Singleton;

/** A ContextProvider that uses FirebaseAuth to get the token. */
@Singleton
class FirebaseContextProvider implements ContextProvider {
private final String TAG = "FirebaseContextProvider";

Expand All @@ -37,6 +40,7 @@ class FirebaseContextProvider implements ContextProvider {
new AtomicReference<>();
private final Executor executor;

@Inject
FirebaseContextProvider(
Provider<InternalAuthProvider> tokenProvider,
Provider<FirebaseInstanceIdInternal> instanceId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@
import com.google.firebase.annotations.concurrent.UiThread;
import com.google.firebase.emulators.EmulatedServiceSettings;
import com.google.firebase.functions.FirebaseFunctionsException.Code;
import dagger.assisted.Assisted;
import dagger.assisted.AssistedInject;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executor;
import javax.inject.Named;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
Expand All @@ -59,9 +62,6 @@ public class FirebaseFunctions {
*/
private static boolean providerInstallStarted = false;

// The FirebaseApp instance
private final FirebaseApp app;

// The network client to use for HTTPS requests.
private final OkHttpClient client;

Expand All @@ -88,15 +88,14 @@ public class FirebaseFunctions {
// Emulator settings
@Nullable private EmulatedServiceSettings emulatorSettings;

@AssistedInject
FirebaseFunctions(
FirebaseApp app,
Context context,
String projectId,
String regionOrCustomDomain,
@Named("projectId") String projectId,
@Assisted String regionOrCustomDomain,
ContextProvider contextProvider,
@Lightweight Executor executor,
@UiThread Executor uiExecutor) {
this.app = app;
this.executor = executor;
this.client = new OkHttpClient();
this.serializer = new Serializer();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright 2018 Google LLC
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Copyright 2018 Google LLC
// Copyright 2022 Google LLC

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops, looks like auto merge does not take these comments into account

//
// 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.functions;

import android.content.Context;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.annotations.concurrent.Lightweight;
import com.google.firebase.annotations.concurrent.UiThread;
import com.google.firebase.appcheck.interop.InternalAppCheckTokenProvider;
import com.google.firebase.auth.internal.InternalAuthProvider;
import com.google.firebase.iid.internal.FirebaseInstanceIdInternal;
import com.google.firebase.inject.Deferred;
import com.google.firebase.inject.Provider;
import dagger.Binds;
import dagger.BindsInstance;
import dagger.Component;
import dagger.Module;
import dagger.Provides;
import java.util.concurrent.Executor;
import javax.inject.Named;
import javax.inject.Singleton;

/** @hide */
@Component(modules = FunctionsComponent.MainModule.class)
@Singleton
interface FunctionsComponent {
FunctionsMultiResourceComponent getMultiResourceComponent();

@Component.Builder
interface Builder {
@BindsInstance
Builder setApplicationContext(Context applicationContext);

@BindsInstance
Builder setFirebaseOptions(FirebaseOptions options);

@BindsInstance
Builder setLiteExecutor(@Lightweight Executor executor);

@BindsInstance
Builder setUiExecutor(@UiThread Executor executor);

@BindsInstance
Builder setAuth(Provider<InternalAuthProvider> auth);

@BindsInstance
Builder setIid(Provider<FirebaseInstanceIdInternal> iid);

@BindsInstance
Builder setAppCheck(Deferred<InternalAppCheckTokenProvider> appCheck);

FunctionsComponent build();
}

@Module
interface MainModule {
@Provides
@Named("projectId")
static String bindProjectId(FirebaseOptions options) {
return options.getProjectId();
}

@Binds
ContextProvider contextProvider(FirebaseContextProvider provider);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@

package com.google.firebase.functions;

import android.content.Context;
import androidx.annotation.GuardedBy;
import androidx.annotation.UiThread;
import com.google.firebase.FirebaseApp;
import com.google.firebase.annotations.concurrent.Lightweight;
import dagger.assisted.Assisted;
import dagger.assisted.AssistedFactory;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executor;
import javax.inject.Inject;
import javax.inject.Singleton;

/** Multi-resource container for Functions. */
@Singleton
class FunctionsMultiResourceComponent {
/**
* A static map from instance key to FirebaseFunctions instances. Instance keys region names.
Expand All @@ -33,41 +33,24 @@ class FunctionsMultiResourceComponent {
@GuardedBy("this")
private final Map<String, FirebaseFunctions> instances = new HashMap<>();

private final Context applicationContext;
private final ContextProvider contextProvider;
private final FirebaseApp app;
private final Executor liteExecutor;
private final Executor uiExecutor;
private final FirebaseFunctionsFactory functionsFactory;

FunctionsMultiResourceComponent(
Context applicationContext,
ContextProvider contextProvider,
FirebaseApp app,
@Lightweight Executor liteExecutor,
@UiThread Executor uiExecutor) {
this.applicationContext = applicationContext;
this.contextProvider = contextProvider;
this.app = app;
this.liteExecutor = liteExecutor;
this.uiExecutor = uiExecutor;
@Inject
FunctionsMultiResourceComponent(FirebaseFunctionsFactory functionsFactory) {
this.functionsFactory = functionsFactory;
}

synchronized FirebaseFunctions get(String regionOrCustomDomain) {
FirebaseFunctions functions = instances.get(regionOrCustomDomain);
String projectId = app.getOptions().getProjectId();

if (functions == null) {
functions =
new FirebaseFunctions(
app,
applicationContext,
projectId,
regionOrCustomDomain,
contextProvider,
liteExecutor,
uiExecutor);
functions = functionsFactory.create(regionOrCustomDomain);
instances.put(regionOrCustomDomain, functions);
}
return functions;
}

@AssistedFactory
interface FirebaseFunctionsFactory {
FirebaseFunctions create(@Assisted String regionOrCustomDomain);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import android.content.Context;
import androidx.annotation.Keep;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.annotations.concurrent.Lightweight;
import com.google.firebase.annotations.concurrent.UiThread;
import com.google.firebase.appcheck.interop.InternalAppCheckTokenProvider;
Expand Down Expand Up @@ -45,34 +45,27 @@ public List<Component<?>> getComponents() {
Qualified<Executor> liteExecutor = Qualified.qualified(Lightweight.class, Executor.class);
Qualified<Executor> uiExecutor = Qualified.qualified(UiThread.class, Executor.class);
return Arrays.asList(
Component.builder(ContextProvider.class)
.add(Dependency.optionalProvider(InternalAuthProvider.class))
.add(Dependency.requiredProvider(FirebaseInstanceIdInternal.class))
.add(Dependency.deferred(InternalAppCheckTokenProvider.class))
.add(Dependency.required(liteExecutor))
.factory(
c ->
new FirebaseContextProvider(
c.getProvider(InternalAuthProvider.class),
c.getProvider(FirebaseInstanceIdInternal.class),
c.getDeferred(InternalAppCheckTokenProvider.class),
c.get(liteExecutor)))
.build(),
Component.builder(FunctionsMultiResourceComponent.class)
.name(LIBRARY_NAME)
.add(Dependency.required(Context.class))
.add(Dependency.required(ContextProvider.class))
.add(Dependency.required(FirebaseApp.class))
.add(Dependency.required(FirebaseOptions.class))
.add(Dependency.optionalProvider(InternalAuthProvider.class))
.add(Dependency.requiredProvider(FirebaseInstanceIdInternal.class))
.add(Dependency.deferred(InternalAppCheckTokenProvider.class))
.add(Dependency.required(liteExecutor))
.add(Dependency.required(uiExecutor))
.factory(
c ->
new FunctionsMultiResourceComponent(
c.get(Context.class),
c.get(ContextProvider.class),
c.get(FirebaseApp.class),
c.get(liteExecutor),
c.get(uiExecutor)))
DaggerFunctionsComponent.builder()
.setApplicationContext(c.get(Context.class))
.setFirebaseOptions(c.get(FirebaseOptions.class))
.setLiteExecutor(c.get(liteExecutor))
.setUiExecutor(c.get(uiExecutor))
.setAuth(c.getProvider(InternalAuthProvider.class))
.setIid(c.getProvider(FirebaseInstanceIdInternal.class))
.setAppCheck(c.getDeferred(InternalAppCheckTokenProvider.class))
.build()
.getMultiResourceComponent())
.build(),
LibraryVersionComponent.create(LIBRARY_NAME, BuildConfig.VERSION_NAME));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Serializer {

private final DateFormat dateFormat;

public Serializer() {
Serializer() {
// Encode Dates as UTC ISO 8601 strings.
dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Expand Down