FIRAuthURLPresenter.m 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <TargetConditionals.h>
  17. #if TARGET_OS_IOS && (!defined(TARGET_OS_VISION) || !TARGET_OS_VISION)
  18. #import <SafariServices/SafariServices.h>
  19. #import "FirebaseAuth/Sources/Public/FirebaseAuth/FIRAuthUIDelegate.h"
  20. #import "FirebaseAuth/Sources/Auth/FIRAuthGlobalWorkQueue.h"
  21. #import "FirebaseAuth/Sources/Utilities/FIRAuthDefaultUIDelegate.h"
  22. #import "FirebaseAuth/Sources/Utilities/FIRAuthErrorUtils.h"
  23. #import "FirebaseAuth/Sources/Utilities/FIRAuthURLPresenter.h"
  24. #import "FirebaseAuth/Sources/Utilities/FIRAuthWebViewController.h"
  25. NS_ASSUME_NONNULL_BEGIN
  26. @interface FIRAuthURLPresenter () <SFSafariViewControllerDelegate, FIRAuthWebViewControllerDelegate>
  27. @end
  28. // Disable unguarded availability warnings because SFSafariViewController is been used throughout
  29. // the code, including as an iVar, which cannot be simply excluded by @available check.
  30. #pragma clang diagnostic push
  31. #pragma clang diagnostic ignored "-Wunguarded-availability"
  32. @implementation FIRAuthURLPresenter {
  33. /** @var _isPresenting
  34. @brief Whether or not some web-based content is being presented.
  35. Accesses to this property are serialized on the global Auth work queue
  36. and thus this variable should not be read or written outside of the work queue.
  37. */
  38. BOOL _isPresenting;
  39. /** @var _callbackMatcher
  40. @brief The callback URL matcher for the current presentation, if one is active.
  41. */
  42. FIRAuthURLCallbackMatcher _Nullable _callbackMatcher;
  43. /** @var _safariViewController
  44. @brief The SFSafariViewController used for the current presentation, if any.
  45. */
  46. SFSafariViewController *_Nullable _safariViewController;
  47. /** @var _webViewController
  48. @brief The FIRAuthWebViewController used for the current presentation, if any.
  49. */
  50. FIRAuthWebViewController *_Nullable _webViewController;
  51. /** @var _UIDelegate
  52. @brief The UIDelegate used to present the SFSafariViewController.
  53. */
  54. id<FIRAuthUIDelegate> _UIDelegate;
  55. /** @var _completion
  56. @brief The completion handler for the current presentation, if one is active.
  57. Accesses to this variable are serialized on the global Auth work queue
  58. and thus this variable should not be read or written outside of the work queue.
  59. @remarks This variable is also used as a flag to indicate a presentation is active.
  60. */
  61. FIRAuthURLPresentationCompletion _Nullable _completion;
  62. }
  63. - (void)presentURL:(NSURL *)URL
  64. UIDelegate:(nullable id<FIRAuthUIDelegate>)UIDelegate
  65. callbackMatcher:(FIRAuthURLCallbackMatcher)callbackMatcher
  66. completion:(FIRAuthURLPresentationCompletion)completion {
  67. if (_isPresenting) {
  68. // Unable to start a new presentation on top of another.
  69. // Invoke the new completion closure and leave the old one as-is
  70. // to be invoked when the presentation finishes.
  71. dispatch_async(dispatch_get_main_queue(), ^() {
  72. completion(nil, [FIRAuthErrorUtils webContextAlreadyPresentedErrorWithMessage:nil]);
  73. });
  74. return;
  75. }
  76. _isPresenting = YES;
  77. _callbackMatcher = callbackMatcher;
  78. _completion = [completion copy];
  79. dispatch_async(dispatch_get_main_queue(), ^() {
  80. self->_UIDelegate = UIDelegate ?: [FIRAuthDefaultUIDelegate defaultUIDelegate];
  81. #if TARGET_OS_MACCATALYST
  82. self->_webViewController = [[FIRAuthWebViewController alloc] initWithURL:URL delegate:self];
  83. UINavigationController *navController =
  84. [[UINavigationController alloc] initWithRootViewController:self->_webViewController];
  85. [self->_UIDelegate presentViewController:navController animated:YES completion:nil];
  86. #else
  87. if ([SFSafariViewController class]) {
  88. self->_safariViewController = [[SFSafariViewController alloc] initWithURL:URL];
  89. self->_safariViewController.delegate = self;
  90. [self->_UIDelegate presentViewController:self->_safariViewController
  91. animated:YES
  92. completion:nil];
  93. return;
  94. } else {
  95. self->_webViewController = [[FIRAuthWebViewController alloc] initWithURL:URL delegate:self];
  96. UINavigationController *navController =
  97. [[UINavigationController alloc] initWithRootViewController:self->_webViewController];
  98. [self->_UIDelegate presentViewController:navController animated:YES completion:nil];
  99. }
  100. #endif
  101. });
  102. }
  103. - (BOOL)canHandleURL:(NSURL *)URL {
  104. if (_isPresenting && _callbackMatcher && _callbackMatcher(URL)) {
  105. [self finishPresentationWithURL:URL error:nil];
  106. return YES;
  107. }
  108. return NO;
  109. }
  110. #pragma mark - SFSafariViewControllerDelegate
  111. - (void)safariViewControllerDidFinish:(SFSafariViewController *)controller {
  112. dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
  113. if (controller == self->_safariViewController) {
  114. self->_safariViewController = nil;
  115. // TODO:Ensure that the SFSafariViewController is actually removed from the screen before
  116. // invoking finishPresentationWithURL:error:
  117. [self finishPresentationWithURL:nil
  118. error:[FIRAuthErrorUtils webContextCancelledErrorWithMessage:nil]];
  119. }
  120. });
  121. }
  122. #pragma mark - FIRAuthwebViewControllerDelegate
  123. - (BOOL)webViewController:(FIRAuthWebViewController *)webViewController canHandleURL:(NSURL *)URL {
  124. __block BOOL result = NO;
  125. dispatch_sync(FIRAuthGlobalWorkQueue(), ^() {
  126. if (webViewController == self->_webViewController) {
  127. result = [self canHandleURL:URL];
  128. }
  129. });
  130. return result;
  131. }
  132. - (void)webViewControllerDidCancel:(FIRAuthWebViewController *)webViewController {
  133. dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
  134. if (webViewController == self->_webViewController) {
  135. [self finishPresentationWithURL:nil
  136. error:[FIRAuthErrorUtils webContextCancelledErrorWithMessage:nil]];
  137. }
  138. });
  139. }
  140. - (void)webViewController:(FIRAuthWebViewController *)webViewController
  141. didFailWithError:(NSError *)error {
  142. dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
  143. if (webViewController == self->_webViewController) {
  144. [self finishPresentationWithURL:nil error:error];
  145. }
  146. });
  147. }
  148. #pragma mark - Private methods
  149. /** @fn finishPresentationWithURL:error:
  150. @brief Finishes the presentation for a given URL, if any.
  151. @param URL The URL to finish presenting.
  152. @param error The error with which to finish presenting, if any.
  153. */
  154. - (void)finishPresentationWithURL:(nullable NSURL *)URL error:(nullable NSError *)error {
  155. _callbackMatcher = nil;
  156. id<FIRAuthUIDelegate> UIDelegate = _UIDelegate;
  157. _UIDelegate = nil;
  158. FIRAuthURLPresentationCompletion completion = [_completion copy];
  159. _completion = NULL;
  160. void (^finishBlock)(void) = ^() {
  161. self->_isPresenting = NO;
  162. completion(URL, error);
  163. };
  164. SFSafariViewController *safariViewController = _safariViewController;
  165. _safariViewController = nil;
  166. FIRAuthWebViewController *webViewController = _webViewController;
  167. _webViewController = nil;
  168. if (safariViewController || webViewController) {
  169. dispatch_async(dispatch_get_main_queue(), ^() {
  170. [UIDelegate dismissViewControllerAnimated:YES
  171. completion:^() {
  172. dispatch_async(FIRAuthGlobalWorkQueue(), finishBlock);
  173. }];
  174. });
  175. } else {
  176. finishBlock();
  177. }
  178. }
  179. #pragma clang diagnostic pop // ignored "-Wunguarded-availability"
  180. @end
  181. NS_ASSUME_NONNULL_END
  182. #endif // TARGET_OS_IOS && (!defined(TARGET_OS_VISION) || !TARGET_OS_VISION)