diff --git a/CHANGELOG.md b/CHANGELOG.md index 9fdefc778..627233412 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ Changelog ### Enhancements +* Add internal api for mutating session payload before sending + [#341](https://github.com/bugsnag/bugsnag-cocoa/pull/341) + * Persist breadcrumbs on disk to allow reading upon next boot in the event of an uncatchable app termination. * Add `+[Bugsnag appDidCrashLastLaunch]` as a helper to determine if the diff --git a/Source/BugsnagConfiguration.h b/Source/BugsnagConfiguration.h index 5aa73fd51..090954475 100644 --- a/Source/BugsnagConfiguration.h +++ b/Source/BugsnagConfiguration.h @@ -56,6 +56,13 @@ typedef void (^BugsnagNotifyBlock)(BugsnagCrashReport *_Nonnull report); typedef bool (^BugsnagBeforeSendBlock)(NSDictionary *_Nonnull rawEventData, BugsnagCrashReport *_Nonnull reports); +/** + * A configuration block for modifying a session. Intended for internal usage only. + * + * @param sessionPayload The session about to be delivered + */ +typedef void(^BeforeSendSession)(NSMutableDictionary *_Nonnull sessionPayload); + /** * A handler for modifying data before sending it to Bugsnag * @@ -126,6 +133,13 @@ BugsnagBreadcrumbs *breadcrumbs; */ @property(readonly, strong, nullable) NSArray *beforeSendBlocks; + +/** + * Hooks for modifying sessions before they are sent to Bugsnag. Intended for internal use only by React Native/Unity. + */ +@property(readonly, strong, nullable) +NSArray *beforeSendSessionBlocks; + /** * Optional handler invoked when a crash or fatal signal occurs */ @@ -205,6 +219,13 @@ BugsnagBreadcrumbs *breadcrumbs; */ - (void)addBeforeSendBlock:(BugsnagBeforeSendBlock _Nonnull)block; +/** + * Add a callback to be invoked before a session is sent to Bugsnag. Intended for internal usage only. + * + * @param block A block which can modify the session + */ +- (void)addBeforeSendSession:(BeforeSendSession _Nonnull)block; + /** * Clear all callbacks */ diff --git a/Source/BugsnagConfiguration.m b/Source/BugsnagConfiguration.m index 821403786..457bfc284 100644 --- a/Source/BugsnagConfiguration.m +++ b/Source/BugsnagConfiguration.m @@ -48,6 +48,7 @@ @interface BugsnagNotifier () @interface BugsnagConfiguration () @property(nonatomic, readwrite, strong) NSMutableArray *beforeNotifyHooks; @property(nonatomic, readwrite, strong) NSMutableArray *beforeSendBlocks; +@property(nonatomic, readwrite, strong) NSMutableArray *beforeSendSessionBlocks; @end @implementation BugsnagConfiguration @@ -62,6 +63,7 @@ - (id)init { _notifyURL = [NSURL URLWithString:BSGDefaultNotifyUrl]; _beforeNotifyHooks = [NSMutableArray new]; _beforeSendBlocks = [NSMutableArray new]; + _beforeSendSessionBlocks = [NSMutableArray new]; _notifyReleaseStages = nil; _breadcrumbs = [BugsnagBreadcrumbs new]; _automaticallyCollectBreadcrumbs = YES; @@ -106,6 +108,10 @@ - (void)addBeforeSendBlock:(BugsnagBeforeSendBlock)block { [(NSMutableArray *)self.beforeSendBlocks addObject:[block copy]]; } +- (void)addBeforeSendSession:(BeforeSendSession)block { + [(NSMutableArray *)self.beforeSendSessionBlocks addObject:[block copy]]; +} + - (void)clearBeforeSendBlocks { [(NSMutableArray *)self.beforeSendBlocks removeAllObjects]; } diff --git a/Source/BugsnagSessionTrackingApiClient.m b/Source/BugsnagSessionTrackingApiClient.m index e63719e7e..fd5c20162 100644 --- a/Source/BugsnagSessionTrackingApiClient.m +++ b/Source/BugsnagSessionTrackingApiClient.m @@ -41,6 +41,12 @@ - (void)deliverSessionsInStore:(BugsnagSessionFileStore *)store { } BugsnagSessionTrackingPayload *payload = [[BugsnagSessionTrackingPayload alloc] initWithSessions:sessions]; NSUInteger sessionCount = payload.sessions.count; + NSMutableDictionary *data = [payload toJson]; + + for (BeforeSendSession cb in self.config.beforeSendSessionBlocks) { + cb(data); + } + if (sessionCount > 0) { NSDictionary *HTTPHeaders = @{ @"Bugsnag-Payload-Version": @"1.0", @@ -48,7 +54,7 @@ - (void)deliverSessionsInStore:(BugsnagSessionFileStore *)store { @"Bugsnag-Sent-At": [BSG_RFC3339DateTool stringFromDate:[NSDate new]] }; [self sendItems:sessions.count - withPayload:[payload toJson] + withPayload:data toURL:sessionURL headers:HTTPHeaders onCompletion:^(NSUInteger sentCount, BOOL success, NSError *error) { diff --git a/Source/BugsnagSessionTrackingPayload.h b/Source/BugsnagSessionTrackingPayload.h index af23141d7..b234d9cac 100644 --- a/Source/BugsnagSessionTrackingPayload.h +++ b/Source/BugsnagSessionTrackingPayload.h @@ -14,7 +14,7 @@ - (instancetype)initWithSessions:(NSArray *)sessions; -- (NSDictionary *)toJson; +- (NSMutableDictionary *)toJson; @property NSArray *sessions; diff --git a/Source/BugsnagSessionTrackingPayload.m b/Source/BugsnagSessionTrackingPayload.m index da6da6f76..b1c192f3d 100644 --- a/Source/BugsnagSessionTrackingPayload.m +++ b/Source/BugsnagSessionTrackingPayload.m @@ -28,8 +28,7 @@ - (instancetype)initWithSessions:(NSArray *)sessions { } -- (NSDictionary *)toJson { - +- (NSMutableDictionary *)toJson { NSMutableDictionary *dict = [NSMutableDictionary new]; NSMutableArray *sessionData = [NSMutableArray new]; @@ -42,7 +41,7 @@ - (NSDictionary *)toJson { NSDictionary *systemInfo = [BSG_KSSystemInfo systemInfo]; BSGDictSetSafeObject(dict, BSGParseAppState(systemInfo, [Bugsnag configuration].appVersion), @"app"); BSGDictSetSafeObject(dict, BSGParseDeviceState(systemInfo), @"device"); - return [NSDictionary dictionaryWithDictionary:dict]; + return dict; } @end