summaryrefslogtreecommitdiff
path: root/platform/darwin
diff options
context:
space:
mode:
Diffstat (limited to 'platform/darwin')
-rw-r--r--platform/darwin/src/MGLMapboxEventsDelegate.h12
-rw-r--r--platform/darwin/src/MGLNetworkConfiguration.h4
-rw-r--r--platform/darwin/src/MGLNetworkConfiguration.m57
-rw-r--r--platform/darwin/src/MGLNetworkConfiguration_Private.h9
4 files changed, 53 insertions, 29 deletions
diff --git a/platform/darwin/src/MGLMapboxEventsDelegate.h b/platform/darwin/src/MGLMapboxEventsDelegate.h
new file mode 100644
index 0000000000..3f76522552
--- /dev/null
+++ b/platform/darwin/src/MGLMapboxEventsDelegate.h
@@ -0,0 +1,12 @@
+#import <Foundation/Foundation.h>
+
+NS_ASSUME_NONNULL_BEGIN
+
+@protocol MGLMapboxEventsDelegate <NSObject>
+
+- (BOOL)shouldReceiveEvents;
+- (void)didReceiveEvent:(NSString *)eventName withAttributes:(NSDictionary *)attributes;
+
+@end
+
+NS_ASSUME_NONNULL_END
diff --git a/platform/darwin/src/MGLNetworkConfiguration.h b/platform/darwin/src/MGLNetworkConfiguration.h
index 6c56050aae..e14298f203 100644
--- a/platform/darwin/src/MGLNetworkConfiguration.h
+++ b/platform/darwin/src/MGLNetworkConfiguration.h
@@ -2,6 +2,8 @@
#import "MGLFoundation.h"
+@protocol MGLMapboxEventsDelegate;
+
NS_ASSUME_NONNULL_BEGIN
/**
@@ -32,6 +34,8 @@ MGL_EXPORT
*/
@property (atomic, strong, null_resettable) NSURLSessionConfiguration *sessionConfiguration;
+@property (nonatomic, weak, nullable) id<MGLMapboxEventsDelegate> eventsDelegate;
+
@end
NS_ASSUME_NONNULL_END
diff --git a/platform/darwin/src/MGLNetworkConfiguration.m b/platform/darwin/src/MGLNetworkConfiguration.m
index 6f5959e70f..00786786d0 100644
--- a/platform/darwin/src/MGLNetworkConfiguration.m
+++ b/platform/darwin/src/MGLNetworkConfiguration.m
@@ -1,4 +1,5 @@
#import "MGLNetworkConfiguration_Private.h"
+#import "MGLMapboxEventsDelegate.h"
NSString * const kMGLDownloadPerformanceEvent = @"mobile.performance_trace";
@@ -9,7 +10,6 @@ static NSString * const MGLResourceType = @"resource_type";
@property (strong) NSURLSessionConfiguration *sessionConfig;
@property (nonatomic, strong) NSMutableDictionary<NSString *, NSDictionary*> *events;
-@property (nonatomic, weak, nullable) id<MGLNetworkEventDelegate> delegate;
@end
@@ -63,34 +63,49 @@ static NSString * const MGLResourceType = @"resource_type";
}
- (void)stopDownloadEvent:(NSString *)urlString {
+ [self sendEventForURL:urlString withAction:nil];
+}
+
+- (void)cancelDownloadEvent:(NSString *)urlString {
+ [self sendEventForURL:urlString withAction:@"cancel"];
+}
+
+- (void)sendEventForURL:(NSString *)urlString withAction:(NSString *)action
+{
if (urlString && [self.events objectForKey:urlString]) {
- if ([self.delegate respondsToSelector:@selector(networkConfiguration:didReceiveNetworkEvent:)])
+ if ([self.eventsDelegate shouldReceiveEvents])
{
- NSDictionary *parameters = [self.events objectForKey:urlString];
- NSDate *startDate = [parameters objectForKey:MGLStartTime];
- NSDate *endDate = [NSDate date];
- NSTimeInterval elapsedTime = [endDate timeIntervalSinceDate:startDate];
- NSDateFormatter* iso8601Formatter = [[NSDateFormatter alloc] init];
- iso8601Formatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ssZ";
-
- NSDictionary *eventAttributes = @{
- @"event" : kMGLDownloadPerformanceEvent,
- @"created" : [iso8601Formatter stringFromDate:[NSDate date]],
- @"sessionId" : [NSUUID UUID].UUIDString,
- @"counters" : @[ @{ @"name" : @"elapsed_time" }, @{ @"value" : @(elapsedTime) } ],
- @"attributes" : @[ @{ @"name" : @"resource" }, @{ @"value" : urlString }, ]
- };
-
- [self.delegate networkConfiguration:self didReceiveNetworkEvent:eventAttributes];
+ NSDictionary *eventAttributes = [self eventAttributesForURL:urlString withAction:action];
+ [self.eventsDelegate didReceiveEvent:kMGLDownloadPerformanceEvent withAttributes:eventAttributes];
}
[self.events removeObjectForKey:urlString];
}
}
-- (void)cancelDownloadEvent:(NSString *)urlString {
- if (urlString && [self.events objectForKey:urlString]) {
- [self.events removeObjectForKey:urlString];
+- (NSDictionary *)eventAttributesForURL:(NSString *)urlString withAction:(NSString *)action
+{
+ NSDictionary *parameters = [self.events objectForKey:urlString];
+ NSDate *startDate = [parameters objectForKey:MGLStartTime];
+ NSDate *endDate = [NSDate date];
+ NSTimeInterval elapsedTime = [endDate timeIntervalSinceDate:startDate];
+ NSDateFormatter* iso8601Formatter = [[NSDateFormatter alloc] init];
+ iso8601Formatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ssZ";
+ NSString *createdDate = [iso8601Formatter stringFromDate:[NSDate date]];
+
+ NSMutableArray *attributes = [NSMutableArray array];
+ [attributes addObject:@{ @"name" : @"resource" , @"value" : urlString }];
+ [attributes addObject:@{ @"name" : MGLResourceType , @"value" : [parameters objectForKey:MGLResourceType] }];
+ if (action) {
+ [attributes addObject:@{ @"name" : @"action" , @"value" : action }];
}
+
+ return @{
+ @"event" : kMGLDownloadPerformanceEvent,
+ @"created" : createdDate,
+ @"sessionId" : [NSUUID UUID].UUIDString,
+ @"counters" : @[ @{ @"name" : @"elapsed_time" , @"value" : @(elapsedTime) } ],
+ @"attributes" : attributes
+ };
}
@end
diff --git a/platform/darwin/src/MGLNetworkConfiguration_Private.h b/platform/darwin/src/MGLNetworkConfiguration_Private.h
index 8d55aea220..2481ee1fea 100644
--- a/platform/darwin/src/MGLNetworkConfiguration_Private.h
+++ b/platform/darwin/src/MGLNetworkConfiguration_Private.h
@@ -1,20 +1,13 @@
#import "MGLNetworkConfiguration.h"
+@protocol MGLMapboxEventsDelegate;
NS_ASSUME_NONNULL_BEGIN
extern NSString * const kMGLDownloadPerformanceEvent;
-@protocol MGLNetworkEventDelegate <NSObject>
-
-@optional
-- (void)networkConfiguration:(MGLNetworkConfiguration *)networkConfiguration didReceiveNetworkEvent:(NSDictionary *)event;
-
-@end
-
@interface MGLNetworkConfiguration (Private)
@property (nonatomic, strong) NSMutableDictionary<NSString*, NSDictionary*> *events;
-@property (nonatomic, weak, nullable) id<MGLNetworkEventDelegate> delegate;
- (void)startDownloadEvent:(NSString *)urlString type:(NSString *)resourceType;
- (void)stopDownloadEvent:(NSString *)urlString;