AuthURLPresenter.swift 6.5 KB

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