AuthURLPresenter.swift 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. self.safariViewController = nil
  87. // TODO: Ensure that the SFSafariViewController is actually removed from the screen
  88. // before invoking finishPresentation
  89. self.finishPresentation(withURL: nil,
  90. error: AuthErrorUtils.webContextCancelledError(message: nil))
  91. }
  92. }
  93. }
  94. // MARK: AuthWebViewControllerDelegate
  95. func webViewControllerDidCancel(_ controller: AuthWebViewController) {
  96. kAuthGlobalWorkQueue.async {
  97. if self.webViewController == controller {
  98. self.finishPresentation(withURL: nil,
  99. error: AuthErrorUtils.webContextCancelledError(message: nil))
  100. }
  101. }
  102. }
  103. func webViewController(_ controller: AuthWebViewController, canHandle url: URL) -> Bool {
  104. var result = false
  105. kAuthGlobalWorkQueue.sync {
  106. if self.webViewController == controller {
  107. result = self.canHandle(url: url)
  108. }
  109. }
  110. return result
  111. }
  112. func webViewController(_ controller: AuthWebViewController,
  113. didFailWithError error: Error) {
  114. kAuthGlobalWorkQueue.async {
  115. if self.webViewController == controller {
  116. self.finishPresentation(withURL: nil, error: error)
  117. }
  118. }
  119. }
  120. /// Whether or not some web-based content is being presented.
  121. ///
  122. /// Accesses to this property are serialized on the global Auth work queue
  123. /// and thus this variable should not be read or written outside of the work queue.
  124. private var isPresenting: Bool = false
  125. /// The callback URL matcher for the current presentation, if one is active.
  126. private var callbackMatcher: ((URL) -> Bool)?
  127. /// The SFSafariViewController used for the current presentation, if any.
  128. private var safariViewController: SFSafariViewController?
  129. /// The `AuthWebViewController` used for the current presentation, if any.
  130. private var webViewController: AuthWebViewController?
  131. /// The UIDelegate used to present the SFSafariViewController.
  132. var uiDelegate: AuthUIDelegate?
  133. /// The completion handler for the current presentation, if one is active.
  134. ///
  135. /// Accesses to this variable are serialized on the global Auth work queue
  136. /// and thus this variable should not be read or written outside of the work queue.
  137. ///
  138. /// This variable is also used as a flag to indicate a presentation is active.
  139. var completion: ((URL?, Error?) -> Void)?
  140. /// Test-only option to validate the calls to the uiDelegate.
  141. var fakeUIDelegate: AuthUIDelegate?
  142. // MARK: Private methods
  143. private func finishPresentation(withURL url: URL?, error: Error?) {
  144. callbackMatcher = nil
  145. let uiDelegate = self.uiDelegate
  146. self.uiDelegate = nil
  147. let completion = self.completion
  148. self.completion = nil
  149. let safariViewController = self.safariViewController
  150. self.safariViewController = nil
  151. let webViewController = self.webViewController
  152. self.webViewController = nil
  153. if safariViewController != nil || webViewController != nil {
  154. DispatchQueue.main.async {
  155. uiDelegate?.dismiss(animated: true) {
  156. self.isPresenting = false
  157. if let completion {
  158. completion(url, error)
  159. }
  160. }
  161. }
  162. } else {
  163. isPresenting = false
  164. if let completion {
  165. completion(url, error)
  166. }
  167. }
  168. }
  169. }
  170. #endif