diff options
author | m-stephen <truestyle2005@163.com> | 2019-12-14 13:38:46 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-12-14 13:38:46 +0800 |
commit | f9b72842761b8f66fc8e5429e936b840cf807dd7 (patch) | |
tree | 21bac914d27529d29306c511dcb3a41dec5c3897 /platform/darwin | |
parent | 5538b6f41d9e5a8f7ab1b5b1c16d58fca2e8e181 (diff) | |
download | qtlocation-mapboxgl-f9b72842761b8f66fc8e5429e936b840cf807dd7.tar.gz |
[ios, macos]Remove iOS/macOS codes from native codes (#16031)
* add source/header
* add ios files
* add configs
* modify name
* http_file_source
* add interface delegate when map init
* fix name
* fix delegate name
* support mac os
* add mac os support
* make optional delegate when mac os
* mac/ios difference
* add ios change log
* cancel iOS/mac OS judgement
* cancel iOS/mac OS judgement
* cancel judgement in .m
* update
* update
* update http_file_source
* update ios
* update mac os
* add mac os file
* add mac os file to `.cmake`
* change names
* add log & fix format
* reset changelog commit
* update changelog
* rename iOS network manager
* Add a test configuration(same as default configuration) when mac os run tests
* re-add account type into `http_file_source`
* refactor
Diffstat (limited to 'platform/darwin')
-rw-r--r-- | platform/darwin/include/mbgl/interface/native_apple_interface.h | 51 | ||||
-rw-r--r-- | platform/darwin/src/MGLNetworkIntegrationManager.h | 8 | ||||
-rw-r--r-- | platform/darwin/src/MGLNetworkIntegrationManager.m | 54 | ||||
-rw-r--r-- | platform/darwin/src/http_file_source.mm | 31 | ||||
-rw-r--r-- | platform/darwin/src/native_apple_interface.m | 73 |
5 files changed, 197 insertions, 20 deletions
diff --git a/platform/darwin/include/mbgl/interface/native_apple_interface.h b/platform/darwin/include/mbgl/interface/native_apple_interface.h new file mode 100644 index 0000000000..d4d207462d --- /dev/null +++ b/platform/darwin/include/mbgl/interface/native_apple_interface.h @@ -0,0 +1,51 @@ +#import <Foundation/Foundation.h> + +NS_ASSUME_NONNULL_BEGIN + +@protocol MGLNativeNetworkDelegate <NSObject> + +@optional + +- (NSString *)skuToken; + +@required + +- (NSURLSessionConfiguration *)sessionConfiguration; + +- (void)startDownloadEvent:(NSString *)event type:(NSString *)type; + +- (void)cancelDownloadEventForResponse:(NSURLResponse *)response; + +- (void)stopDownloadEventForResponse:(NSURLResponse *)response; + +- (void)debugLog:(NSString *)format, ...; + +- (void)errorLog:(NSString *)format, ...; + +@end + +#define MGL_APPLE_EXPORT __attribute__((visibility ("default"))) + +@interface MGLNativeNetworkManager: NSObject + ++ (MGLNativeNetworkManager *)sharedManager; + +@property (nonatomic, weak) id<MGLNativeNetworkDelegate> delegate; + +@property (nonatomic, readonly) NSString *skuToken; + +@property (nonatomic, readonly) NSURLSessionConfiguration *sessionConfiguration; + +- (void)startDownloadEvent:(NSString *)event type:(NSString *)type; + +- (void)cancelDownloadEventForResponse:(NSURLResponse *)response; + +- (void)stopDownloadEventForResponse:(NSURLResponse *)response; + +- (void)debugLog:(NSString *)format, ...; + +- (void)errorLog:(NSString *)format, ...; + +@end + +NS_ASSUME_NONNULL_END diff --git a/platform/darwin/src/MGLNetworkIntegrationManager.h b/platform/darwin/src/MGLNetworkIntegrationManager.h new file mode 100644 index 0000000000..2c929e16f8 --- /dev/null +++ b/platform/darwin/src/MGLNetworkIntegrationManager.h @@ -0,0 +1,8 @@ +#import <Foundation/Foundation.h> +#include <mbgl/interface/native_apple_interface.h> + +@interface MGLNetworkIntegrationManager : NSObject <MGLNativeNetworkDelegate> + ++ (MGLNetworkIntegrationManager *)sharedManager; + +@end diff --git a/platform/darwin/src/MGLNetworkIntegrationManager.m b/platform/darwin/src/MGLNetworkIntegrationManager.m new file mode 100644 index 0000000000..79c7f15156 --- /dev/null +++ b/platform/darwin/src/MGLNetworkIntegrationManager.m @@ -0,0 +1,54 @@ +#import "MGLNetworkIntegrationManager.h" + +#import "MGLLoggingConfiguration_Private.h" +#import "MGLNetworkConfiguration_Private.h" + +#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR +#import "MGLAccountManager_Private.h" +#endif + +@implementation MGLNetworkIntegrationManager + +static MGLNetworkIntegrationManager *instance = nil; + ++ (MGLNetworkIntegrationManager *)sharedManager { + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + instance = [[MGLNetworkIntegrationManager alloc] init]; + }); + return instance; +} + +#pragma mark - MGLNativeAppleInterfaceManager delegate - + +- (NSURLSessionConfiguration *)sessionConfiguration { + return [MGLNetworkConfiguration sharedManager].sessionConfiguration; +} + +#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR +- (NSString *)skuToken { + return MGLAccountManager.skuToken; +} +#endif + +- (void)startDownloadEvent:(NSString *)event type:(NSString *)type { + [[MGLNetworkConfiguration sharedManager] startDownloadEvent:event type:@"tile"]; +} + +- (void)cancelDownloadEventForResponse:(NSURLResponse *)response { + [[MGLNetworkConfiguration sharedManager] cancelDownloadEventForResponse:response]; +} + +- (void)stopDownloadEventForResponse:(NSURLResponse *)response { + [[MGLNetworkConfiguration sharedManager] stopDownloadEventForResponse:response]; +} + +- (void)debugLog:(NSString *)format, ... { + MGLLogDebug(format); +} + +- (void)errorLog:(NSString *)format, ... { + MGLLogError(format); +} + +@end diff --git a/platform/darwin/src/http_file_source.mm b/platform/darwin/src/http_file_source.mm index dbf92aa462..6f68111203 100644 --- a/platform/darwin/src/http_file_source.mm +++ b/platform/darwin/src/http_file_source.mm @@ -8,12 +8,7 @@ #import <Foundation/Foundation.h> -#import "MGLLoggingConfiguration_Private.h" -#import "MGLNetworkConfiguration_Private.h" - -#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR -#import "MGLAccountManager_Private.h" -#endif +#include <mbgl/interface/native_apple_interface.h> #include <mutex> #include <chrono> @@ -88,14 +83,10 @@ class HTTPFileSource::Impl { public: Impl() { @autoreleasepool { - NSURLSessionConfiguration *sessionConfig = [MGLNetworkConfiguration sharedManager].sessionConfiguration; + NSURLSessionConfiguration *sessionConfig = MGLNativeNetworkManager.sharedManager.sessionConfiguration; session = [NSURLSession sessionWithConfiguration:sessionConfig]; userAgent = getUserAgent(); - -#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR - accountType = [[NSUserDefaults standardUserDefaults] integerForKey:MGLMapboxAccountTypeKey]; -#endif } } @@ -195,7 +186,7 @@ HTTPFileSource::HTTPFileSource() HTTPFileSource::~HTTPFileSource() = default; -MGL_EXPORT +MGL_APPLE_EXPORT BOOL isValidMapboxEndpoint(NSURL *url) { return ([url.host isEqualToString:@"mapbox.com"] || [url.host hasSuffix:@".mapbox.com"] || @@ -203,7 +194,7 @@ BOOL isValidMapboxEndpoint(NSURL *url) { [url.host hasSuffix:@".mapbox.cn"]); } -MGL_EXPORT +MGL_APPLE_EXPORT NSURL *resourceURLWithAccountType(const Resource& resource, NSInteger accountType) { NSURL *url = [NSURL URLWithString:@(resource.url.c_str())]; @@ -217,7 +208,7 @@ NSURL *resourceURLWithAccountType(const Resource& resource, NSInteger accountTyp [queryItems addObject:[NSURLQueryItem queryItemWithName:@"offline" value:@"true"]]; } else { // Only add SKU token to requests not tagged as "offline" usage. - [queryItems addObject:[NSURLQueryItem queryItemWithName:@"sku" value:MGLAccountManager.skuToken]]; + [queryItems addObject:[NSURLQueryItem queryItemWithName:@"sku" value:MGLNativeNetworkManager.sharedManager.skuToken]]; } if (components.queryItems) { @@ -239,7 +230,7 @@ std::unique_ptr<AsyncRequest> HTTPFileSource::request(const Resource& resource, @autoreleasepool { NSURL *url = resourceURLWithAccountType(resource, impl->accountType); - MGLLogDebug(@"Requesting URI: %@", url.relativePath); + [MGLNativeNetworkManager.sharedManager debugLog:@"Requesting URI: %@", url.relativePath]; NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url]; if (resource.priorEtag) { @@ -255,22 +246,22 @@ std::unique_ptr<AsyncRequest> HTTPFileSource::request(const Resource& resource, const bool isTile = resource.kind == mbgl::Resource::Kind::Tile; if (isTile) { - [[MGLNetworkConfiguration sharedManager] startDownloadEvent:url.relativePath type:@"tile"]; + [MGLNativeNetworkManager.sharedManager startDownloadEvent:url.relativePath type:@"tile"]; } request->task = [impl->session dataTaskWithRequest:req completionHandler:^(NSData* data, NSURLResponse* res, NSError* error) { if (error && [error code] == NSURLErrorCancelled) { - [[MGLNetworkConfiguration sharedManager] cancelDownloadEventForResponse:res]; + [MGLNativeNetworkManager.sharedManager cancelDownloadEventForResponse:res]; return; } - [[MGLNetworkConfiguration sharedManager] stopDownloadEventForResponse:res]; + [MGLNativeNetworkManager.sharedManager stopDownloadEventForResponse:res]; Response response; using Error = Response::Error; if (error) { - MGLLogError(@"Requesting: %@ failed with error: %@", res.URL.relativePath, error); + [MGLNativeNetworkManager.sharedManager errorLog:@"Requesting: %@ failed with error: %@", res.URL.relativePath, error]; if (data) { response.data = @@ -303,7 +294,7 @@ std::unique_ptr<AsyncRequest> HTTPFileSource::request(const Resource& resource, } } else if ([res isKindOfClass:[NSHTTPURLResponse class]]) { const long responseCode = [(NSHTTPURLResponse *)res statusCode]; - MGLLogDebug(@"Requesting %@ returned responseCode: %lu", res.URL.relativePath, responseCode); + [MGLNativeNetworkManager.sharedManager debugLog:@"Requesting %@ returned responseCode: %lu", res.URL.relativePath, responseCode]; NSDictionary *headers = [(NSHTTPURLResponse *)res allHeaderFields]; NSString *cache_control = [headers objectForKey:@"Cache-Control"]; diff --git a/platform/darwin/src/native_apple_interface.m b/platform/darwin/src/native_apple_interface.m new file mode 100644 index 0000000000..07dce0d5b0 --- /dev/null +++ b/platform/darwin/src/native_apple_interface.m @@ -0,0 +1,73 @@ +#import <Foundation/Foundation.h> +#import <mbgl/interface/native_apple_interface.h> + +@implementation MGLNativeNetworkManager + +static MGLNativeNetworkManager *instance = nil; + ++ (MGLNativeNetworkManager *)sharedManager { + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + instance = [[MGLNativeNetworkManager alloc] init]; + }); + 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* sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration]; + + sessionConfiguration.timeoutIntervalForResource = 30; + sessionConfiguration.HTTPMaximumConnectionsPerHost = 8; + sessionConfiguration.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData; + sessionConfiguration.URLCache = nil; + + return sessionConfiguration; +} + +- (NSString *)skuToken { + if(_delegate && [_delegate respondsToSelector:@selector(skuToken)]) { + return [_delegate skuToken]; + } + return nil; +} + +- (void)startDownloadEvent:(NSString *)event type:(NSString *)type { + if (_delegate && [_delegate respondsToSelector:@selector(startDownloadEvent:type:)]) { + [_delegate startDownloadEvent:event type:type]; + } +} + +- (void)cancelDownloadEventForResponse:(NSURLResponse *)response { + if (_delegate && [_delegate respondsToSelector:@selector(cancelDownloadEventForResponse:)]) { + return [_delegate cancelDownloadEventForResponse:response]; + } +} + +- (void)stopDownloadEventForResponse:(NSURLResponse *)response { + if (_delegate && [_delegate respondsToSelector:@selector(stopDownloadEventForResponse:)]) { + return [_delegate stopDownloadEventForResponse:response]; + } +} + +- (void)debugLog:(NSString *)format, ...{ + if (_delegate && [_delegate respondsToSelector:@selector(debugLog:)]) { + return [_delegate debugLog:format]; + } +} + +- (void)errorLog:(NSString *)format, ...{ + if (_delegate && [_delegate respondsToSelector:@selector(errorLog:)]) { + return [_delegate errorLog:format]; + } +} + +@end |