Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Lazily initialize GeckoWebExecutor #3653

Merged
merged 3 commits into from
Jul 8, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -8,6 +8,7 @@
import android.app.Application;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Looper;

import androidx.annotation.NonNull;

Expand Down Expand Up @@ -52,11 +53,18 @@ public void onCreate() {
return;
}

// Fix potential Gecko static initialization order.
// GeckoResult.ALLOW and GeckoResult.DENY static initializer might get a null mDispatcher
// depending on how JVM classloader does the initialization job.
// See https://github.com/MozillaReality/FirefoxReality/issues/3651
Looper.getMainLooper().getThread();

TelemetryWrapper.init(this, EngineProvider.INSTANCE.getDefaultClient(this));
GleanMetricsService.init(this, EngineProvider.INSTANCE.getDefaultClient(this));
}

protected void onActivityCreate(@NonNull Context activityContext) {
EngineProvider.INSTANCE.getDefaultGeckoWebExecutor(activityContext);
mPlaces = new Places(this);
mServices = new Services(this, mPlaces);
mAccounts = new Accounts(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,24 @@ object EngineProvider {
return runtime!!
}

fun createGeckoWebExecutor(context: Context): GeckoWebExecutor {
private fun createGeckoWebExecutor(context: Context): GeckoWebExecutor {
return GeckoWebExecutor(getOrCreateRuntime(context))
}

fun getDefaultGeckoWebExecutor(context: Context): GeckoWebExecutor {
if (executor == null) {
executor = createGeckoWebExecutor(context)
client?.let { it.executor = executor }

}

return executor!!
}

fun createClient(context: Context): GeckoViewFetchClient {
return GeckoViewFetchClient(context)
val client = GeckoViewFetchClient(context)
client.executor = executor
return client
}

fun getDefaultClient(context: Context): GeckoViewFetchClient {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ class GeckoViewFetchClient(
) : Client() {

@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
internal var executor: GeckoWebExecutor = EngineProvider.createGeckoWebExecutor(context)
internal var executor: GeckoWebExecutor? = null

@Throws(IOException::class)
override fun fetch(request: Request): Response {
if (executor == null) {
throw IOException("GeckoWebExecutor not initialized")
}
val webRequest = request.toWebRequest(defaultHeaders)

val readTimeOut = request.readTimeout ?: maxReadTimeOut
Expand All @@ -47,7 +50,7 @@ class GeckoViewFetchClient(
if (request.redirect == Request.Redirect.MANUAL) {
fetchFlags += GeckoWebExecutor.FETCH_FLAGS_NO_REDIRECTS
}
val webResponse = executor.fetch(webRequest, fetchFlags).poll(readTimeOutMillis)
val webResponse = executor!!.fetch(webRequest, fetchFlags).poll(readTimeOutMillis)
webResponse?.toResponse() ?: throw IOException("Fetch failed with null response")
} catch (e: TimeoutException) {
throw SocketTimeoutException()
Expand Down