AuthURLPresenter.swift 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // Copyright 2023 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #if os(iOS)
  15. import Foundation
  16. import SafariServices
  17. import UIKit
  18. import WebKit
  19. /// A Class responsible for presenting URL via SFSafariViewController or WKWebView.
  20. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  21. class AuthURLPresenter: NSObject,
  22. @preconcurrency SFSafariViewControllerDelegate,
  23. AuthWebViewControllerDelegate {
  24. /// Presents an URL to interact with user.
  25. /// - Parameter url: The URL to present.
  26. /// - Parameter uiDelegate: The UI delegate to present view controller.
  27. /// - Parameter completion: A block to be called either synchronously if the presentation fails
  28. /// to start, or asynchronously in future on an unspecified thread once the presentation
  29. /// finishes.
  30. @MainActor func present(_ url: URL,
  31. uiDelegate: AuthUIDelegate?,
  32. callbackMatcher: @Sendable @escaping (URL?) -> Bool,
  33. completion: @MainActor @escaping (URL?, Error?) -> Void) {
  34. if isPresenting {
  35. // Unable to start a new presentation on top of another.
  36. // Invoke the new completion closure and leave the old one as-is
  37. // to be invoked when the presentation finishes.
  38. DispatchQueue.main.async {
  39. completion(nil, AuthErrorUtils.webContextCancelledError(message: nil))
  40. }
  41. return
  42. }
  43. isPresenting = true
  44. self.callbackMatcher = callbackMatcher
  45. self.completion = completion
  46. DispatchQueue.main.async {
  47. self.uiDelegate = uiDelegate ?? AuthDefaultUIDelegate.defaultUIDelegate()
  48. #if targetEnvironment(macCatalyst)
  49. self.webViewController = AuthWebViewController(url: url, delegate: self)
  50. if let webViewController = self.webViewController {
  51. let navController = UINavigationController(rootViewController: webViewController)
  52. if let fakeUIDelegate = self.fakeUIDelegate {
  53. fakeUIDelegate.present(navController, animated: true)
  54. } else {
  55. self.uiDelegate?.present(navController, animated: true)
  56. }
  57. }
  58. #else
  59. self.safariViewController = SFSafariViewController(url: url)
  60. self.safariViewController?.delegate = self
  61. if let safariViewController = self.safariViewController {
  62. if let fakeUIDelegate = self.fakeUIDelegate {
  63. fakeUIDelegate.present(safariViewController, animated: true)
  64. } else {
  65. self.uiDelegate?.present(safariViewController, animated: true)
  66. }
  67. }
  68. #endif
  69. }
  70. }
  71. /// Determines if a URL was produced by the currently presented URL.
  72. /// - Parameter url: The URL to handle.
  73. /// - Returns: Whether the URL could be handled or not.
  74. @MainActor func canHandle(url: URL) -> Bool {
  75. if isPresenting,
  76. let callbackMatcher = callbackMatcher,
  77. callbackMatcher(url) {
  78. finishPresentation(withURL: url, error: nil)
  79. return true
  80. }
  81. return false
  82. }
  83. // MARK: SFSafariViewControllerDelegate
  84. @MainActor func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
  85. if controller == safariViewController {
  86. safariViewController = nil
  87. // TODO: Ensure that the SFSafariViewController is actually removed from the screen
  88. // before invoking finishPresentation
  89. finishPresentation(withURL: nil,
  90. error: AuthErrorUtils.webContextCancelledError(message: nil))
  91. }
  92. }
  93. // MARK: AuthWebViewControllerDelegate
  94. @MainActor func webViewControllerDidCancel(_ controller: AuthWebViewController) {
  95. if webViewController == controller {
  96. finishPresentation(withURL: nil,
  97. error: AuthErrorUtils.webContextCancelledError(message: nil))
  98. }
  99. }
  100. @MainActor func webViewController(_ controller: AuthWebViewController,
  101. canHandle url: URL) -> Bool {
  102. var result = false
  103. if webViewController == controller {
  104. result = canHandle(url: url)
  105. }
  106. return result
  107. }
  108. @MainActor func webViewController(_ controller: AuthWebViewController,
  109. didFailWithError error: Error) {
  110. if webViewController == controller {
  111. finishPresentation(withURL: nil, error: error)
  112. }
  113. }
  114. /// Whether or not some web-based content is being presented.
  115. ///
  116. /// Accesses to this property are serialized on the global Auth work queue
  117. /// and thus this variable should not be read or written outside of the work queue.
  118. private var isPresenting: Bool = false
  119. /// The callback URL matcher for the current presentation, if one is active.
  120. private var callbackMatcher: ((URL) -> Bool)?
  121. /// The SFSafariViewController used for the current presentation, if any.
  122. private var safariViewController: SFSafariViewController?
  123. /// The `AuthWebViewController` used for the current presentation, if any.
  124. private var webViewController: AuthWebViewController?
  125. /// The UIDelegate used to present the SFSafariViewController.
  126. var uiDelegate: AuthUIDelegate?
  127. /// The completion handler for the current presentation, if one is active.
  128. ///
  129. /// Accesses to this variable are serialized on the global Auth work queue
  130. /// and thus this variable should not be read or written outside of the work queue.
  131. ///
  132. /// This variable is also used as a flag to indicate a presentation is active.
  133. var completion: ((URL?, Error?) -> Void)?
  134. /// Test-only option to validate the calls to the uiDelegate.
  135. var fakeUIDelegate: AuthUIDelegate?
  136. // MARK: Private methods
  137. @MainActor private func finishPresentation(withURL url: URL?, error: Error?) {
  138. callbackMatcher = nil
  139. let uiDelegate = self.uiDelegate
  140. self.uiDelegate = nil
  141. let completion = self.completion
  142. self.completion = nil
  143. let safariViewController = self.safariViewController
  144. self.safariViewController = nil
  145. let webViewController = self.webViewController
  146. self.webViewController = nil
  147. if safariViewController != nil || webViewController != nil {
  148. uiDelegate?.dismiss(animated: true) {
  149. self.isPresenting = false
  150. if let completion {
  151. completion(url, error)
  152. }
  153. }
  154. } else {
  155. isPresenting = false
  156. if let completion {
  157. completion(url, error)
  158. }
  159. }
  160. }
  161. }
  162. #endif