Skip to content

Commit 3754b7e

Browse files
authored
Do not continue with fetch if we do not get a valid IID. (#4745)
* Do not continue with fetch if we do not get a valid IID. * Address review changes to test file. * Update test file based on review comments. * Fix warnings. * Fix build. * Fix build.
1 parent 1d2dbc4 commit 3754b7e

File tree

3 files changed

+436
-36
lines changed

3 files changed

+436
-36
lines changed

FirebaseRemoteConfig/Sources/RCNFetch.m

Lines changed: 53 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -197,58 +197,76 @@ - (void)fetchConfigWithExpirationDuration:(NSTimeInterval)expirationDuration
197197

198198
#pragma mark - Fetch helpers
199199

200-
/// Refresh instance ID token before fetching config. Instance ID is an optional field in config
201-
/// request.
200+
/// Refresh instance ID token before fetching config. Instance ID is now mandatory for fetch
201+
/// requests to work.(b/14751422).
202202
- (void)refreshInstanceIDTokenAndFetchCheckInInfoWithCompletionHandler:
203203
(FIRRemoteConfigFetchCompletion)completionHandler {
204204
FIRInstanceID *instanceID = [FIRInstanceID instanceID];
205-
// Only refresh instance ID when a valid sender ID is provided. If not, continue without
206-
// fetching instance ID. Instance ID is for data analytics purpose, which is only optional for
207-
// config fetching.
208205
if (!_options.GCMSenderID) {
209-
[self fetchCheckinInfoWithCompletionHandler:completionHandler];
210-
return;
206+
NSString *errorDescription = @"Failed to get GCMSenderID";
207+
FIRLogError(kFIRLoggerRemoteConfig, @"I-RCN000074", @"%@",
208+
[NSString stringWithFormat:@"%@", errorDescription]);
209+
return [self
210+
reportCompletionOnHandler:completionHandler
211+
withStatus:FIRRemoteConfigFetchStatusFailure
212+
withError:[NSError errorWithDomain:FIRRemoteConfigErrorDomain
213+
code:FIRRemoteConfigErrorInternalError
214+
userInfo:@{
215+
NSLocalizedDescriptionKey : errorDescription
216+
}]];
211217
}
212218
FIRInstanceIDTokenHandler instanceIDHandler = ^(NSString *token, NSError *error) {
213-
if (error) {
214-
FIRLogError(kFIRLoggerRemoteConfig, @"I-RCN000020",
215-
@"Failed to register InstanceID with error : %@.", error);
219+
if (!token || error) {
220+
NSString *errorDescription =
221+
[NSString stringWithFormat:@"Failed to get InstanceID token. Error : %@.", error];
222+
FIRLogError(kFIRLoggerRemoteConfig, @"I-RCN000073", @"%@",
223+
[NSString stringWithFormat:@"%@", errorDescription]);
224+
return [self
225+
reportCompletionOnHandler:completionHandler
226+
withStatus:FIRRemoteConfigFetchStatusFailure
227+
withError:[NSError errorWithDomain:FIRRemoteConfigErrorDomain
228+
code:FIRRemoteConfigErrorInternalError
229+
userInfo:@{
230+
NSLocalizedDescriptionKey : errorDescription
231+
}]];
216232
}
217233

218234
// If the token is available, try to get the instanceID.
219235
__weak RCNConfigFetch *weakSelf = self;
220-
if (token) {
221-
[instanceID getIDWithHandler:^(NSString *_Nullable identity, NSError *_Nullable error) {
222-
RCNConfigFetch *strongSelf = weakSelf;
223-
224-
// Dispatch to the RC serial queue to update settings on the queue.
225-
dispatch_async(strongSelf->_lockQueue, ^{
226-
RCNConfigFetch *strongSelfQueue = weakSelf;
236+
[instanceID getIDWithHandler:^(NSString *_Nullable identity, NSError *_Nullable error) {
237+
RCNConfigFetch *strongSelf = weakSelf;
227238

228-
// Update config settings with the IID and token.
229-
strongSelfQueue->_settings.configInstanceIDToken = [token copy];
230-
strongSelfQueue->_settings.configInstanceID = identity;
239+
// Dispatch to the RC serial queue to update settings on the queue.
240+
dispatch_async(strongSelf->_lockQueue, ^{
241+
RCNConfigFetch *strongSelfQueue = weakSelf;
231242

232-
if (identity && !error) {
233-
FIRLogInfo(kFIRLoggerRemoteConfig, @"I-RCN000022", @"Success to get iid : %@.",
234-
strongSelfQueue->_settings.configInstanceID);
235-
} else {
236-
FIRLogWarning(kFIRLoggerRemoteConfig, @"I-RCN000055", @"Error getting iid : %@.",
237-
error);
238-
}
243+
// Update config settings with the IID and token.
244+
strongSelfQueue->_settings.configInstanceIDToken = [token copy];
245+
strongSelfQueue->_settings.configInstanceID = identity;
246+
247+
if (!identity || error) {
248+
NSString *errorDescription =
249+
[NSString stringWithFormat:@"Error getting iid : %@.", error];
250+
FIRLogError(kFIRLoggerRemoteConfig, @"I-RCN000055", @"%@",
251+
[NSString stringWithFormat:@"%@", errorDescription]);
252+
return [self
253+
reportCompletionOnHandler:completionHandler
254+
withStatus:FIRRemoteConfigFetchStatusFailure
255+
withError:[NSError
256+
errorWithDomain:FIRRemoteConfigErrorDomain
257+
code:FIRRemoteConfigErrorInternalError
258+
userInfo:@{
259+
NSLocalizedDescriptionKey : errorDescription
260+
}]];
261+
}
239262

240-
// Continue the fetch regardless of whether fetch of instance ID succeeded.
241-
[strongSelfQueue fetchCheckinInfoWithCompletionHandler:completionHandler];
242-
});
243-
}];
263+
FIRLogInfo(kFIRLoggerRemoteConfig, @"I-RCN000022", @"Success to get iid : %@.",
264+
strongSelfQueue->_settings.configInstanceID);
244265

245-
} else {
246-
dispatch_async(self->_lockQueue, ^{
247-
RCNConfigFetch *strongSelfQueue = weakSelf;
248266
// Continue the fetch regardless of whether fetch of instance ID succeeded.
249267
[strongSelfQueue fetchCheckinInfoWithCompletionHandler:completionHandler];
250268
});
251-
}
269+
}];
252270
};
253271
FIRLogDebug(kFIRLoggerRemoteConfig, @"I-RCN000039", @"Starting requesting token.");
254272
// Note: We expect the GCMSenderID to always be available by the time this request is made.

0 commit comments

Comments
 (0)