| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- // Copyright 2019 Google
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- #include <stdatomic.h>
- #if __has_include(<FBLPromises/FBLPromises.h>)
- #import <FBLPromises/FBLPromises.h>
- #else
- #import "FBLPromises.h"
- #endif
- #import "FIRCLSApplicationIdentifierModel.h"
- #include "FIRCLSCrashedMarkerFile.h"
- #import "FIRCLSDataCollectionArbiter.h"
- #import "FIRCLSDefines.h"
- #include "FIRCLSException.h"
- #import "FIRCLSFileManager.h"
- #include "FIRCLSGlobals.h"
- #import "FIRCLSHost.h"
- #include "FIRCLSProfiling.h"
- #import "FIRCLSReport_Private.h"
- #import "FIRCLSSettings.h"
- #import "FIRCLSUserDefaults.h"
- #include "FIRCLSUserLogging.h"
- #include "FIRCLSUtility.h"
- #import "FIRCLSByteUtility.h"
- #import "FIRCLSFABHost.h"
- #import "FIRCLSLogger.h"
- #import "FIRCLSReportManager.h"
- #import "FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
- #import "FirebaseInstallations/Source/Library/Private/FirebaseInstallationsInternal.h"
- #import "Interop/Analytics/Public/FIRAnalyticsInterop.h"
- #import <GoogleDataTransport/GDTCORTargets.h>
- #import <GoogleDataTransport/GDTCORTransport.h>
- #if TARGET_OS_IPHONE
- #import <UIKit/UIKit.h>
- #endif
- FIRCLSContext _firclsContext;
- dispatch_queue_t _firclsLoggingQueue;
- dispatch_queue_t _firclsBinaryImageQueue;
- dispatch_queue_t _firclsExceptionQueue;
- static atomic_bool _hasInitializedInstance;
- NSString *const FIRCLSGoogleTransportMappingID = @"1206";
- /// Empty protocol to register with FirebaseCore's component system.
- @protocol FIRCrashlyticsInstanceProvider <NSObject>
- @end
- @interface FIRCrashlytics () <FIRLibrary, FIRCrashlyticsInstanceProvider>
- @property(nonatomic) BOOL didPreviouslyCrash;
- @property(nonatomic, copy) NSString *googleAppID;
- @property(nonatomic) FIRCLSDataCollectionArbiter *dataArbiter;
- @property(nonatomic) FIRCLSFileManager *fileManager;
- @property(nonatomic) FIRCLSReportManager *reportManager;
- @property(nonatomic) GDTCORTransport *googleTransport;
- @end
- @implementation FIRCrashlytics
- #pragma mark - Singleton Support
- - (instancetype)initWithApp:(FIRApp *)app
- appInfo:(NSDictionary *)appInfo
- installations:(FIRInstallations *)installations
- analytics:(id<FIRAnalyticsInterop>)analytics {
- self = [super init];
- if (self) {
- bool expectedCalled = NO;
- if (!atomic_compare_exchange_strong(&_hasInitializedInstance, &expectedCalled, YES)) {
- FIRCLSErrorLog(@"Cannot instantiate more than one instance of Crashlytics.");
- return nil;
- }
- FIRCLSProfileMark mark = FIRCLSProfilingStart();
- NSLog(@"[Firebase/Crashlytics] Version %@", @CLS_SDK_DISPLAY_VERSION);
- FIRCLSDeveloperLog("Crashlytics", @"Running on %@, %@ (%@)", FIRCLSHostModelInfo(),
- FIRCLSHostOSDisplayVersion(), FIRCLSHostOSBuildVersion());
- _googleTransport = [[GDTCORTransport alloc] initWithMappingID:FIRCLSGoogleTransportMappingID
- transformers:nil
- target:kGDTCORTargetCSH];
- _fileManager = [[FIRCLSFileManager alloc] init];
- _googleAppID = app.options.googleAppID;
- _dataArbiter = [[FIRCLSDataCollectionArbiter alloc] initWithApp:app withAppInfo:appInfo];
- FIRCLSApplicationIdentifierModel *appModel = [[FIRCLSApplicationIdentifierModel alloc] init];
- FIRCLSSettings *settings = [[FIRCLSSettings alloc] initWithFileManager:_fileManager
- appIDModel:appModel];
- _reportManager = [[FIRCLSReportManager alloc] initWithFileManager:_fileManager
- installations:installations
- analytics:analytics
- googleAppID:_googleAppID
- dataArbiter:_dataArbiter
- googleTransport:_googleTransport
- appIDModel:appModel
- settings:settings];
- // Process did crash during previous execution
- NSString *crashedMarkerFileName = [NSString stringWithUTF8String:FIRCLSCrashedMarkerFileName];
- NSString *crashedMarkerFileFullPath =
- [[_fileManager rootPath] stringByAppendingPathComponent:crashedMarkerFileName];
- _didPreviouslyCrash = [_fileManager fileExistsAtPath:crashedMarkerFileFullPath];
- if (_didPreviouslyCrash) {
- // Delete the crash file marker in the background ensure start up is as fast as possible
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
- [self.fileManager removeItemAtPath:crashedMarkerFileFullPath];
- });
- }
- [[[_reportManager startWithProfilingMark:mark] then:^id _Nullable(NSNumber *_Nullable value) {
- if (![value boolValue]) {
- FIRCLSErrorLog(@"Crash reporting could not be initialized");
- }
- return value;
- }] catch:^void(NSError *error) {
- FIRCLSErrorLog(@"Crash reporting failed to initialize with error: %@", error);
- }];
- }
- return self;
- }
- + (void)load {
- [FIRApp registerInternalLibrary:(Class<FIRLibrary>)self
- withName:@"firebase-crashlytics"
- withVersion:@CLS_SDK_DISPLAY_VERSION];
- }
- + (NSArray<FIRComponent *> *)componentsToRegister {
- FIRDependency *analyticsDep =
- [FIRDependency dependencyWithProtocol:@protocol(FIRAnalyticsInterop)];
- FIRComponentCreationBlock creationBlock =
- ^id _Nullable(FIRComponentContainer *container, BOOL *isCacheable) {
- if (!container.app.isDefaultApp) {
- FIRCLSErrorLog(@"Crashlytics must be used with the default Firebase app.");
- return nil;
- }
- id<FIRAnalyticsInterop> analytics = FIR_COMPONENT(FIRAnalyticsInterop, container);
- FIRInstallations *installations = [FIRInstallations installationsWithApp:container.app];
- *isCacheable = YES;
- return [[FIRCrashlytics alloc] initWithApp:container.app
- appInfo:NSBundle.mainBundle.infoDictionary
- installations:installations
- analytics:analytics];
- };
- FIRComponent *component =
- [FIRComponent componentWithProtocol:@protocol(FIRCrashlyticsInstanceProvider)
- instantiationTiming:FIRInstantiationTimingEagerInDefaultApp
- dependencies:@[ analyticsDep ]
- creationBlock:creationBlock];
- return @[ component ];
- }
- + (instancetype)crashlytics {
- // The container will return the same instance since isCacheable is set
- FIRApp *defaultApp = [FIRApp defaultApp]; // Missing configure will be logged here.
- // Get the instance from the `FIRApp`'s container. This will create a new instance the
- // first time it is called, and since `isCacheable` is set in the component creation
- // block, it will return the existing instance on subsequent calls.
- id<FIRCrashlyticsInstanceProvider> instance =
- FIR_COMPONENT(FIRCrashlyticsInstanceProvider, defaultApp.container);
- // In the component creation block, we return an instance of `FIRCrashlytics`. Cast it and
- // return it.
- return (FIRCrashlytics *)instance;
- }
- - (void)setCrashlyticsCollectionEnabled:(BOOL)enabled {
- [self.dataArbiter setCrashlyticsCollectionEnabled:enabled];
- }
- - (BOOL)isCrashlyticsCollectionEnabled {
- return [self.dataArbiter isCrashlyticsCollectionEnabled];
- }
- #pragma mark - API: didCrashDuringPreviousExecution
- - (BOOL)didCrashDuringPreviousExecution {
- return self.didPreviouslyCrash;
- }
- - (void)processDidCrashDuringPreviousExecution {
- NSString *crashedMarkerFileName = [NSString stringWithUTF8String:FIRCLSCrashedMarkerFileName];
- NSString *crashedMarkerFileFullPath =
- [[self.fileManager rootPath] stringByAppendingPathComponent:crashedMarkerFileName];
- self.didPreviouslyCrash = [self.fileManager fileExistsAtPath:crashedMarkerFileFullPath];
- if (self.didPreviouslyCrash) {
- // Delete the crash file marker in the background ensure start up is as fast as possible
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
- [self.fileManager removeItemAtPath:crashedMarkerFileFullPath];
- });
- }
- }
- #pragma mark - API: Logging
- - (void)log:(NSString *)msg {
- FIRCLSLog(@"%@", msg);
- }
- - (void)logWithFormat:(NSString *)format, ... {
- va_list args;
- va_start(args, format);
- [self logWithFormat:format arguments:args];
- va_end(args);
- }
- - (void)logWithFormat:(NSString *)format arguments:(va_list)args {
- [self log:[[NSString alloc] initWithFormat:format arguments:args]];
- }
- #pragma mark - API: Accessors
- - (void)checkForUnsentReportsWithCompletion:(void (^)(BOOL))completion {
- [[self.reportManager checkForUnsentReports] then:^id _Nullable(NSNumber *_Nullable value) {
- completion([value boolValue]);
- return nil;
- }];
- }
- - (void)sendUnsentReports {
- [self.reportManager sendUnsentReports];
- }
- - (void)deleteUnsentReports {
- [self.reportManager deleteUnsentReports];
- }
- #pragma mark - API: setUserID
- - (void)setUserID:(NSString *)userID {
- FIRCLSUserLoggingRecordInternalKeyValue(FIRCLSUserIdentifierKey, userID);
- }
- #pragma mark - API: setCustomValue
- - (void)setCustomValue:(id)value forKey:(NSString *)key {
- FIRCLSUserLoggingRecordUserKeyValue(key, value);
- }
- #pragma mark - API: Development Platform
- // These two methods are depercated by our own API, so
- // its ok to implement them
- #pragma clang diagnostic push
- #pragma clang diagnostic ignored "-Wdeprecated-implementations"
- + (void)setDevelopmentPlatformName:(NSString *)name {
- [[self crashlytics] setDevelopmentPlatformName:name];
- }
- + (void)setDevelopmentPlatformVersion:(NSString *)version {
- [[self crashlytics] setDevelopmentPlatformVersion:version];
- }
- #pragma clang diagnostic pop
- - (NSString *)developmentPlatformName {
- FIRCLSErrorLog(@"developmentPlatformName is write-only");
- return nil;
- }
- - (void)setDevelopmentPlatformName:(NSString *)developmentPlatformName {
- FIRCLSUserLoggingRecordInternalKeyValue(FIRCLSDevelopmentPlatformNameKey,
- developmentPlatformName);
- }
- - (NSString *)developmentPlatformVersion {
- FIRCLSErrorLog(@"developmentPlatformVersion is write-only");
- return nil;
- }
- - (void)setDevelopmentPlatformVersion:(NSString *)developmentPlatformVersion {
- FIRCLSUserLoggingRecordInternalKeyValue(FIRCLSDevelopmentPlatformVersionKey,
- developmentPlatformVersion);
- }
- #pragma mark - API: Errors and Exceptions
- - (void)recordError:(NSError *)error {
- FIRCLSUserLoggingRecordError(error, nil);
- }
- - (void)recordExceptionModel:(FIRExceptionModel *)exceptionModel {
- FIRCLSExceptionRecordModel(exceptionModel);
- }
- @end
|