Skip to content

Commit

Permalink
Extend logging of image prefetching to include react_native contextChain
Browse files Browse the repository at this point in the history
Summary:
This diff extends the logging of image prefetching to include react_native ID in the ContextChain of the Fresco logger.

This is necesary to properly assign pre-fetching of RN Android images to the react_native ID

changelog: [Internal][Android]

Reviewed By: fkgozali

Differential Revision: D21362266

fbshipit-source-id: ff64f0ebd12f61b713996558eb2d962f96ad8d94
  • Loading branch information
mdvacca authored and facebook-github-bot committed May 2, 2020
1 parent 3da8103 commit eb60019
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ rn_android_library(
react_native_target("java/com/facebook/react/modules/fresco:fresco"),
react_native_target("java/com/facebook/react/module/annotations:annotations"),
react_native_target("java/com/facebook/react/views/imagehelper:imagehelper"),
react_native_target("java/com/facebook/react/views/image:image"),
],
exported_deps = [
react_native_target("java/com/facebook/fbreact/specs:FBReactNativeSpec"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.module.annotations.ReactModule;
import com.facebook.react.modules.fresco.ReactNetworkImageRequest;
import com.facebook.react.views.image.ReactCallerContextFactory;
import com.facebook.react.views.imagehelper.ImageSource;

@ReactModule(name = ImageLoaderModule.NAME)
Expand All @@ -43,20 +44,16 @@ public class ImageLoaderModule extends NativeImageLoaderAndroidSpec
private static final String ERROR_PREFETCH_FAILURE = "E_PREFETCH_FAILURE";
private static final String ERROR_GET_SIZE_FAILURE = "E_GET_SIZE_FAILURE";
public static final String NAME = "ImageLoader";
private static final Object DEFAULT_CALLER_CONTEXT = new Object();

private @Nullable final Object mCallerContext;
private final Object mEnqueuedRequestMonitor = new Object();
private final SparseArray<DataSource<Void>> mEnqueuedRequests = new SparseArray<>();
private ImagePipeline mImagePipeline;
private final ImagePipeline mImagePipeline;
private @Nullable ReactCallerContextFactory mCallerContextFactory;

public ImageLoaderModule(ReactApplicationContext reactContext) {
this(reactContext, null);
}

public ImageLoaderModule(ReactApplicationContext reactContext, ImagePipeline imagePipeline) {
super(reactContext);
mCallerContext = Fresco.getImagePipeline();
mImagePipeline = imagePipeline;
this(reactContext, DEFAULT_CALLER_CONTEXT);
}

public ImageLoaderModule(ReactApplicationContext reactContext, Object callerContext) {
Expand All @@ -65,6 +62,22 @@ public ImageLoaderModule(ReactApplicationContext reactContext, Object callerCont
mImagePipeline = Fresco.getImagePipeline();
}

public ImageLoaderModule(
ReactApplicationContext reactContext,
ImagePipeline imagePipeline,
ReactCallerContextFactory callerContextFactory) {
super(reactContext);
mCallerContextFactory = callerContextFactory;
mImagePipeline = imagePipeline;
mCallerContext = null;
}

private @Nullable Object getCallerContext() {
return mCallerContextFactory != null
? mCallerContextFactory.getOrCreateCallerContext("", "")
: mCallerContext;
}

@Override
@NonNull
public String getName() {
Expand All @@ -89,7 +102,7 @@ public void getSize(final String uriString, final Promise promise) {
ImageRequest request = ImageRequestBuilder.newBuilderWithSource(source.getUri()).build();

DataSource<CloseableReference<CloseableImage>> dataSource =
Fresco.getImagePipeline().fetchDecodedImage(request, mCallerContext);
mImagePipeline.fetchDecodedImage(request, getCallerContext());

DataSubscriber<CloseableReference<CloseableImage>> dataSubscriber =
new BaseDataSubscriber<CloseableReference<CloseableImage>>() {
Expand Down Expand Up @@ -150,7 +163,7 @@ public void getSizeWithHeaders(
ReactNetworkImageRequest.fromBuilderWithHeaders(imageRequestBuilder, headers);

DataSource<CloseableReference<CloseableImage>> dataSource =
Fresco.getImagePipeline().fetchDecodedImage(request, mCallerContext);
mImagePipeline.fetchDecodedImage(request, getCallerContext());

DataSubscriber<CloseableReference<CloseableImage>> dataSubscriber =
new BaseDataSubscriber<CloseableReference<CloseableImage>>() {
Expand Down Expand Up @@ -210,7 +223,7 @@ public void prefetchImage(
ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri).build();

DataSource<Void> prefetchSource =
Fresco.getImagePipeline().prefetchToDiskCache(request, mCallerContext);
mImagePipeline.prefetchToDiskCache(request, getCallerContext());
DataSubscriber<Void> prefetchSubscriber =
new BaseDataSubscriber<Void>() {
@Override
Expand Down Expand Up @@ -257,7 +270,7 @@ public void queryCache(final ReadableArray uris, final Promise promise) {
@Override
protected void doInBackgroundGuarded(Void... params) {
WritableMap result = Arguments.createMap();
ImagePipeline imagePipeline = Fresco.getImagePipeline();
ImagePipeline imagePipeline = mImagePipeline;
for (int i = 0; i < uris.size(); i++) {
String uriString = uris.getString(i);
final Uri uri = Uri.parse(uriString);
Expand Down

0 comments on commit eb60019

Please sign in to comment.