Skip to content

Commit fdcdca4

Browse files
fkgozalifacebook-github-bot
authored andcommitted
iOS: Introduced RCTImageURLLoaderWithAttribution
Summary: Changelog: [iOS] [Changed] - New internal image attribution support, but files importing RCTImageLoader.h must be converted to ObjC++ This new interface is the same as RCTImageURLLoader, but with additional support to pass in optional attribution information. The attribution info is not strictly defined (we may do so in the future though), and it's up to the hosting application and RCTImageURLLoader classes to handle it. Reviewed By: sammy-SC Differential Revision: D18492882 fbshipit-source-id: c3870c60e6c2e7c65758fc3235ebf5db369e07dc
1 parent 806a2b8 commit fdcdca4

6 files changed

+136
-11
lines changed

Libraries/Image/RCTImageLoader.mm

+68-11
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,23 @@
1515
#import <React/RCTDefines.h>
1616
#import <React/RCTImageCache.h>
1717
#import <React/RCTImageLoader.h>
18+
#import <React/RCTImageLoaderWithAttributionProtocol.h>
1819
#import <React/RCTImageUtils.h>
1920
#import <React/RCTLog.h>
2021
#import <React/RCTNetworking.h>
2122
#import <React/RCTUtils.h>
2223

2324
#import "RCTImagePlugins.h"
2425

26+
using namespace facebook::react;
27+
2528
static NSInteger RCTImageBytesForImage(UIImage *image)
2629
{
2730
NSInteger singleImageBytes = image.size.width * image.size.height * image.scale * image.scale * 4;
2831
return image.images ? image.images.count * singleImageBytes : singleImageBytes;
2932
}
3033

31-
@interface RCTImageLoader() <NativeImageLoaderIOSSpec>
34+
@interface RCTImageLoader() <NativeImageLoaderIOSSpec, RCTImageLoaderWithAttributionProtocol>
3235

3336
@end
3437

@@ -288,11 +291,32 @@ - (RCTImageLoaderCancellationBlock) loadImageWithURLRequest:(NSURLRequest *)imag
288291
scale:1
289292
clipped:YES
290293
resizeMode:RCTResizeModeStretch
294+
attribution:{}
291295
progressBlock:nil
292296
partialLoadBlock:nil
293297
completionBlock:callback];
294298
}
295299

300+
- (RCTImageLoaderCancellationBlock)loadImageWithURLRequest:(NSURLRequest *)imageURLRequest
301+
size:(CGSize)size
302+
scale:(CGFloat)scale
303+
clipped:(BOOL)clipped
304+
resizeMode:(RCTResizeMode)resizeMode
305+
progressBlock:(RCTImageLoaderProgressBlock)progressBlock
306+
partialLoadBlock:(RCTImageLoaderPartialLoadBlock)partialLoadBlock
307+
completionBlock:(RCTImageLoaderCompletionBlock)completionBlock
308+
{
309+
return [self loadImageWithURLRequest:imageURLRequest
310+
size:size
311+
scale:scale
312+
clipped:clipped
313+
resizeMode:resizeMode
314+
attribution:{}
315+
progressBlock:progressBlock
316+
partialLoadBlock:partialLoadBlock
317+
completionBlock:completionBlock];
318+
}
319+
296320
- (void)dequeueTasks
297321
{
298322
dispatch_async(_URLRequestQueue, ^{
@@ -363,6 +387,7 @@ - (RCTImageLoaderCancellationBlock)_loadImageOrDataWithURLRequest:(NSURLRequest
363387
size:(CGSize)size
364388
scale:(CGFloat)scale
365389
resizeMode:(RCTResizeMode)resizeMode
390+
attribution:(const ImageURLLoaderAttribution &)attribution
366391
progressBlock:(RCTImageLoaderProgressBlock)progressHandler
367392
partialLoadBlock:(RCTImageLoaderPartialLoadBlock)partialLoadHandler
368393
completionBlock:(void (^)(NSError *error, id imageOrData, BOOL cacheResult, NSURLResponse *response))completionBlock
@@ -383,6 +408,9 @@ - (RCTImageLoaderCancellationBlock)_loadImageOrDataWithURLRequest:(NSURLRequest
383408
request = mutableRequest;
384409
}
385410

411+
// Create a copy here so the value is retained when accessed in the blocks below.
412+
ImageURLLoaderAttribution attributionCopy(attribution);
413+
386414
// Find suitable image URL loader
387415
id<RCTImageURLLoader> loadHandler = [self imageURLLoaderForURL:request.URL];
388416
BOOL requiresScheduling = [loadHandler respondsToSelector:@selector(requiresScheduling)] ?
@@ -417,13 +445,25 @@ - (RCTImageLoaderCancellationBlock)_loadImageOrDataWithURLRequest:(NSURLRequest
417445
// If the loader doesn't require scheduling we call it directly on
418446
// the main queue.
419447
if (loadHandler && !requiresScheduling) {
448+
if ([loadHandler conformsToProtocol:@protocol(RCTImageURLLoaderWithAttribution)]) {
449+
return [(id<RCTImageURLLoaderWithAttribution>)loadHandler loadImageForURL:request.URL
450+
size:size
451+
scale:scale
452+
resizeMode:resizeMode
453+
attribution:attributionCopy
454+
progressHandler:progressHandler
455+
partialLoadHandler:partialLoadHandler
456+
completionHandler:^(NSError *error, UIImage *image) {
457+
completionHandler(error, image, nil);
458+
}];
459+
}
420460
return [loadHandler loadImageForURL:request.URL
421461
size:size
422462
scale:scale
423463
resizeMode:resizeMode
424464
progressHandler:progressHandler
425465
partialLoadHandler:partialLoadHandler
426-
completionHandler:^(NSError *error, UIImage *image){
466+
completionHandler:^(NSError *error, UIImage *image) {
427467
completionHandler(error, image, nil);
428468
}];
429469
}
@@ -441,15 +481,29 @@ - (RCTImageLoaderCancellationBlock)_loadImageOrDataWithURLRequest:(NSURLRequest
441481
}
442482

443483
if (loadHandler) {
444-
dispatch_block_t cancelLoadLocal = [loadHandler loadImageForURL:request.URL
445-
size:size
446-
scale:scale
447-
resizeMode:resizeMode
448-
progressHandler:progressHandler
449-
partialLoadHandler:partialLoadHandler
450-
completionHandler:^(NSError *error, UIImage *image) {
451-
completionHandler(error, image, nil);
452-
}];
484+
dispatch_block_t cancelLoadLocal;
485+
if ([loadHandler conformsToProtocol:@protocol(RCTImageURLLoaderWithAttribution)]) {
486+
cancelLoadLocal = [(id<RCTImageURLLoaderWithAttribution>)loadHandler loadImageForURL:request.URL
487+
size:size
488+
scale:scale
489+
resizeMode:resizeMode
490+
attribution:attributionCopy
491+
progressHandler:progressHandler
492+
partialLoadHandler:partialLoadHandler
493+
completionHandler:^(NSError *error, UIImage *image) {
494+
completionHandler(error, image, nil);
495+
}];
496+
} else {
497+
cancelLoadLocal = [loadHandler loadImageForURL:request.URL
498+
size:size
499+
scale:scale
500+
resizeMode:resizeMode
501+
progressHandler:progressHandler
502+
partialLoadHandler:partialLoadHandler
503+
completionHandler:^(NSError *error, UIImage *image) {
504+
completionHandler(error, image, nil);
505+
}];
506+
}
453507
[cancelLoadLock lock];
454508
cancelLoad = cancelLoadLocal;
455509
[cancelLoadLock unlock];
@@ -607,6 +661,7 @@ - (RCTImageLoaderCancellationBlock)loadImageWithURLRequest:(NSURLRequest *)image
607661
scale:(CGFloat)scale
608662
clipped:(BOOL)clipped
609663
resizeMode:(RCTResizeMode)resizeMode
664+
attribution:(const ImageURLLoaderAttribution &)attribution
610665
progressBlock:(RCTImageLoaderProgressBlock)progressBlock
611666
partialLoadBlock:(RCTImageLoaderPartialLoadBlock)partialLoadBlock
612667
completionBlock:(RCTImageLoaderCompletionBlock)completionBlock
@@ -673,6 +728,7 @@ - (RCTImageLoaderCancellationBlock)loadImageWithURLRequest:(NSURLRequest *)image
673728
size:size
674729
scale:scale
675730
resizeMode:resizeMode
731+
attribution:attribution
676732
progressBlock:progressBlock
677733
partialLoadBlock:partialLoadBlock
678734
completionBlock:completionHandler];
@@ -830,6 +886,7 @@ - (RCTImageLoaderCancellationBlock)getImageSizeForURLRequest:(NSURLRequest *)ima
830886
size:CGSizeZero
831887
scale:1
832888
resizeMode:RCTResizeModeStretch
889+
attribution:{}
833890
progressBlock:NULL
834891
partialLoadBlock:NULL
835892
completionBlock:completion];

Libraries/Image/RCTImageLoaderProtocol.h

+1
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,5 @@
113113
* protocol. This method should be called in bridgeDidInitializeModule.
114114
*/
115115
- (void)setImageCache:(id<RCTImageCache>)cache;
116+
116117
@end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#import <UIKit/UIKit.h>
9+
10+
#import <React/RCTImageLoaderProtocol.h>
11+
#import <React/RCTImageURLLoaderWithAttribution.h>
12+
13+
@protocol RCTImageLoaderWithAttributionProtocol<RCTImageLoaderProtocol>
14+
15+
/**
16+
* Same as the variant in RCTImageURLLoaderProtocol, but allows passing attribution
17+
* information that each image URL loader can process.
18+
*/
19+
- (RCTImageLoaderCancellationBlock)loadImageWithURLRequest:(NSURLRequest *)imageURLRequest
20+
size:(CGSize)size
21+
scale:(CGFloat)scale
22+
clipped:(BOOL)clipped
23+
resizeMode:(RCTResizeMode)resizeMode
24+
attribution:(const facebook::react::ImageURLLoaderAttribution &)attribution
25+
progressBlock:(RCTImageLoaderProgressBlock)progressBlock
26+
partialLoadBlock:(RCTImageLoaderPartialLoadBlock)partialLoadBlock
27+
completionBlock:(RCTImageLoaderCompletionBlock)completionBlock;
28+
29+
@end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#import <React/RCTImageURLLoader.h>
9+
10+
namespace facebook {
11+
namespace react {
12+
13+
struct ImageURLLoaderAttribution {
14+
int32_t surfaceId = 0;
15+
};
16+
17+
} // namespace react
18+
} // namespace facebook
19+
20+
/**
21+
* Same as the RCTImageURLLoader interface, but allows passing in optional `attribution` information.
22+
* This is useful for per-app logging and other instrumentation.
23+
*/
24+
@protocol RCTImageURLLoaderWithAttribution <RCTImageURLLoader>
25+
26+
/**
27+
* Same as the RCTImageURLLoader variant above, but allows optional `attribution` information.
28+
*/
29+
- (RCTImageLoaderCancellationBlock)loadImageForURL:(NSURL *)imageURL
30+
size:(CGSize)size
31+
scale:(CGFloat)scale
32+
resizeMode:(RCTResizeMode)resizeMode
33+
attribution:(const facebook::react::ImageURLLoaderAttribution &)attribution
34+
progressHandler:(RCTImageLoaderProgressBlock)progressHandler
35+
partialLoadHandler:(RCTImageLoaderPartialLoadBlock)partialLoadHandler
36+
completionHandler:(RCTImageLoaderCompletionBlock)completionHandler;
37+
38+
@end
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)