From 9009089a933013df93a35c095021a0c102f8788b Mon Sep 17 00:00:00 2001 From: Julian Rex Date: Wed, 25 Mar 2020 17:56:40 -0400 Subject: Add NSURLSession delegation to http_file_source.mm (#16321) --- .../mbgl/interface/native_apple_interface.h | 4 ++ platform/darwin/src/http_file_source.mm | 21 ++++++- platform/darwin/src/native_apple_interface.m | 66 ++++++++++++---------- 3 files changed, 58 insertions(+), 33 deletions(-) diff --git a/platform/darwin/include/mbgl/interface/native_apple_interface.h b/platform/darwin/include/mbgl/interface/native_apple_interface.h index d4d207462d..5453e798cf 100644 --- a/platform/darwin/include/mbgl/interface/native_apple_interface.h +++ b/platform/darwin/include/mbgl/interface/native_apple_interface.h @@ -2,12 +2,16 @@ NS_ASSUME_NONNULL_BEGIN +@class MGLNativeNetworkManager; + @protocol MGLNativeNetworkDelegate @optional - (NSString *)skuToken; +- (NSURLSession *)sessionForNetworkManager:(MGLNativeNetworkManager *)networkManager; + @required - (NSURLSessionConfiguration *)sessionConfiguration; diff --git a/platform/darwin/src/http_file_source.mm b/platform/darwin/src/http_file_source.mm index a72a97e299..1b5a67ea5d 100644 --- a/platform/darwin/src/http_file_source.mm +++ b/platform/darwin/src/http_file_source.mm @@ -249,10 +249,27 @@ std::unique_ptr HTTPFileSource::request(const Resource& resource, if (isTile) { [MGLNativeNetworkManager.sharedManager startDownloadEvent:url.relativePath type:@"tile"]; } - - request->task = [impl->session + + __block NSURLSession *session; + + // Use the delegate's session if there is one, otherwise use the one that + // was created when this class was constructed. + MGLNativeNetworkManager *networkManager = MGLNativeNetworkManager.sharedManager; + if ([networkManager.delegate respondsToSelector:@selector(sessionForNetworkManager:)]) { + session = [networkManager.delegate sessionForNetworkManager:networkManager]; + } + + if (!session) { + session = impl->session; + } + + assert(session); + + request->task = [session dataTaskWithRequest:req completionHandler:^(NSData* data, NSURLResponse* res, NSError* error) { + session = nil; + if (error && [error code] == NSURLErrorCancelled) { [MGLNativeNetworkManager.sharedManager cancelDownloadEventForResponse:res]; return; diff --git a/platform/darwin/src/native_apple_interface.m b/platform/darwin/src/native_apple_interface.m index 07dce0d5b0..6e59120b71 100644 --- a/platform/darwin/src/native_apple_interface.m +++ b/platform/darwin/src/native_apple_interface.m @@ -13,61 +13,65 @@ static MGLNativeNetworkManager *instance = nil; return instance; } -- (NSURLSessionConfiguration *)sessionConfiguration { - if (_delegate && [_delegate respondsToSelector:@selector(sessionConfiguration)]) { - return [_delegate sessionConfiguration]; - } - // For testing. Since we get a `nil` return when SDK is modualar, we use this for testing requests. - // Same as `[MGLNetworkConfiguration defaultSessionConfiguration]` in `MGLNetworkConfiguration.m`. - return [self testSessionConfiguration]; -} - -- (NSURLSessionConfiguration *)testSessionConfiguration { ++ (NSURLSessionConfiguration *)testSessionConfiguration { NSURLSessionConfiguration* sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration]; - + sessionConfiguration.timeoutIntervalForResource = 30; sessionConfiguration.HTTPMaximumConnectionsPerHost = 8; sessionConfiguration.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData; sessionConfiguration.URLCache = nil; - + return sessionConfiguration; } +#pragma mark - Optionals + - (NSString *)skuToken { - if(_delegate && [_delegate respondsToSelector:@selector(skuToken)]) { - return [_delegate skuToken]; + if([self.delegate respondsToSelector:@selector(skuToken)]) { + return [self.delegate skuToken]; } return nil; } -- (void)startDownloadEvent:(NSString *)event type:(NSString *)type { - if (_delegate && [_delegate respondsToSelector:@selector(startDownloadEvent:type:)]) { - [_delegate startDownloadEvent:event type:type]; +#pragma mark - Required + +- (NSURLSessionConfiguration *)sessionConfiguration { + NSURLSessionConfiguration *configuration = [_delegate sessionConfiguration]; + + if (!configuration) { + // TODO: Remove + NSLog(@"Using testSessionConfiguration"); + + // For testing. Since we get a `nil` return when SDK is modular, we use + // this for testing requests. + // Same as `[MGLNetworkConfiguration defaultSessionConfiguration]` in + // `MGLNetworkConfiguration.m`. + configuration = [MGLNativeNetworkManager testSessionConfiguration]; } + + return configuration; +} + +- (void)startDownloadEvent:(NSString *)event type:(NSString *)type { + [self.delegate startDownloadEvent:event type:type]; } - (void)cancelDownloadEventForResponse:(NSURLResponse *)response { - if (_delegate && [_delegate respondsToSelector:@selector(cancelDownloadEventForResponse:)]) { - return [_delegate cancelDownloadEventForResponse:response]; - } + [self.delegate cancelDownloadEventForResponse:response]; } - (void)stopDownloadEventForResponse:(NSURLResponse *)response { - if (_delegate && [_delegate respondsToSelector:@selector(stopDownloadEventForResponse:)]) { - return [_delegate stopDownloadEventForResponse:response]; - } + [self.delegate stopDownloadEventForResponse:response]; } -- (void)debugLog:(NSString *)format, ...{ - if (_delegate && [_delegate respondsToSelector:@selector(debugLog:)]) { - return [_delegate debugLog:format]; - } +- (void)debugLog:(NSString *)format, ... { + // TODO: Replace with existing mbgl logging handling. + [self.delegate debugLog:format]; } -- (void)errorLog:(NSString *)format, ...{ - if (_delegate && [_delegate respondsToSelector:@selector(errorLog:)]) { - return [_delegate errorLog:format]; - } +- (void)errorLog:(NSString *)format, ... { + // TODO: Replace with existing mbgl logging handling. + [self.delegate errorLog:format]; } @end -- cgit v1.2.1