AuthURLPresenter.swift 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. SFSafariViewControllerDelegate, AuthWebViewControllerDelegate {
  23. /// Presents an URL to interact with user.
  24. /// - Parameter url: The URL to present.
  25. /// - Parameter uiDelegate: The UI delegate to present view controller.
  26. /// - Parameter completion: A block to be called either synchronously if the presentation fails
  27. /// to start, or asynchronously in future on an unspecified thread once the presentation
  28. /// finishes.
  29. func present(_ url: URL,
  30. uiDelegate: AuthUIDelegate?,
  31. callbackMatcher: @escaping (URL?) -> Bool,
  32. completion: @escaping (URL?, Error?) -> Void) {
  33. if isPresenting {
  34. // Unable to start a new presentation on top of another.
  35. // Invoke the new completion closure and leave the old one as-is
  36. // to be invoked when the presentation finishes.
  37. DispatchQueue.main.async {
  38. completion(nil, AuthErrorUtils.webContextCancelledError(message: nil))
  39. }
  40. return
  41. }
  42. isPresenting = true
  43. self.callbackMatcher = callbackMatcher
  44. self.completion = completion
  45. DispatchQueue.main.async {
  46. self.uiDelegate = uiDelegate ?? AuthDefaultUIDelegate.defaultUIDelegate()
  47. #if targetEnvironment(macCatalyst)
  48. self.webViewController = AuthWebViewController(url: url, delegate: self)
  49. if let webViewController = self.webViewController {
  50. let navController = UINavigationController(rootViewController: webViewController)
  51. if let fakeUIDelegate = self.fakeUIDelegate {
  52. fakeUIDelegate.present(navController, animated: true)
  53. } else {
  54. self.uiDelegate?.present(navController, animated: true)
  55. }
  56. }
  57. #else
  58. self.safariViewController = SFSafariViewController(url: url)
  59. self.safariViewController?.delegate = self
  60. if let safariViewController = self.safariViewController {
  61. if let fakeUIDelegate = self.fakeUIDelegate {
  62. fakeUIDelegate.present(safariViewController, animated: true)
  63. } else {
  64. self.uiDelegate?.present(safariViewController, animated: true)
  65. }
  66. }
  67. #endif
  68. }
  69. }
  70. /// Determines if a URL was produced by the currently presented URL.
  71. /// - Parameter url: The URL to handle.
  72. /// - Returns: Whether the URL could be handled or not.
  73. func canHandle(url: URL) -> Bool {
  74. if isPresenting,
  75. let callbackMatcher = callbackMatcher,
  76. callbackMatcher(url) {
  77. finishPresentation(withURL: url, error: nil)
  78. return true
  79. }
  80. return false
  81. }
  82. // MARK: SFSafariViewControllerDelegate
  83. func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
  84. kAuthGlobalWorkQueue.async {
  85. if controller == self.safariViewController {
  86. // TODO: Ensure that the SFSafariViewController is actually removed from the screen
  87. // before invoking finishPresentation
  88. self.finishPresentation(withURL: nil,
  89. error: AuthErrorUtils.webContextCancelledError(message: nil))
  90. }
  91. }
  92. }
  93. // MARK: AuthWebViewControllerDelegate
  94. func webViewControllerDidCancel(_ controller: AuthWebViewController) {
  95. kAuthGlobalWorkQueue.async {
  96. if self.webViewController == controller {
  97. self.finishPresentation(withURL: nil,
  98. error: AuthErrorUtils.webContextCancelledError(message: nil))
  99. }
  100. }
  101. }
  102. func webViewController(_ controller: AuthWebViewController, canHandle url: URL) -> Bool {
  103. var result = false
  104. kAuthGlobalWorkQueue.sync {
  105. if self.webViewController == controller {
  106. result = self.canHandle(url: url)
  107. }
  108. }
  109. return result
  110. }
  111. func webViewController(_ controller: AuthWebViewController,
  112. didFailWithError error: Error) {
  113. kAuthGlobalWorkQueue.async {
  114. if self.webViewController == controller {
  115. self.finishPresentation(withURL: nil, error: error)
  116. }
  117. }
  118. }
  119. /// Whether or not some web-based content is being presented.
  120. ///
  121. /// Accesses to this property are serialized on the global Auth work queue
  122. /// and thus this variable should not be read or written outside of the work queue.
  123. private var isPresenting: Bool = false
  124. /// The callback URL matcher for the current presentation, if one is active.
  125. private var callbackMatcher: ((URL) -> Bool)?
  126. /// The SFSafariViewController used for the current presentation, if any.
  127. private var safariViewController: SFSafariViewController?
  128. /// The `AuthWebViewController` used for the current presentation, if any.
  129. private var webViewController: AuthWebViewController?
  130. /// The UIDelegate used to present the SFSafariViewController.
  131. var uiDelegate: AuthUIDelegate?
  132. /// The completion handler for the current presentation, if one is active.
  133. ///
  134. /// Accesses to this variable are serialized on the global Auth work queue
  135. /// and thus this variable should not be read or written outside of the work queue.
  136. ///
  137. /// This variable is also used as a flag to indicate a presentation is active.
  138. var completion: ((URL?, Error?) -> Void)?
  139. /// Test-only option to validate the calls to the uiDelegate.
  140. var fakeUIDelegate: AuthUIDelegate?
  141. // MARK: Private methods
  142. private func finishPresentation(withURL url: URL?, error: Error?) {
  143. callbackMatcher = nil
  144. let uiDelegate = self.uiDelegate
  145. self.uiDelegate = nil
  146. let completion = self.completion
  147. self.completion = nil
  148. let safariViewController = self.safariViewController
  149. self.safariViewController = nil
  150. let webViewController = self.webViewController
  151. self.webViewController = nil
  152. if safariViewController != nil || webViewController != nil {
  153. DispatchQueue.main.async {
  154. uiDelegate?.dismiss(animated: true) {
  155. self.isPresenting = false
  156. if let completion {
  157. completion(url, error)
  158. }
  159. }
  160. }
  161. } else {
  162. isPresenting = false
  163. if let completion {
  164. completion(url, error)
  165. }
  166. }
  167. }
  168. }
  169. #endif