Skip to content

Commit

Permalink
iOS Fabric: added support for image instrumentation [2]
Browse files Browse the repository at this point in the history
Summary:
Passing thru image instrumentation activities to the image loader class, which now supports Fabric instrumentation.

Changelog: [Internal]

Reviewed By: mdvacca, voznesenskym

Differential Revision: D19047898

fbshipit-source-id: d12cb5a06a83e85347629a25e593d30cb9020fe6
  • Loading branch information
fkgozali authored and facebook-github-bot committed Dec 17, 2019
1 parent 55142ef commit eb95b2f
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 9 deletions.
43 changes: 40 additions & 3 deletions Libraries/Image/RCTImageLoader.mm
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ - (RCTImageURLLoaderRequest *)_loadImageOrDataWithURLRequest:(NSURLRequest *)req
completionHandler:^(NSError *error, UIImage *image) {
completionHandler(error, image, nil);
}];
return [[RCTImageURLLoaderRequest alloc] initWithRequestId:nil cancellationBlock:cb];
return [[RCTImageURLLoaderRequest alloc] initWithRequestId:nil imageURL:request.URL cancellationBlock:cb];
}

// All access to URL cache must be serialized
Expand Down Expand Up @@ -574,7 +574,7 @@ - (RCTImageURLLoaderRequest *)_loadImageOrDataWithURLRequest:(NSURLRequest *)req
}
});

return [[RCTImageURLLoaderRequest alloc] initWithRequestId:requestId cancellationBlock:^{
return [[RCTImageURLLoaderRequest alloc] initWithRequestId:requestId imageURL:request.URL cancellationBlock:^{
BOOL alreadyCancelled = atomic_fetch_or(cancelled.get(), 1);
if (alreadyCancelled) {
return;
Expand Down Expand Up @@ -700,6 +700,8 @@ - (RCTImageLoaderCancellationBlock)_loadURLRequest:(NSURLRequest *)request
};
}

#pragma mark - RCTImageLoaderWithAttributionProtocol

- (RCTImageURLLoaderRequest *)loadImageWithURLRequest:(NSURLRequest *)imageURLRequest
size:(CGSize)size
scale:(CGFloat)scale
Expand Down Expand Up @@ -777,7 +779,42 @@ - (RCTImageURLLoaderRequest *)loadImageWithURLRequest:(NSURLRequest *)imageURLRe
partialLoadBlock:partialLoadBlock
completionBlock:completionHandler];
cancelLoad = loaderRequest.cancellationBlock;
return [[RCTImageURLLoaderRequest alloc] initWithRequestId:loaderRequest.requestId cancellationBlock:cancellationBlock];
return [[RCTImageURLLoaderRequest alloc] initWithRequestId:loaderRequest.requestId imageURL:imageURLRequest.URL cancellationBlock:cancellationBlock];
}

- (void)trackURLImageContentDidSetForRequest:(RCTImageURLLoaderRequest *)loaderRequest
{
if (!loaderRequest) {
return;
}

id<RCTImageURLLoader> loadHandler = [self imageURLLoaderForURL:loaderRequest.imageURL];
if ([loadHandler respondsToSelector:@selector(trackURLImageContentDidSetForRequest:)]) {
[(id<RCTImageURLLoaderWithAttribution>)loadHandler trackURLImageContentDidSetForRequest:loaderRequest];
}
}

- (void)trackURLImageVisibilityForRequest:(RCTImageURLLoaderRequest *)loaderRequest imageView:(UIView *)imageView
{
if (!loaderRequest || !imageView) {
return;
}

id<RCTImageURLLoader> loadHandler = [self imageURLLoaderForURL:loaderRequest.imageURL];
if ([loadHandler respondsToSelector:@selector(trackURLImageVisibilityForRequest:imageView:)]) {
[(id<RCTImageURLLoaderWithAttribution>)loadHandler trackURLImageVisibilityForRequest:loaderRequest imageView:imageView];
}
}

- (void)trackURLImageDidDestroy:(RCTImageURLLoaderRequest *)loaderRequest
{
if (!loaderRequest) {
return;
}
id<RCTImageURLLoader> loadHandler = [self imageURLLoaderForURL:loaderRequest.imageURL];
if ([loadHandler respondsToSelector:@selector(trackURLImageDidDestroy:)]) {
[(id<RCTImageURLLoaderWithAttribution>)loadHandler trackURLImageDidDestroy:loaderRequest];
}
}

- (RCTImageLoaderCancellationBlock)decodeImageData:(NSData *)data
Expand Down
14 changes: 14 additions & 0 deletions Libraries/Image/RCTImageLoaderWithAttributionProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,19 @@ RCT_EXTERN void RCTEnableImageLoadingPerfInstrumentation(BOOL enabled);
progressBlock:(RCTImageLoaderProgressBlock)progressBlock
partialLoadBlock:(RCTImageLoaderPartialLoadBlock)partialLoadBlock
completionBlock:(RCTImageLoaderCompletionBlock)completionBlock;
/**
* Image instrumentation - notify that the image content (UIImage) has been set on the native view.
*/
- (void)trackURLImageContentDidSetForRequest:(RCTImageURLLoaderRequest *)loaderRequest;

/**
* Image instrumentation - start tracking the on-screen visibility of the native image view.
*/
- (void)trackURLImageVisibilityForRequest:(RCTImageURLLoaderRequest *)loaderRequest imageView:(UIView *)imageView;

/**
* Image instrumentation - notify that the native image view was destroyed.
*/
- (void)trackURLImageDidDestroy:(RCTImageURLLoaderRequest *)loaderRequest;

@end
18 changes: 17 additions & 1 deletion Libraries/Image/RCTImageURLLoaderWithAttribution.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ struct ImageURLLoaderAttribution {
@interface RCTImageURLLoaderRequest : NSObject

@property (nonatomic, strong, readonly) NSString *requestId;
@property (nonatomic, strong, readonly) NSURL *imageURL;
@property (nonatomic, copy, readonly) RCTImageLoaderCancellationBlock cancellationBlock;

- (instancetype)initWithRequestId:(NSString *)requestId cancellationBlock:(RCTImageLoaderCancellationBlock)cancellationBlock;
- (instancetype)initWithRequestId:(NSString *)requestId imageURL:(NSURL *)imageURL cancellationBlock:(RCTImageLoaderCancellationBlock)cancellationBlock;
- (void)cancel;

@end
Expand All @@ -48,4 +49,19 @@ struct ImageURLLoaderAttribution {
partialLoadHandler:(RCTImageLoaderPartialLoadBlock)partialLoadHandler
completionHandler:(RCTImageLoaderCompletionBlock)completionHandler;

/**
* Image instrumentation - notify that the image content (UIImage) has been set on the native view.
*/
- (void)trackURLImageContentDidSetForRequest:(RCTImageURLLoaderRequest *)loaderRequest;

/**
* Image instrumentation - start tracking the on-screen visibility of the native image view.
*/
- (void)trackURLImageVisibilityForRequest:(RCTImageURLLoaderRequest *)loaderRequest imageView:(UIView *)imageView;

/**
* Image instrumentation - notify that the native image view was destroyed.
*/
- (void)trackURLImageDidDestroy:(RCTImageURLLoaderRequest *)loaderRequest;

@end
3 changes: 2 additions & 1 deletion Libraries/Image/RCTImageURLLoaderWithAttribution.mm
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@

@implementation RCTImageURLLoaderRequest

- (instancetype)initWithRequestId:(NSString *)requestId cancellationBlock:(RCTImageLoaderCancellationBlock)cancellationBlock
- (instancetype)initWithRequestId:(NSString *)requestId imageURL:(NSURL *)imageURL cancellationBlock:(RCTImageLoaderCancellationBlock)cancellationBlock
{
if (self = [super init]) {
_requestId = requestId;
_imageURL = imageURL;
_cancellationBlock = cancellationBlock;
}

Expand Down
12 changes: 12 additions & 0 deletions ReactCommon/fabric/imagemanager/platform/cxx/ImageRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,17 @@ const std::shared_ptr<const ImageResponseObserverCoordinator>
// Not implemented
abort();
}

const std::shared_ptr<const ImageInstrumentation>
&ImageRequest::getSharedImageInstrumentation() const {
// Not implemented
abort();
}

const ImageInstrumentation &ImageRequest::getImageInstrumentation() const {
// Not implemented
abort();
}

} // namespace react
} // namespace facebook
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,18 @@ namespace react {
class RCTImageInstrumentationProxy final : public ImageInstrumentation {
public:
RCTImageInstrumentationProxy(id<RCTImageLoaderWithAttributionProtocol> imageLoader);
~RCTImageInstrumentationProxy();

void didSetImage() const override;
void didEnterVisibilityRange() const override;
void didExitVisibilityRange() const override;

void trackNativeImageView(UIView *imageView) const;
void setImageURLLoaderRequest(RCTImageURLLoaderRequest *request);

private:
__weak id<RCTImageLoaderWithAttributionProtocol> imageLoader_;
RCTImageURLLoaderRequest *imageURLLoaderRequest_;
};

} // namespace react
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,25 @@
{
}

RCTImageInstrumentationProxy::~RCTImageInstrumentationProxy()
{
if (!imageURLLoaderRequest_) {
return;
}
[imageLoader_ trackURLImageDidDestroy:imageURLLoaderRequest_];
}

void RCTImageInstrumentationProxy::didSetImage() const
{
if (!RCTImageLoadingPerfInstrumentationEnabled()) {
return;
}

// TODO (T58941612): Not yet supported.
if (imageLoader_) {
if (!imageURLLoaderRequest_) {
return;
}

[imageLoader_ trackURLImageContentDidSetForRequest:imageURLLoaderRequest_];
}

void RCTImageInstrumentationProxy::didEnterVisibilityRange() const
Expand All @@ -33,6 +43,9 @@
}

// TODO (T58941612): Not yet supported.
if (!imageURLLoaderRequest_) {
return;
}
}

void RCTImageInstrumentationProxy::didExitVisibilityRange() const
Expand All @@ -42,6 +55,9 @@
}

// TODO (T58941612): Not yet supported.
if (!imageURLLoaderRequest_) {
return;
}
}

void RCTImageInstrumentationProxy::trackNativeImageView(UIView *imageView) const
Expand All @@ -50,7 +66,15 @@
return;
}

// TODO (T58941612): Not yet supported.
if (!imageURLLoaderRequest_) {
return;
}
[imageLoader_ trackURLImageVisibilityForRequest:imageURLLoaderRequest_ imageView:imageView];
}

void RCTImageInstrumentationProxy::setImageURLLoaderRequest(RCTImageURLLoaderRequest *request)
{
imageURLLoaderRequest_ = request;
}

} // namespace react
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ - (ImageRequest)requestImage:(ImageSource)imageSource surfaceId:(SurfaceId)surfa
{
SystraceSection s("RCTImageManager::requestImage");

auto imageRequest = ImageRequest(imageSource, std::make_unique<RCTImageInstrumentationProxy>(_imageLoader));
auto imageInstrumentation = std::make_shared<RCTImageInstrumentationProxy>(_imageLoader);
auto imageRequest = ImageRequest(imageSource, imageInstrumentation);
auto weakObserverCoordinator =
(std::weak_ptr<const ImageResponseObserverCoordinator>)imageRequest.getSharedObserverCoordinator();

Expand Down Expand Up @@ -97,6 +98,10 @@ - (ImageRequest)requestImage:(ImageSource)imageSource surfaceId:(SurfaceId)surfa
completionBlock:completionBlock];
RCTImageLoaderCancellationBlock cancelationBlock = loaderRequest.cancellationBlock;
sharedCancelationFunction.assign([cancelationBlock]() { cancelationBlock(); });

if (imageInstrumentation) {
imageInstrumentation->setImageURLLoaderRequest(loaderRequest);
}
});

return imageRequest;
Expand Down

0 comments on commit eb95b2f

Please sign in to comment.