From ff2984963cbf0f31e374228c7f702f27b5753336 Mon Sep 17 00:00:00 2001 From: Sam Judd Date: Mon, 13 Jan 2020 09:51:51 -0800 Subject: [PATCH] Internal change PiperOrigin-RevId: 289462005 --- .../cronet/src/main/AndroidManifest.xml | 11 ++++- .../cronet/CronetEngineSingleton.java | 42 +++++++++++++++++++ .../integration/cronet/CronetGlideModule.java | 32 ++++++++++++++ .../cronet/CronetLibraryGlideModule.java | 32 ++++++++++++++ 4 files changed, 115 insertions(+), 2 deletions(-) create mode 100644 integration/cronet/src/main/java/com/bumptech/glide/integration/cronet/CronetEngineSingleton.java create mode 100644 integration/cronet/src/main/java/com/bumptech/glide/integration/cronet/CronetGlideModule.java create mode 100644 integration/cronet/src/main/java/com/bumptech/glide/integration/cronet/CronetLibraryGlideModule.java diff --git a/integration/cronet/src/main/AndroidManifest.xml b/integration/cronet/src/main/AndroidManifest.xml index 5c2324a61a..eaf612d991 100644 --- a/integration/cronet/src/main/AndroidManifest.xml +++ b/integration/cronet/src/main/AndroidManifest.xml @@ -1,3 +1,10 @@ - - + + + + + + diff --git a/integration/cronet/src/main/java/com/bumptech/glide/integration/cronet/CronetEngineSingleton.java b/integration/cronet/src/main/java/com/bumptech/glide/integration/cronet/CronetEngineSingleton.java new file mode 100644 index 0000000000..9c926342e0 --- /dev/null +++ b/integration/cronet/src/main/java/com/bumptech/glide/integration/cronet/CronetEngineSingleton.java @@ -0,0 +1,42 @@ +package com.bumptech.glide.integration.cronet; + +import android.content.Context; +import org.chromium.net.CronetEngine; + +/** + * Class controlling singleton instance of the CronetEngine. Ensures at most one instance of the + * CronetEngine is created. + */ +// NOTE: This is a standalone class and not a memoized supplier as the CronetEngine creations +// requires a parameter, namedly a Context reference. +public final class CronetEngineSingleton { + + // non instantiable + private CronetEngineSingleton() {} + + private static volatile CronetEngine cronetEngineSingleton; + + public static CronetEngine getSingleton(Context context) { + + // Lazily create the engine. + if (cronetEngineSingleton == null) { + synchronized (CronetEngineSingleton.class) { + // have to re-check since this might have changed before synchronization, but we don't + // want to synchronize just to check for null. + if (cronetEngineSingleton == null) { + cronetEngineSingleton = createEngine(context); + } + } + } + + return cronetEngineSingleton; + } + + private static CronetEngine createEngine(Context context) { + return new CronetEngine.Builder(context) + .enableHttpCache(CronetEngine.Builder.HTTP_CACHE_DISABLED, 0) + .enableHttp2(true) + .enableQuic(false) + .build(); + } +} diff --git a/integration/cronet/src/main/java/com/bumptech/glide/integration/cronet/CronetGlideModule.java b/integration/cronet/src/main/java/com/bumptech/glide/integration/cronet/CronetGlideModule.java new file mode 100644 index 0000000000..24003c930a --- /dev/null +++ b/integration/cronet/src/main/java/com/bumptech/glide/integration/cronet/CronetGlideModule.java @@ -0,0 +1,32 @@ +package com.bumptech.glide.integration.cronet; + +import android.content.Context; +import com.bumptech.glide.Glide; +import com.bumptech.glide.GlideBuilder; +import com.bumptech.glide.Registry; +import com.bumptech.glide.load.model.GlideUrl; +import com.bumptech.glide.module.GlideModule; +import com.google.android.apps.common.proguard.UsedByReflection; +import java.io.InputStream; +import java.nio.ByteBuffer; + +/** + * A {@link GlideModule} that registers components allowing remote image fetching to be done using + * Cronet. + */ +@UsedByReflection("meta-data") +public final class CronetGlideModule implements GlideModule { + + @Override + public void applyOptions(Context context, GlideBuilder builder) {} + + @Override + public void registerComponents(Context context, Glide glide, Registry registry) { + CronetRequestFactory factory = + new CronetRequestFactoryImpl(() -> CronetEngineSingleton.getSingleton(context)); + registry.replace( + GlideUrl.class, InputStream.class, new ChromiumUrlLoader.StreamFactory(factory, null)); + registry.prepend( + GlideUrl.class, ByteBuffer.class, new ChromiumUrlLoader.ByteBufferFactory(factory, null)); + } +} diff --git a/integration/cronet/src/main/java/com/bumptech/glide/integration/cronet/CronetLibraryGlideModule.java b/integration/cronet/src/main/java/com/bumptech/glide/integration/cronet/CronetLibraryGlideModule.java new file mode 100644 index 0000000000..ea90064ca9 --- /dev/null +++ b/integration/cronet/src/main/java/com/bumptech/glide/integration/cronet/CronetLibraryGlideModule.java @@ -0,0 +1,32 @@ +package com.bumptech.glide.integration.cronet; + +import android.content.Context; +import androidx.annotation.NonNull; +import com.bumptech.glide.Glide; +import com.bumptech.glide.Registry; +import com.bumptech.glide.annotation.GlideModule; +import com.bumptech.glide.integration.cronet.ChromiumUrlLoader.ByteBufferFactory; +import com.bumptech.glide.integration.cronet.ChromiumUrlLoader.StreamFactory; +import com.bumptech.glide.load.model.GlideUrl; +import com.bumptech.glide.module.LibraryGlideModule; +import java.io.InputStream; +import java.nio.ByteBuffer; + +/** + * A {@link LibraryGlideModule} that registers components allowing remote image fetching to be done + * using Cronet. + */ +@GlideModule +public final class CronetLibraryGlideModule extends LibraryGlideModule { + + @Override + public void registerComponents( + @NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) { + CronetRequestFactory factory = + new CronetRequestFactoryImpl(() -> CronetEngineSingleton.getSingleton(context)); + registry.replace( + GlideUrl.class, InputStream.class, new StreamFactory(factory, null /* dataLogger */)); + registry.prepend( + GlideUrl.class, ByteBuffer.class, new ByteBufferFactory(factory, null /* dataLogger */)); + } +}