summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian Rex <julian.rex@gmail.com>2020-03-24 16:27:57 -0400
committerAlexander Shalamov <alexander.shalamov@mapbox.com>2020-03-25 16:03:02 +0200
commit9b88cc1633228d1b328d49eb0fed7bbb1b013776 (patch)
treea4d16bee68c9855fd1a3ac76e0ff49afd92020c4
parentacf663dab3fe30f30f2826a89a4067c634e2bf62 (diff)
downloadqtlocation-mapboxgl-9b88cc1633228d1b328d49eb0fed7bbb1b013776.tar.gz
Partial revert, so public API does not change. Adds a session delegate method.
-rw-r--r--platform/darwin/include/mbgl/interface/native_apple_interface.h8
-rw-r--r--platform/darwin/src/http_file_source.mm19
-rw-r--r--platform/darwin/src/native_apple_interface.m87
3 files changed, 63 insertions, 51 deletions
diff --git a/platform/darwin/include/mbgl/interface/native_apple_interface.h b/platform/darwin/include/mbgl/interface/native_apple_interface.h
index 8f41f725ac..5453e798cf 100644
--- a/platform/darwin/include/mbgl/interface/native_apple_interface.h
+++ b/platform/darwin/include/mbgl/interface/native_apple_interface.h
@@ -2,15 +2,19 @@
NS_ASSUME_NONNULL_BEGIN
+@class MGLNativeNetworkManager;
+
@protocol MGLNativeNetworkDelegate <NSObject>
@optional
- (NSString *)skuToken;
+- (NSURLSession *)sessionForNetworkManager:(MGLNativeNetworkManager *)networkManager;
+
@required
-- (NSURLSession *)session;
+- (NSURLSessionConfiguration *)sessionConfiguration;
- (void)startDownloadEvent:(NSString *)event type:(NSString *)type;
@@ -34,7 +38,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, readonly) NSString *skuToken;
-@property (nonatomic, readonly) NSURLSession *session;
+@property (nonatomic, readonly) NSURLSessionConfiguration *sessionConfiguration;
- (void)startDownloadEvent:(NSString *)event type:(NSString *)type;
diff --git a/platform/darwin/src/http_file_source.mm b/platform/darwin/src/http_file_source.mm
index 262dedb1df..5c4a9c1b82 100644
--- a/platform/darwin/src/http_file_source.mm
+++ b/platform/darwin/src/http_file_source.mm
@@ -84,10 +84,14 @@ class HTTPFileSource::Impl {
public:
Impl() {
@autoreleasepool {
+ NSURLSessionConfiguration *sessionConfig = MGLNativeNetworkManager.sharedManager.sessionConfiguration;
+ session = [NSURLSession sessionWithConfiguration:sessionConfig];
+
userAgent = getUserAgent();
}
}
+ NSURLSession* session = nil;
NSString* userAgent = nil;
NSInteger accountType = 0;
@@ -246,7 +250,20 @@ std::unique_ptr<AsyncRequest> HTTPFileSource::request(const Resource& resource,
[MGLNativeNetworkManager.sharedManager startDownloadEvent:url.relativePath type:@"tile"];
}
- NSURLSession *session = [MGLNativeNetworkManager.sharedManager.session copy];
+ 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
diff --git a/platform/darwin/src/native_apple_interface.m b/platform/darwin/src/native_apple_interface.m
index 86d2fe6780..6e59120b71 100644
--- a/platform/darwin/src/native_apple_interface.m
+++ b/platform/darwin/src/native_apple_interface.m
@@ -1,21 +1,6 @@
#import <Foundation/Foundation.h>
#import <mbgl/interface/native_apple_interface.h>
-static NSURLSessionConfiguration *testSessionConfiguration() {
- NSURLSessionConfiguration* sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
-
- sessionConfiguration.timeoutIntervalForResource = 30;
- sessionConfiguration.HTTPMaximumConnectionsPerHost = 8;
- sessionConfiguration.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
- sessionConfiguration.URLCache = nil;
-
- return sessionConfiguration;
-}
-
-@interface MGLNativeNetworkManager ()
-@property (nonatomic) NSURLSession *testSession;
-@end
-
@implementation MGLNativeNetworkManager
static MGLNativeNetworkManager *instance = nil;
@@ -28,59 +13,65 @@ static MGLNativeNetworkManager *instance = nil;
return instance;
}
-- (instancetype)init {
- if ((self = [super init])) {
- _testSession = [NSURLSession sessionWithConfiguration:testSessionConfiguration()];
- }
- return self;
-}
++ (NSURLSessionConfiguration *)testSessionConfiguration {
+ NSURLSessionConfiguration* sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
-- (NSURLSession *)session {
- NSURLSession *session;
- if (_delegate && [_delegate respondsToSelector:@selector(session)]) {
- session = [_delegate session];
- }
+ sessionConfiguration.timeoutIntervalForResource = 30;
+ sessionConfiguration.HTTPMaximumConnectionsPerHost = 8;
+ sessionConfiguration.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
+ sessionConfiguration.URLCache = nil;
- // 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 session ?: self.testSession;
+ 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