Skip to content
Open
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 build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ dependencies {
testImplementation(libs.kotlinx.coroutines.play.services)
testImplementation(libs.kotlinx.coroutines.swing)
testImplementation(libs.kotlinx.coroutines.test)
testImplementation(libs.okhttp.mockwebserver)
// firebase aars
aar(platform(libs.google.firebase.bom))
aar(libs.google.firebase.firestore)
Expand Down
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-t
kotlinx-serialization-core = { module = "org.jetbrains.kotlinx:kotlinx-serialization-core", version.ref = "kotlinx-serialization" }
kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinx-serialization" }
okhttp = { module = "com.squareup.okhttp3:okhttp", version = "3.12.13" }
okhttp-mockwebserver = { module = "com.squareup.okhttp3:mockwebserver", version = "3.12.13" }
robolectric-android-all = { module = "org.robolectric:android-all", version = "14-robolectric-10818077" }
xerial-sqlite-jdbc = { module = "org.xerial:sqlite-jdbc", version = "3.46.1.0" }

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/android/os/AsyncTask.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package android.os

import kotlinx.coroutines.Dispatchers
import com.google.firebase.FirebasePlatform
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
Expand All @@ -19,9 +19,9 @@ abstract class AsyncTask {

fun execute(vararg params: Any): AsyncTask {
GlobalScope.launch {
withContext(Dispatchers.Main) { onPreExecute() }
withContext(FirebasePlatform.firebasePlatform.mainDispatcher) { onPreExecute() }
val result = doInBackground(*params)
withContext(Dispatchers.Main) { onPostExecute(result) }
withContext(FirebasePlatform.firebasePlatform.mainDispatcher) { onPostExecute(result) }
}
return this
}
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/android/os/Handler.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package android.os

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import com.google.firebase.FirebasePlatform
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

Expand All @@ -10,14 +9,14 @@ open class Handler(looper: Looper?, callback: Handler.Callback?) {
constructor(looper: Looper) : this(looper, null)

fun post(runnable: Runnable): Boolean {
GlobalScope.launch(Dispatchers.Main) {
FirebasePlatform.firebasePlatform.mainScope.launch {
runnable.run()
}
return true
}

fun postDelayed(runnable: Runnable, time: Long): Boolean {
GlobalScope.launch(Dispatchers.Main) {
FirebasePlatform.firebasePlatform.mainScope.launch {
delay(time)
runnable.run()
}
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/google/firebase/FirebasePlatform.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
package com.google.firebase

import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import java.io.File

abstract class FirebasePlatform {

open val mainDispatcher: CoroutineDispatcher
get() = Dispatchers.Default

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Defaulting to Dispatchers.Default moves all Handler.post / listener callbacks onto a multi-threaded pool.

The android.os.Handler stub is what the bundled Firestore/Database AARs use to deliver callbacks. On Android these run in order on a single looper thread, and previously Dispatchers.Main (Swing/JavaFX) gave the same single-thread guarantee. With SupervisorJob() + Dispatchers.Default, two Handler.post(...) calls can run concurrently and out of order on different worker threads, so snapshot/child-event listeners can observe interleaved or reordered events.

It is also a silent behavior change for existing consumers: callbacks that used to arrive on the UI thread (Compose Desktop / Swing) now arrive on a DefaultDispatcher-worker and must be marshalled manually.

Suggest making the default single-threaded (e.g. Dispatchers.Default.limitedParallelism(1) or a dedicated single-thread executor) and calling out the change in the README.


Generated by Claude Code


internal val mainScope: CoroutineScope by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
CoroutineScope(SupervisorJob() + mainDispatcher)
}

companion object {

internal lateinit var firebasePlatform: FirebasePlatform
Expand Down
72 changes: 69 additions & 3 deletions src/main/java/com/google/firebase/auth/AuthCredential.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,75 @@
package com.google.firebase.auth;

import kotlin.NotImplementedError;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

public abstract class AuthCredential {
private final String provider;
private final String idToken;
private final String accessToken;
private final String rawNonce;

protected AuthCredential(String provider) {
this(provider, null, null, null);
}

protected AuthCredential(String provider, String idToken, String accessToken, String rawNonce) {
this.provider = requireValue(provider, "provider");
this.idToken = optionalValue(idToken, "idToken");
this.accessToken = optionalValue(accessToken, "accessToken");
this.rawNonce = optionalValue(rawNonce, "rawNonce");

if (this.rawNonce != null && this.idToken == null) {
throw new IllegalArgumentException("An ID token is required when a raw nonce is supplied.");
}
}

public class AuthCredential {
public String getProvider() {
throw new NotImplementedError();
return provider;
}

public String getSignInMethod() {
return provider;
}

String toIdpPostBody() {
List<String> values = new ArrayList<>();
values.add(formValue("providerId", provider));
if (idToken != null) {
values.add(formValue("id_token", idToken));
}
if (accessToken != null) {
values.add(formValue("access_token", accessToken));
}
if (rawNonce != null) {
values.add(formValue("nonce", rawNonce));
}
return String.join("&", values);
}

@Override
public String toString() {
return getClass().getSimpleName() + "{provider='" + provider + "'}";
}

static String requireValue(String value, String name) {
if (value == null || value.trim().isEmpty()) {
throw new IllegalArgumentException(name + " must not be null or blank.");
}
return value;
}

static String optionalValue(String value, String name) {
if (value != null && value.trim().isEmpty()) {
throw new IllegalArgumentException(name + " must not be blank.");
}
return value;
}

private static String formValue(String name, String value) {
return URLEncoder.encode(name, StandardCharsets.UTF_8) + "=" +
URLEncoder.encode(value, StandardCharsets.UTF_8);
}
}
Loading