Skip to content

Commit cf2d02c

Browse files
[google_sign_in] PR 3/4 Migrate ViewProvider and GID SDK wrappers from Objective-C to Swift (flutter#12657)
Migrates `ViewProvider` and the GID SDK wrappers from Objective-C to Swift. The plugin class from PR 2/4 now talks to Swift protocols instead of `FSIViewProvider` / `FSIGIDSignIn`. The Swift GID API takes a non-optional presenter. Obj-C `nonnull` was not enforced at runtime, so a nil registrar view (headless / deallocated engine) was a silent no-op. Force-unwrapping that in Swift would crash. This PR completes with an `NSError` instead, mapped to a Flutter error like other unexpected failures. Adds unit tests for the missing-presenter path (`signIn` / `addScopes` on iOS and macOS). Bumps `google_sign_in_ios` to **6.3.4**. PR 3/4 of the Obj-C → Swift migration. Depends on PR 2/4 (plugin class). Continues flutter#119103 ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent 456fa7f commit cf2d02c

12 files changed

Lines changed: 468 additions & 587 deletions

File tree

‎packages/google_sign_in/google_sign_in_ios/CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 6.3.5
2+
3+
* Migrates ViewProvider and GID SDK wrappers from Objective-C to Swift.
4+
* Returns an error when no host view is available to present Google Sign-In.
5+
16
## 6.3.4
27

38
* Migrates the plugin class from Objective-C to Swift.

‎packages/google_sign_in/google_sign_in_ios/darwin/Tests/GoogleSignInTests.swift‎

Lines changed: 59 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import Testing
1717
import Flutter
1818
#endif
1919

20-
// Test implementation of FSIViewProvider.
21-
class TestViewProvider: NSObject, FSIViewProvider {
20+
// Test implementation of ViewProvider.
21+
class TestViewProvider: ViewProvider {
2222
#if os(OSX)
2323
// The view containing the Flutter content.
2424
var view: NSView?
@@ -28,17 +28,17 @@ class TestViewProvider: NSObject, FSIViewProvider {
2828
#endif
2929
}
3030

31-
// Test implementation of FSIGIDSignIn.
32-
class TestSignIn: NSObject, FSIGIDSignIn {
31+
// Test implementation of GIDSignInProtocol.
32+
class TestSignIn: NSObject, GIDSignInProtocol {
3333
var configuration: GIDConfiguration?
3434

3535
// To cause methods to throw an exception.
3636
var exception: NSException?
3737

3838
// Results to use in completion callbacks.
39-
var user: (any FSIGIDGoogleUser)?
39+
var user: (any GIDGoogleUserProtocol)?
4040
var error: Error?
41-
var signInResult: (any FSIGIDSignInResult)?
41+
var signInResult: (any GIDSignInResultProtocol)?
4242

4343
// Passed parameters.
4444
var hint: String?
@@ -62,7 +62,7 @@ class TestSignIn: NSObject, FSIGIDSignIn {
6262
return handleURLResult
6363
}
6464

65-
func restorePreviousSignIn(completion: (((any FSIGIDGoogleUser)?, Error?) -> Void)?) {
65+
func restorePreviousSignIn(completion: (((any GIDGoogleUserProtocol)?, Error?) -> Void)?) {
6666
if let exception = exception {
6767
exception.raise()
6868
}
@@ -90,7 +90,7 @@ class TestSignIn: NSObject, FSIGIDSignIn {
9090
hint: String?,
9191
additionalScopes: [String]?,
9292
nonce: String?,
93-
completion: ((FSIGIDSignInResult?, Error?) -> Void)?
93+
completion: ((GIDSignInResultProtocol?, Error?) -> Void)?
9494
) {
9595
if let exception = exception {
9696
exception.raise()
@@ -111,7 +111,7 @@ class TestSignIn: NSObject, FSIGIDSignIn {
111111
hint: String?,
112112
additionalScopes: [String]?,
113113
nonce: String?,
114-
completion: (((any FSIGIDSignInResult)?, Error?) -> Void)?
114+
completion: (((any GIDSignInResultProtocol)?, Error?) -> Void)?
115115
) {
116116
if let exception = exception {
117117
exception.raise()
@@ -129,8 +129,8 @@ class TestSignIn: NSObject, FSIGIDSignIn {
129129
#endif
130130
}
131131

132-
// Test implementation of FSIGIDProfileData.
133-
class TestProfileData: NSObject, FSIGIDProfileData {
132+
// Test implementation of GIDProfileDataProtocol.
133+
class TestProfileData: NSObject, GIDProfileDataProtocol {
134134
var email: String
135135
var name: String
136136
// A URL to return from imageURLWithDimension:.
@@ -151,8 +151,8 @@ class TestProfileData: NSObject, FSIGIDProfileData {
151151
}
152152
}
153153

154-
// Test implementation of FSIGIDToken.
155-
final class TestToken: NSObject, FSIGIDToken {
154+
// Test implementation of GIDTokenProtocol.
155+
final class TestToken: NSObject, GIDTokenProtocol {
156156
let tokenString: String
157157
let expirationDate: Date?
158158

@@ -162,31 +162,31 @@ final class TestToken: NSObject, FSIGIDToken {
162162
}
163163
}
164164

165-
// Test implementation of FSIGIDSignInResult.
166-
class TestSignInResult: NSObject, FSIGIDSignInResult {
167-
var user: any FSIGIDGoogleUser
165+
// Test implementation of GIDSignInResultProtocol.
166+
class TestSignInResult: NSObject, GIDSignInResultProtocol {
167+
var user: any GIDGoogleUserProtocol
168168
var serverAuthCode: String?
169169

170-
init(user: any FSIGIDGoogleUser, serverAuthCode: String? = nil) {
170+
init(user: any GIDGoogleUserProtocol, serverAuthCode: String? = nil) {
171171
self.user = user
172172
self.serverAuthCode = serverAuthCode
173173
}
174174
}
175175

176-
// Test implementation of FSIGIDGoogleUser.
177-
class TestGoogleUser: NSObject, FSIGIDGoogleUser {
176+
// Test implementation of GIDGoogleUserProtocol.
177+
class TestGoogleUser: NSObject, GIDGoogleUserProtocol {
178178
var userID: String?
179-
var profile: (any FSIGIDProfileData)?
179+
var profile: (any GIDProfileDataProtocol)?
180180
var grantedScopes: [String]?
181-
var accessToken: any FSIGIDToken = TestToken("Access")
182-
var refreshToken: any FSIGIDToken = TestToken("Refresh")
183-
var idToken: (any FSIGIDToken)?
181+
var accessToken: any GIDTokenProtocol = TestToken("Access")
182+
var refreshToken: any GIDTokenProtocol = TestToken("Refresh")
183+
var idToken: (any GIDTokenProtocol)?
184184

185185
// An exception to throw from methods.
186186
var exception: NSException?
187187

188188
// The result to return from addScopes:presentingViewController:completion:.
189-
var result: (any FSIGIDSignInResult)?
189+
var result: (any GIDSignInResultProtocol)?
190190

191191
// The error to return from methods.
192192
var error: Error?
@@ -203,7 +203,7 @@ class TestGoogleUser: NSObject, FSIGIDGoogleUser {
203203
userID = userIdentifier
204204
}
205205

206-
func refreshTokensIfNeeded(completion: @escaping ((any FSIGIDGoogleUser)?, Error?) -> Void) {
206+
func refreshTokensIfNeeded(completion: @escaping ((any GIDGoogleUserProtocol)?, Error?) -> Void) {
207207
if let exception = exception {
208208
exception.raise()
209209
}
@@ -214,7 +214,7 @@ class TestGoogleUser: NSObject, FSIGIDGoogleUser {
214214
func addScopes(
215215
_ scopes: [String],
216216
presenting presentingViewController: UIViewController?,
217-
completion: (((any FSIGIDSignInResult)?, Error?) -> Void)?
217+
completion: (((any GIDSignInResultProtocol)?, Error?) -> Void)?
218218
) {
219219
self.requestedScopes = scopes
220220
self.presentingViewController = presentingViewController
@@ -227,7 +227,7 @@ class TestGoogleUser: NSObject, FSIGIDGoogleUser {
227227
func addScopes(
228228
_ scopes: [String],
229229
presenting presentingWindow: NSWindow?,
230-
completion: (((any FSIGIDSignInResult)?, Error?) -> Void)?
230+
completion: (((any GIDSignInResultProtocol)?, Error?) -> Void)?
231231
) {
232232
self.requestedScopes = scopes
233233
self.presentingWindow = presentingWindow
@@ -877,6 +877,38 @@ struct GoogleSignInPluginTests {
877877
}
878878
}
879879

880+
@Suite("wrappers") struct WrapperTests {
881+
@Test func signInCompletesWithErrorWhenPresenterIsNil() async {
882+
let wrapper = GIDSignInWrapper()
883+
await confirmation("completion called") { confirmed in
884+
wrapper.signIn(
885+
withPresenting: nil, hint: nil, additionalScopes: nil, nonce: nil
886+
) { result, error in
887+
#expect(result == nil)
888+
let nsError = error as NSError?
889+
#expect(nsError?.domain == "google_sign_in")
890+
#expect(
891+
nsError?.localizedDescription
892+
== "No host view available to present Google Sign-In.")
893+
confirmed()
894+
}
895+
}
896+
}
897+
898+
@Test func pluginSignInWithoutPresenterReturnsFlutterError() async {
899+
let plugin = GoogleSignInPlugin(
900+
signIn: GIDSignInWrapper(), viewProvider: TestViewProvider())
901+
await confirmation("completion called") { confirmed in
902+
plugin.signIn(withScopeHint: [], nonce: nil) { result, error in
903+
#expect(result == nil)
904+
#expect(error?.code == "google_sign_in: 0")
905+
#expect(error?.message == "No host view available to present Google Sign-In.")
906+
confirmed()
907+
}
908+
}
909+
}
910+
}
911+
880912
#if os(iOS) || targetEnvironment(macCatalyst)
881913
@Suite("topViewController")
882914
@MainActor

‎packages/google_sign_in/google_sign_in_ios/darwin/google_sign_in_ios/Package.swift‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ let package = Package(
3333
),
3434
.target(
3535
name: "google_sign_in_ios_objc",
36-
dependencies: [
37-
.product(name: "GoogleSignIn", package: "GoogleSignIn-iOS")
38-
],
3936
publicHeadersPath: "include",
4037
cSettings: [
4138
.headerSearchPath("include/google_sign_in_ios"),

‎packages/google_sign_in/google_sign_in_ios/darwin/google_sign_in_ios/Sources/google_sign_in_ios/GoogleSignInPlugin.swift‎

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -99,20 +99,20 @@ private func pigeonErrorCode(for gidSignInErrorCode: Int) -> FSIGoogleSignInErro
9999
public final class GoogleSignInPlugin: NSObject, FlutterPlugin, FSIGoogleSignInApi {
100100
/// Instance used to manage Google Sign In authentication including
101101
/// sign in, sign out, and requesting additional scopes.
102-
let signIn: any FSIGIDSignIn
102+
let signIn: any GIDSignInProtocol
103103

104104
/// A mapping of user IDs to GIDGoogleUser instances to use for follow-up calls.
105-
var usersByIdentifier: [String: any FSIGIDGoogleUser] = [:]
105+
var usersByIdentifier: [String: any GIDGoogleUserProtocol] = [:]
106106

107107
/// The contents of GoogleService-Info.plist, if it exists.
108108
private let googleServiceProperties: [String: Any]?
109109

110110
/// The view provider, to access the current Flutter view.
111-
private let viewProvider: any FSIViewProvider
111+
private let viewProvider: ViewProvider
112112

113113
public static func register(with registrar: FlutterPluginRegistrar) {
114114
let instance = GoogleSignInPlugin(
115-
viewProvider: FSIDefaultViewProvider(registrar: registrar))
115+
viewProvider: DefaultViewProvider(registrar: registrar))
116116
registrar.addApplicationDelegate(instance)
117117
#if os(iOS)
118118
registrar.addSceneDelegate(instance)
@@ -124,22 +124,22 @@ public final class GoogleSignInPlugin: NSObject, FlutterPlugin, FSIGoogleSignInA
124124
}
125125

126126
/// Inject view provider for testing.
127-
convenience init(viewProvider: any FSIViewProvider) {
128-
self.init(signIn: FSIGIDSignInWrapper(), viewProvider: viewProvider)
127+
convenience init(viewProvider: ViewProvider) {
128+
self.init(signIn: GIDSignInWrapper(), viewProvider: viewProvider)
129129
}
130130

131-
/// Inject `FSIGIDSignIn` for testing.
132-
convenience init(signIn: any FSIGIDSignIn, viewProvider: any FSIViewProvider) {
131+
/// Inject `GIDSignInProtocol` for testing.
132+
convenience init(signIn: any GIDSignInProtocol, viewProvider: ViewProvider) {
133133
self.init(
134134
signIn: signIn,
135135
viewProvider: viewProvider,
136136
googleServiceProperties: loadGoogleServiceInfo())
137137
}
138138

139-
/// Inject `FSIGIDSignIn` and `googleServiceProperties` for testing.
139+
/// Inject `GIDSignInProtocol` and `googleServiceProperties` for testing.
140140
init(
141-
signIn: any FSIGIDSignIn,
142-
viewProvider: any FSIViewProvider,
141+
signIn: any GIDSignInProtocol,
142+
viewProvider: ViewProvider,
143143
googleServiceProperties: [String: Any]?
144144
) {
145145
self.signIn = signIn
@@ -307,7 +307,7 @@ public final class GoogleSignInPlugin: NSObject, FlutterPlugin, FSIGoogleSignInA
307307
hint: String?,
308308
additionalScopes: [String]?,
309309
nonce: String?,
310-
completion: @escaping (FSIGIDSignInResult?, Error?) -> Void
310+
completion: @escaping (GIDSignInResultProtocol?, Error?) -> Void
311311
) -> NSException? {
312312
#if os(macOS)
313313
let presenting = viewProvider.view?.window
@@ -331,8 +331,8 @@ public final class GoogleSignInPlugin: NSObject, FlutterPlugin, FSIGoogleSignInA
331331
/// Swift frames is undefined behavior.
332332
private func performAddScopes(
333333
_ scopes: [String],
334-
for user: any FSIGIDGoogleUser,
335-
completion: @escaping (FSIGIDSignInResult?, Error?) -> Void
334+
for user: any GIDGoogleUserProtocol,
335+
completion: @escaping (GIDSignInResultProtocol?, Error?) -> Void
336336
) -> NSException? {
337337
#if os(macOS)
338338
let presenting = viewProvider.view?.window
@@ -369,7 +369,7 @@ public final class GoogleSignInPlugin: NSObject, FlutterPlugin, FSIGoogleSignInA
369369
}
370370

371371
private func handleAuthResult(
372-
user: (any FSIGIDGoogleUser)?,
372+
user: (any GIDGoogleUserProtocol)?,
373373
serverAuthCode: String?,
374374
error: Error?,
375375
completion: @escaping (FSISignInResult?, FlutterError?) -> Void
@@ -397,7 +397,7 @@ public final class GoogleSignInPlugin: NSObject, FlutterPlugin, FSIGoogleSignInA
397397
}
398398

399399
private func didSignIn(
400-
for user: any FSIGIDGoogleUser,
400+
for user: any GIDGoogleUserProtocol,
401401
serverAuthCode: String?,
402402
completion: @escaping (FSISignInResult?, FlutterError?) -> Void
403403
) {

0 commit comments

Comments
 (0)