Skip to content

Commit 93d0810

Browse files
authored
Introducing Synchronous Methods to Public APIs (firebase#146)
* Adding the sync APIs for FirebaseAuth * Added more tests; Added sync APIs for FirebaseMessaging * Removing Task references from database, iid and fcm APIs * Fixing a typo * Minor code clean up * Updated javadocs; Renamed internal helpers of FirebaseMessaging for consistency * Removed the deprecated FirebaseCredential API (firebase#149) * Removing the Task API (firebase#152) * Removed the deprecated FirebaseCredential API * Removing the deprecated Task API * Dropping Support for App Engine Java 7 Runtime (firebase#153) * Dropping support for GAE 7 * Removed GaeThreadFactory, GaeExecutorService and RevivingScheduledExecutor * Removed the deprecated FirebaseCredential API * Removing GAE java7 related APIs (GaeThreadFactory, RevivingScheduledExecutor) * Removed GaePlatform implementation * Added FirebaseScheduledExecutor * Updated documentation * Some minor nits from code reviews * Calling super method in DefaultRunLoop executor * Removing Deprecated LogWrapper API (firebase#154) * Dropping support for GAE 7 * Removed GaeThreadFactory, GaeExecutorService and RevivingScheduledExecutor * Removed the deprecated FirebaseCredential API * Removing GAE java7 related APIs (GaeThreadFactory, RevivingScheduledExecutor) * Removed GaePlatform implementation * Added FirebaseScheduledExecutor * Updated documentation * Removing LogWrapper API * Removing PrefixedLogger * Removing test config file * Updated CHANGELOG * Minor clean ups pointed out in the code review * Minor API doc fixes (firebase#171)
1 parent 031965f commit 93d0810

99 files changed

Lines changed: 1388 additions & 6833 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎CHANGELOG.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
# Unreleased
22

3+
- [added] `FirebaseAuth`, `FirebaseMessaging` and `FirebaseInstanceId`
4+
interfaces now expose a set of blocking methods. Each operation has
5+
blocking an asynchronous versions.
6+
- [changed] Removed the deprecated `FirebaseCredential` interface.
7+
- [changed] Removed the deprecated `Task` interface along with the
8+
`com.google.firebase.tasks` package.
9+
- [changed] Dropped support for App Engine's Java 7 runtime. Developers
10+
are advised to use the Admin SDK with Java 8 when deploying to App
11+
Engine.
12+
- [changed] Removed the deprecated `FirebaseDatabase.setLogLevel()` API
13+
and the related logging utilities. Developers should use SLF4J to
14+
configure logging directly.
315

416
# v5.11.0
517

‎src/main/java/com/google/firebase/FirebaseApp.java‎

Lines changed: 41 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
import static com.google.common.base.Preconditions.checkArgument;
2020
import static com.google.common.base.Preconditions.checkNotNull;
2121
import static com.google.common.base.Preconditions.checkState;
22-
import static java.nio.charset.StandardCharsets.UTF_8;
2322

2423
import com.google.api.client.googleapis.util.Utils;
2524
import com.google.api.client.json.JsonFactory;
2625
import com.google.api.client.json.JsonParser;
26+
import com.google.api.core.ApiFuture;
2727
import com.google.auth.oauth2.AccessToken;
2828
import com.google.auth.oauth2.GoogleCredentials;
2929
import com.google.auth.oauth2.OAuth2Credentials;
@@ -34,18 +34,13 @@
3434
import com.google.common.base.MoreObjects;
3535
import com.google.common.base.Strings;
3636
import com.google.common.collect.ImmutableList;
37-
import com.google.common.io.BaseEncoding;
3837
import com.google.firebase.internal.FirebaseAppStore;
38+
import com.google.firebase.internal.FirebaseScheduledExecutor;
3939
import com.google.firebase.internal.FirebaseService;
40-
import com.google.firebase.internal.GaeThreadFactory;
40+
import com.google.firebase.internal.ListenableFuture2ApiFuture;
4141
import com.google.firebase.internal.NonNull;
4242
import com.google.firebase.internal.Nullable;
4343

44-
import com.google.firebase.internal.RevivingScheduledExecutor;
45-
import com.google.firebase.tasks.Task;
46-
import com.google.firebase.tasks.Tasks;
47-
48-
import java.io.FileNotFoundException;
4944
import java.io.FileReader;
5045
import java.io.IOException;
5146
import java.util.ArrayList;
@@ -176,6 +171,9 @@ public static FirebaseApp getInstance(@NonNull String name) {
176171
* by looking up the {@code FIREBASE_CONFIG} environment variable. If the value of
177172
* the variable starts with <code>'{'</code>, it is parsed as a JSON object. Otherwise it is
178173
* treated as a file name and the JSON content is read from the corresponding file.
174+
*
175+
* @throws IllegalStateException if the default app has already been initialized.
176+
* @throws IllegalArgumentException if an error occurs while loading options from the environment.
179177
*/
180178
public static FirebaseApp initializeApp() {
181179
return initializeApp(DEFAULT_APP_NAME);
@@ -185,13 +183,23 @@ public static FirebaseApp initializeApp() {
185183
* Initializes a named {@link FirebaseApp} instance using Google Application Default Credentials.
186184
* Loads additional {@link FirebaseOptions} from the environment in the same way as the
187185
* {@link #initializeApp()} method.
186+
*
187+
* @throws IllegalStateException if an app with the same name has already been initialized.
188+
* @throws IllegalArgumentException if an error occurs while loading options from the environment.
188189
*/
189190
public static FirebaseApp initializeApp(String name) {
190-
return initializeApp(getOptionsFromEnvironment(), name);
191+
try {
192+
return initializeApp(getOptionsFromEnvironment(), name);
193+
} catch (IOException e) {
194+
throw new IllegalArgumentException(
195+
"Failed to load settings from the system's environment variables", e);
196+
}
191197
}
192198

193199
/**
194200
* Initializes the default {@link FirebaseApp} instance using the given options.
201+
*
202+
* @throws IllegalStateException if the default app has already been initialized.
195203
*/
196204
public static FirebaseApp initializeApp(FirebaseOptions options) {
197205
return initializeApp(options, DEFAULT_APP_NAME);
@@ -241,19 +249,6 @@ static void clearInstancesForTest() {
241249
}
242250
}
243251

244-
/**
245-
* Returns persistence key. Exists to support getting {@link FirebaseApp} persistence key after
246-
* the app has been deleted.
247-
*/
248-
static String getPersistenceKey(String name, FirebaseOptions options) {
249-
return BaseEncoding.base64Url().omitPadding().encode(name.getBytes(UTF_8));
250-
}
251-
252-
/** Use this key to store data per FirebaseApp. */
253-
String getPersistenceKey() {
254-
return FirebaseApp.getPersistenceKey(getName(), getOptions());
255-
}
256-
257252
private static List<String> getAllAppNames() {
258253
Set<String> allAppNames = new HashSet<>();
259254
synchronized (appsLock) {
@@ -317,10 +312,7 @@ String getProjectId() {
317312

318313
@Override
319314
public boolean equals(Object o) {
320-
if (!(o instanceof FirebaseApp)) {
321-
return false;
322-
}
323-
return name.equals(((FirebaseApp) o).getName());
315+
return o instanceof FirebaseApp && name.equals(((FirebaseApp) o).getName());
324316
}
325317

326318
@Override
@@ -383,8 +375,8 @@ private ScheduledExecutorService ensureScheduledExecutorService() {
383375
synchronized (lock) {
384376
checkNotDeleted();
385377
if (scheduledExecutor == null) {
386-
scheduledExecutor = new RevivingScheduledExecutor(threadManager.getThreadFactory(),
387-
"firebase-scheduled-worker", GaeThreadFactory.isAvailable());
378+
scheduledExecutor = new FirebaseScheduledExecutor(getThreadFactory(),
379+
"firebase-scheduled-worker");
388380
}
389381
}
390382
}
@@ -395,10 +387,9 @@ ThreadFactory getThreadFactory() {
395387
return threadManager.getThreadFactory();
396388
}
397389

398-
// TODO: Return an ApiFuture once Task API is fully removed.
399-
<T> Task<T> submit(Callable<T> command) {
390+
<T> ApiFuture<T> submit(Callable<T> command) {
400391
checkNotNull(command);
401-
return Tasks.call(executors.getListeningExecutor(), command);
392+
return new ListenableFuture2ApiFuture<>(executors.getListeningExecutor().submit(command));
402393
}
403394

404395
<T> ScheduledFuture<T> schedule(Callable<T> command, long delayMillis) {
@@ -462,7 +453,7 @@ static class TokenRefresher implements CredentialsChangedListener {
462453
}
463454

464455
@Override
465-
public final synchronized void onChanged(OAuth2Credentials credentials) throws IOException {
456+
public final synchronized void onChanged(OAuth2Credentials credentials) {
466457
if (state.get() != State.STARTED) {
467458
return;
468459
}
@@ -569,33 +560,26 @@ enum State {
569560
}
570561
}
571562

572-
private static FirebaseOptions getOptionsFromEnvironment() {
563+
private static FirebaseOptions getOptionsFromEnvironment() throws IOException {
573564
String defaultConfig = System.getenv(FIREBASE_CONFIG_ENV_VAR);
574-
try {
575-
if (Strings.isNullOrEmpty(defaultConfig)) {
576-
return new FirebaseOptions.Builder()
577-
.setCredentials(GoogleCredentials.getApplicationDefault())
578-
.build();
579-
}
580-
JsonFactory jsonFactory = Utils.getDefaultJsonFactory();
581-
FirebaseOptions.Builder builder = new FirebaseOptions.Builder();
582-
JsonParser parser;
583-
if (defaultConfig.startsWith("{")) {
584-
parser = jsonFactory.createJsonParser(defaultConfig);
585-
} else {
586-
FileReader reader;
587-
reader = new FileReader(defaultConfig);
588-
parser = jsonFactory.createJsonParser(reader);
589-
}
590-
parser.parseAndClose(builder);
591-
builder.setCredentials(GoogleCredentials.getApplicationDefault());
592-
593-
return builder.build();
565+
if (Strings.isNullOrEmpty(defaultConfig)) {
566+
return new FirebaseOptions.Builder()
567+
.setCredentials(GoogleCredentials.getApplicationDefault())
568+
.build();
569+
}
594570

595-
} catch (FileNotFoundException e) {
596-
throw new IllegalStateException(e);
597-
} catch (IOException e) {
598-
throw new IllegalStateException(e);
571+
JsonFactory jsonFactory = Utils.getDefaultJsonFactory();
572+
FirebaseOptions.Builder builder = new FirebaseOptions.Builder();
573+
JsonParser parser;
574+
if (defaultConfig.startsWith("{")) {
575+
parser = jsonFactory.createJsonParser(defaultConfig);
576+
} else {
577+
FileReader reader;
578+
reader = new FileReader(defaultConfig);
579+
parser = jsonFactory.createJsonParser(reader);
599580
}
581+
parser.parseAndClose(builder);
582+
builder.setCredentials(GoogleCredentials.getApplicationDefault());
583+
return builder.build();
600584
}
601585
}

‎src/main/java/com/google/firebase/FirebaseOptions.java‎

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,35 @@
2525
import com.google.api.client.util.Key;
2626
import com.google.auth.oauth2.GoogleCredentials;
2727
import com.google.common.base.Strings;
28-
import com.google.firebase.auth.FirebaseCredential;
29-
import com.google.firebase.auth.FirebaseCredentials;
30-
import com.google.firebase.auth.internal.BaseCredential;
31-
import com.google.firebase.auth.internal.FirebaseCredentialsAdapter;
28+
import com.google.common.collect.ImmutableList;
3229
import com.google.firebase.internal.FirebaseThreadManagers;
3330
import com.google.firebase.internal.NonNull;
3431
import com.google.firebase.internal.Nullable;
3532

3633
import java.util.HashMap;
34+
import java.util.List;
3735
import java.util.Map;
3836

3937
/** Configurable Firebase options. */
4038
public final class FirebaseOptions {
4139

42-
// TODO: deprecate and remove it once we can fetch these from Remote Config.
40+
private static final List<String> FIREBASE_SCOPES =
41+
ImmutableList.of(
42+
// Enables access to Firebase Realtime Database.
43+
"https://www.googleapis.com/auth/firebase.database",
44+
45+
// Enables access to the email address associated with a project.
46+
"https://www.googleapis.com/auth/userinfo.email",
47+
48+
// Enables access to Google Identity Toolkit (for user management APIs).
49+
"https://www.googleapis.com/auth/identitytoolkit",
50+
51+
// Enables access to Google Cloud Storage.
52+
"https://www.googleapis.com/auth/devstorage.full_control",
53+
54+
// Enables access to Google Cloud Firestore
55+
"https://www.googleapis.com/auth/cloud-platform",
56+
"https://www.googleapis.com/auth/datastore");
4357

4458
private final String databaseUrl;
4559
private final String storageBucket;
@@ -55,7 +69,7 @@ public final class FirebaseOptions {
5569
private FirebaseOptions(@NonNull FirebaseOptions.Builder builder) {
5670
this.credentials = checkNotNull(builder.credentials,
5771
"FirebaseOptions must be initialized with setCredentials().")
58-
.createScoped(BaseCredential.FIREBASE_SCOPES);
72+
.createScoped(FIREBASE_SCOPES);
5973
this.databaseUrl = builder.databaseUrl;
6074
this.databaseAuthVariableOverride = builder.databaseAuthVariableOverride;
6175
this.projectId = builder.projectId;
@@ -260,24 +274,6 @@ public Builder setCredentials(GoogleCredentials credentials) {
260274
return this;
261275
}
262276

263-
/**
264-
* Sets the <code>FirebaseCredential</code> to use to authenticate the SDK.
265-
*
266-
* @param credential A <code>FirebaseCredential</code> used to authenticate the SDK. See {@link
267-
* FirebaseCredentials} for default implementations.
268-
* @return This <code>Builder</code> instance is returned so subsequent calls can be chained.
269-
* @deprecated Use {@link FirebaseOptions.Builder#setCredentials(GoogleCredentials)}.
270-
*/
271-
public Builder setCredential(@NonNull FirebaseCredential credential) {
272-
checkNotNull(credential);
273-
if (credential instanceof BaseCredential) {
274-
this.credentials = ((BaseCredential) credential).getGoogleCredentials();
275-
} else {
276-
this.credentials = new FirebaseCredentialsAdapter(credential);
277-
}
278-
return this;
279-
}
280-
281277
/**
282278
* Sets the <code>auth</code> variable to be used by the Realtime Database rules.
283279
*

‎src/main/java/com/google/firebase/ImplFirebaseTrampolines.java‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616

1717
package com.google.firebase;
1818

19+
import com.google.api.core.ApiFuture;
1920
import com.google.auth.oauth2.GoogleCredentials;
2021
import com.google.firebase.internal.FirebaseService;
2122
import com.google.firebase.internal.NonNull;
2223

23-
import com.google.firebase.tasks.Task;
2424
import java.util.concurrent.Callable;
2525
import java.util.concurrent.ThreadFactory;
2626

@@ -47,14 +47,6 @@ public static boolean isDefaultApp(@NonNull FirebaseApp app) {
4747
return app.isDefaultApp();
4848
}
4949

50-
public static String getPersistenceKey(@NonNull FirebaseApp app) {
51-
return app.getPersistenceKey();
52-
}
53-
54-
public static String getPersistenceKey(String name, FirebaseOptions options) {
55-
return FirebaseApp.getPersistenceKey(name, options);
56-
}
57-
5850
public static <T extends FirebaseService> T getService(
5951
@NonNull FirebaseApp app, @NonNull String id, @NonNull Class<T> type) {
6052
return type.cast(app.getService(id));
@@ -70,7 +62,8 @@ public static ThreadFactory getThreadFactory(@NonNull FirebaseApp app) {
7062
return app.getThreadFactory();
7163
}
7264

73-
public static <T> Task<T> submitCallable(@NonNull FirebaseApp app, @NonNull Callable<T> command) {
65+
public static <T> ApiFuture<T> submitCallable(
66+
@NonNull FirebaseApp app, @NonNull Callable<T> command) {
7467
return app.submit(command);
7568
}
7669

‎src/main/java/com/google/firebase/ThreadManager.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ final void releaseFirebaseExecutors(
5757
* {@link #getThreadFactory()} method.
5858
*
5959
* @param app A {@link FirebaseApp} instance.
60-
* @return A non-null {@link ExecutorService} instance.
60+
* @return A non-null <code>ExecutorService</code> instance.
6161
*/
6262
@NonNull
6363
protected abstract ExecutorService getExecutor(@NonNull FirebaseApp app);

0 commit comments

Comments
 (0)