Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[ios, macos]Remove iOS/macOS codes from native codes (#16031)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
m-stephen authored Dec 14, 2019
1 parent 5538b6f commit f9b7284
Show file tree
Hide file tree
Showing 14 changed files with 250 additions and 23 deletions.
2 changes: 2 additions & 0 deletions next/platform/macos/macos.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ target_sources(
${MBGL_ROOT}/platform/darwin/src/run_loop.cpp
${MBGL_ROOT}/platform/darwin/src/string_nsstring.mm
${MBGL_ROOT}/platform/darwin/src/timer.cpp
${MBGL_ROOT}/platform/darwin/src/native_apple_interface.m
${MBGL_ROOT}/platform/darwin/src/MGLNetworkIntegrationManager.m
${MBGL_ROOT}/platform/default/src/mbgl/gfx/headless_backend.cpp
${MBGL_ROOT}/platform/default/src/mbgl/gfx/headless_frontend.cpp
${MBGL_ROOT}/platform/default/src/mbgl/gl/headless_backend.cpp
Expand Down
51 changes: 51 additions & 0 deletions platform/darwin/include/mbgl/interface/native_apple_interface.h
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions platform/darwin/src/MGLNetworkIntegrationManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#import <Foundation/Foundation.h>
#include <mbgl/interface/native_apple_interface.h>

@interface MGLNetworkIntegrationManager : NSObject <MGLNativeNetworkDelegate>

+ (MGLNetworkIntegrationManager *)sharedManager;

@end
54 changes: 54 additions & 0 deletions platform/darwin/src/MGLNetworkIntegrationManager.m
Original file line number Diff line number Diff line change
@@ -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
31 changes: 11 additions & 20 deletions platform/darwin/src/http_file_source.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -88,14 +83,10 @@ void cancel() {
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
}
}

Expand Down Expand Up @@ -195,15 +186,15 @@ void cancel() {

HTTPFileSource::~HTTPFileSource() = default;

MGL_EXPORT
MGL_APPLE_EXPORT
BOOL isValidMapboxEndpoint(NSURL *url) {
return ([url.host isEqualToString:@"mapbox.com"] ||
[url.host hasSuffix:@".mapbox.com"] ||
[url.host isEqualToString:@"mapbox.cn"] ||
[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())];
Expand All @@ -217,7 +208,7 @@ BOOL isValidMapboxEndpoint(NSURL *url) {
[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) {
Expand All @@ -239,7 +230,7 @@ BOOL isValidMapboxEndpoint(NSURL *url) {

@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) {
Expand All @@ -255,22 +246,22 @@ BOOL isValidMapboxEndpoint(NSURL *url) {
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 =
Expand Down Expand Up @@ -303,7 +294,7 @@ BOOL isValidMapboxEndpoint(NSURL *url) {
}
} 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"];
Expand Down
73 changes: 73 additions & 0 deletions platform/darwin/src/native_apple_interface.m
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion platform/ios/core-files.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
"platform/default/src/mbgl/util/monotonic_timer.cpp",
"platform/default/src/mbgl/util/png_writer.cpp",
"platform/default/src/mbgl/util/thread_local.cpp",
"platform/default/src/mbgl/util/utf.cpp"
"platform/default/src/mbgl/util/utf.cpp",
"platform/darwin/src/native_apple_interface.m"
],
"public_headers": {
"mbgl/storage/reachability.h": "platform/darwin/include/mbgl/storage/reachability.h",
"mbgl/interface/native_apple_interface.h": "platform/darwin/include/mbgl/interface/native_apple_interface.h",
"mbgl/util/image+MGLAdditions.hpp": "platform/darwin/include/mbgl/util/image+MGLAdditions.hpp",
"mbgl/gfx/headless_backend.hpp": "platform/default/include/mbgl/gfx/headless_backend.hpp",
"mbgl/gfx/headless_frontend.hpp": "platform/default/include/mbgl/gfx/headless_frontend.hpp",
Expand Down
20 changes: 20 additions & 0 deletions platform/ios/ios.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,10 @@
CAFB3C15234505D500399265 /* MGLMapSnapshotter_Private.h in Headers */ = {isa = PBXBuildFile; fileRef = CAFB3C13234505D500399265 /* MGLMapSnapshotter_Private.h */; };
CF75A91522D85E860058A5C4 /* MGLLoggingConfiguration.mm in Sources */ = {isa = PBXBuildFile; fileRef = CF75A91422D85E860058A5C4 /* MGLLoggingConfiguration.mm */; };
CF75A91622D85E860058A5C4 /* MGLLoggingConfiguration.mm in Sources */ = {isa = PBXBuildFile; fileRef = CF75A91422D85E860058A5C4 /* MGLLoggingConfiguration.mm */; };
CFF9F98623A24BF500B0DE92 /* MGLNetworkIntegrationManager.h in Headers */ = {isa = PBXBuildFile; fileRef = CFF9F98423A24BF400B0DE92 /* MGLNetworkIntegrationManager.h */; };
CFF9F98723A24BF500B0DE92 /* MGLNetworkIntegrationManager.h in Headers */ = {isa = PBXBuildFile; fileRef = CFF9F98423A24BF400B0DE92 /* MGLNetworkIntegrationManager.h */; };
CFF9F98823A24BF500B0DE92 /* MGLNetworkIntegrationManager.m in Sources */ = {isa = PBXBuildFile; fileRef = CFF9F98523A24BF400B0DE92 /* MGLNetworkIntegrationManager.m */; };
CFF9F98923A24BF500B0DE92 /* MGLNetworkIntegrationManager.m in Sources */ = {isa = PBXBuildFile; fileRef = CFF9F98523A24BF400B0DE92 /* MGLNetworkIntegrationManager.m */; };
DA00FC8E1D5EEB0D009AABC8 /* MGLAttributionInfo.h in Headers */ = {isa = PBXBuildFile; fileRef = DA00FC8C1D5EEB0D009AABC8 /* MGLAttributionInfo.h */; settings = {ATTRIBUTES = (Public, ); }; };
DA00FC8F1D5EEB0D009AABC8 /* MGLAttributionInfo.h in Headers */ = {isa = PBXBuildFile; fileRef = DA00FC8C1D5EEB0D009AABC8 /* MGLAttributionInfo.h */; settings = {ATTRIBUTES = (Public, ); }; };
DA00FC901D5EEB0D009AABC8 /* MGLAttributionInfo.mm in Sources */ = {isa = PBXBuildFile; fileRef = DA00FC8D1D5EEB0D009AABC8 /* MGLAttributionInfo.mm */; };
Expand Down Expand Up @@ -1226,6 +1230,8 @@
CAE7AD5420F46EF5003B6782 /* MGLMapSnapshotterSwiftTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MGLMapSnapshotterSwiftTests.swift; sourceTree = "<group>"; };
CAFB3C13234505D500399265 /* MGLMapSnapshotter_Private.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MGLMapSnapshotter_Private.h; sourceTree = "<group>"; };
CF75A91422D85E860058A5C4 /* MGLLoggingConfiguration.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MGLLoggingConfiguration.mm; sourceTree = "<group>"; };
CFF9F98423A24BF400B0DE92 /* MGLNetworkIntegrationManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = MGLNetworkIntegrationManager.h; path = ../../darwin/src/MGLNetworkIntegrationManager.h; sourceTree = "<group>"; };
CFF9F98523A24BF400B0DE92 /* MGLNetworkIntegrationManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = MGLNetworkIntegrationManager.m; path = ../../darwin/src/MGLNetworkIntegrationManager.m; sourceTree = "<group>"; };
DA00FC8C1D5EEB0D009AABC8 /* MGLAttributionInfo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MGLAttributionInfo.h; sourceTree = "<group>"; };
DA00FC8D1D5EEB0D009AABC8 /* MGLAttributionInfo.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MGLAttributionInfo.mm; sourceTree = "<group>"; };
DA0CD58F1CF56F6A00A5F5A5 /* MGLFeatureTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = MGLFeatureTests.mm; path = ../../darwin/test/MGLFeatureTests.mm; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1961,6 +1967,15 @@
name = Annotations;
sourceTree = "<group>";
};
CF85C39E23A249FC00BEBFFF /* Interface */ = {
isa = PBXGroup;
children = (
CFF9F98423A24BF400B0DE92 /* MGLNetworkIntegrationManager.h */,
CFF9F98523A24BF400B0DE92 /* MGLNetworkIntegrationManager.m */,
);
name = Interface;
sourceTree = "<group>";
};
DA1DC9411CB6C1C2006E619F = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -2188,6 +2203,7 @@
DA8848331CBAFB2A00AB86E3 /* Kit */ = {
isa = PBXGroup;
children = (
CF85C39E23A249FC00BEBFFF /* Interface */,
DAD165841CF4D06B001FF4B9 /* Annotations */,
35CE617F1D4165C2004F2359 /* Categories */,
DA8848881CBB036000AB86E3 /* SMCalloutView */,
Expand Down Expand Up @@ -2632,6 +2648,7 @@
55E5665B21C2A2080008B8B5 /* MMEEventLogger.h in Headers */,
55E5665C21C2A2080008B8B5 /* MMEEventLogReportViewController.h in Headers */,
55E5665D21C2A2080008B8B5 /* MMEEventsConfiguration.h in Headers */,
CFF9F98623A24BF500B0DE92 /* MGLNetworkIntegrationManager.h in Headers */,
55E5666021C2A2080008B8B5 /* MMEConfigurator.h in Headers */,
55E5666221C2A2080008B8B5 /* MMELocationManager.h in Headers */,
55E5666321C2A2080008B8B5 /* MMEMetrics.h in Headers */,
Expand Down Expand Up @@ -2826,6 +2843,7 @@
35D13AC41D3D19DD00AFB4E0 /* MGLFillStyleLayer.h in Headers */,
9C6E284322A982670056B7BE /* MMETypes.h in Headers */,
DABFB86E1CBE9A0F00D62B32 /* MGLCalloutView.h in Headers */,
CFF9F98723A24BF500B0DE92 /* MGLNetworkIntegrationManager.h in Headers */,
96E516FC20005A4400A02306 /* MGLUserLocationHeadingIndicator.h in Headers */,
1F7454971ECD450D00021D39 /* MGLLight_Private.h in Headers */,
9C6E283C22A982670056B7BE /* MMEEventsManager.h in Headers */,
Expand Down Expand Up @@ -3406,6 +3424,7 @@
35136D451D42275100C20EFD /* MGLSymbolStyleLayer.mm in Sources */,
CF75A91522D85E860058A5C4 /* MGLLoggingConfiguration.mm in Sources */,
35599DED1D46F14E0048254D /* MGLStyleValue.mm in Sources */,
CFF9F98823A24BF500B0DE92 /* MGLNetworkIntegrationManager.m in Sources */,
DA8848211CBAFA6200AB86E3 /* MGLOfflinePack.mm in Sources */,
0778DD441F67556C00A73B34 /* MGLComputedShapeSource.mm in Sources */,
3557F7B21E1D27D300CCA5E6 /* MGLDistanceFormatter.m in Sources */,
Expand Down Expand Up @@ -3533,6 +3552,7 @@
DAA4E4261CBB730400178DFB /* MGLStyle.mm in Sources */,
CF75A91622D85E860058A5C4 /* MGLLoggingConfiguration.mm in Sources */,
DAA32CC31E4C6B65006F8D24 /* MGLDistanceFormatter.m in Sources */,
CFF9F98923A24BF500B0DE92 /* MGLNetworkIntegrationManager.m in Sources */,
DAA4E41D1CBB730400178DFB /* MGLGeometry.mm in Sources */,
40834BFB1FE05E1800C1BD0D /* MMEAPIClient.m in Sources */,
1FCCEC37222605C400302E3B /* MGLSDKMetricsManager.m in Sources */,
Expand Down
Loading

0 comments on commit f9b7284

Please sign in to comment.