Skip to content

Commit c3f8bc9

Browse files
authored
[Auth] Use async flush method to get header value (#13853)
1 parent 1e63a55 commit c3f8bc9

File tree

6 files changed

+36
-15
lines changed

6 files changed

+36
-15
lines changed

FirebaseAuth/Sources/Swift/Backend/AuthBackend.swift

+8-9
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ class AuthBackend {
9898
class func request(withURL url: URL,
9999
contentType: String,
100100
requestConfiguration: AuthRequestConfiguration) async -> URLRequest {
101+
// Kick off tasks for the async header values.
102+
async let heartbeatsHeaderValue = requestConfiguration.heartbeatLogger?.asyncHeaderValue()
103+
async let appCheckTokenHeaderValue = requestConfiguration.appCheck?
104+
.getToken(forcingRefresh: true)
105+
101106
var request = URLRequest(url: url)
102107
request.setValue(contentType, forHTTPHeaderField: "Content-Type")
103108
let additionalFrameworkMarker = requestConfiguration
@@ -106,13 +111,6 @@ class AuthBackend {
106111
request.setValue(clientVersion, forHTTPHeaderField: "X-Client-Version")
107112
request.setValue(Bundle.main.bundleIdentifier, forHTTPHeaderField: "X-Ios-Bundle-Identifier")
108113
request.setValue(requestConfiguration.appID, forHTTPHeaderField: "X-Firebase-GMPID")
109-
if let heartbeatLogger = requestConfiguration.heartbeatLogger {
110-
// The below call synchronously dispatches to a queue. To avoid blocking
111-
// the shared concurrency queue, `async let` will spawn the process on
112-
// a separate thread.
113-
async let heartbeatsHeaderValue = heartbeatLogger.headerValue()
114-
await request.setValue(heartbeatsHeaderValue, forHTTPHeaderField: "X-Firebase-Client")
115-
}
116114
request.httpMethod = requestConfiguration.httpMethod
117115
let preferredLocalizations = Bundle.main.preferredLocalizations
118116
if preferredLocalizations.count > 0 {
@@ -122,8 +120,9 @@ class AuthBackend {
122120
languageCode.count > 0 {
123121
request.setValue(languageCode, forHTTPHeaderField: "X-Firebase-Locale")
124122
}
125-
if let appCheck = requestConfiguration.appCheck {
126-
let tokenResult = await appCheck.getToken(forcingRefresh: false)
123+
// Wait for the async header values.
124+
await request.setValue(heartbeatsHeaderValue, forHTTPHeaderField: "X-Firebase-Client")
125+
if let tokenResult = await appCheckTokenHeaderValue {
127126
if let error = tokenResult.error {
128127
AuthLog.logWarning(code: "I-AUT000018",
129128
message: "Error getting App Check token; using placeholder " +

FirebaseAuth/Tests/Unit/AuthBackendRPCImplementationTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ class AuthBackendRPCImplementationTests: RPCBaseTests {
534534

535535
#if COCOAPODS || SWIFT_PACKAGE
536536
private class FakeHeartbeatLogger: NSObject, FIRHeartbeatLoggerProtocol {
537-
func headerValue() -> String? {
537+
func asyncHeaderValue() async -> String? {
538538
let payload = flushHeartbeatsIntoPayload()
539539
guard !payload.isEmpty else {
540540
return nil

FirebaseCore/Extension/FIRHeartbeatLogger.h

+10-5
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,19 @@ typedef NS_ENUM(NSInteger, FIRDailyHeartbeatCode) {
3535
/// Asynchronously logs a heartbeat.
3636
- (void)log;
3737

38-
#ifndef FIREBASE_BUILD_CMAKE
39-
/// Return the headerValue for the HeartbeatLogger.
40-
- (NSString *_Nullable)headerValue;
41-
#endif // FIREBASE_BUILD_CMAKE
42-
4338
/// Gets the heartbeat code for today.
4439
- (FIRDailyHeartbeatCode)heartbeatCodeForToday;
4540

41+
#ifndef FIREBASE_BUILD_CMAKE
42+
/// Returns the header value for the heartbeat logger via the given completion handler..
43+
- (void)asyncHeaderValueWithCompletionHandler:(void (^)(NSString *_Nullable))completionHandler
44+
API_AVAILABLE(ios(13.0), macosx(10.15), macCatalyst(13.0), tvos(13.0), watchos(6.0));
45+
46+
/// Return the header value for the heartbeat logger.
47+
- (NSString *_Nullable)
48+
headerValue NS_SWIFT_UNAVAILABLE("Use `asyncHeaderValue() async -> String?` instead.");
49+
#endif // FIREBASE_BUILD_CMAKE
50+
4651
@end
4752

4853
#ifndef FIREBASE_BUILD_CMAKE

FirebaseCore/Sources/FIRHeartbeatLogger.m

+7
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ - (NSString *_Nullable)headerValue {
7474
return FIRHeaderValueFromHeartbeatsPayload([self flushHeartbeatsIntoPayload]);
7575
}
7676

77+
- (void)asyncHeaderValueWithCompletionHandler:(void (^)(NSString *_Nullable))completionHandler
78+
API_AVAILABLE(ios(13.0), macosx(10.15), macCatalyst(13.0), tvos(13.0), watchos(6.0)) {
79+
[self flushHeartbeatsIntoPayloadWithCompletionHandler:^(FIRHeartbeatsPayload *payload) {
80+
completionHandler(FIRHeaderValueFromHeartbeatsPayload(payload));
81+
}];
82+
}
83+
7784
- (FIRHeartbeatsPayload *)flushHeartbeatsIntoPayload {
7885
FIRHeartbeatsPayload *payload = [_heartbeatController flush];
7986
return payload;

FirebaseInstallations/Source/Tests/Unit/FIRInstallationsAPIServiceTests.m

+5
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ - (NSString *_Nullable)headerValue {
6969
return FIRHeaderValueFromHeartbeatsPayload([self flushHeartbeatsIntoPayload]);
7070
}
7171

72+
- (void)asyncHeaderValueWithCompletionHandler:
73+
(nonnull void (^)(NSString *_Nullable))completionHandler {
74+
[self doesNotRecognizeSelector:_cmd];
75+
}
76+
7277
@end
7378

7479
#pragma mark - FIRInstallationsAPIService + Internal

FirebaseMessaging/Tests/UnitTests/FIRMessagingTokenOperationsTest.m

+5
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ - (NSString *_Nullable)headerValue {
102102
return @"unimplemented";
103103
}
104104

105+
- (void)asyncHeaderValueWithCompletionHandler:
106+
(nonnull void (^)(NSString *_Nullable))completionHandler {
107+
[self doesNotRecognizeSelector:_cmd];
108+
}
109+
105110
@end
106111

107112
#pragma mark - FIRMessagingTokenOperationsTest

0 commit comments

Comments
 (0)