Skip to content

Commit

Permalink
feat(ios): add ios bundle load operation queue
Browse files Browse the repository at this point in the history
  • Loading branch information
ozonelmy authored and hippy-actions[bot] committed Jun 28, 2023
1 parent eab6d8a commit 5d05b0d
Show file tree
Hide file tree
Showing 18 changed files with 56 additions and 45 deletions.
7 changes: 5 additions & 2 deletions framework/ios/base/bridge/HippyBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,12 @@ - (void)beginLoadingBundle:(NSURL *)bundleURL
__block NSData *script = nil;
self.loadingCount++;
dispatch_group_enter(group);
NSOperationQueue *bundleQueue = [[NSOperationQueue alloc] init];
bundleQueue.maxConcurrentOperationCount = 1;
bundleQueue.name = @"com.hippy.bundleQueue";
HippyBundleLoadOperation *fetchOp = [[HippyBundleLoadOperation alloc] initWithBridge:self
bundleURL:bundleURL
queue:HippyBridgeQueue()];
queue:bundleQueue];
fetchOp.onLoad = ^(NSData *source, NSError *error) {
if (error) {
HippyBridgeFatal(error, weakSelf);
Expand Down Expand Up @@ -348,7 +351,7 @@ - (void)beginLoadingBundle:(NSURL *)bundleURL
}
dispatch_group_leave(group);
}];
} queue:HippyBridgeQueue()];
} queue:bundleQueue];
//set dependency
[executeOp addDependency:fetchOp];
if (_lastOperation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ NS_ASSUME_NONNULL_BEGIN

@interface HippyBundleExecutionOperation : NSOperation

- (instancetype)initWithBlock:(dispatch_block_t)block queue:(dispatch_queue_t)queue;
- (instancetype)initWithBlock:(dispatch_block_t)block queue:(NSOperationQueue *)queue;

@end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,19 @@ @interface HippyBundleExecutionOperation () {

dispatch_semaphore_t _statusSem;
dispatch_semaphore_t _dependencySem;
dispatch_queue_t _opQueue;
}

@end

@implementation HippyBundleExecutionOperation

- (instancetype)initWithBlock:(dispatch_block_t)block queue:(dispatch_queue_t)queue {
- (instancetype)initWithBlock:(dispatch_block_t)block queue:(NSOperationQueue *)queue {
self = [super init];
if (self) {
_block = [block copy];
_dependencies = [NSMutableSet setWithCapacity:8];
_statusSem = dispatch_semaphore_create(1);
_dependencySem = dispatch_semaphore_create(1);
_opQueue = queue;
self.ready = YES;
}
return self;
Expand Down Expand Up @@ -77,12 +75,7 @@ - (void)main {
self.finished = NO;
self.executing = YES;
if (_block) {
if (_opQueue) {
dispatch_sync(_opQueue, _block);
}
else {
_block();
}
_block();
}
self.finished = YES;
self.executing = NO;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ typedef void (^HippyBundleLoadCompletionBlock)(NSData *, NSError *);

@property(nonatomic, copy) HippyBundleLoadCompletionBlock onLoad;

- (instancetype)initWithBridge:(HippyBridge *)bridge bundleURL:(NSURL *)bundleURL queue:(dispatch_queue_t)queue;
- (instancetype)initWithBridge:(HippyBridge *)bridge bundleURL:(NSURL *)bundleURL queue:(NSOperationQueue *)queue;

@end

Expand Down
14 changes: 4 additions & 10 deletions framework/ios/base/bundleoperations/HippyBundleLoadOperation.mm
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ @interface HippyBundleLoadOperation () {
BOOL _asynchronous;
BOOL _ready;
std::mutex _statusMutex;
dispatch_queue_t _finishQueue;
NSOperationQueue *_finishQueue;
}

@end
Expand All @@ -46,7 +46,7 @@ @implementation HippyBundleLoadOperation
@synthesize finished = _finished;
@synthesize executing = _executing;

- (instancetype)initWithBridge:(HippyBridge *)bridge bundleURL:(NSURL *)bundleURL queue:(dispatch_queue_t)queue {
- (instancetype)initWithBridge:(HippyBridge *)bridge bundleURL:(NSURL *)bundleURL queue:(NSOperationQueue *)queue {
self = [super init];
if (self) {
_bridge = bridge;
Expand Down Expand Up @@ -78,6 +78,7 @@ - (void)main {
method:@"get"
params:nil
body:nil
queue:_finishQueue
progress:nil
completionHandler:^(NSData * _Nonnull data, NSURLResponse * _Nonnull response, NSError * _Nonnull error) {
HippyBundleLoadOperation *strongSelf = weakSelf;
Expand All @@ -90,14 +91,7 @@ - (void)main {
[performanceLogger markStopForTag:HippyPLScriptDownload forKey:bundleURL];
[performanceLogger setValue:sourceLength forTag:HippyPLBundleSize forKey:bundleURL];
if (strongSelf.onLoad) {
if (strongSelf->_finishQueue) {
dispatch_sync(strongSelf->_finishQueue, ^{
strongSelf.onLoad(data, error);
});
}
else {
strongSelf.onLoad(data, error);
}
strongSelf.onLoad(data, error);
}
strongSelf.finished = YES;
strongSelf.executing = NO;
Expand Down
2 changes: 2 additions & 0 deletions framework/ios/module/imageloader/HippyImageLoaderModule.mm
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ @implementation HippyImageLoaderModule
method:@"Get"
params:nil
body:nil
queue:nil
progress:nil
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (!error) {
Expand Down Expand Up @@ -100,6 +101,7 @@ @implementation HippyImageLoaderModule
method:@"Get"
params:nil
body:nil
queue:nil
progress:nil
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

Expand Down
1 change: 1 addition & 0 deletions framework/ios/module/loader/HippyBridge+VFSLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ NS_ASSUME_NONNULL_BEGIN
method:(NSString *_Nullable)method
params:(NSDictionary<NSString *, NSString *> *_Nullable)httpHeaders
body:(NSData *_Nullable)body
queue:(NSOperationQueue *_Nullable)queue
progress:(void(^ _Nullable)(NSUInteger current, NSUInteger total))progress
completionHandler:(void (^)(NSData *_Nullable data, NSURLResponse *_Nullable response, NSError *_Nullable error))completionHandler;
@end
Expand Down
3 changes: 2 additions & 1 deletion framework/ios/module/loader/HippyBridge+VFSLoader.mm
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ - (void)loadContentsAsynchronouslyFromUrl:(NSString *)urlString
method:(NSString *_Nullable)method
params:(NSDictionary<NSString *, NSString *> *)httpHeaders
body:(NSData *)body
queue:(NSOperationQueue *_Nullable)queue
progress:(void(^)(NSUInteger current, NSUInteger total))progress
completionHandler:(void (^)(NSData *data, NSURLResponse *response, NSError *error))completionHandler {
if (!urlString || !completionHandler) {
Expand All @@ -52,7 +53,7 @@ - (void)loadContentsAsynchronouslyFromUrl:(NSString *)urlString
if (body) {
[request setHTTPBody:body];
}
loader->RequestUntrustedContent(request, progress, completionHandler);
loader->RequestUntrustedContent(request, queue, progress, completionHandler);
}
}

Expand Down
4 changes: 3 additions & 1 deletion framework/ios/module/loader/HippyFileHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ class HippyFileHandler : public VFSUriHandler {
std::function<void(std::shared_ptr<hippy::JobResponse>)> cb,
std::function<std::shared_ptr<UriHandler>()> next) override;

virtual void RequestUntrustedContent(NSURLRequest *request, VFSHandlerProgressBlock progress,
virtual void RequestUntrustedContent(NSURLRequest *request,
NSOperationQueue *queue,
VFSHandlerProgressBlock progress,
VFSHandlerCompletionBlock completion, VFSGetNextHandlerBlock next) override;
private:
__weak HippyBridge *bridge_;
Expand Down
9 changes: 6 additions & 3 deletions framework/ios/module/loader/HippyFileHandler.mm
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@
FOOTSTONE_UNIMPLEMENTED();
}

void HippyFileHandler::RequestUntrustedContent(NSURLRequest *request, VFSHandlerProgressBlock progress,
VFSHandlerCompletionBlock completion, VFSGetNextHandlerBlock next) {
void HippyFileHandler::RequestUntrustedContent(NSURLRequest *request,
NSOperationQueue *queue,
VFSHandlerProgressBlock progress,
VFSHandlerCompletionBlock completion,
VFSGetNextHandlerBlock next) {
if (!completion) {
return;
}
Expand Down Expand Up @@ -71,5 +74,5 @@
}
NSMutableURLRequest *req = [request mutableCopy];
[req setURL:absoluteURL];
VFSUriHandler::RequestUntrustedContent(req, progress, completion, next);
VFSUriHandler::RequestUntrustedContent(req, queue, progress, completion, next);
}
1 change: 1 addition & 0 deletions framework/ios/module/network/HippyNetWork.mm
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ @implementation HippyNetWork
method:method?:@"Get"
params:vfsParams
body:data
queue:nil
progress:nil
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSStringEncoding encoding = GetStringEncodingFromURLResponse(response);
Expand Down
4 changes: 3 additions & 1 deletion modules/vfs/ios/VFSUriHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ class VFSUriHandler : public hippy::vfs::UriHandler {
std::function<void(std::shared_ptr<hippy::JobResponse>)> cb,
std::function<std::shared_ptr<UriHandler>()> next) override;

virtual void RequestUntrustedContent(NSURLRequest *request, VFSHandlerProgressBlock progress,
virtual void RequestUntrustedContent(NSURLRequest *request,
NSOperationQueue *queue,
VFSHandlerProgressBlock progress,
VFSHandlerCompletionBlock completion,
VFSGetNextHandlerBlock next);
inline void SetLoader(const std::shared_ptr<VFSUriLoader> &loader){weakLoader_ = loader;}
Expand Down
7 changes: 4 additions & 3 deletions modules/vfs/ios/VFSUriHandler.mm
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ static bool CheckRequestFromCPP(const std::unordered_map<std::string, std::strin
cb(job_resp);
return;
}
loader->RequestUntrustedContent(req, ^(NSUInteger current, NSUInteger total) {
loader->RequestUntrustedContent(req, nil, ^(NSUInteger current, NSUInteger total) {
request->GetProgressCallback()(current, total);
}, ^(NSData *data, NSURLResponse *resp, NSError *error) {
RetCode code = RetCodeFromNSError(error);
Expand All @@ -170,6 +170,7 @@ static bool CheckRequestFromCPP(const std::unordered_map<std::string, std::strin
}

void VFSUriHandler::RequestUntrustedContent(NSURLRequest *request,
NSOperationQueue *queue,
VFSHandlerProgressBlock progress,
VFSHandlerCompletionBlock completion,
VFSGetNextHandlerBlock next) {
Expand All @@ -189,12 +190,12 @@ static bool CheckRequestFromCPP(const std::unordered_map<std::string, std::strin
return;
}
NSURLSessionDataProgress *dataProgress = [[NSURLSessionDataProgress alloc] initWithProgress:progress result:completion];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:dataProgress delegateQueue:nil];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:dataProgress delegateQueue:queue];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request];
if (!dataTask) {
auto nextHandler = next();
if (nextHandler) {
nextHandler->RequestUntrustedContent(request, progress, completion, next);
nextHandler->RequestUntrustedContent(request, queue, progress, completion, next);
}
else {
//try to forward to cpp uri handler
Expand Down
6 changes: 3 additions & 3 deletions modules/vfs/ios/VFSUriLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ class VFSUriLoader : public hippy::vfs::UriLoader {

virtual void AddConvenientDefaultHandler(const std::shared_ptr<VFSUriHandler>& handler);
virtual const std::list<std::shared_ptr<VFSUriHandler>> &GetConvenientDefaultHandlers();
virtual void RequestUntrustedContent(NSString *urlString, VFSHandlerProgressBlock progress, VFSHandlerCompletionBlock completion);
virtual void RequestUntrustedContent(NSString *urlString, NSString *method, NSDictionary<NSString *, NSString *> *httpHeader, NSData *body, VFSHandlerProgressBlock progress, VFSHandlerCompletionBlock completion);
virtual void RequestUntrustedContent(NSURLRequest *request, VFSHandlerProgressBlock progress, VFSHandlerCompletionBlock completion);
virtual void RequestUntrustedContent(NSString *urlString, NSOperationQueue *operationQueue, VFSHandlerProgressBlock progress, VFSHandlerCompletionBlock completion);
virtual void RequestUntrustedContent(NSString *urlString, NSString *method, NSOperationQueue *operationQueue, NSDictionary<NSString *, NSString *> *httpHeader, NSData *body, VFSHandlerProgressBlock progress, VFSHandlerCompletionBlock completion);
virtual void RequestUntrustedContent(NSURLRequest *request, NSOperationQueue *operationQueue, VFSHandlerProgressBlock progress, VFSHandlerCompletionBlock completion);
private:
std::shared_ptr<VFSUriHandler> GetNextConvinentHandler(std::list<std::shared_ptr<VFSUriHandler>>::iterator &cur_con_handler_it,
const std::list<std::shared_ptr<VFSUriHandler>>::iterator &end_con_handler_it);
Expand Down
21 changes: 14 additions & 7 deletions modules/vfs/ios/VFSUriLoader.mm
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,18 @@
hippy::vfs::UriLoader::RequestUntrustedContent(request, cb);
}

void VFSUriLoader::RequestUntrustedContent(NSString *urlString, VFSHandlerProgressBlock progress, VFSHandlerCompletionBlock completion) {
void VFSUriLoader::RequestUntrustedContent(NSString *urlString, NSOperationQueue *operationQueue,
VFSHandlerProgressBlock progress,
VFSHandlerCompletionBlock completion) {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
RequestUntrustedContent(request, progress, completion);
RequestUntrustedContent(request, operationQueue, progress, completion);
}

void VFSUriLoader::RequestUntrustedContent(NSString *urlString, NSString *method, NSDictionary<NSString *, NSString *> *httpHeader, NSData *body, VFSHandlerProgressBlock progress, VFSHandlerCompletionBlock completion) {
void VFSUriLoader::RequestUntrustedContent(NSString *urlString, NSString *method,
NSOperationQueue *operationQueue,
NSDictionary<NSString *, NSString *> *httpHeader, NSData *body,
VFSHandlerProgressBlock progress,
VFSHandlerCompletionBlock completion) {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
if (method) {
[request setHTTPMethod:method];
Expand All @@ -104,10 +110,11 @@
if (body) {
[request setHTTPBody:body];
}
RequestUntrustedContent(request, progress, completion);
RequestUntrustedContent(request, operationQueue, progress, completion);
}

void VFSUriLoader::RequestUntrustedContent(NSURLRequest *request, VFSHandlerProgressBlock progress, VFSHandlerCompletionBlock completion) {
void VFSUriLoader::RequestUntrustedContent(NSURLRequest *request, NSOperationQueue *operationQueue,
VFSHandlerProgressBlock progress, VFSHandlerCompletionBlock completion) {
if (!request || !completion) {
return;
}
Expand All @@ -134,12 +141,12 @@
auto &cur_convenient = (*cur_convenient_it);
//check if convenient loader exists, or forward to cpp loader
if (cur_convenient) {
cur_convenient->RequestUntrustedContent(request, progress, completion, block);
cur_convenient->RequestUntrustedContent(request, operationQueue, progress, completion, block);
}
else {
string_view uri = NSStringToU8StringView([requestURL absoluteString]);
auto meta = NSDictionaryToStringUnorderedMap([request allHTTPHeaderFields]);
auto progressCallback = [progress](int64_t current, int64_t total){
auto progressCallback = [progress, operationQueue](int64_t current, int64_t total){
if (progress) {
progress(current, total);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ typedef NS_ENUM(NSUInteger, ImageDataError) {
CGFloat bottomRightRadius;
} BorderRadiusStruct;

static NSOperationQueue *animated_image_queue() {
static NSOperationQueue *animated_image_queue(void) {
static dispatch_once_t onceToken;
static NSOperationQueue *_animatedImageOQ = nil;
dispatch_once(&onceToken, ^{
_animatedImageOQ = [[NSOperationQueue alloc] init];
_animatedImageOQ.name = @"com.hippy.animatedImage";
});
return _animatedImageOQ;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ - (void)loadImageSource:(NSString *)path forView:(NativeRenderImageView *)view {
if (!loader) {
return;
}
loader->RequestUntrustedContent(path, nil, ^(NSData *data, NSURLResponse *response, NSError *error) {
loader->RequestUntrustedContent(path, nil, nil, ^(NSData *data, NSURLResponse *response, NSError *error) {
NativeRenderImpl *renderImpl = self.renderImpl;
id<HPImageProviderProtocol> imageProvider = nil;
if (renderImpl) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ - (void)loadImageSource:(NSString *)path forView:(NativeRenderView *)view {
if (!loader) {
return;
}
loader->RequestUntrustedContent(path, nil, ^(NSData *data, NSURLResponse *response, NSError *error) {
loader->RequestUntrustedContent(path, nil, nil, ^(NSData *data, NSURLResponse *response, NSError *error) {
NativeRenderImpl *renderImpl = self.renderImpl;
id<HPImageProviderProtocol> imageProvider = nil;
if (renderImpl) {
Expand Down

0 comments on commit 5d05b0d

Please sign in to comment.