Skip to content

Commit 8de5cb2

Browse files
authored
Wrap diagnostics notification in collection flag check. (#1979)
* Wrap diagnostics notification in collection flag check. Some of the diagnostics notifications were missed and not covered by the data collection flag. * Remove redundant notification call, move Core diagnostics API call. * Removed configure with no options test.
1 parent 5cfe567 commit 8de5cb2

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

Example/Core/Tests/FIRAppTest.m

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -690,8 +690,6 @@ - (void)testGlobalDataCollectionClearedAfterDelete {
690690
}
691691

692692
- (void)testGlobalDataCollectionNoDiagnosticsSent {
693-
[FIRApp configure];
694-
695693
// Add an observer for the diagnostics notification - both with and without an object to ensure it
696694
// catches it either way. Currently no object is sent, but in the future that could change.
697695
[self.notificationCenter addMockObserver:self.observerMock
@@ -707,6 +705,9 @@ - (void)testGlobalDataCollectionNoDiagnosticsSent {
707705
OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY])
708706
.andReturn(@NO);
709707

708+
// Ensure configure doesn't fire a notification.
709+
[FIRApp configure];
710+
710711
NSError *error = [NSError errorWithDomain:@"com.firebase" code:42 userInfo:nil];
711712
[[FIRApp defaultApp] sendLogsWithServiceName:@"Service" version:@"Version" error:error];
712713

Firebase/Core/FIRApp.m

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,19 @@ @implementation FIRApp
107107
+ (void)configure {
108108
FIROptions *options = [FIROptions defaultOptions];
109109
if (!options) {
110-
[[NSNotificationCenter defaultCenter]
111-
postNotificationName:kFIRAppDiagnosticsNotification
112-
object:nil
113-
userInfo:@{
114-
kFIRAppDiagnosticsConfigurationTypeKey : @(FIRConfigTypeCore),
115-
kFIRAppDiagnosticsErrorKey : [FIRApp errorForMissingOptions]
116-
}];
110+
// Read the Info.plist to see if the flag is set. At this point we can't check any user defaults
111+
// since the app isn't configured at all, so only rely on the Info.plist value.
112+
NSNumber *collectionEnabledPlistValue = [[self class] readDataCollectionSwitchFromPlist];
113+
if (!collectionEnabledPlistValue || [collectionEnabledPlistValue boolValue]) {
114+
[[NSNotificationCenter defaultCenter]
115+
postNotificationName:kFIRAppDiagnosticsNotification
116+
object:nil
117+
userInfo:@{
118+
kFIRAppDiagnosticsConfigurationTypeKey : @(FIRConfigTypeCore),
119+
kFIRAppDiagnosticsErrorKey : [FIRApp errorForMissingOptions]
120+
}];
121+
}
122+
117123
[NSException raise:kFirebaseCoreErrorDomain
118124
format:
119125
@"`[FIRApp configure];` (`FirebaseApp.configure()` in Swift) could not find "
@@ -274,13 +280,6 @@ + (void)addAppToAppDictionary:(FIRApp *)app {
274280
}
275281
if ([app configureCore]) {
276282
sAllApps[app.name] = app;
277-
[[NSNotificationCenter defaultCenter]
278-
postNotificationName:kFIRAppDiagnosticsNotification
279-
object:nil
280-
userInfo:@{
281-
kFIRAppDiagnosticsConfigurationTypeKey : @(FIRConfigTypeCore),
282-
kFIRAppDiagnosticsFIRAppKey : app
283-
}];
284283
} else {
285284
[NSException raise:kFirebaseCoreErrorDomain
286285
format:
@@ -317,7 +316,7 @@ - (void)getTokenForcingRefresh:(BOOL)forceRefresh withCallback:(FIRTokenCallback
317316
- (BOOL)configureCore {
318317
[self checkExpectedBundleID];
319318
if (![self isAppIDValid]) {
320-
if (_options.usingOptionsFromDefaultPlist) {
319+
if (_options.usingOptionsFromDefaultPlist && [self isDataCollectionDefaultEnabled]) {
321320
[[NSNotificationCenter defaultCenter]
322321
postNotificationName:kFIRAppDiagnosticsNotification
323322
object:nil
@@ -329,6 +328,16 @@ - (BOOL)configureCore {
329328
return NO;
330329
}
331330

331+
if ([self isDataCollectionDefaultEnabled]) {
332+
[[NSNotificationCenter defaultCenter]
333+
postNotificationName:kFIRAppDiagnosticsNotification
334+
object:nil
335+
userInfo:@{
336+
kFIRAppDiagnosticsConfigurationTypeKey : @(FIRConfigTypeCore),
337+
kFIRAppDiagnosticsFIRAppKey : self
338+
}];
339+
}
340+
332341
#if TARGET_OS_IOS
333342
// Initialize the Analytics once there is a valid options under default app. Analytics should
334343
// always initialize first by itself before the other SDKs.

0 commit comments

Comments
 (0)