This repository has been archived by the owner on Jun 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 122
Adds NSURLSession delegate to MGLNetworkConfiguration #228
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
dc180c7
Experimental: Adds "private" NSURLSession to MGLNetworkConfiguration
julianrex 20b808b
Make MGLNetworkConfiguration the delegate for MGLNativeAppleInterface…
julianrex 9c4e899
Remove MGLNetworkIntegrationManager
julianrex 4c3e375
Adds a session delegate
julianrex e088d15
Add tests for session delegation
julianrex 1c30fdd
Refactor integration test - needed so that tests run triggering all.
julianrex b969bc2
Remove packs at end of test, so we don't affect other tests :(
julianrex 2e968d8
Build for mac
julianrex f4f8b82
Remove commented out property
julianrex 7673ea9
Clean up setNativeNetworkManagerDelegate, comments, tests.
julianrex 6cc74fa
Rename set.. as reset
julianrex 0dabdbc
Missing file.
julianrex 05c5ec1
Update for macos.
julianrex da6a161
Add test for additional headers.
julianrex e2b24c0
Apply suggestions from code review
9d2efab
Make delegate method optional (and add type specification to delegate…
julianrex File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,27 @@ | ||
#import "MGLNetworkConfiguration_Private.h" | ||
#import "MGLNetworkIntegrationManager.h" | ||
#import "MGLLoggingConfiguration_Private.h" | ||
#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR | ||
#import "MGLAccountManager_Private.h" | ||
#endif | ||
|
||
#import "MGLReachability.h" | ||
|
||
static NSString * const MGLStartTime = @"start_time"; | ||
static NSString * const MGLResourceType = @"resource_type"; | ||
NSString * const kMGLDownloadPerformanceEvent = @"mobile.performance_trace"; | ||
|
||
@interface MGLNetworkConfiguration () | ||
@interface MGLNetworkConfiguration () <MGLNativeNetworkDelegate> | ||
|
||
@property (strong) NSURLSessionConfiguration *sessionConfig; | ||
@property (nonatomic, strong) NSMutableDictionary<NSString *, NSDictionary*> *events; | ||
@property (nonatomic, weak) id<MGLNetworkConfigurationMetricsDelegate> metricsDelegate; | ||
@property (nonatomic) dispatch_queue_t eventsQueue; | ||
|
||
@end | ||
|
||
@implementation MGLNetworkConfiguration | ||
{ | ||
NSURLSessionConfiguration *_sessionConfig; | ||
} | ||
|
||
- (instancetype)init { | ||
if (self = [super init]) { | ||
|
@@ -34,26 +40,51 @@ + (instancetype)sharedManager { | |
_sharedManager = [[self alloc] init]; | ||
}); | ||
|
||
[self setNativeNetworkManagerDelegateToDefault]; | ||
// Notice, this is reset for each call. This is primarily for testing purposes. | ||
// TODO: Consider only calling this for testing? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this to-do still relevant? (×2) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll create a new ticket to track this. |
||
[_sharedManager resetNativeNetworkManagerDelegate]; | ||
|
||
return _sharedManager; | ||
} | ||
|
||
+ (void)setNativeNetworkManagerDelegateToDefault { | ||
- (void)resetNativeNetworkManagerDelegate { | ||
// Tell core about our network integration. `delegate` here is not (yet) | ||
// intended to be set to nil, except for testing. | ||
[MGLNativeNetworkManager sharedManager].delegate = | ||
MGLNetworkIntegrationManager.sharedManager; | ||
[MGLNativeNetworkManager sharedManager].delegate = self; | ||
} | ||
|
||
- (void)setSessionConfiguration:(NSURLSessionConfiguration *)sessionConfiguration { | ||
@synchronized (self) { | ||
if (sessionConfiguration == nil) { | ||
_sessionConfig = [self defaultSessionConfiguration]; | ||
} else { | ||
_sessionConfig = sessionConfiguration; | ||
} | ||
+ (NSURLSessionConfiguration *)defaultSessionConfiguration { | ||
NSURLSessionConfiguration* sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration]; | ||
|
||
sessionConfiguration.timeoutIntervalForResource = 30; | ||
sessionConfiguration.HTTPMaximumConnectionsPerHost = 8; | ||
sessionConfiguration.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData; | ||
sessionConfiguration.URLCache = nil; | ||
|
||
return sessionConfiguration; | ||
} | ||
|
||
#pragma mark - MGLNativeNetworkDelegate | ||
|
||
- (NSURLSession *)sessionForNetworkManager:(MGLNativeNetworkManager *)networkManager { | ||
// Note: this method is NOT called on the main thread. | ||
NSURLSession *session; | ||
if ([self.delegate respondsToSelector:@selector(sessionForNetworkConfiguration:)]) { | ||
session = [self.delegate sessionForNetworkConfiguration:self]; | ||
} | ||
|
||
// Check for a background session; string checking is fragile, but this is not | ||
// a deal breaker as we're only doing this to provide more clarity to the | ||
// developer | ||
NSAssert(![session isKindOfClass:NSClassFromString(@"__NSURLBackgroundSession")], | ||
@"Background NSURLSessions are not yet supported"); | ||
|
||
if (session.delegate) { | ||
NSAssert(![session.delegate conformsToProtocol:@protocol(NSURLSessionDataDelegate)], | ||
@"Session delegates conforming to NSURLSessionDataDelegate are not yet supported"); | ||
} | ||
|
||
return session; | ||
} | ||
|
||
- (NSURLSessionConfiguration *)sessionConfiguration { | ||
|
@@ -64,21 +95,27 @@ - (NSURLSessionConfiguration *)sessionConfiguration { | |
return sessionConfig; | ||
} | ||
|
||
- (NSURLSessionConfiguration *)defaultSessionConfiguration { | ||
NSURLSessionConfiguration* sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration]; | ||
|
||
sessionConfiguration.timeoutIntervalForResource = 30; | ||
sessionConfiguration.HTTPMaximumConnectionsPerHost = 8; | ||
sessionConfiguration.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData; | ||
sessionConfiguration.URLCache = nil; | ||
|
||
return sessionConfiguration; | ||
- (void)setSessionConfiguration:(NSURLSessionConfiguration *)sessionConfiguration { | ||
@synchronized (self) { | ||
if (sessionConfiguration == nil) { | ||
_sessionConfig = [MGLNetworkConfiguration defaultSessionConfiguration]; | ||
} else { | ||
_sessionConfig = sessionConfiguration; | ||
} | ||
} | ||
} | ||
|
||
#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR | ||
- (NSString *)skuToken { | ||
return MGLAccountManager.skuToken; | ||
} | ||
#endif | ||
|
||
- (void)startDownloadEvent:(NSString *)urlString type:(NSString *)resourceType { | ||
if (urlString && ![self eventDictionaryForKey:urlString]) { | ||
if (urlString && resourceType && ![self eventDictionaryForKey:urlString]) { | ||
NSDate *startDate = [NSDate date]; | ||
[self setEventDictionary:@{ MGLStartTime: startDate, MGLResourceType: resourceType } forKey:urlString]; | ||
[self setEventDictionary:@{ MGLStartTime: startDate, MGLResourceType: resourceType } | ||
forKey:urlString]; | ||
} | ||
} | ||
|
||
|
@@ -90,6 +127,16 @@ - (void)cancelDownloadEventForResponse:(NSURLResponse *)response { | |
[self sendEventForURLResponse:response withAction:@"cancel"]; | ||
} | ||
|
||
- (void)debugLog:(NSString *)format, ... { | ||
MGLLogDebug(format); | ||
} | ||
|
||
- (void)errorLog:(NSString *)format, ... { | ||
MGLLogError(format); | ||
} | ||
|
||
#pragma mark - Event management | ||
|
||
- (void)sendEventForURLResponse:(NSURLResponse *)response withAction:(NSString *)action | ||
{ | ||
if ([response isKindOfClass:[NSURLResponse class]]) { | ||
|
@@ -103,7 +150,6 @@ - (void)sendEventForURLResponse:(NSURLResponse *)response withAction:(NSString * | |
}); | ||
} | ||
} | ||
|
||
} | ||
|
||
- (NSDictionary *)eventAttributesForURL:(NSURLResponse *)response withAction:(NSString *)action | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#import <XCTest/XCTest.h> | ||
#import <Mapbox/Mapbox.h> | ||
#import "MGLTestUtility.h" | ||
|
||
#define MGLTestFail(myself, ...) \ | ||
_XCTPrimitiveFail(myself, __VA_ARGS__) | ||
|
||
#define MGLTestAssert(myself, expression, ...) \ | ||
_XCTPrimitiveAssertTrue(myself, expression, @#expression, __VA_ARGS__) | ||
|
||
#define MGLTestAssertEqualWithAccuracy(myself, expression1, expression2, accuracy, ...) \ | ||
_XCTPrimitiveAssertEqualWithAccuracy(myself, expression1, @#expression1, expression2, @#expression2, accuracy, @#accuracy, __VA_ARGS__) | ||
#define MGLTestAssertNil(myself, expression, ...) \ | ||
_XCTPrimitiveAssertNil(myself, expression, @#expression, __VA_ARGS__) | ||
|
||
#define MGLTestAssertNotNil(myself, expression, ...) \ | ||
_XCTPrimitiveAssertNotNil(myself, expression, @#expression, __VA_ARGS__) | ||
|
||
#define MGLTestWarning(expression, format, ...) \ | ||
({ \ | ||
if (!(expression)) { \ | ||
NSString *message = [NSString stringWithFormat:format, ##__VA_ARGS__]; \ | ||
printf("warning: Test Case '%s' at line %d: '%s' %s\n", __PRETTY_FUNCTION__, __LINE__, #expression, message.UTF8String); \ | ||
} \ | ||
}) | ||
|
||
@interface MGLIntegrationTestCase: XCTestCase | ||
@end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What’s an example of a place where this object can be assigned safely? Consider that fixing #227 may require us to invoke
+[MGLOfflineStorage sharedStorage]
in an+initialize
method.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ugh. Let's address that when we fix #227. Hopefully we can find a way around without having to use an
+initialize
.