Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(ios): refactor hippyBridge's vfs category methods #4146

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions framework/ios/base/bridge/HippyBridge+BundleLoad.mm
Original file line number Diff line number Diff line change
Expand Up @@ -216,16 +216,14 @@ - (void)fetchBundleWithURL:(NSURL *)bundleURL completion:(void (^)(NSData *sourc
HippyAssertParam(completion);
// Fetch the bundle
// Call the completion handler with the fetched data or error
[self loadContentsAsynchronouslyFromUrl:bundleURL.absoluteString
method:@"get"
params:nil
body:nil
queue:nil
progress:nil
completionHandler:^(NSData * _Nullable data,
NSDictionary * _Nullable userInfo,
NSURLResponse * _Nullable response,
NSError * _Nullable error) {
[self loadContentsAsyncFromUrl:bundleURL.absoluteString
params:nil
queue:nil
progress:nil
completionHandler:^(NSData * _Nullable data,
NSDictionary * _Nullable userInfo,
NSURLResponse * _Nullable response,
NSError * _Nullable error) {
completion(data, error);
}];
}
Expand Down
12 changes: 5 additions & 7 deletions framework/ios/module/imageloader/HippyImageLoaderModule.mm
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,11 @@ @implementation HippyImageLoaderModule
}

HIPPY_EXPORT_METHOD(getSize:(NSString *)urlString resolver:(HippyPromiseResolveBlock)resolve rejecter:(HippyPromiseRejectBlock)reject) {
[self.bridge loadContentsAsynchronouslyFromUrl:urlString
method:@"Get"
params:nil
body:nil
queue:nil
progress:nil
completionHandler:^(NSData *data, NSDictionary *userInfo, NSURLResponse *response, NSError *error) {
[self.bridge loadContentsAsyncFromUrl:urlString
params:nil
queue:nil
progress:nil
completionHandler:^(NSData *data, NSDictionary *userInfo, NSURLResponse *response, NSError *error) {
if (!error) {
id<HippyImageProviderProtocol> imageProvider = [self imageProviderForData:data];
if (!imageProvider) {
Expand Down
32 changes: 19 additions & 13 deletions framework/ios/module/network/HippyNetWork.mm
Original file line number Diff line number Diff line change
Expand Up @@ -95,23 +95,29 @@ @implementation HippyNetWork
}
}];

NSData *data = nil;
if (body) {
data = [body dataUsingEncoding:NSUTF8StringEncoding];
}

// Record request start time
CFTimeInterval startTime = CACurrentMediaTime();

// Construct url request
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:HippyURLWithString(url, nil)];
if (method) {
[request setHTTPMethod:method];
}
if (vfsParams) {
for (NSString *key in vfsParams) {
[request setValue:vfsParams[key] forHTTPHeaderField:key];
}
}
if (body) {
[request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
}

// Send Request
[self.bridge loadContentsAsynchronouslyFromUrl:url
method:method ?: @"GET"
params:vfsParams
body:data
queue:nil
progress:nil
completionHandler:^(NSData *data, NSDictionary *userInfo,
NSURLResponse *response, NSError *error) {
[self.bridge loadContentsAsyncWithRequest:request
queue:nil
progress:nil
completionHandler:^(NSData *data, NSDictionary *userInfo,
NSURLResponse *response, NSError *error) {
NSStringEncoding encoding = GetStringEncodingFromURLResponse(response);
NSString *dataStr = [[NSString alloc] initWithData:data encoding:encoding];
NSUInteger statusCode = 0;
Expand Down
33 changes: 26 additions & 7 deletions modules/vfs/ios/HippyBridge+VFSLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,34 @@

NS_ASSUME_NONNULL_BEGIN

/// Category of HippyBridge responsible for loading data
@interface HippyBridge (VFSLoader)

- (void)loadContentsAsynchronouslyFromUrl:(NSString *)urlString
method:(NSString *_Nullable)method
params:(NSDictionary<NSString *, NSString *> *_Nullable)httpHeaders
body:(NSData *_Nullable)body
queue:(NSOperationQueue *_Nullable)queue
progress:(VFSHandlerProgressBlock _Nullable)progress
completionHandler:(VFSHandlerCompletionBlock)completionHandler;
/// Load data from url (GET)
/// - Parameters:
/// - urlString: request url
/// - httpHeaders: http headers, optional
/// - queue: operation queue, optional
/// - progress: progress callback, optional
/// - completionHandler: completion callback
- (void)loadContentsAsyncFromUrl:(NSString *)urlString
params:(nullable NSDictionary<NSString *, NSString *> *)httpHeaders
queue:(nullable NSOperationQueue *)queue
progress:(nullable VFSHandlerProgressBlock)progress
completionHandler:(VFSHandlerCompletionBlock)completionHandler;


/// Load data using given request
/// - Parameters:
/// - request: URLRequest
/// - queue: operation queue, optional
/// - progress: progress callback, optional
/// - completionHandler: completion callback
- (void)loadContentsAsyncWithRequest:(NSURLRequest *)request
queue:(nullable NSOperationQueue *)queue
progress:(nullable VFSHandlerProgressBlock)progress
completionHandler:(VFSHandlerCompletionBlock)completionHandler;

@end

NS_ASSUME_NONNULL_END
31 changes: 18 additions & 13 deletions modules/vfs/ios/HippyBridge+VFSLoader.mm
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,36 @@

@implementation HippyBridge (VFSLoader)

- (void)loadContentsAsynchronouslyFromUrl:(NSString *)urlString
method:(NSString *_Nullable)method
params:(NSDictionary<NSString *, NSString *> *)httpHeaders
body:(NSData *)body
queue:(NSOperationQueue *_Nullable)queue
progress:(VFSHandlerProgressBlock)progress
completionHandler:(VFSHandlerCompletionBlock)completionHandler {
- (void)loadContentsAsyncFromUrl:(NSString *)urlString
params:(NSDictionary<NSString *,NSString *> *)httpHeaders
queue:(NSOperationQueue *)queue
progress:(VFSHandlerProgressBlock)progress
completionHandler:(VFSHandlerCompletionBlock)completionHandler {
if (!urlString || !completionHandler) {
return;
}
std::shared_ptr<VFSUriLoader> loader = [self vfsUriLoader].lock();
if (loader) {
NSURL *url = HippyURLWithString(urlString, nil);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
if (method) {
[request setHTTPMethod:method];
}
if (httpHeaders) {
for (NSString *key in httpHeaders) {
[request setValue:httpHeaders[key] forHTTPHeaderField:key];
}
}
if (body) {
[request setHTTPBody:body];
}
loader->RequestUntrustedContent(request, nil, queue, progress, completionHandler);
}
}

- (void)loadContentsAsyncWithRequest:(NSURLRequest *)request
queue:(NSOperationQueue *_Nullable)queue
progress:(VFSHandlerProgressBlock)progress
completionHandler:(VFSHandlerCompletionBlock)completionHandler {
if (!request || !completionHandler) {
return;
}
std::shared_ptr<VFSUriLoader> loader = [self vfsUriLoader].lock();
if (loader) {
loader->RequestUntrustedContent(request, nil, queue, progress, completionHandler);
}
}
Expand Down
Loading