Skip to content

Commit 729eb40

Browse files
authored
Merge sessions-main with master (#10787)
1 parent a79d472 commit 729eb40

36 files changed

+519
-188
lines changed

Crashlytics/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# Unreleased
2+
- [added] Added dependency on Firebase Sessions SDK to power future metrics and debugging features in Crashlytics
3+
14
# 10.4.0
25
- [added] Updated Crashlytics to include the Firebase Installation ID for consistency with other products (#10645).
36

Crashlytics/Crashlytics/Components/FIRCLSContext.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ typedef struct {
8686
const char* rootPath;
8787
const char* previouslyCrashedFileRootPath;
8888
const char* sessionId;
89+
const char* appQualitySessionId;
8990
const char* betaToken;
9091
bool errorsEnabled;
9192
bool customExceptionsEnabled;
@@ -96,10 +97,12 @@ typedef struct {
9697
} FIRCLSContextInitData;
9798

9899
#ifdef __OBJC__
99-
bool FIRCLSContextInitialize(FIRCLSInternalReport* report,
100-
FIRCLSSettings* settings,
101-
FIRCLSFileManager* fileManager);
102-
100+
bool FIRCLSContextInitialize(FIRCLSContextInitData* initData, FIRCLSFileManager* fileManager);
101+
FIRCLSContextInitData FIRCLSContextBuildInitData(FIRCLSInternalReport* report,
102+
FIRCLSSettings* settings,
103+
FIRCLSFileManager* fileManager,
104+
NSString* appQualitySessionId);
105+
bool FIRCLSContextRecordMetadata(NSString* rootPath, const FIRCLSContextInitData* initData);
103106
#endif
104107

105108
void FIRCLSContextBaseInit(void);

Crashlytics/Crashlytics/Components/FIRCLSContext.m

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@
4747

4848
static const int64_t FIRCLSContextInitWaitTime = 5LL * NSEC_PER_SEC;
4949

50-
static bool FIRCLSContextRecordMetadata(const char* path, const FIRCLSContextInitData* initData);
5150
static const char* FIRCLSContextAppendToRoot(NSString* root, NSString* component);
5251
static void FIRCLSContextAllocate(FIRCLSContext* context);
5352

5453
FIRCLSContextInitData FIRCLSContextBuildInitData(FIRCLSInternalReport* report,
5554
FIRCLSSettings* settings,
56-
FIRCLSFileManager* fileManager) {
55+
FIRCLSFileManager* fileManager,
56+
NSString* appQualitySessionId) {
5757
// Because we need to start the crash reporter right away,
5858
// it starts up either with default settings, or cached settings
5959
// from the last time they were fetched
@@ -64,6 +64,7 @@ FIRCLSContextInitData FIRCLSContextBuildInitData(FIRCLSInternalReport* report,
6464

6565
initData.customBundleId = nil;
6666
initData.sessionId = [[report identifier] UTF8String];
67+
initData.appQualitySessionId = [appQualitySessionId UTF8String];
6768
initData.rootPath = [[report path] UTF8String];
6869
initData.previouslyCrashedFileRootPath = [[fileManager rootPath] UTF8String];
6970
initData.errorsEnabled = [settings errorReportingEnabled];
@@ -77,12 +78,7 @@ FIRCLSContextInitData FIRCLSContextBuildInitData(FIRCLSInternalReport* report,
7778
return initData;
7879
}
7980

80-
bool FIRCLSContextInitialize(FIRCLSInternalReport* report,
81-
FIRCLSSettings* settings,
82-
FIRCLSFileManager* fileManager) {
83-
FIRCLSContextInitData initDataObj = FIRCLSContextBuildInitData(report, settings, fileManager);
84-
FIRCLSContextInitData* initData = &initDataObj;
85-
81+
bool FIRCLSContextInitialize(FIRCLSContextInitData* initData, FIRCLSFileManager* fileManager) {
8682
if (!initData) {
8783
return false;
8884
}
@@ -100,7 +96,7 @@ bool FIRCLSContextInitialize(FIRCLSInternalReport* report,
10096

10197
// setup our SDK log file synchronously, because other calls may depend on it
10298
_firclsContext.readonly->logPath = FIRCLSContextAppendToRoot(rootPath, @"sdk.log");
103-
_firclsContext.readonly->initialReportPath = FIRCLSDupString([report.path UTF8String]);
99+
_firclsContext.readonly->initialReportPath = FIRCLSDupString(initData->rootPath);
104100
if (!FIRCLSUnlinkIfExists(_firclsContext.readonly->logPath)) {
105101
FIRCLSErrorLog(@"Unable to write initialize SDK write paths %s", strerror(errno));
106102
}
@@ -209,9 +205,7 @@ bool FIRCLSContextInitialize(FIRCLSInternalReport* report,
209205
}
210206

211207
dispatch_group_async(group, queue, ^{
212-
const char* metaDataPath = [[rootPath stringByAppendingPathComponent:FIRCLSReportMetadataFile]
213-
fileSystemRepresentation];
214-
if (!FIRCLSContextRecordMetadata(metaDataPath, initData)) {
208+
if (!FIRCLSContextRecordMetadata(rootPath, initData)) {
215209
FIRCLSSDKLog("Unable to record context metadata\n");
216210
}
217211
});
@@ -360,7 +354,8 @@ bool FIRCLSContextMarkAndCheckIfCrashed(void) {
360354

361355
static bool FIRCLSContextRecordIdentity(FIRCLSFile* file,
362356
const char* sessionId,
363-
const char* betaToken) {
357+
const char* betaToken,
358+
const char* appQualitySessionId) {
364359
FIRCLSFileWriteSectionStart(file, "identity");
365360

366361
FIRCLSFileWriteHashStart(file);
@@ -371,6 +366,8 @@ static bool FIRCLSContextRecordIdentity(FIRCLSFile* file,
371366
FIRCLSFileWriteHashEntryUint64(file, "started_at", time(NULL));
372367

373368
FIRCLSFileWriteHashEntryString(file, "session_id", sessionId);
369+
FIRCLSFileWriteHashEntryString(file, "app_quality_session_id", appQualitySessionId);
370+
374371
// install_id is written into the proto directly. This is only left here to
375372
// support Apple Report Converter.
376373
FIRCLSFileWriteHashEntryString(file, "install_id", "");
@@ -404,11 +401,13 @@ static bool FIRCLSContextRecordApplication(FIRCLSFile* file, const char* customB
404401
return true;
405402
}
406403

407-
static bool FIRCLSContextRecordMetadata(const char* path, const FIRCLSContextInitData* initData) {
404+
bool FIRCLSContextRecordMetadata(NSString* rootPath, const FIRCLSContextInitData* initData) {
408405
const char* sessionId = initData->sessionId;
409406
const char* betaToken = initData->betaToken;
410407
const char* customBundleId = initData->customBundleId;
411-
408+
const char* appQualitySessionId = initData->appQualitySessionId;
409+
const char* path =
410+
[[rootPath stringByAppendingPathComponent:FIRCLSReportMetadataFile] fileSystemRepresentation];
412411
if (!FIRCLSUnlinkIfExists(path)) {
413412
FIRCLSSDKLog("Unable to unlink existing metadata file %s\n", strerror(errno));
414413
}
@@ -420,7 +419,7 @@ static bool FIRCLSContextRecordMetadata(const char* path, const FIRCLSContextIni
420419
return false;
421420
}
422421

423-
if (!FIRCLSContextRecordIdentity(&file, sessionId, betaToken)) {
422+
if (!FIRCLSContextRecordIdentity(&file, sessionId, betaToken, appQualitySessionId)) {
424423
FIRCLSSDKLog("Unable to write out identity metadata\n");
425424
}
426425

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// Copyright 2022 Google LLC
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
#import <Foundation/Foundation.h>
17+
18+
#import "Crashlytics/Crashlytics/Models/FIRCLSFileManager.h"
19+
#import "Crashlytics/Crashlytics/Models/FIRCLSInternalReport.h"
20+
#import "Crashlytics/Crashlytics/Models/FIRCLSSettings.h"
21+
22+
NS_ASSUME_NONNULL_BEGIN
23+
24+
///
25+
/// The ContextManager determines when to build the context object,
26+
/// and write its metadata. It was created because the FIRCLSContext
27+
/// is interacted with via functions, which makes it hard to include in tests.
28+
/// In addition, we this class is responsible for re-writing the Metadata object
29+
/// when the App Quality Session ID changes.
30+
///
31+
@interface FIRCLSContextManager : NSObject
32+
33+
/// This should be set immediately when the FirebaseSessions SDK generates
34+
/// a new Session ID.
35+
@property(nonatomic, copy) NSString *appQualitySessionId;
36+
37+
- (BOOL)setupContextWithReport:(FIRCLSInternalReport *)report
38+
settings:(FIRCLSSettings *)settings
39+
fileManager:(FIRCLSFileManager *)fileManager;
40+
41+
@end
42+
43+
NS_ASSUME_NONNULL_END
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
//
2+
// Copyright 2022 Google LLC
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
#import "Crashlytics/Crashlytics/Controllers/FIRCLSContextManager.h"
17+
18+
#import "Crashlytics/Crashlytics/Components/FIRCLSContext.h"
19+
20+
@interface FIRCLSContextManager ()
21+
22+
@property(nonatomic, assign) BOOL hasInitializedContext;
23+
24+
@property(nonatomic, strong) FIRCLSInternalReport *report;
25+
@property(nonatomic, strong) FIRCLSSettings *settings;
26+
@property(nonatomic, strong) FIRCLSFileManager *fileManager;
27+
28+
@end
29+
30+
@implementation FIRCLSContextManager
31+
32+
- (instancetype)init {
33+
self = [super init];
34+
if (!self) {
35+
return self;
36+
}
37+
38+
_appQualitySessionId = @"";
39+
40+
return self;
41+
}
42+
43+
- (BOOL)setupContextWithReport:(FIRCLSInternalReport *)report
44+
settings:(FIRCLSSettings *)settings
45+
fileManager:(FIRCLSFileManager *)fileManager {
46+
_report = report;
47+
_settings = settings;
48+
_fileManager = fileManager;
49+
50+
_hasInitializedContext = true;
51+
52+
FIRCLSContextInitData initDataObj = self.buildInitData;
53+
return FIRCLSContextInitialize(&initDataObj, self.fileManager);
54+
}
55+
56+
- (void)setAppQualitySessionId:(NSString *)appQualitySessionId {
57+
_appQualitySessionId = appQualitySessionId;
58+
59+
// This may be called before the context is originally initialized. In that case
60+
// skip the write because it will be written as soon as the context is initialized.
61+
// On future Session ID updates, this will be true and the context metadata will be
62+
// rewritten.
63+
if (!self.hasInitializedContext) {
64+
return;
65+
}
66+
67+
FIRCLSContextInitData initDataObj = self.buildInitData;
68+
if (!FIRCLSContextRecordMetadata(self.report.path, &initDataObj)) {
69+
FIRCLSErrorLog(@"Failed to write context file while updating App Quality Session ID");
70+
}
71+
}
72+
73+
- (FIRCLSContextInitData)buildInitData {
74+
return FIRCLSContextBuildInitData(self.report, self.settings, self.fileManager,
75+
self.appQualitySessionId);
76+
}
77+
78+
@end

Crashlytics/Crashlytics/Controllers/FIRCLSManagerData.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ NS_ASSUME_NONNULL_BEGIN
2525
@class FIRCLSOnDemandModel;
2626
@class FIRCLSSettings;
2727
@class FIRCLSLaunchMarkerModel;
28+
@class FIRCLSContextManager;
2829
@class GDTCORTransport;
2930
@protocol FIRAnalyticsInterop;
3031

@@ -80,6 +81,9 @@ NS_ASSUME_NONNULL_BEGIN
8081
// Settings fetched from the server
8182
@property(nonatomic, strong) FIRCLSSettings *settings;
8283

84+
// Sets up the Context and writes Metadata files to the crash report
85+
@property(nonatomic, strong) FIRCLSContextManager *contextManager;
86+
8387
// These queues function together as a single startup queue
8488
@property(nonatomic, strong) NSOperationQueue *operationQueue;
8589
@property(nonatomic, strong) dispatch_queue_t dispatchQueue;

Crashlytics/Crashlytics/Controllers/FIRCLSManagerData.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#import "Crashlytics/Crashlytics/Controllers/FIRCLSManagerData.h"
1616

1717
#import "Crashlytics/Crashlytics/Components/FIRCLSApplication.h"
18+
#import "Crashlytics/Crashlytics/Controllers/FIRCLSContextManager.h"
1819
#import "Crashlytics/Crashlytics/Models/FIRCLSExecutionIdentifierModel.h"
1920
#import "Crashlytics/Crashlytics/Models/FIRCLSInstallIdentifierModel.h"
2021
#import "Crashlytics/Crashlytics/Models/FIRCLSSettings.h"
@@ -44,6 +45,7 @@ - (instancetype)initWithGoogleAppID:(NSString *)googleAppID
4445
_dataArbiter = dataArbiter;
4546
_settings = settings;
4647
_onDemandModel = onDemandModel;
48+
_contextManager = [[FIRCLSContextManager alloc] init];
4749

4850
_appIDModel = [[FIRCLSApplicationIdentifierModel alloc] init];
4951
_installIDModel = [[FIRCLSInstallIdentifierModel alloc] initWithInstallations:installations];

Crashlytics/Crashlytics/Controllers/FIRCLSReportManager.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
@class FIRCLSExistingReportManager;
2323
@class FIRCLSAnalyticsManager;
2424
@class FIRCLSManagerData;
25+
@class FIRCLSContextManager;
2526

2627
NS_ASSUME_NONNULL_BEGIN
2728

Crashlytics/Crashlytics/Controllers/FIRCLSReportManager.m

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#import "Crashlytics/Crashlytics/Components/FIRCLSApplication.h"
4242
#import "Crashlytics/Crashlytics/Components/FIRCLSUserLogging.h"
4343
#import "Crashlytics/Crashlytics/Controllers/FIRCLSAnalyticsManager.h"
44+
#import "Crashlytics/Crashlytics/Controllers/FIRCLSContextManager.h"
4445
#import "Crashlytics/Crashlytics/Controllers/FIRCLSExistingReportManager.h"
4546
#import "Crashlytics/Crashlytics/Controllers/FIRCLSManagerData.h"
4647
#import "Crashlytics/Crashlytics/Controllers/FIRCLSMetricKitManager.h"
@@ -135,6 +136,8 @@ @interface FIRCLSReportManager () {
135136
@property(nonatomic, strong) FIRCLSAnalyticsManager *analyticsManager;
136137
@property(nonatomic, strong) FIRCLSExistingReportManager *existingReportManager;
137138

139+
@property(nonatomic, strong) FIRCLSContextManager *contextManager;
140+
138141
// Internal Managers
139142
@property(nonatomic, strong) FIRCLSSettingsManager *settingsManager;
140143
@property(nonatomic, strong) FIRCLSNotificationManager *notificationManager;
@@ -165,6 +168,7 @@ - (instancetype)initWithManagerData:(FIRCLSManagerData *)managerData
165168
_installIDModel = managerData.installIDModel;
166169
_settings = managerData.settings;
167170
_executionIDModel = managerData.executionIDModel;
171+
_contextManager = managerData.contextManager;
168172

169173
_existingReportManager = existingReportManager;
170174
_analyticsManager = analyticsManager;
@@ -416,7 +420,9 @@ - (BOOL)startCrashReporterWithProfilingMark:(FIRCLSProfileMark)mark
416420
return NO;
417421
}
418422

419-
if (!FIRCLSContextInitialize(report, self.settings, _fileManager)) {
423+
if (![self.contextManager setupContextWithReport:report
424+
settings:self.settings
425+
fileManager:_fileManager]) {
420426
return NO;
421427
}
422428

0 commit comments

Comments
 (0)