Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move codeBundleId from configuration to non-public client property #548

Merged
merged 1 commit into from
Apr 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ Bugsnag Notifiers on other platforms.
* Hide additional methods from public API
[#552](https://github.com/bugsnag/bugsnag-cocoa/pull/552)

* 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 @@ -59,6 +59,7 @@ - (void)addBreadcrumbWithBlock:(void (^_Nonnull)(BugsnagBreadcrumb *_Nonnull))bl
- (void)internalClientNotify:(NSException *_Nonnull)exception
withData:(NSDictionary *_Nullable)metadata
block:(BugsnagOnErrorBlock _Nullable)block;
@property (nonatomic) NSString *codeBundleId;
@end

@interface BugsnagMetadata ()
Expand Down Expand Up @@ -343,7 +344,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 = [event valueForKeyPath:@"user.config.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 @@ -304,6 +306,14 @@ - (NSDictionary *_Nonnull)toDictionary;
- (id)deepCopy;
@end

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

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

@interface BugsnagUser ()
- (instancetype)initWithDictionary:(NSDictionary *)dict;
@end
Expand Down Expand Up @@ -584,6 +594,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 @@ -195,7 +195,6 @@ typedef NS_OPTIONS(NSUInteger, BSGEnabledErrorType) {
*/
@property (readwrite, retain, nullable) NSString *bundleVersion;

@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 @@ -123,7 +123,6 @@ - (nonnull id)copyWithZone:(nullable NSZone *)zone {
[copy setAutoTrackSessions:self.autoTrackSessions];
[copy setBundleVersion:self.bundleVersion];
// 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
8 changes: 6 additions & 2 deletions Source/BugsnagEvent.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@

@interface BugsnagAppWithState ()
+ (BugsnagAppWithState *)appWithDictionary:(NSDictionary *)event
config:(BugsnagConfiguration *)config;
config:(BugsnagConfiguration *)config
codeBundleId:(NSString *)codeBundleId;
- (NSDictionary *)toDict;
+ (BugsnagAppWithState *)appWithOomData:(NSDictionary *)event;
@end
Expand All @@ -49,6 +50,8 @@ + (instancetype _Nullable)breadcrumbFromDict:(NSDictionary *_Nonnull)dict;

@interface BugsnagUser ()
- (NSDictionary *)toJson;
- (instancetype)initWithUserId:(NSString *)userId name:(NSString *)name emailAddress:(NSString *)emailAddress;
- (instancetype)initWithDictionary:(NSDictionary *)dict;
@end

@interface BugsnagConfiguration (BugsnagEvent)
Expand Down Expand Up @@ -283,6 +286,7 @@ - (BOOL)shouldBeSent;

@property NSArray *redactedKeys;

@property(nonatomic) NSString *codeBundleId;
@end

@implementation BugsnagEvent
Expand Down Expand Up @@ -348,7 +352,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"];

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,11 +40,16 @@ @interface Bugsnag ()
+ (BugsnagClient *)client;
@end

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

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

@interface BugsnagConfiguration ()
Expand Down Expand Up @@ -82,6 +87,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