diff --git a/platform/darwin/bazel/files.bzl b/platform/darwin/bazel/files.bzl index 872b0352b..7ee28ac9b 100644 --- a/platform/darwin/bazel/files.bzl +++ b/platform/darwin/bazel/files.bzl @@ -109,6 +109,7 @@ MLN_DARWIN_OBJC_HEADERS = [ "src/NSValue+MLNAdditions.h", "src/MLNPluginLayer.h", "src/MLNPluginStyleLayer.h", + "src/MLNNetworkResponse.h", ] MLN_DARWIN_OBJCPP_HEADERS = [ @@ -224,6 +225,7 @@ MLN_DARWIN_PUBLIC_OBJCPP_SOURCE = [ "src/NSValue+MLNStyleAttributeAdditions.mm", "src/MLNPluginLayer.mm", "src/MLNPluginStyleLayer.mm", + "src/MLNNetworkResponse.mm", ] MLN_DARWIN_PUBLIC_OBJCPP_CUSTOM_DRAWABLE_SOURCE = [ "src/MLNCustomDrawableStyleLayer_Private.h", diff --git a/platform/darwin/core/http_file_source.mm b/platform/darwin/core/http_file_source.mm index 62ced8e7d..1964968dc 100644 --- a/platform/darwin/core/http_file_source.mm +++ b/platform/darwin/core/http_file_source.mm @@ -12,7 +12,6 @@ #import #include - #include #include @@ -292,11 +291,24 @@ std::unique_ptr HTTPFileSource::request(const Resource& resource, assert(session); + if ([networkManager.delegate respondsToSelector:@selector(willSendRequest:)]) { + req = [networkManager.delegate willSendRequest:req]; + } + request->task = [session dataTaskWithRequest:req completionHandler:^(NSData* data, NSURLResponse* res, NSError* error) { session = nil; + if ([networkManager.delegate respondsToSelector:@selector(didReceiveResponse:)]) { + MLNInternalNetworkResponse *networkResponse = [MLNInternalNetworkResponse responseWithData:data urlResponse:res error:error]; + networkResponse = [networkManager.delegate didReceiveResponse:networkResponse]; + data = networkResponse.data; + res = networkResponse.response; + error = networkResponse.error; + } + + if (error && [error code] == NSURLErrorCancelled) { [MLNNativeNetworkManager.sharedManager cancelDownloadEventForResponse:res]; return; diff --git a/platform/darwin/core/native_apple_interface.m b/platform/darwin/core/native_apple_interface.m index edd3b981d..149feca30 100644 --- a/platform/darwin/core/native_apple_interface.m +++ b/platform/darwin/core/native_apple_interface.m @@ -1,6 +1,23 @@ #import #import +@implementation MLNInternalNetworkResponse + ++(MLNInternalNetworkResponse *)responseWithData:(NSData *)data + urlResponse:(NSURLResponse *)response + error:(NSError *)error { + + MLNInternalNetworkResponse *tempResult = [[MLNInternalNetworkResponse alloc] init]; + tempResult.data = data; + tempResult.response = response; + tempResult.error = error; + return tempResult; +} + +@end + + + @implementation MLNNativeNetworkManager static MLNNativeNetworkManager *instance = nil; diff --git a/platform/darwin/include/mbgl/interface/native_apple_interface.h b/platform/darwin/include/mbgl/interface/native_apple_interface.h index 7633819bb..1118da1e3 100644 --- a/platform/darwin/include/mbgl/interface/native_apple_interface.h +++ b/platform/darwin/include/mbgl/interface/native_apple_interface.h @@ -4,12 +4,28 @@ NS_ASSUME_NONNULL_BEGIN @class MLNNativeNetworkManager; +@interface MLNInternalNetworkResponse : NSObject + +@property (retain, nullable) NSError *error; +@property (retain, nullable) NSData *data; +@property (retain, nullable) NSURLResponse *response; + ++ (MLNInternalNetworkResponse *)responseWithData:(NSData *)data + urlResponse:(NSURLResponse *)response + error:(NSError *)error; + +@end + @protocol MLNNativeNetworkDelegate @optional - (NSURLSession *)sessionForNetworkManager:(MLNNativeNetworkManager *)networkManager; +- (NSMutableURLRequest *)willSendRequest:(NSMutableURLRequest *)request; + +- (MLNInternalNetworkResponse *)didReceiveResponse:(MLNInternalNetworkResponse *)response; + @required - (NSURLSessionConfiguration *)sessionConfiguration; diff --git a/platform/darwin/src/MLNNetworkConfiguration.h b/platform/darwin/src/MLNNetworkConfiguration.h index 01c0fee46..19fc298e2 100644 --- a/platform/darwin/src/MLNNetworkConfiguration.h +++ b/platform/darwin/src/MLNNetworkConfiguration.h @@ -1,6 +1,7 @@ #import #import "MLNFoundation.h" +#import "MLNNetworkResponse.h" NS_ASSUME_NONNULL_BEGIN @@ -24,6 +25,11 @@ NS_ASSUME_NONNULL_BEGIN are not supported at this time. */ - (NSURLSession *)sessionForNetworkConfiguration:(MLNNetworkConfiguration *)configuration; + +- (NSMutableURLRequest *)willSendRequest:(NSMutableURLRequest *)request; + +- (MLNNetworkResponse *)didReceiveResponse:(MLNNetworkResponse *)response; + @end /** diff --git a/platform/darwin/src/MLNNetworkConfiguration.mm b/platform/darwin/src/MLNNetworkConfiguration.mm index 9cc8db3ac..dbc4816e0 100644 --- a/platform/darwin/src/MLNNetworkConfiguration.mm +++ b/platform/darwin/src/MLNNetworkConfiguration.mm @@ -87,6 +87,39 @@ NSString * const kMLNDownloadPerformanceEvent = @"mobile.performance_trace"; return session; } +- (NSMutableURLRequest *)willSendRequest:(NSMutableURLRequest *)request { + + if ([self.delegate respondsToSelector:@selector(willSendRequest:)]) { + return [self.delegate willSendRequest:request]; + } + + return request; + +} + +- (MLNInternalNetworkResponse *)didReceiveResponse:(MLNInternalNetworkResponse *)response { + + if ([self.delegate respondsToSelector:@selector(didReceiveResponse:)]) { + + MLNNetworkResponse *tempResponse = [MLNNetworkResponse responseWithData:response.data + urlResponse:response.response + error:response.error]; + if (tempResponse) { + MLNInternalNetworkResponse *internalResponse = [MLNInternalNetworkResponse responseWithData:tempResponse.data + urlResponse:tempResponse.response + error:tempResponse.error]; + return internalResponse; + } else { + return nil; + } + + } + + return response; + +} + + - (NSURLSessionConfiguration *)sessionConfiguration { NSURLSessionConfiguration *sessionConfig = nil; @synchronized (self) { diff --git a/platform/darwin/src/MLNNetworkResponse.h b/platform/darwin/src/MLNNetworkResponse.h new file mode 100644 index 000000000..b425c4540 --- /dev/null +++ b/platform/darwin/src/MLNNetworkResponse.h @@ -0,0 +1,19 @@ +#import +#import "MLNFoundation.h" + +NS_ASSUME_NONNULL_BEGIN + +MLN_EXPORT +@interface MLNNetworkResponse : NSObject + +@property (retain, nullable) NSError *error; +@property (retain, nullable) NSData *data; +@property (retain, nullable) NSURLResponse *response; + ++ (MLNNetworkResponse *)responseWithData:(NSData *)data + urlResponse:(NSURLResponse *)response + error:(NSError *)error; + +@end + +NS_ASSUME_NONNULL_END diff --git a/platform/darwin/src/MLNNetworkResponse.mm b/platform/darwin/src/MLNNetworkResponse.mm new file mode 100644 index 000000000..516aafbd7 --- /dev/null +++ b/platform/darwin/src/MLNNetworkResponse.mm @@ -0,0 +1,17 @@ +#import +#import "MLNNetworkResponse.h" + +@implementation MLNNetworkResponse + ++(MLNNetworkResponse *)responseWithData:(NSData *)data + urlResponse:(NSURLResponse *)response + error:(NSError *)error { + + MLNNetworkResponse *tempResult = [[MLNNetworkResponse alloc] init]; + tempResult.data = data; + tempResult.response = response; + tempResult.error = error; + return tempResult; +} + +@end