FIRCLSDataCollectionArbiter.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. #import "Crashlytics/Crashlytics/DataCollection/FIRCLSDataCollectionArbiter.h"
  15. #if __has_include(<FBLPromises/FBLPromises.h>)
  16. #import <FBLPromises/FBLPromises.h>
  17. #else
  18. #import "FBLPromises.h"
  19. #endif
  20. #import "FirebaseCore/Extension/FirebaseCoreInternal.h"
  21. #import "Crashlytics/Crashlytics/FIRCLSUserDefaults/FIRCLSUserDefaults.h"
  22. // The legacy data collection setting allows Fabric customers to turn off auto-
  23. // initialization, but can be overridden by calling [Fabric with:].
  24. //
  25. // While we support Fabric, we must have two different versions, because
  26. // they require these slightly different semantics.
  27. NSString *const FIRCLSLegacyCrashlyticsCollectionKey = @"firebase_crashlytics_collection_enabled";
  28. // The new data collection setting can be set by an API that is stored in FIRCLSUserDefaults
  29. NSString *const FIRCLSDataCollectionEnabledKey = @"com.crashlytics.data_collection";
  30. // The new data collection setting also allows Firebase customers to turn off data
  31. // collection in their Info.plist, and can be overridden by setting it to true using
  32. // the setCrashlyticsCollectionEnabled API.
  33. NSString *const FIRCLSCrashlyticsCollectionKey = @"FirebaseCrashlyticsCollectionEnabled";
  34. typedef NS_ENUM(NSInteger, FIRCLSDataCollectionSetting) {
  35. FIRCLSDataCollectionSettingNotSet = 0,
  36. FIRCLSDataCollectionSettingEnabled = 1,
  37. FIRCLSDataCollectionSettingDisabled = 2,
  38. };
  39. @interface FIRCLSDataCollectionArbiter () {
  40. NSLock *_mutex;
  41. FBLPromise *_dataCollectionEnabled;
  42. BOOL _promiseResolved;
  43. FIRApp *_app;
  44. NSDictionary *_appInfo;
  45. }
  46. @end
  47. @implementation FIRCLSDataCollectionArbiter
  48. - (instancetype)initWithApp:(FIRApp *)app withAppInfo:(NSDictionary *)dict {
  49. self = [super init];
  50. if (self) {
  51. _mutex = [[NSLock alloc] init];
  52. _appInfo = dict;
  53. _app = app;
  54. if ([FIRCLSDataCollectionArbiter isCrashlyticsCollectionEnabledWithApp:app withAppInfo:dict]) {
  55. _dataCollectionEnabled = [FBLPromise resolvedWith:nil];
  56. _promiseResolved = YES;
  57. } else {
  58. _dataCollectionEnabled = [FBLPromise pendingPromise];
  59. _promiseResolved = NO;
  60. }
  61. }
  62. return self;
  63. }
  64. /*
  65. * Legacy collection key that we provide for customers to disable Crash reporting.
  66. * Customers can later turn on Crashlytics using Fabric.with if they choose to do so.
  67. *
  68. * This flag is unsupported for the "New SDK"
  69. */
  70. - (BOOL)isLegacyDataCollectionKeyInPlist {
  71. if ([_appInfo objectForKey:FIRCLSLegacyCrashlyticsCollectionKey]) {
  72. return true;
  73. }
  74. return false;
  75. }
  76. // This functionality is called in the initializer before self is fully initialized,
  77. // so a class method is used. The instance method below allows for a consistent clean API.
  78. + (BOOL)isCrashlyticsCollectionEnabledWithApp:(FIRApp *)app withAppInfo:(NSDictionary *)dict {
  79. FIRCLSDataCollectionSetting stickySetting = [FIRCLSDataCollectionArbiter stickySetting];
  80. if (stickySetting != FIRCLSDataCollectionSettingNotSet) {
  81. return stickySetting == FIRCLSDataCollectionSettingEnabled;
  82. }
  83. id firebaseCrashlyticsCollectionEnabled = [dict objectForKey:FIRCLSCrashlyticsCollectionKey];
  84. if ([firebaseCrashlyticsCollectionEnabled isKindOfClass:[NSString class]] ||
  85. [firebaseCrashlyticsCollectionEnabled isKindOfClass:[NSNumber class]]) {
  86. return [firebaseCrashlyticsCollectionEnabled boolValue];
  87. }
  88. return [app isDataCollectionDefaultEnabled];
  89. }
  90. - (BOOL)isCrashlyticsCollectionEnabled {
  91. return [FIRCLSDataCollectionArbiter isCrashlyticsCollectionEnabledWithApp:_app
  92. withAppInfo:_appInfo];
  93. }
  94. - (void)setCrashlyticsCollectionEnabled:(BOOL)enabled {
  95. FIRCLSUserDefaults *userDefaults = [FIRCLSUserDefaults standardUserDefaults];
  96. FIRCLSDataCollectionSetting setting =
  97. enabled ? FIRCLSDataCollectionSettingEnabled : FIRCLSDataCollectionSettingDisabled;
  98. [userDefaults setInteger:setting forKey:FIRCLSDataCollectionEnabledKey];
  99. [userDefaults synchronize];
  100. [_mutex lock];
  101. if (enabled) {
  102. if (!_promiseResolved) {
  103. [_dataCollectionEnabled fulfill:nil];
  104. _promiseResolved = YES;
  105. }
  106. } else {
  107. if (_promiseResolved) {
  108. _dataCollectionEnabled = [FBLPromise pendingPromise];
  109. _promiseResolved = NO;
  110. }
  111. }
  112. [_mutex unlock];
  113. }
  114. + (FIRCLSDataCollectionSetting)stickySetting {
  115. FIRCLSUserDefaults *userDefaults = [FIRCLSUserDefaults standardUserDefaults];
  116. return [userDefaults integerForKey:FIRCLSDataCollectionEnabledKey];
  117. }
  118. - (FBLPromise *)waitForCrashlyticsCollectionEnabled {
  119. FBLPromise *result = nil;
  120. [_mutex lock];
  121. result = _dataCollectionEnabled;
  122. [_mutex unlock];
  123. return result;
  124. }
  125. @end