Proyectos de Subversion Iphone Microlearning

Rev

| Ultima modificación | Ver Log |

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