Skip to content

Commit e80fade

Browse files
committed
Make app check token result sendable
1 parent 4774502 commit e80fade

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

FirebaseAI/Sources/GenerativeAIService.swift

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ struct GenerativeAIService {
213213
private func fetchAppCheckToken(appCheck: AppCheckInterop) async throws
214214
-> FIRAppCheckTokenResultInterop {
215215
if useLimitedUseAppCheckTokens {
216-
if let token = await appCheck.getLimitedUseToken?() {
216+
if let token = await getLimitedUseAppCheckToken(appCheck: appCheck) {
217217
return token
218218
}
219219

@@ -234,6 +234,25 @@ struct GenerativeAIService {
234234
return await appCheck.getToken(forcingRefresh: false)
235235
}
236236

237+
private func getLimitedUseAppCheckToken(appCheck: AppCheckInterop) async -> FIRAppCheckTokenResultInterop? {
238+
// At the moment, `await` doesn’t get along with Objective-C’s optional protocol methods.
239+
await withCheckedContinuation { (continuation: CheckedContinuation<FIRAppCheckTokenResultInterop?, Never>) in
240+
guard
241+
useLimitedUseAppCheckTokens,
242+
// `getLimitedUseToken(completion:)` is an optional protocol method. Optional binding
243+
// is performed to make sure `continuation` is called even if the method’s not implemented.
244+
let limitedUseTokenClosure = appCheck.getLimitedUseToken
245+
else {
246+
return continuation.resume(returning: nil)
247+
}
248+
249+
limitedUseTokenClosure { tokenResult in
250+
// The placeholder token should be used in the case of App Check error.
251+
continuation.resume(returning: tokenResult)
252+
}
253+
}
254+
}
255+
237256
private func httpResponse(urlResponse: URLResponse) throws -> HTTPURLResponse {
238257
// The following condition should always be true: "Whenever you make HTTP URL load requests, any
239258
// response objects you get back from the URLSession, NSURLConnection, or NSURLDownload class

FirebaseAI/Tests/Unit/Fakes/AppCheckInteropFake.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ class AppCheckInteropFake: NSObject, AppCheckInterop {
5252
fatalError("\(#function) not implemented.")
5353
}
5454

55-
private class AppCheckTokenResultInteropFake: NSObject, FIRAppCheckTokenResultInterop {
56-
var token: String
57-
var error: Error?
55+
private class AppCheckTokenResultInteropFake: NSObject, FIRAppCheckTokenResultInterop, @unchecked Sendable {
56+
let token: String
57+
let error: Error?
5858

5959
init(token: String, error: Error?) {
6060
self.token = token

FirebaseAppCheck/Interop/Public/FirebaseAppCheckInterop/FIRAppCheckTokenResultInterop.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
NS_ASSUME_NONNULL_BEGIN
2020

21+
NS_SWIFT_SENDABLE
2122
@protocol FIRAppCheckTokenResultInterop <NSObject>
2223

2324
/// App Check token in the case of success or a dummy token in the case of a failure.

FirebaseAppCheck/Sources/Core/FIRAppCheckTokenResult.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
NS_ASSUME_NONNULL_BEGIN
2222

23+
NS_SWIFT_SENDABLE
2324
@interface FIRAppCheckTokenResult : NSObject <FIRAppCheckTokenResultInterop>
2425

2526
- (instancetype)initWithToken:(NSString *)token error:(nullable NSError *)error;

0 commit comments

Comments
 (0)