Skip to content

Commit

Permalink
feat: move codeBundleId from configuration to non-public client property
Browse files Browse the repository at this point in the history
  • Loading branch information
fractalwrench committed Apr 20, 2020
1 parent 8119d2f commit 3272997
Show file tree
Hide file tree
Showing 20 changed files with 114 additions and 36 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ Bugsnag Notifiers on other platforms.

## Enhancements

* Move `codeBundleId` from configuration to non-public client property
[#548](https://github.com/bugsnag/bugsnag-cocoa/pull/548)

* Add structured app/device fields to `BugsnagSession`
[#546](https://github.com/bugsnag/bugsnag-cocoa/pull/546)

Expand Down
13 changes: 12 additions & 1 deletion Source/BSGOutOfMemoryWatchdog.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ @interface BSGOutOfMemoryWatchdog ()
@property(nonatomic, getter=didOOMLastLaunch) BOOL oomLastLaunch;
@property(nonatomic, strong, readwrite) NSMutableDictionary *cachedFileInfo;
@property(nonatomic, strong, readwrite) NSDictionary *lastBootCachedFileInfo;
@property(nonatomic) NSString *codeBundleId;
@end

@interface Bugsnag ()
Expand Down Expand Up @@ -164,6 +165,16 @@ - (void)handleUpdateSession:(NSNotification *)note {
[self writeSentinelFile];
}

- (void)setCodeBundleId:(NSString *)codeBundleId {
_codeBundleId = codeBundleId;
self.cachedFileInfo[@"app"][@"codeBundleId"] = codeBundleId;

if ([self isWatching]) {
[self writeSentinelFile];
}
}


- (BOOL)computeDidOOMLastLaunchWithConfig:(BugsnagConfiguration *)config {
if ([[NSFileManager defaultManager] fileExistsAtPath:self.sentinelFilePath]) {
NSDictionary *lastBootInfo = [self readSentinelFile];
Expand Down Expand Up @@ -248,7 +259,7 @@ - (NSMutableDictionary *)generateCacheInfoWithConfig:(BugsnagConfiguration *)con
app[@"version"] = systemInfo[@BSG_KSSystemField_BundleShortVersion] ?: @"";
app[@"bundleVersion"] = systemInfo[@BSG_KSSystemField_BundleVersion] ?: @"";
// 'codeBundleId' only (optionally) exists for React Native clients and defaults otherwise to nil
app[@"codeBundleId"] = [config codeBundleId];
app[@"codeBundleId"] = self.codeBundleId;
#if BSGOOMAvailable
UIApplicationState state = [BSG_KSSystemInfo currentAppState];
app[@"inForeground"] = @([BSG_KSSystemInfo isInForeground:state]);
Expand Down
5 changes: 4 additions & 1 deletion Source/Bugsnag.m
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ @interface BugsnagEvent ()
@interface BugsnagClient ()
- (void)startListeningForStateChangeNotification:(NSString *_Nonnull)notificationName;
- (void)addBreadcrumbWithBlock:(void (^_Nonnull)(BugsnagBreadcrumb *_Nonnull))block;
@property (nonatomic) NSString *codeBundleId;
@end

@interface BugsnagMetadata ()
Expand Down Expand Up @@ -329,7 +330,9 @@ + (void)removeOnSessionBlock:(BugsnagOnSessionBlock _Nonnull )block
* Intended for internal use only - sets the code bundle id for React Native
*/
+ (void)updateCodeBundleId:(NSString *)codeBundleId {
self.configuration.codeBundleId = codeBundleId;
if ([self bugsnagStarted]) {
self.client.codeBundleId = codeBundleId;
}
}

// =============================================================================
Expand Down
6 changes: 4 additions & 2 deletions Source/BugsnagApp.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,25 @@ + (BugsnagApp *)deserializeFromJson:(NSDictionary *)json {

+ (BugsnagApp *)appWithDictionary:(NSDictionary *)event
config:(BugsnagConfiguration *)config
codeBundleId:(NSString *)codeBundleId
{
BugsnagApp *app = [BugsnagApp new];
[self populateFields:app dictionary:event config:config];
[self populateFields:app dictionary:event config:config codeBundleId:codeBundleId];
return app;
}

+ (void)populateFields:(BugsnagApp *)app
dictionary:(NSDictionary *)event
config:(BugsnagConfiguration *)config
codeBundleId:(NSString *)codeBundleId
{
NSDictionary *system = event[BSGKeySystem];
app.id = system[@"CFBundleIdentifier"];
app.bundleVersion = system[@"CFBundleVersion"];
app.dsymUuid = system[@"app_uuid"];
app.version = [event valueForKeyPath:@"user.config.appVersion"] ?: system[@"CFBundleShortVersionString"];
app.releaseStage = [event valueForKeyPath:@"user.config.releaseStage"] ?: config.releaseStage;
app.codeBundleId = config.codeBundleId;
app.codeBundleId = codeBundleId;
app.type = config.appType;
}

Expand Down
6 changes: 4 additions & 2 deletions Source/BugsnagAppWithState.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
@interface BugsnagApp ()
+ (void)populateFields:(BugsnagApp *)app
dictionary:(NSDictionary *)event
config:(BugsnagConfiguration *)config;
config:(BugsnagConfiguration *)config
codeBundleId:(NSString *)codeBundleId;

- (NSDictionary *)toDict;
@end
Expand All @@ -36,6 +37,7 @@ + (BugsnagAppWithState *)appWithOomData:(NSDictionary *)event

+ (BugsnagAppWithState *)appWithDictionary:(NSDictionary *)event
config:(BugsnagConfiguration *)config
codeBundleId:(NSString *)codeBundleId
{
BugsnagAppWithState *app = [BugsnagAppWithState new];
NSDictionary *system = event[BSGKeySystem];
Expand All @@ -48,7 +50,7 @@ + (BugsnagAppWithState *)appWithDictionary:(NSDictionary *)event
app.durationInForeground = activeTimeSinceLaunch;
app.duration = activeTimeSinceLaunch + backgroundTimeSinceLaunch;
app.inForeground = [stats[@"application_in_foreground"] boolValue];
[BugsnagApp populateFields:app dictionary:event config:config];
[BugsnagApp populateFields:app dictionary:event config:config codeBundleId:codeBundleId];
return app;
}

Expand Down
16 changes: 16 additions & 0 deletions Source/BugsnagClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#import "BugsnagLogger.h"
#import "BugsnagKeys.h"
#import "BugsnagSessionTracker.h"
#import "BugsnagSessionTrackingApiClient.h"
#import "BugsnagPluginClient.h"
#import "BSGOutOfMemoryWatchdog.h"
#import "BSG_RFC3339DateTool.h"
Expand Down Expand Up @@ -269,6 +270,7 @@ @interface BugsnagClient ()
@property (nonatomic, strong) BugsnagPluginClient *pluginClient;
@property (nonatomic) BOOL appDidCrashLastLaunch;
@property (nonatomic, strong) BugsnagMetadata *metadata;
@property (nonatomic) NSString *codeBundleId;
#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
// The previous device orientation - iOS only
@property (nonatomic, strong) NSString *lastOrientation;
Expand Down Expand Up @@ -296,6 +298,14 @@ - (NSDictionary *_Nonnull)toDictionary;
- (id)deepCopy;
@end

@interface BSGOutOfMemoryWatchdog ()
@property(nonatomic) NSString *codeBundleId;
@end

@interface BugsnagSessionTracker ()
@property(nonatomic) NSString *codeBundleId;
@end

@interface Bugsnag ()
+ (NSDateFormatter *_Nonnull)payloadDateFormatter;
@end
Expand Down Expand Up @@ -572,6 +582,12 @@ - (void)computeDidCrashLastLaunch {
#endif
}

- (void)setCodeBundleId:(NSString *)codeBundleId {
_codeBundleId = codeBundleId;
self.oomWatchdog.codeBundleId = codeBundleId;
self.sessionTracker.codeBundleId = codeBundleId;
}

/**
* Removes observers and listeners to prevent allocations when the app is terminated
*/
Expand Down
1 change: 0 additions & 1 deletion Source/BugsnagConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ typedef NS_OPTIONS(NSUInteger, BSGErrorType) {
*/
@property BSGEnabledBreadcrumbType enabledBreadcrumbTypes;

@property(retain, nullable) NSString *codeBundleId;
@property(retain, nullable) NSString *appType;

/**
Expand Down
1 change: 0 additions & 1 deletion Source/BugsnagConfiguration.m
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ - (nonnull id)copyWithZone:(nullable NSZone *)zone {
[copy setAutoDetectErrors:self.autoDetectErrors];
[copy setAutoTrackSessions:self.autoTrackSessions];
// Skip breadcrumbs - none should have been set
[copy setCodeBundleId:self.codeBundleId];
[copy setConfig:[[BugsnagMetadata alloc] initWithDictionary:[[self.config toDictionary] mutableCopy]]];
[copy setContext:self.context];
[copy setEnabledBreadcrumbTypes:self.enabledBreadcrumbTypes];
Expand Down
6 changes: 4 additions & 2 deletions Source/BugsnagEvent.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@

@interface BugsnagAppWithState ()
+ (BugsnagAppWithState *)appWithDictionary:(NSDictionary *)event
config:(BugsnagConfiguration *)config;
config:(BugsnagConfiguration *)config
codeBundleId:(NSString *)codeBundleId;
- (NSDictionary *)toDict;
+ (BugsnagAppWithState *)appWithOomData:(NSDictionary *)event;
@end
Expand Down Expand Up @@ -312,6 +313,7 @@ - (BOOL)shouldBeSent;
@property(readwrite, copy, nullable) NSString *releaseStage;

@property NSArray *redactedKeys;
@property(nonatomic) NSString *codeBundleId;
@end

@implementation BugsnagEvent
Expand Down Expand Up @@ -383,7 +385,7 @@ - (instancetype)initWithKSReport:(NSDictionary *)report {

_context = BSGParseContext(report);
_device = [BugsnagDeviceWithState deviceWithDictionary:report];
_app = [BugsnagAppWithState appWithDictionary:report config:config];
_app = [BugsnagAppWithState appWithDictionary:report config:config codeBundleId:self.codeBundleId];
_groupingHash = BSGParseGroupingHash(report);
_overrides = [report valueForKeyPath:@"user.overrides"];
_customException = BSGParseCustomException(report, [_errorClass copy], [_errorMessage copy]);
Expand Down
16 changes: 13 additions & 3 deletions Source/BugsnagSessionTracker.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,24 @@ @interface BugsnagConfiguration ()

@interface BugsnagApp ()
+ (BugsnagApp *)appWithDictionary:(NSDictionary *)event
config:(BugsnagConfiguration *)config;
config:(BugsnagConfiguration *)config
codeBundleId:(NSString *)codeBundleId;
@end

@interface BugsnagDevice ()
+ (BugsnagDevice *)deviceWithDictionary:(NSDictionary *)event;
@end

@interface BugsnagSessionTrackingApiClient ()
@property (nonatomic) NSString *codeBundleId;
@end

@interface BugsnagSessionTracker ()
@property (weak, nonatomic) BugsnagConfiguration *config;
@property (strong, nonatomic) BugsnagSessionFileStore *sessionStore;
@property (strong, nonatomic) BugsnagSessionTrackingApiClient *apiClient;
@property (strong, nonatomic) NSDate *backgroundStartTime;

@property (nonatomic) NSString *codeBundleId;
@property (strong, readwrite) BugsnagSession *currentSession;

/**
Expand All @@ -78,6 +83,11 @@ - (instancetype)initWithConfig:(BugsnagConfiguration *)config
return self;
}

- (void)setCodeBundleId:(NSString *)codeBundleId {
_codeBundleId = codeBundleId;
_apiClient.codeBundleId = codeBundleId;
}

#pragma mark - Creating and sending a new session

- (void)startNewSession {
Expand Down Expand Up @@ -129,7 +139,7 @@ - (void)startNewSessionWithAutoCaptureValue:(BOOL)isAutoCaptured {
}

NSDictionary *systemInfo = [BSG_KSSystemInfo systemInfo];
BugsnagApp *app = [BugsnagApp appWithDictionary:@{@"system": systemInfo} config:self.config];
BugsnagApp *app = [BugsnagApp appWithDictionary:@{@"system": systemInfo} config:self.config codeBundleId:self.codeBundleId];
BugsnagDevice *device = [BugsnagDevice deviceWithDictionary:@{@"system": systemInfo}];
BugsnagSession *newSession = [[BugsnagSession alloc] initWithId:[[NSUUID UUID] UUIDString]
startDate:[NSDate date]
Expand Down
7 changes: 6 additions & 1 deletion Source/BugsnagSessionTrackingApiClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ @interface BugsnagConfiguration ()

@interface BugsnagSessionTrackingApiClient ()
@property NSMutableSet *activeIds;
@property(nonatomic) NSString *codeBundleId;
@end


@implementation BugsnagSessionTrackingApiClient

- (instancetype)initWithConfig:(BugsnagConfiguration *)configuration
Expand Down Expand Up @@ -58,7 +60,10 @@ - (void)deliverSessionsInStore:(BugsnagSessionFileStore *)store {
BugsnagSession *session = [[BugsnagSession alloc] initWithDictionary:filesWithIds[fileId]];

[self.sendQueue addOperationWithBlock:^{
BugsnagSessionTrackingPayload *payload = [[BugsnagSessionTrackingPayload alloc] initWithSessions:@[session] config:[Bugsnag configuration]];
BugsnagSessionTrackingPayload *payload = [[BugsnagSessionTrackingPayload alloc]
initWithSessions:@[session]
config:[Bugsnag configuration]
codeBundleId:self.codeBundleId];
NSUInteger sessionCount = payload.sessions.count;
NSMutableDictionary *data = [payload toJson];
NSDictionary *HTTPHeaders = @{
Expand Down
3 changes: 2 additions & 1 deletion Source/BugsnagSessionTrackingPayload.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
@interface BugsnagSessionTrackingPayload : NSObject

- (instancetype)initWithSessions:(NSArray<BugsnagSession *> *)sessions
config:(BugsnagConfiguration *)config;
config:(BugsnagConfiguration *)config
codeBundleId:(NSString *)codeBundleId;

- (NSMutableDictionary *)toJson;

Expand Down
6 changes: 5 additions & 1 deletion Source/BugsnagSessionTrackingPayload.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,27 @@ - (NSDictionary *)toDictionary;

@interface BugsnagApp ()
+ (BugsnagApp *)appWithDictionary:(NSDictionary *)event
config:(BugsnagConfiguration *)config;
config:(BugsnagConfiguration *)config
codeBundleId:(NSString *)codeBundleId;

- (NSDictionary *)toDict;
@end

@interface BugsnagSessionTrackingPayload ()
@property (nonatomic) BugsnagConfiguration *config;
@property(nonatomic, copy) NSString *codeBundleId;
@end

@implementation BugsnagSessionTrackingPayload

- (instancetype)initWithSessions:(NSArray<BugsnagSession *> *)sessions
config:(BugsnagConfiguration *)config
codeBundleId:(NSString *)codeBundleId
{
if (self = [super init]) {
_sessions = sessions;
_config = config;
_codeBundleId = codeBundleId;
}
return self;
}
Expand Down
7 changes: 7 additions & 0 deletions Source/BugsnagSink.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,15 @@ @interface Bugsnag ()
+ (BugsnagClient *)client;
@end

@interface BugsnagClient ()
@property (nonatomic) NSString *codeBundleId;
@end

@interface BugsnagEvent ()
- (NSDictionary *_Nonnull)toJson;
- (BOOL)shouldBeSent;
@property NSArray *redactedKeys;
@property (nonatomic) NSString *codeBundleId;
@end

@interface BugsnagConfiguration ()
Expand Down Expand Up @@ -81,6 +86,8 @@ - (void)filterReports:(NSDictionary <NSString *, NSDictionary *> *)reports
for (NSString *fileKey in reports) {
NSDictionary *report = reports[fileKey];
BugsnagEvent *bugsnagReport = [[BugsnagEvent alloc] initWithKSReport:report];
bugsnagReport.codeBundleId = [Bugsnag client].codeBundleId;

if (![bugsnagReport shouldBeSent])
continue;
BOOL shouldSend = YES;
Expand Down
Loading

0 comments on commit 3272997

Please sign in to comment.