-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollforward exposing cronet glide modules
PiperOrigin-RevId: 289954530
- Loading branch information
1 parent
1dd0576
commit 3d54ef5
Showing
4 changed files
with
127 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
<manifest package="com.bumptech.glide.integration.cronet"> | ||
<application /> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.bumptech.glide.integration.cronet"> | ||
<application> | ||
<meta-data | ||
android:name="com.bumptech.glide.integration.cronet.CronetGlideModule" | ||
android:value="GlideModule" /> | ||
</application> | ||
</manifest> |
42 changes: 42 additions & 0 deletions
42
...ion/cronet/src/main/java/com/bumptech/glide/integration/cronet/CronetEngineSingleton.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...gration/cronet/src/main/java/com/bumptech/glide/integration/cronet/CronetGlideModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
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.common.base.Supplier; | ||
import java.io.InputStream; | ||
import java.nio.ByteBuffer; | ||
import org.chromium.net.CronetEngine; | ||
|
||
/** | ||
* A {@link GlideModule} that registers components allowing remote image fetching to be done using | ||
* Cronet. | ||
*/ | ||
public final class CronetGlideModule implements GlideModule { | ||
|
||
@Override | ||
public void applyOptions(Context context, GlideBuilder builder) {} | ||
|
||
@Override | ||
public void registerComponents(final Context context, Glide glide, Registry registry) { | ||
CronetRequestFactory factory = | ||
new CronetRequestFactoryImpl( | ||
new Supplier<CronetEngine>() { | ||
@Override | ||
public CronetEngine get() { | ||
return 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)); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
.../cronet/src/main/java/com/bumptech/glide/integration/cronet/CronetLibraryGlideModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
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 com.google.common.base.Supplier; | ||
import java.io.InputStream; | ||
import java.nio.ByteBuffer; | ||
import org.chromium.net.CronetEngine; | ||
|
||
/** | ||
* 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( | ||
final @NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) { | ||
CronetRequestFactory factory = | ||
new CronetRequestFactoryImpl( | ||
new Supplier<CronetEngine>() { | ||
@Override | ||
public CronetEngine get() { | ||
return 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 */)); | ||
} | ||
} |