Add NSURLSession delegation to http_file_source.mm (#16321)
This commit is contained in:
@@ -2,12 +2,16 @@
|
|||||||
|
|
||||||
NS_ASSUME_NONNULL_BEGIN
|
NS_ASSUME_NONNULL_BEGIN
|
||||||
|
|
||||||
|
@class MGLNativeNetworkManager;
|
||||||
|
|
||||||
@protocol MGLNativeNetworkDelegate <NSObject>
|
@protocol MGLNativeNetworkDelegate <NSObject>
|
||||||
|
|
||||||
@optional
|
@optional
|
||||||
|
|
||||||
- (NSString *)skuToken;
|
- (NSString *)skuToken;
|
||||||
|
|
||||||
|
- (NSURLSession *)sessionForNetworkManager:(MGLNativeNetworkManager *)networkManager;
|
||||||
|
|
||||||
@required
|
@required
|
||||||
|
|
||||||
- (NSURLSessionConfiguration *)sessionConfiguration;
|
- (NSURLSessionConfiguration *)sessionConfiguration;
|
||||||
|
|||||||
@@ -249,10 +249,27 @@ std::unique_ptr<AsyncRequest> HTTPFileSource::request(const Resource& resource,
|
|||||||
if (isTile) {
|
if (isTile) {
|
||||||
[MGLNativeNetworkManager.sharedManager startDownloadEvent:url.relativePath type:@"tile"];
|
[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
|
dataTaskWithRequest:req
|
||||||
completionHandler:^(NSData* data, NSURLResponse* res, NSError* error) {
|
completionHandler:^(NSData* data, NSURLResponse* res, NSError* error) {
|
||||||
|
session = nil;
|
||||||
|
|
||||||
if (error && [error code] == NSURLErrorCancelled) {
|
if (error && [error code] == NSURLErrorCancelled) {
|
||||||
[MGLNativeNetworkManager.sharedManager cancelDownloadEventForResponse:res];
|
[MGLNativeNetworkManager.sharedManager cancelDownloadEventForResponse:res];
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -13,61 +13,65 @@ static MGLNativeNetworkManager *instance = nil;
|
|||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (NSURLSessionConfiguration *)sessionConfiguration {
|
+ (NSURLSessionConfiguration *)testSessionConfiguration {
|
||||||
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* sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
|
NSURLSessionConfiguration* sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
|
||||||
|
|
||||||
sessionConfiguration.timeoutIntervalForResource = 30;
|
sessionConfiguration.timeoutIntervalForResource = 30;
|
||||||
sessionConfiguration.HTTPMaximumConnectionsPerHost = 8;
|
sessionConfiguration.HTTPMaximumConnectionsPerHost = 8;
|
||||||
sessionConfiguration.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
|
sessionConfiguration.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
|
||||||
sessionConfiguration.URLCache = nil;
|
sessionConfiguration.URLCache = nil;
|
||||||
|
|
||||||
return sessionConfiguration;
|
return sessionConfiguration;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#pragma mark - Optionals
|
||||||
|
|
||||||
- (NSString *)skuToken {
|
- (NSString *)skuToken {
|
||||||
if(_delegate && [_delegate respondsToSelector:@selector(skuToken)]) {
|
if([self.delegate respondsToSelector:@selector(skuToken)]) {
|
||||||
return [_delegate skuToken];
|
return [self.delegate skuToken];
|
||||||
}
|
}
|
||||||
return nil;
|
return nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)startDownloadEvent:(NSString *)event type:(NSString *)type {
|
#pragma mark - Required
|
||||||
if (_delegate && [_delegate respondsToSelector:@selector(startDownloadEvent:type:)]) {
|
|
||||||
[_delegate startDownloadEvent:event type:type];
|
- (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 {
|
- (void)cancelDownloadEventForResponse:(NSURLResponse *)response {
|
||||||
if (_delegate && [_delegate respondsToSelector:@selector(cancelDownloadEventForResponse:)]) {
|
[self.delegate cancelDownloadEventForResponse:response];
|
||||||
return [_delegate cancelDownloadEventForResponse:response];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)stopDownloadEventForResponse:(NSURLResponse *)response {
|
- (void)stopDownloadEventForResponse:(NSURLResponse *)response {
|
||||||
if (_delegate && [_delegate respondsToSelector:@selector(stopDownloadEventForResponse:)]) {
|
[self.delegate stopDownloadEventForResponse:response];
|
||||||
return [_delegate stopDownloadEventForResponse:response];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)debugLog:(NSString *)format, ...{
|
- (void)debugLog:(NSString *)format, ... {
|
||||||
if (_delegate && [_delegate respondsToSelector:@selector(debugLog:)]) {
|
// TODO: Replace with existing mbgl logging handling.
|
||||||
return [_delegate debugLog:format];
|
[self.delegate debugLog:format];
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)errorLog:(NSString *)format, ...{
|
- (void)errorLog:(NSString *)format, ... {
|
||||||
if (_delegate && [_delegate respondsToSelector:@selector(errorLog:)]) {
|
// TODO: Replace with existing mbgl logging handling.
|
||||||
return [_delegate errorLog:format];
|
[self.delegate errorLog:format];
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|||||||
Reference in New Issue
Block a user