iOS Fabric: added support for image instrumentation [2]

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
This commit is contained in:
Kevin Gozali
2019-12-16 22:50:45 -08:00
committed by Facebook Github Bot
parent 55142efd3a
commit eb95b2f855
8 changed files with 121 additions and 9 deletions
+40 -3
View File
@@ -507,7 +507,7 @@ static UIImage *RCTResizeImageIfNeeded(UIImage *image,
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
@@ -574,7 +574,7 @@ static UIImage *RCTResizeImageIfNeeded(UIImage *image,
}
});
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;
@@ -700,6 +700,8 @@ static UIImage *RCTResizeImageIfNeeded(UIImage *image,
};
}
#pragma mark - RCTImageLoaderWithAttributionProtocol
- (RCTImageURLLoaderRequest *)loadImageWithURLRequest:(NSURLRequest *)imageURLRequest
size:(CGSize)size
scale:(CGFloat)scale
@@ -777,7 +779,42 @@ static UIImage *RCTResizeImageIfNeeded(UIImage *image,
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
@@ -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
@@ -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
@@ -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
@@ -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;
}
@@ -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
@@ -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
@@ -15,15 +15,25 @@ RCTImageInstrumentationProxy::RCTImageInstrumentationProxy(id<RCTImageLoaderWith
{
}
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
@@ -33,6 +43,9 @@ void RCTImageInstrumentationProxy::didEnterVisibilityRange() const
}
// TODO (T58941612): Not yet supported.
if (!imageURLLoaderRequest_) {
return;
}
}
void RCTImageInstrumentationProxy::didExitVisibilityRange() const
@@ -42,6 +55,9 @@ void RCTImageInstrumentationProxy::didExitVisibilityRange() const
}
// TODO (T58941612): Not yet supported.
if (!imageURLLoaderRequest_) {
return;
}
}
void RCTImageInstrumentationProxy::trackNativeImageView(UIView *imageView) const
@@ -50,7 +66,15 @@ void RCTImageInstrumentationProxy::trackNativeImageView(UIView *imageView) const
return;
}
// TODO (T58941612): Not yet supported.
if (!imageURLLoaderRequest_) {
return;
}
[imageLoader_ trackURLImageVisibilityForRequest:imageURLLoaderRequest_ imageView:imageView];
}
void RCTImageInstrumentationProxy::setImageURLLoaderRequest(RCTImageURLLoaderRequest *request)
{
imageURLLoaderRequest_ = request;
}
} // namespace react
@@ -41,7 +41,8 @@ using namespace facebook::react;
{
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();
@@ -97,6 +98,10 @@ using namespace facebook::react;
completionBlock:completionBlock];
RCTImageLoaderCancellationBlock cancelationBlock = loaderRequest.cancellationBlock;
sharedCancelationFunction.assign([cancelationBlock]() { cancelationBlock(); });
if (imageInstrumentation) {
imageInstrumentation->setImageURLLoaderRequest(loaderRequest);
}
});
return imageRequest;