Skip to content
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-database/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
and are not mentioned in our documentation.
- [changed] Improved error messages for certain Number types that are not
supported by our serialization layer (#272).
- [internal] Updated the SDK initialization process and removed usages of
deprecated method

# 16.0.6
- [fixed] Fixed an issue that could cause a NullPointerException during the
Expand Down
3 changes: 3 additions & 0 deletions firebase-database/firebase-database.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ dependencies {
implementation "com.google.android.gms:play-services-basement:$playServicesVersion"
implementation "com.google.android.gms:play-services-base:$playServicesVersion"
implementation "com.google.android.gms:play-services-tasks:$playServicesVersion"
implementation('com.google.firebase:firebase-auth-interop:16.0.1') {
exclude group: "com.google.firebase", module: "firebase-common"
}

androidTestImplementation "com.android.support:support-annotations:$supportAnnotationsVersion"
androidTestImplementation 'com.android.support.test:rules:1.0.2'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import java.util.List;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -44,11 +43,6 @@
public class FirebaseDatabaseTest {
@Rule public RetryRule retryRule = new RetryRule(3);

@Before
public void setup() {
IntegrationTestHelpers.ensureAppInitialized();
}

@After
public void tearDown() {
IntegrationTestHelpers.failOnFirstUncaughtException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import android.support.test.InstrumentationRegistry;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.database.android.AndroidAuthTokenProvider;
import com.google.firebase.database.core.CompoundWrite;
import com.google.firebase.database.core.Context;
import com.google.firebase.database.core.CoreTestHelpers;
Expand Down Expand Up @@ -256,15 +255,13 @@ public static DatabaseConfig newFrozenTestConfig() {
}

public static DatabaseConfig newTestConfig() {
IntegrationTestHelpers.ensureAppInitialized();

TestRunLoop runLoop = new TestRunLoop();
DatabaseConfig config = new DatabaseConfig();
config.setLogLevel(Logger.Level.DEBUG);
config.setEventTarget(new TestEventTarget());
config.setRunLoop(runLoop);
config.setFirebaseApp(FirebaseApp.getInstance());
config.setAuthTokenProvider(new TestTokenProvider(runLoop.getExecutorService()));
config.setAuthTokenProvider(AndroidAuthTokenProvider.forUnauthenticatedAccess());
return config;
}

Expand Down Expand Up @@ -449,18 +446,4 @@ public static Object applyPath(Object value, Path path) {
public static void assertContains(String str, String substr) {
assertTrue("'" + str + "' does not contain '" + substr + "'.", str.contains(substr));
}

public static void ensureAppInitialized() {
if (!appInitialized) {
appInitialized = true;
android.content.Context context = InstrumentationRegistry.getTargetContext();
FirebaseApp.initializeApp(
context,
new FirebaseOptions.Builder()
.setApiKey(context.getResources().getString(R.string.google_api_key))
.setApplicationId("sdflhkj")
.setDatabaseUrl(IntegrationTestValues.getNamespace())
.build());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
Expand Down Expand Up @@ -2027,7 +2028,8 @@ public void run() {
}

@Override
public void addTokenChangeListener(TokenChangeListener listener) {}
public void addTokenChangeListener(
ExecutorService executorService, TokenChangeListener listener) {}

@Override
public void removeTokenChangeListener(TokenChangeListener listener) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@

import android.support.annotation.Keep;
import android.support.annotation.RestrictTo;
import com.google.firebase.FirebaseApp;
import com.google.firebase.auth.internal.InternalAuthProvider;
import com.google.firebase.components.Component;
import com.google.firebase.components.ComponentRegistrar;
import com.google.firebase.components.Dependency;
import com.google.firebase.platforminfo.LibraryVersionComponent;
import java.util.Collections;
import java.util.Arrays;
import java.util.List;

/** @hide */
Expand All @@ -28,7 +31,15 @@
public class DatabaseRegistrar implements ComponentRegistrar {
@Override
public List<Component<?>> getComponents() {
return Collections.singletonList(
return Arrays.asList(
Component.builder(FirebaseDatabaseComponent.class)
.add(Dependency.required(FirebaseApp.class))
.add(Dependency.optional(InternalAuthProvider.class))
.factory(
c ->
new FirebaseDatabaseComponent(
c.get(FirebaseApp.class), c.get(InternalAuthProvider.class)))
.build(),
LibraryVersionComponent.create("fire-rtdb", BuildConfig.VERSION_NAME));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package com.google.firebase.database;

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

import android.support.annotation.NonNull;
import android.text.TextUtils;
import com.google.firebase.FirebaseApp;
Expand All @@ -27,8 +29,6 @@
import com.google.firebase.database.core.utilities.ParsedUrl;
import com.google.firebase.database.core.utilities.Utilities;
import com.google.firebase.database.core.utilities.Validation;
import java.util.HashMap;
import java.util.Map;

/**
* The entry point for accessing a Firebase Database. You can get an instance by calling {@link
Expand All @@ -40,17 +40,6 @@ public class FirebaseDatabase {

private static final String SDK_VERSION = "3.0.0";

/**
* A static map of FirebaseApp and RepoInfo to FirebaseDatabase instance. To ensure thread-
* safety, it should only be accessed in getInstance(), which is a synchronized method.
*
* <p>TODO: This serves a duplicate purpose as RepoManager. We should clean up. TODO: We should
* maybe be conscious of leaks and make this a weak map or similar but we have a lot of work to do
* to allow FirebaseDatabase/Repo etc. to be GC'd.
*/
private static final Map<String /* App name */, Map<RepoInfo, FirebaseDatabase>>
databaseInstances = new HashMap<>();

private final FirebaseApp app;
private final RepoInfo repoInfo;
private final DatabaseConfig config;
Expand Down Expand Up @@ -116,13 +105,6 @@ public static synchronized FirebaseDatabase getInstance(
+ "FirebaseApp or from your getInstance() call.");
}

Map<RepoInfo, FirebaseDatabase> instances = databaseInstances.get(app.getName());

if (instances == null) {
instances = new HashMap<>();
databaseInstances.put(app.getName(), instances);
}

ParsedUrl parsedUrl = Utilities.parseUrl(url);
if (!parsedUrl.path.isEmpty()) {
throw new DatabaseException(
Expand All @@ -133,23 +115,10 @@ public static synchronized FirebaseDatabase getInstance(
+ parsedUrl.path.toString());
}

FirebaseDatabase database = instances.get(parsedUrl.repoInfo);

if (database == null) {
DatabaseConfig config = new DatabaseConfig();
// If this is the default app, don't set the session persistence key so that we use our
// default ("default") instead of the FirebaseApp default ("[DEFAULT]") so that we
// preserve the default location used by the legacy Firebase SDK.
if (!app.isDefaultApp()) {
config.setSessionPersistenceKey(app.getName());
}
config.setFirebaseApp(app);

database = new FirebaseDatabase(app, parsedUrl.repoInfo, config);
instances.put(parsedUrl.repoInfo, database);
}

return database;
checkNotNull(app, "Provided FirebaseApp must not be null.");
FirebaseDatabaseComponent component = app.get(FirebaseDatabaseComponent.class);
checkNotNull(component, "Firebase Database component is not present.");
return component.get(parsedUrl.repoInfo);
}

/** This exists so Repo can create FirebaseDatabase objects to keep legacy tests working. */
Expand All @@ -160,7 +129,7 @@ static FirebaseDatabase createForTests(
return db;
}

private FirebaseDatabase(FirebaseApp app, RepoInfo repoInfo, DatabaseConfig config) {
FirebaseDatabase(FirebaseApp app, RepoInfo repoInfo, DatabaseConfig config) {
this.app = app;
this.repoInfo = repoInfo;
this.config = config;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright 2019 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.database;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.google.firebase.FirebaseApp;
import com.google.firebase.auth.internal.InternalAuthProvider;
import com.google.firebase.database.android.AndroidAuthTokenProvider;
import com.google.firebase.database.core.AuthTokenProvider;
import com.google.firebase.database.core.DatabaseConfig;
import com.google.firebase.database.core.RepoInfo;
import java.util.HashMap;
import java.util.Map;

class FirebaseDatabaseComponent {
/**
* A map of RepoInfo to FirebaseDatabase instance.
*
* <p>TODO: This serves a duplicate purpose as RepoManager. We should clean up. TODO: We should
* maybe be conscious of leaks and make this a weak map or similar but we have a lot of work to do
* to allow FirebaseDatabase/Repo etc. to be GC'd.
*/
private final Map<RepoInfo, FirebaseDatabase> instances = new HashMap<>();

private final FirebaseApp app;
private final AuthTokenProvider authProvider;

FirebaseDatabaseComponent(@NonNull FirebaseApp app, @Nullable InternalAuthProvider authProvider) {
this.app = app;

if (authProvider != null) {
this.authProvider = AndroidAuthTokenProvider.forAuthenticatedAccess(authProvider);
} else {
this.authProvider = AndroidAuthTokenProvider.forUnauthenticatedAccess();
}
}

/** Provides instances of Firebase Database for the given RepoInfo */
@NonNull
synchronized FirebaseDatabase get(RepoInfo repo) {
FirebaseDatabase database = instances.get(repo);
if (database == null) {
DatabaseConfig config = new DatabaseConfig();
// If this is the default app, don't set the session persistence key so that we use our
// default ("default") instead of the FirebaseApp default ("[DEFAULT]") so that we
// preserve the default location used by the legacy Firebase SDK.
if (!app.isDefaultApp()) {
config.setSessionPersistenceKey(app.getName());
}
config.setFirebaseApp(app);
config.setAuthTokenProvider(authProvider);

database = new FirebaseDatabase(app, repo, config);
instances.put(repo, database);
}
return database;
}
}
Loading