| 1 | 
           efrain | 
           1 | 
           // Copyright 2019 Google
  | 
        
        
            | 
            | 
           2 | 
           //
  | 
        
        
            | 
            | 
           3 | 
           // Licensed under the Apache License, Version 2.0 (the "License");
  | 
        
        
            | 
            | 
           4 | 
           // you may not use this file except in compliance with the License.
  | 
        
        
            | 
            | 
           5 | 
           // You may obtain a copy of the License at
  | 
        
        
            | 
            | 
           6 | 
           //
  | 
        
        
            | 
            | 
           7 | 
           //      http://www.apache.org/licenses/LICENSE-2.0
  | 
        
        
            | 
            | 
           8 | 
           //
  | 
        
        
            | 
            | 
           9 | 
           // Unless required by applicable law or agreed to in writing, software
  | 
        
        
            | 
            | 
           10 | 
           // distributed under the License is distributed on an "AS IS" BASIS,
  | 
        
        
            | 
            | 
           11 | 
           // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  | 
        
        
            | 
            | 
           12 | 
           // See the License for the specific language governing permissions and
  | 
        
        
            | 
            | 
           13 | 
           // limitations under the License.
  | 
        
        
            | 
            | 
           14 | 
              | 
        
        
            | 
            | 
           15 | 
           //
  | 
        
        
            | 
            | 
           16 | 
           // The report manager has the ability to send to two different endpoints.
  | 
        
        
            | 
            | 
           17 | 
           //
  | 
        
        
            | 
            | 
           18 | 
           // The old legacy flow for a report goes through the following states/folders:
  | 
        
        
            | 
            | 
           19 | 
           // 1. active - .clsrecords optimized for crash time persistence
  | 
        
        
            | 
            | 
           20 | 
           // 2. processing - .clsrecords with attempted symbolication
  | 
        
        
            | 
            | 
           21 | 
           // 3. prepared-legacy - .multipartmime of compressed .clsrecords
  | 
        
        
            | 
            | 
           22 | 
           //
  | 
        
        
            | 
            | 
           23 | 
           // The new flow for a report goes through the following states/folders:
  | 
        
        
            | 
            | 
           24 | 
           // 1. active - .clsrecords optimized for crash time persistence
  | 
        
        
            | 
            | 
           25 | 
           // 2. processing - .clsrecords with attempted symbolication
  | 
        
        
            | 
            | 
           26 | 
           // 3. prepared - .clsrecords moved from processing with no changes
  | 
        
        
            | 
            | 
           27 | 
           //
  | 
        
        
            | 
            | 
           28 | 
           // The code was designed so the report processing workflows are not dramatically different from one
  | 
        
        
            | 
            | 
           29 | 
           // another. The design will help avoid having a lot of conditional code blocks throughout the
  | 
        
        
            | 
            | 
           30 | 
           // codebase.
  | 
        
        
            | 
            | 
           31 | 
           //
  | 
        
        
            | 
            | 
           32 | 
              | 
        
        
            | 
            | 
           33 | 
           #include <stdatomic.h>
  | 
        
        
            | 
            | 
           34 | 
              | 
        
        
            | 
            | 
           35 | 
           #if __has_include(<FBLPromises/FBLPromises.h>)
  | 
        
        
            | 
            | 
           36 | 
           #import <FBLPromises/FBLPromises.h>
  | 
        
        
            | 
            | 
           37 | 
           #else
  | 
        
        
            | 
            | 
           38 | 
           #import "FBLPromises.h"
  | 
        
        
            | 
            | 
           39 | 
           #endif
  | 
        
        
            | 
            | 
           40 | 
              | 
        
        
            | 
            | 
           41 | 
           #import "Crashlytics/Crashlytics/Components/FIRCLSApplication.h"
  | 
        
        
            | 
            | 
           42 | 
           #import "Crashlytics/Crashlytics/Components/FIRCLSUserLogging.h"
  | 
        
        
            | 
            | 
           43 | 
           #import "Crashlytics/Crashlytics/Controllers/FIRCLSAnalyticsManager.h"
  | 
        
        
            | 
            | 
           44 | 
           #import "Crashlytics/Crashlytics/Controllers/FIRCLSExistingReportManager.h"
  | 
        
        
            | 
            | 
           45 | 
           #import "Crashlytics/Crashlytics/Controllers/FIRCLSManagerData.h"
  | 
        
        
            | 
            | 
           46 | 
           #import "Crashlytics/Crashlytics/Controllers/FIRCLSMetricKitManager.h"
  | 
        
        
            | 
            | 
           47 | 
           #import "Crashlytics/Crashlytics/Controllers/FIRCLSNotificationManager.h"
  | 
        
        
            | 
            | 
           48 | 
           #import "Crashlytics/Crashlytics/DataCollection/FIRCLSDataCollectionArbiter.h"
  | 
        
        
            | 
            | 
           49 | 
           #import "Crashlytics/Crashlytics/DataCollection/FIRCLSDataCollectionToken.h"
  | 
        
        
            | 
            | 
           50 | 
           #import "Crashlytics/Crashlytics/Helpers/FIRCLSDefines.h"
  | 
        
        
            | 
            | 
           51 | 
           #import "Crashlytics/Crashlytics/Helpers/FIRCLSFeatures.h"
  | 
        
        
            | 
            | 
           52 | 
           #import "Crashlytics/Crashlytics/Helpers/FIRCLSLogger.h"
  | 
        
        
            | 
            | 
           53 | 
           #import "Crashlytics/Crashlytics/Models/FIRCLSFileManager.h"
  | 
        
        
            | 
            | 
           54 | 
           #import "Crashlytics/Crashlytics/Models/FIRCLSInternalReport.h"
  | 
        
        
            | 
            | 
           55 | 
           #import "Crashlytics/Crashlytics/Models/FIRCLSLaunchMarkerModel.h"
  | 
        
        
            | 
            | 
           56 | 
           #import "Crashlytics/Crashlytics/Models/FIRCLSSettings.h"
  | 
        
        
            | 
            | 
           57 | 
           #import "Crashlytics/Crashlytics/Models/FIRCLSSymbolResolver.h"
  | 
        
        
            | 
            | 
           58 | 
           #import "Crashlytics/Crashlytics/Operations/Reports/FIRCLSProcessReportOperation.h"
  | 
        
        
            | 
            | 
           59 | 
           #import "Crashlytics/Crashlytics/Settings/Models/FIRCLSApplicationIdentifierModel.h"
  | 
        
        
            | 
            | 
           60 | 
              | 
        
        
            | 
            | 
           61 | 
           #include "Crashlytics/Crashlytics/Components/FIRCLSGlobals.h"
  | 
        
        
            | 
            | 
           62 | 
           #include "Crashlytics/Crashlytics/Helpers/FIRCLSUtility.h"
  | 
        
        
            | 
            | 
           63 | 
              | 
        
        
            | 
            | 
           64 | 
           #import "Crashlytics/Crashlytics/Models/FIRCLSExecutionIdentifierModel.h"
  | 
        
        
            | 
            | 
           65 | 
           #import "Crashlytics/Crashlytics/Models/FIRCLSInstallIdentifierModel.h"
  | 
        
        
            | 
            | 
           66 | 
           #import "Crashlytics/Crashlytics/Settings/FIRCLSSettingsManager.h"
  | 
        
        
            | 
            | 
           67 | 
           #import "Crashlytics/Shared/FIRCLSConstants.h"
  | 
        
        
            | 
            | 
           68 | 
              | 
        
        
            | 
            | 
           69 | 
           #import "Crashlytics/Crashlytics/Controllers/FIRCLSReportManager_Private.h"
  | 
        
        
            | 
            | 
           70 | 
              | 
        
        
            | 
            | 
           71 | 
           #if TARGET_OS_IPHONE
  | 
        
        
            | 
            | 
           72 | 
           #import <UIKit/UIKit.h>
  | 
        
        
            | 
            | 
           73 | 
           #else
  | 
        
        
            | 
            | 
           74 | 
           #import <AppKit/AppKit.h>
  | 
        
        
            | 
            | 
           75 | 
           #endif
  | 
        
        
            | 
            | 
           76 | 
              | 
        
        
            | 
            | 
           77 | 
           /**
  | 
        
        
            | 
            | 
           78 | 
            * A FirebaseReportAction is used to indicate how to handle unsent reports.
  | 
        
        
            | 
            | 
           79 | 
            */
  | 
        
        
            | 
            | 
           80 | 
           typedef NS_ENUM(NSInteger, FIRCLSReportAction) {
  | 
        
        
            | 
            | 
           81 | 
             /** Upload the reports to Crashlytics. */
  | 
        
        
            | 
            | 
           82 | 
             FIRCLSReportActionSend,
  | 
        
        
            | 
            | 
           83 | 
             /** Delete the reports without uploading them. */
  | 
        
        
            | 
            | 
           84 | 
             FIRCLSReportActionDelete,
  | 
        
        
            | 
            | 
           85 | 
           };
  | 
        
        
            | 
            | 
           86 | 
              | 
        
        
            | 
            | 
           87 | 
           /**
  | 
        
        
            | 
            | 
           88 | 
            * This is just a helper to make code using FirebaseReportAction more readable.
  | 
        
        
            | 
            | 
           89 | 
            */
  | 
        
        
            | 
            | 
           90 | 
           typedef NSNumber FIRCLSWrappedReportAction;
  | 
        
        
            | 
            | 
           91 | 
           @implementation NSNumber (FIRCLSWrappedReportAction)
  | 
        
        
            | 
            | 
           92 | 
           - (FIRCLSReportAction)reportActionValue {
  | 
        
        
            | 
            | 
           93 | 
             return [self intValue];
  | 
        
        
            | 
            | 
           94 | 
           }
  | 
        
        
            | 
            | 
           95 | 
           @end
  | 
        
        
            | 
            | 
           96 | 
              | 
        
        
            | 
            | 
           97 | 
           @interface FIRCLSReportManager () {
  | 
        
        
            | 
            | 
           98 | 
             FIRCLSFileManager *_fileManager;
  | 
        
        
            | 
            | 
           99 | 
             dispatch_queue_t _dispatchQueue;
  | 
        
        
            | 
            | 
           100 | 
             NSOperationQueue *_operationQueue;
  | 
        
        
            | 
            | 
           101 | 
             id<FIRAnalyticsInterop> _analytics;
  | 
        
        
            | 
            | 
           102 | 
              | 
        
        
            | 
            | 
           103 | 
             // A promise that will be resolved when unsent reports are found on the device, and
  | 
        
        
            | 
            | 
           104 | 
             // processReports: can be called to decide how to deal with them.
  | 
        
        
            | 
            | 
           105 | 
             FBLPromise<FIRCrashlyticsReport *> *_unsentReportsAvailable;
  | 
        
        
            | 
            | 
           106 | 
              | 
        
        
            | 
            | 
           107 | 
             // A promise that will be resolved when the user has provided an action that they want to perform
  | 
        
        
            | 
            | 
           108 | 
             // for all the unsent reports.
  | 
        
        
            | 
            | 
           109 | 
             FBLPromise<FIRCLSWrappedReportAction *> *_reportActionProvided;
  | 
        
        
            | 
            | 
           110 | 
              | 
        
        
            | 
            | 
           111 | 
             // A promise that will be resolved when all unsent reports have been "handled". They won't
  | 
        
        
            | 
            | 
           112 | 
             // necessarily have been uploaded, but we will know whether they should be sent or deleted, and
  | 
        
        
            | 
            | 
           113 | 
             // the initial work to make that happen will have been processed on the work queue.
  | 
        
        
            | 
            | 
           114 | 
             //
  | 
        
        
            | 
            | 
           115 | 
             // Currently only used for testing
  | 
        
        
            | 
            | 
           116 | 
             FBLPromise *_unsentReportsHandled;
  | 
        
        
            | 
            | 
           117 | 
              | 
        
        
            | 
            | 
           118 | 
             // A token to make sure that checkForUnsentReports only gets called once.
  | 
        
        
            | 
            | 
           119 | 
             atomic_bool _checkForUnsentReportsCalled;
  | 
        
        
            | 
            | 
           120 | 
              | 
        
        
            | 
            | 
           121 | 
             BOOL _registeredAnalyticsEventListener;
  | 
        
        
            | 
            | 
           122 | 
           }
  | 
        
        
            | 
            | 
           123 | 
              | 
        
        
            | 
            | 
           124 | 
           @property(nonatomic, readonly) NSString *googleAppID;
  | 
        
        
            | 
            | 
           125 | 
           @property(nonatomic, strong) GDTCORTransport *googleTransport;
  | 
        
        
            | 
            | 
           126 | 
              | 
        
        
            | 
            | 
           127 | 
           @property(nonatomic, strong) FIRCLSDataCollectionArbiter *dataArbiter;
  | 
        
        
            | 
            | 
           128 | 
           @property(nonatomic, strong) FIRCLSSettings *settings;
  | 
        
        
            | 
            | 
           129 | 
           @property(nonatomic, strong) FIRCLSLaunchMarkerModel *launchMarker;
  | 
        
        
            | 
            | 
           130 | 
              | 
        
        
            | 
            | 
           131 | 
           @property(nonatomic, strong) FIRCLSApplicationIdentifierModel *appIDModel;
  | 
        
        
            | 
            | 
           132 | 
           @property(nonatomic, strong) FIRCLSInstallIdentifierModel *installIDModel;
  | 
        
        
            | 
            | 
           133 | 
           @property(nonatomic, strong) FIRCLSExecutionIdentifierModel *executionIDModel;
  | 
        
        
            | 
            | 
           134 | 
              | 
        
        
            | 
            | 
           135 | 
           @property(nonatomic, strong) FIRCLSAnalyticsManager *analyticsManager;
  | 
        
        
            | 
            | 
           136 | 
           @property(nonatomic, strong) FIRCLSExistingReportManager *existingReportManager;
  | 
        
        
            | 
            | 
           137 | 
              | 
        
        
            | 
            | 
           138 | 
           // Internal Managers
  | 
        
        
            | 
            | 
           139 | 
           @property(nonatomic, strong) FIRCLSSettingsManager *settingsManager;
  | 
        
        
            | 
            | 
           140 | 
           @property(nonatomic, strong) FIRCLSNotificationManager *notificationManager;
  | 
        
        
            | 
            | 
           141 | 
           #if CLS_METRICKIT_SUPPORTED
  | 
        
        
            | 
            | 
           142 | 
           @property(nonatomic, strong) FIRCLSMetricKitManager *metricKitManager;
  | 
        
        
            | 
            | 
           143 | 
           #endif
  | 
        
        
            | 
            | 
           144 | 
              | 
        
        
            | 
            | 
           145 | 
           @end
  | 
        
        
            | 
            | 
           146 | 
              | 
        
        
            | 
            | 
           147 | 
           @implementation FIRCLSReportManager
  | 
        
        
            | 
            | 
           148 | 
              | 
        
        
            | 
            | 
           149 | 
           - (instancetype)initWithManagerData:(FIRCLSManagerData *)managerData
  | 
        
        
            | 
            | 
           150 | 
                         existingReportManager:(FIRCLSExistingReportManager *)existingReportManager
  | 
        
        
            | 
            | 
           151 | 
                              analyticsManager:(FIRCLSAnalyticsManager *)analyticsManager {
  | 
        
        
            | 
            | 
           152 | 
             self = [super init];
  | 
        
        
            | 
            | 
           153 | 
             if (!self) {
  | 
        
        
            | 
            | 
           154 | 
               return nil;
  | 
        
        
            | 
            | 
           155 | 
             }
  | 
        
        
            | 
            | 
           156 | 
              | 
        
        
            | 
            | 
           157 | 
             _fileManager = managerData.fileManager;
  | 
        
        
            | 
            | 
           158 | 
             _analytics = managerData.analytics;
  | 
        
        
            | 
            | 
           159 | 
             _googleAppID = [managerData.googleAppID copy];
  | 
        
        
            | 
            | 
           160 | 
             _dataArbiter = managerData.dataArbiter;
  | 
        
        
            | 
            | 
           161 | 
             _googleTransport = managerData.googleTransport;
  | 
        
        
            | 
            | 
           162 | 
             _operationQueue = managerData.operationQueue;
  | 
        
        
            | 
            | 
           163 | 
             _dispatchQueue = managerData.dispatchQueue;
  | 
        
        
            | 
            | 
           164 | 
             _appIDModel = managerData.appIDModel;
  | 
        
        
            | 
            | 
           165 | 
             _installIDModel = managerData.installIDModel;
  | 
        
        
            | 
            | 
           166 | 
             _settings = managerData.settings;
  | 
        
        
            | 
            | 
           167 | 
             _executionIDModel = managerData.executionIDModel;
  | 
        
        
            | 
            | 
           168 | 
              | 
        
        
            | 
            | 
           169 | 
             _existingReportManager = existingReportManager;
  | 
        
        
            | 
            | 
           170 | 
             _analyticsManager = analyticsManager;
  | 
        
        
            | 
            | 
           171 | 
              | 
        
        
            | 
            | 
           172 | 
             _unsentReportsAvailable = [FBLPromise pendingPromise];
  | 
        
        
            | 
            | 
           173 | 
             _reportActionProvided = [FBLPromise pendingPromise];
  | 
        
        
            | 
            | 
           174 | 
             _unsentReportsHandled = [FBLPromise pendingPromise];
  | 
        
        
            | 
            | 
           175 | 
              | 
        
        
            | 
            | 
           176 | 
             _checkForUnsentReportsCalled = NO;
  | 
        
        
            | 
            | 
           177 | 
              | 
        
        
            | 
            | 
           178 | 
             _settingsManager = [[FIRCLSSettingsManager alloc] initWithAppIDModel:self.appIDModel
  | 
        
        
            | 
            | 
           179 | 
                                                                   installIDModel:self.installIDModel
  | 
        
        
            | 
            | 
           180 | 
                                                                         settings:self.settings
  | 
        
        
            | 
            | 
           181 | 
                                                                      fileManager:self.fileManager
  | 
        
        
            | 
            | 
           182 | 
                                                                      googleAppID:self.googleAppID];
  | 
        
        
            | 
            | 
           183 | 
              | 
        
        
            | 
            | 
           184 | 
             _notificationManager = [[FIRCLSNotificationManager alloc] init];
  | 
        
        
            | 
            | 
           185 | 
              | 
        
        
            | 
            | 
           186 | 
             // This needs to be called before any values are read from settings
  | 
        
        
            | 
            | 
           187 | 
             NSTimeInterval currentTimestamp = [NSDate timeIntervalSinceReferenceDate];
  | 
        
        
            | 
            | 
           188 | 
             [self.settings reloadFromCacheWithGoogleAppID:self.googleAppID currentTimestamp:currentTimestamp];
  | 
        
        
            | 
            | 
           189 | 
              | 
        
        
            | 
            | 
           190 | 
           #if CLS_METRICKIT_SUPPORTED
  | 
        
        
            | 
            | 
           191 | 
             if (@available(iOS 15, *)) {
  | 
        
        
            | 
            | 
           192 | 
               if (self.settings.metricKitCollectionEnabled) {
  | 
        
        
            | 
            | 
           193 | 
                 FIRCLSDebugLog(@"MetricKit data collection enabled.");
  | 
        
        
            | 
            | 
           194 | 
                 _metricKitManager = [[FIRCLSMetricKitManager alloc] initWithManagerData:managerData
  | 
        
        
            | 
            | 
           195 | 
                                                                   existingReportManager:existingReportManager
  | 
        
        
            | 
            | 
           196 | 
                                                                             fileManager:_fileManager];
  | 
        
        
            | 
            | 
           197 | 
               }
  | 
        
        
            | 
            | 
           198 | 
             }
  | 
        
        
            | 
            | 
           199 | 
           #endif
  | 
        
        
            | 
            | 
           200 | 
              | 
        
        
            | 
            | 
           201 | 
             _launchMarker = [[FIRCLSLaunchMarkerModel alloc] initWithFileManager:_fileManager];
  | 
        
        
            | 
            | 
           202 | 
              | 
        
        
            | 
            | 
           203 | 
             return self;
  | 
        
        
            | 
            | 
           204 | 
           }
  | 
        
        
            | 
            | 
           205 | 
              | 
        
        
            | 
            | 
           206 | 
           // This method returns a promise that is resolved with a wrapped FirebaseReportAction once the user
  | 
        
        
            | 
            | 
           207 | 
           // has indicated whether they want to upload currently cached reports. This method should only be
  | 
        
        
            | 
            | 
           208 | 
           // called when we have determined there is at least 1 unsent report. This method waits until either:
  | 
        
        
            | 
            | 
           209 | 
           //    1. Data collection becomes enabled, in which case, the promise will be resolved with Send.
  | 
        
        
            | 
            | 
           210 | 
           //    2. The developer uses the processCrashReports API to indicate whether the report
  | 
        
        
            | 
            | 
           211 | 
           //       should be sent or deleted, at which point the promise will be resolved with the action.
  | 
        
        
            | 
            | 
           212 | 
           - (FBLPromise<FIRCLSWrappedReportAction *> *)waitForReportAction {
  | 
        
        
            | 
            | 
           213 | 
             FIRCrashlyticsReport *unsentReport = self.existingReportManager.newestUnsentReport;
  | 
        
        
            | 
            | 
           214 | 
             [_unsentReportsAvailable fulfill:unsentReport];
  | 
        
        
            | 
            | 
           215 | 
              | 
        
        
            | 
            | 
           216 | 
             // If data collection gets enabled while we are waiting for an action, go ahead and send the
  | 
        
        
            | 
            | 
           217 | 
             // reports, and any subsequent explicit response will be ignored.
  | 
        
        
            | 
            | 
           218 | 
             FBLPromise<FIRCLSWrappedReportAction *> *collectionEnabled =
  | 
        
        
            | 
            | 
           219 | 
                 [[self.dataArbiter waitForCrashlyticsCollectionEnabled]
  | 
        
        
            | 
            | 
           220 | 
                     then:^id _Nullable(NSNumber *_Nullable value) {
  | 
        
        
            | 
            | 
           221 | 
                       return @(FIRCLSReportActionSend);
  | 
        
        
            | 
            | 
           222 | 
                     }];
  | 
        
        
            | 
            | 
           223 | 
              | 
        
        
            | 
            | 
           224 | 
             // Wait for either the processReports callback to be called, or data collection to be enabled.
  | 
        
        
            | 
            | 
           225 | 
             return [FBLPromise race:@[ collectionEnabled, _reportActionProvided ]];
  | 
        
        
            | 
            | 
           226 | 
           }
  | 
        
        
            | 
            | 
           227 | 
              | 
        
        
            | 
            | 
           228 | 
           /*
  | 
        
        
            | 
            | 
           229 | 
            * This method returns a promise that is resolved once
  | 
        
        
            | 
            | 
           230 | 
            * MetricKit diagnostic reports have been received by `metricKitManager`.
  | 
        
        
            | 
            | 
           231 | 
            */
  | 
        
        
            | 
            | 
           232 | 
           - (FBLPromise *)waitForMetricKitData {
  | 
        
        
            | 
            | 
           233 | 
             // If the platform is not iOS or the iOS version is less than 15, immediately resolve the promise
  | 
        
        
            | 
            | 
           234 | 
             // since no MetricKit diagnostics will be available.
  | 
        
        
            | 
            | 
           235 | 
             FBLPromise *promise = [FBLPromise resolvedWith:nil];
  | 
        
        
            | 
            | 
           236 | 
           #if CLS_METRICKIT_SUPPORTED
  | 
        
        
            | 
            | 
           237 | 
             if (@available(iOS 15, *)) {
  | 
        
        
            | 
            | 
           238 | 
               if (self.settings.metricKitCollectionEnabled) {
  | 
        
        
            | 
            | 
           239 | 
                 promise = [self.metricKitManager waitForMetricKitDataAvailable];
  | 
        
        
            | 
            | 
           240 | 
               }
  | 
        
        
            | 
            | 
           241 | 
             }
  | 
        
        
            | 
            | 
           242 | 
             return promise;
  | 
        
        
            | 
            | 
           243 | 
           #endif
  | 
        
        
            | 
            | 
           244 | 
             return promise;
  | 
        
        
            | 
            | 
           245 | 
           }
  | 
        
        
            | 
            | 
           246 | 
              | 
        
        
            | 
            | 
           247 | 
           - (FBLPromise<FIRCrashlyticsReport *> *)checkForUnsentReports {
  | 
        
        
            | 
            | 
           248 | 
             bool expectedCalled = NO;
  | 
        
        
            | 
            | 
           249 | 
             if (!atomic_compare_exchange_strong(&_checkForUnsentReportsCalled, &expectedCalled, YES)) {
  | 
        
        
            | 
            | 
           250 | 
               FIRCLSErrorLog(@"Either checkForUnsentReports or checkAndUpdateUnsentReports should be called "
  | 
        
        
            | 
            | 
           251 | 
                              @"once per execution.");
  | 
        
        
            | 
            | 
           252 | 
               return [FBLPromise resolvedWith:nil];
  | 
        
        
            | 
            | 
           253 | 
             }
  | 
        
        
            | 
            | 
           254 | 
             return _unsentReportsAvailable;
  | 
        
        
            | 
            | 
           255 | 
           }
  | 
        
        
            | 
            | 
           256 | 
              | 
        
        
            | 
            | 
           257 | 
           - (FBLPromise *)sendUnsentReports {
  | 
        
        
            | 
            | 
           258 | 
             [_reportActionProvided fulfill:@(FIRCLSReportActionSend)];
  | 
        
        
            | 
            | 
           259 | 
             return _unsentReportsHandled;
  | 
        
        
            | 
            | 
           260 | 
           }
  | 
        
        
            | 
            | 
           261 | 
              | 
        
        
            | 
            | 
           262 | 
           - (FBLPromise *)deleteUnsentReports {
  | 
        
        
            | 
            | 
           263 | 
             [_reportActionProvided fulfill:@(FIRCLSReportActionDelete)];
  | 
        
        
            | 
            | 
           264 | 
             return _unsentReportsHandled;
  | 
        
        
            | 
            | 
           265 | 
           }
  | 
        
        
            | 
            | 
           266 | 
              | 
        
        
            | 
            | 
           267 | 
           - (FBLPromise<NSNumber *> *)startWithProfilingMark:(FIRCLSProfileMark)mark {
  | 
        
        
            | 
            | 
           268 | 
             NSString *executionIdentifier = self.executionIDModel.executionID;
  | 
        
        
            | 
            | 
           269 | 
              | 
        
        
            | 
            | 
           270 | 
             // This needs to be called before the new report is created for
  | 
        
        
            | 
            | 
           271 | 
             // this run of the app.
  | 
        
        
            | 
            | 
           272 | 
             [self.existingReportManager collectExistingReports];
  | 
        
        
            | 
            | 
           273 | 
              | 
        
        
            | 
            | 
           274 | 
             if (![self validateAppIdentifiers]) {
  | 
        
        
            | 
            | 
           275 | 
               return [FBLPromise resolvedWith:@NO];
  | 
        
        
            | 
            | 
           276 | 
             }
  | 
        
        
            | 
            | 
           277 | 
              | 
        
        
            | 
            | 
           278 | 
           #if DEBUG
  | 
        
        
            | 
            | 
           279 | 
             FIRCLSDebugLog(@"Root: %@", [_fileManager rootPath]);
  | 
        
        
            | 
            | 
           280 | 
           #endif
  | 
        
        
            | 
            | 
           281 | 
              | 
        
        
            | 
            | 
           282 | 
             if (![_fileManager createReportDirectories]) {
  | 
        
        
            | 
            | 
           283 | 
               return [FBLPromise resolvedWith:@NO];
  | 
        
        
            | 
            | 
           284 | 
             }
  | 
        
        
            | 
            | 
           285 | 
              | 
        
        
            | 
            | 
           286 | 
             BOOL launchFailure = [self.launchMarker checkForAndCreateLaunchMarker];
  | 
        
        
            | 
            | 
           287 | 
              | 
        
        
            | 
            | 
           288 | 
             FIRCLSInternalReport *report = [self setupCurrentReport:executionIdentifier];
  | 
        
        
            | 
            | 
           289 | 
             if (!report) {
  | 
        
        
            | 
            | 
           290 | 
               FIRCLSErrorLog(@"Unable to setup a new report");
  | 
        
        
            | 
            | 
           291 | 
             }
  | 
        
        
            | 
            | 
           292 | 
              | 
        
        
            | 
            | 
           293 | 
             if (![self startCrashReporterWithProfilingMark:mark report:report]) {
  | 
        
        
            | 
            | 
           294 | 
               FIRCLSErrorLog(@"Unable to start crash reporter");
  | 
        
        
            | 
            | 
           295 | 
               report = nil;
  | 
        
        
            | 
            | 
           296 | 
             }
  | 
        
        
            | 
            | 
           297 | 
              | 
        
        
            | 
            | 
           298 | 
           #if CLS_METRICKIT_SUPPORTED
  | 
        
        
            | 
            | 
           299 | 
             if (@available(iOS 15, *)) {
  | 
        
        
            | 
            | 
           300 | 
               if (self.settings.metricKitCollectionEnabled) {
  | 
        
        
            | 
            | 
           301 | 
                 [self.metricKitManager registerMetricKitManager];
  | 
        
        
            | 
            | 
           302 | 
               }
  | 
        
        
            | 
            | 
           303 | 
             }
  | 
        
        
            | 
            | 
           304 | 
           #endif
  | 
        
        
            | 
            | 
           305 | 
              | 
        
        
            | 
            | 
           306 | 
             FBLPromise<NSNumber *> *promise;
  | 
        
        
            | 
            | 
           307 | 
              | 
        
        
            | 
            | 
           308 | 
             if ([self.dataArbiter isCrashlyticsCollectionEnabled]) {
  | 
        
        
            | 
            | 
           309 | 
               FIRCLSDebugLog(@"Automatic data collection is enabled.");
  | 
        
        
            | 
            | 
           310 | 
               FIRCLSDebugLog(@"Unsent reports will be uploaded at startup");
  | 
        
        
            | 
            | 
           311 | 
               FIRCLSDataCollectionToken *dataCollectionToken = [FIRCLSDataCollectionToken validToken];
  | 
        
        
            | 
            | 
           312 | 
              | 
        
        
            | 
            | 
           313 | 
               [self beginSettingsWithToken:dataCollectionToken];
  | 
        
        
            | 
            | 
           314 | 
              | 
        
        
            | 
            | 
           315 | 
               // Wait for MetricKit data to be available, then continue to send reports and resolve promise.
  | 
        
        
            | 
            | 
           316 | 
               promise = [[self waitForMetricKitData]
  | 
        
        
            | 
            | 
           317 | 
                   onQueue:_dispatchQueue
  | 
        
        
            | 
            | 
           318 | 
                      then:^id _Nullable(id _Nullable metricKitValue) {
  | 
        
        
            | 
            | 
           319 | 
                        [self beginReportUploadsWithToken:dataCollectionToken blockingSend:launchFailure];
  | 
        
        
            | 
            | 
           320 | 
              | 
        
        
            | 
            | 
           321 | 
                        // If data collection is enabled, the SDK will not notify the user
  | 
        
        
            | 
            | 
           322 | 
                        // when unsent reports are available, or respect Send / DeleteUnsentReports
  | 
        
        
            | 
            | 
           323 | 
                        [self->_unsentReportsAvailable fulfill:nil];
  | 
        
        
            | 
            | 
           324 | 
                        return @(report != nil);
  | 
        
        
            | 
            | 
           325 | 
                      }];
  | 
        
        
            | 
            | 
           326 | 
             } else {
  | 
        
        
            | 
            | 
           327 | 
               FIRCLSDebugLog(@"Automatic data collection is disabled.");
  | 
        
        
            | 
            | 
           328 | 
               FIRCLSDebugLog(@"[Crashlytics:Crash] %d unsent reports are available. Waiting for "
  | 
        
        
            | 
            | 
           329 | 
                              @"send/deleteUnsentReports to be called.",
  | 
        
        
            | 
            | 
           330 | 
                              self.existingReportManager.unsentReportsCount);
  | 
        
        
            | 
            | 
           331 | 
              | 
        
        
            | 
            | 
           332 | 
               // Wait for an action to get sent, either from processReports: or automatic data collection,
  | 
        
        
            | 
            | 
           333 | 
               // and for MetricKit data to be available.
  | 
        
        
            | 
            | 
           334 | 
               promise = [[FBLPromise all:@[ [self waitForReportAction], [self waitForMetricKitData] ]]
  | 
        
        
            | 
            | 
           335 | 
                   onQueue:_dispatchQueue
  | 
        
        
            | 
            | 
           336 | 
                      then:^id _Nullable(NSArray *_Nullable wrappedActionAndData) {
  | 
        
        
            | 
            | 
           337 | 
                        // Process the actions for the reports on disk.
  | 
        
        
            | 
            | 
           338 | 
                        FIRCLSReportAction action = [[wrappedActionAndData firstObject] reportActionValue];
  | 
        
        
            | 
            | 
           339 | 
              | 
        
        
            | 
            | 
           340 | 
                        if (action == FIRCLSReportActionSend) {
  | 
        
        
            | 
            | 
           341 | 
                          FIRCLSDebugLog(@"Sending unsent reports.");
  | 
        
        
            | 
            | 
           342 | 
                          FIRCLSDataCollectionToken *dataCollectionToken =
  | 
        
        
            | 
            | 
           343 | 
                              [FIRCLSDataCollectionToken validToken];
  | 
        
        
            | 
            | 
           344 | 
              | 
        
        
            | 
            | 
           345 | 
                          [self beginSettingsWithToken:dataCollectionToken];
  | 
        
        
            | 
            | 
           346 | 
              | 
        
        
            | 
            | 
           347 | 
                          [self beginReportUploadsWithToken:dataCollectionToken blockingSend:NO];
  | 
        
        
            | 
            | 
           348 | 
              | 
        
        
            | 
            | 
           349 | 
                        } else if (action == FIRCLSReportActionDelete) {
  | 
        
        
            | 
            | 
           350 | 
                          FIRCLSDebugLog(@"Deleting unsent reports.");
  | 
        
        
            | 
            | 
           351 | 
                          [self.existingReportManager deleteUnsentReports];
  | 
        
        
            | 
            | 
           352 | 
                        } else {
  | 
        
        
            | 
            | 
           353 | 
                          FIRCLSErrorLog(@"Unknown report action: %d", action);
  | 
        
        
            | 
            | 
           354 | 
                        }
  | 
        
        
            | 
            | 
           355 | 
                        return @(report != nil);
  | 
        
        
            | 
            | 
           356 | 
                      }];
  | 
        
        
            | 
            | 
           357 | 
             }
  | 
        
        
            | 
            | 
           358 | 
              | 
        
        
            | 
            | 
           359 | 
             if (report != nil) {
  | 
        
        
            | 
            | 
           360 | 
               // capture the start-up time here, but record it asynchronously
  | 
        
        
            | 
            | 
           361 | 
               double endMark = FIRCLSProfileEnd(mark);
  | 
        
        
            | 
            | 
           362 | 
              | 
        
        
            | 
            | 
           363 | 
               dispatch_async(FIRCLSGetLoggingQueue(), ^{
  | 
        
        
            | 
            | 
           364 | 
                 FIRCLSUserLoggingWriteInternalKeyValue(FIRCLSStartTimeKey, [@(endMark) description]);
  | 
        
        
            | 
            | 
           365 | 
               });
  | 
        
        
            | 
            | 
           366 | 
             }
  | 
        
        
            | 
            | 
           367 | 
              | 
        
        
            | 
            | 
           368 | 
             // To make the code more predictable and therefore testable, don't resolve the startup promise
  | 
        
        
            | 
            | 
           369 | 
             // until the operations that got queued up for processing reports have been processed through the
  | 
        
        
            | 
            | 
           370 | 
             // work queue.
  | 
        
        
            | 
            | 
           371 | 
             NSOperationQueue *__weak queue = _operationQueue;
  | 
        
        
            | 
            | 
           372 | 
             FBLPromise *__weak unsentReportsHandled = _unsentReportsHandled;
  | 
        
        
            | 
            | 
           373 | 
             promise = [promise then:^id _Nullable(NSNumber *_Nullable value) {
  | 
        
        
            | 
            | 
           374 | 
               FBLPromise *allOpsFinished = [FBLPromise pendingPromise];
  | 
        
        
            | 
            | 
           375 | 
               [queue addOperationWithBlock:^{
  | 
        
        
            | 
            | 
           376 | 
                 [allOpsFinished fulfill:nil];
  | 
        
        
            | 
            | 
           377 | 
               }];
  | 
        
        
            | 
            | 
           378 | 
              | 
        
        
            | 
            | 
           379 | 
               return [allOpsFinished onQueue:dispatch_get_main_queue()
  | 
        
        
            | 
            | 
           380 | 
                                         then:^id _Nullable(id _Nullable allOpsFinishedValue) {
  | 
        
        
            | 
            | 
           381 | 
                                           // Signal that to callers of processReports that everything is
  | 
        
        
            | 
            | 
           382 | 
                                           // finished.
  | 
        
        
            | 
            | 
           383 | 
                                           [unsentReportsHandled fulfill:nil];
  | 
        
        
            | 
            | 
           384 | 
                                           return value;
  | 
        
        
            | 
            | 
           385 | 
                                         }];
  | 
        
        
            | 
            | 
           386 | 
             }];
  | 
        
        
            | 
            | 
           387 | 
              | 
        
        
            | 
            | 
           388 | 
             return promise;
  | 
        
        
            | 
            | 
           389 | 
           }
  | 
        
        
            | 
            | 
           390 | 
              | 
        
        
            | 
            | 
           391 | 
           - (void)beginSettingsWithToken:(FIRCLSDataCollectionToken *)token {
  | 
        
        
            | 
            | 
           392 | 
             if (self.settings.isCacheExpired) {
  | 
        
        
            | 
            | 
           393 | 
               // This method can be called more than once if the user calls
  | 
        
        
            | 
            | 
           394 | 
               // SendUnsentReports again, so don't repeat the settings fetch
  | 
        
        
            | 
            | 
           395 | 
               static dispatch_once_t settingsFetchOnceToken;
  | 
        
        
            | 
            | 
           396 | 
               dispatch_once(&settingsFetchOnceToken, ^{
  | 
        
        
            | 
            | 
           397 | 
                 [self.settingsManager beginSettingsWithGoogleAppId:self.googleAppID token:token];
  | 
        
        
            | 
            | 
           398 | 
               });
  | 
        
        
            | 
            | 
           399 | 
             }
  | 
        
        
            | 
            | 
           400 | 
           }
  | 
        
        
            | 
            | 
           401 | 
              | 
        
        
            | 
            | 
           402 | 
           - (void)beginReportUploadsWithToken:(FIRCLSDataCollectionToken *)token
  | 
        
        
            | 
            | 
           403 | 
                                  blockingSend:(BOOL)blockingSend {
  | 
        
        
            | 
            | 
           404 | 
             if (self.settings.collectReportsEnabled) {
  | 
        
        
            | 
            | 
           405 | 
               [self.existingReportManager sendUnsentReportsWithToken:token asUrgent:blockingSend];
  | 
        
        
            | 
            | 
           406 | 
              | 
        
        
            | 
            | 
           407 | 
             } else {
  | 
        
        
            | 
            | 
           408 | 
               FIRCLSInfoLog(@"Collect crash reports is disabled");
  | 
        
        
            | 
            | 
           409 | 
               [self.existingReportManager deleteUnsentReports];
  | 
        
        
            | 
            | 
           410 | 
             }
  | 
        
        
            | 
            | 
           411 | 
           }
  | 
        
        
            | 
            | 
           412 | 
              | 
        
        
            | 
            | 
           413 | 
           - (BOOL)startCrashReporterWithProfilingMark:(FIRCLSProfileMark)mark
  | 
        
        
            | 
            | 
           414 | 
                                                report:(FIRCLSInternalReport *)report {
  | 
        
        
            | 
            | 
           415 | 
             if (!report) {
  | 
        
        
            | 
            | 
           416 | 
               return NO;
  | 
        
        
            | 
            | 
           417 | 
             }
  | 
        
        
            | 
            | 
           418 | 
              | 
        
        
            | 
            | 
           419 | 
             if (!FIRCLSContextInitialize(report, self.settings, _fileManager)) {
  | 
        
        
            | 
            | 
           420 | 
               return NO;
  | 
        
        
            | 
            | 
           421 | 
             }
  | 
        
        
            | 
            | 
           422 | 
              | 
        
        
            | 
            | 
           423 | 
             [self.notificationManager registerNotificationListener];
  | 
        
        
            | 
            | 
           424 | 
              | 
        
        
            | 
            | 
           425 | 
             [self.analyticsManager registerAnalyticsListener];
  | 
        
        
            | 
            | 
           426 | 
              | 
        
        
            | 
            | 
           427 | 
             [self crashReportingSetupCompleted:mark];
  | 
        
        
            | 
            | 
           428 | 
              | 
        
        
            | 
            | 
           429 | 
             return YES;
  | 
        
        
            | 
            | 
           430 | 
           }
  | 
        
        
            | 
            | 
           431 | 
              | 
        
        
            | 
            | 
           432 | 
           - (void)crashReportingSetupCompleted:(FIRCLSProfileMark)mark {
  | 
        
        
            | 
            | 
           433 | 
             // check our handlers
  | 
        
        
            | 
            | 
           434 | 
             FIRCLSDispatchAfter(2.0, dispatch_get_main_queue(), ^{
  | 
        
        
            | 
            | 
           435 | 
               FIRCLSExceptionCheckHandlers((__bridge void *)(self));
  | 
        
        
            | 
            | 
           436 | 
           #if CLS_SIGNAL_SUPPORTED
  | 
        
        
            | 
            | 
           437 | 
               FIRCLSSignalCheckHandlers();
  | 
        
        
            | 
            | 
           438 | 
           #endif
  | 
        
        
            | 
            | 
           439 | 
           #if CLS_MACH_EXCEPTION_SUPPORTED
  | 
        
        
            | 
            | 
           440 | 
               FIRCLSMachExceptionCheckHandlers();
  | 
        
        
            | 
            | 
           441 | 
           #endif
  | 
        
        
            | 
            | 
           442 | 
             });
  | 
        
        
            | 
            | 
           443 | 
              | 
        
        
            | 
            | 
           444 | 
             // remove the launch failure marker and record the startup time
  | 
        
        
            | 
            | 
           445 | 
             dispatch_async(dispatch_get_main_queue(), ^{
  | 
        
        
            | 
            | 
           446 | 
               [self.launchMarker removeLaunchFailureMarker];
  | 
        
        
            | 
            | 
           447 | 
               dispatch_async(FIRCLSGetLoggingQueue(), ^{
  | 
        
        
            | 
            | 
           448 | 
                 FIRCLSUserLoggingWriteInternalKeyValue(FIRCLSFirstRunloopTurnTimeKey,
  | 
        
        
            | 
            | 
           449 | 
                                                        [@(FIRCLSProfileEnd(mark)) description]);
  | 
        
        
            | 
            | 
           450 | 
               });
  | 
        
        
            | 
            | 
           451 | 
             });
  | 
        
        
            | 
            | 
           452 | 
           }
  | 
        
        
            | 
            | 
           453 | 
              | 
        
        
            | 
            | 
           454 | 
           - (BOOL)validateAppIdentifiers {
  | 
        
        
            | 
            | 
           455 | 
             // When the ApplicationIdentifierModel fails to initialize, it is usually due to
  | 
        
        
            | 
            | 
           456 | 
             // failing computeExecutableInfo. This can happen if the user sets the
  | 
        
        
            | 
            | 
           457 | 
             // Exported Symbols File in Build Settings, and leaves off the one symbol
  | 
        
        
            | 
            | 
           458 | 
             // that Crashlytics needs, "__mh_execute_header" (wich is defined in mach-o/ldsyms.h as
  | 
        
        
            | 
            | 
           459 | 
             // _MH_EXECUTE_SYM). From https://github.com/firebase/firebase-ios-sdk/issues/5020
  | 
        
        
            | 
            | 
           460 | 
             if (!self.appIDModel) {
  | 
        
        
            | 
            | 
           461 | 
               FIRCLSErrorLog(@"Crashlytics could not find the symbol for the app's main function and cannot "
  | 
        
        
            | 
            | 
           462 | 
                              @"start up. This can be resolved 2 ways depending on your setup:\n 1. If you "
  | 
        
        
            | 
            | 
           463 | 
                              @"have Exported Symbols File set in your Build Settings, add "
  | 
        
        
            | 
            | 
           464 | 
                              @"\"__mh_execute_header\" as a newline in your Exported Symbols File.\n 2. If "
  | 
        
        
            | 
            | 
           465 | 
                              @"you have -exported_symbols_list in your linker flags, remove it.");
  | 
        
        
            | 
            | 
           466 | 
               return NO;
  | 
        
        
            | 
            | 
           467 | 
             }
  | 
        
        
            | 
            | 
           468 | 
              | 
        
        
            | 
            | 
           469 | 
             if (self.appIDModel.bundleID.length == 0) {
  | 
        
        
            | 
            | 
           470 | 
               FIRCLSErrorLog(@"An application must have a valid bundle identifier in its Info.plist");
  | 
        
        
            | 
            | 
           471 | 
               return NO;
  | 
        
        
            | 
            | 
           472 | 
             }
  | 
        
        
            | 
            | 
           473 | 
              | 
        
        
            | 
            | 
           474 | 
             if ([self.dataArbiter isLegacyDataCollectionKeyInPlist]) {
  | 
        
        
            | 
            | 
           475 | 
               FIRCLSErrorLog(@"Found legacy data collection key in app's Info.plist: "
  | 
        
        
            | 
            | 
           476 | 
                              @"firebase_crashlytics_collection_enabled");
  | 
        
        
            | 
            | 
           477 | 
               FIRCLSErrorLog(@"Please update your Info.plist to use the new data collection key: "
  | 
        
        
            | 
            | 
           478 | 
                              @"FirebaseCrashlyticsCollectionEnabled");
  | 
        
        
            | 
            | 
           479 | 
               FIRCLSErrorLog(@"The legacy data collection Info.plist value could be overridden by "
  | 
        
        
            | 
            | 
           480 | 
                              @"calling: [Fabric with:...]");
  | 
        
        
            | 
            | 
           481 | 
               FIRCLSErrorLog(@"The new value can be overridden by calling: [[FIRCrashlytics "
  | 
        
        
            | 
            | 
           482 | 
                              @"crashlytics] setCrashlyticsCollectionEnabled:<isEnabled>]");
  | 
        
        
            | 
            | 
           483 | 
              | 
        
        
            | 
            | 
           484 | 
               return NO;
  | 
        
        
            | 
            | 
           485 | 
             }
  | 
        
        
            | 
            | 
           486 | 
              | 
        
        
            | 
            | 
           487 | 
             return YES;
  | 
        
        
            | 
            | 
           488 | 
           }
  | 
        
        
            | 
            | 
           489 | 
              | 
        
        
            | 
            | 
           490 | 
           - (FIRCLSInternalReport *)setupCurrentReport:(NSString *)executionIdentifier {
  | 
        
        
            | 
            | 
           491 | 
             [self.launchMarker createLaunchFailureMarker];
  | 
        
        
            | 
            | 
           492 | 
              | 
        
        
            | 
            | 
           493 | 
             NSString *reportPath = [_fileManager setupNewPathForExecutionIdentifier:executionIdentifier];
  | 
        
        
            | 
            | 
           494 | 
              | 
        
        
            | 
            | 
           495 | 
             return [[FIRCLSInternalReport alloc] initWithPath:reportPath
  | 
        
        
            | 
            | 
           496 | 
                                           executionIdentifier:executionIdentifier];
  | 
        
        
            | 
            | 
           497 | 
           }
  | 
        
        
            | 
            | 
           498 | 
              | 
        
        
            | 
            | 
           499 | 
           @end
  |