aboutsummaryrefslogtreecommitdiffhomepage
path: root/Firebase/Auth/Source/FIRAuthURLPresenter.m
blob: d8e359367c8010b0b1f080584777c4b72fa752c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 * Copyright 2017 Google
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#import "FIRAuthURLPresenter.h"

#import <SafariServices/SafariServices.h>

#import "FIRAuthDefaultUIDelegate.h"
#import "FIRAuthErrorUtils.h"
#import "FIRAuthGlobalWorkQueue.h"
#import "FIRAuthUIDelegate.h"
#import "FIRAuthWebViewController.h"

NS_ASSUME_NONNULL_BEGIN

@interface FIRAuthURLPresenter () <SFSafariViewControllerDelegate,
                                   FIRAuthWebViewControllerDelegate>
@end

// Disable unguarded availability warnings because SFSafariViewController is been used throughout
// the code, including as an iVar, which cannot be simply excluded by @available check.
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunguarded-availability"

@implementation FIRAuthURLPresenter {
  /** @var _isPresenting
      @brief Whether or not some web-based content is being presented.
   */
  BOOL _isPresenting;

  /** @var _callbackMatcher
      @brief The callback URL matcher for the current presentation, if one is active.
   */
  FIRAuthURLCallbackMatcher _Nullable _callbackMatcher;

  /** @var _safariViewController
      @brief The SFSafariViewController used for the current presentation, if any.
   */
  SFSafariViewController *_Nullable _safariViewController;

  /** @var _webViewController
      @brief The FIRAuthWebViewController used for the current presentation, if any.
   */
  FIRAuthWebViewController *_Nullable _webViewController;

  /** @var _UIDelegate
      @brief The UIDelegate used to present the SFSafariViewController.
   */
  id<FIRAuthUIDelegate> _UIDelegate;

  /** @var _completion
      @brief The completion handler for the current presentaion, if one is active.
      @remarks This variable is also used as a flag to indicate a presentation is active.
   */
  FIRAuthURLPresentationCompletion _Nullable _completion;
}

- (void)presentURL:(NSURL *)URL
        UIDelegate:(nullable id<FIRAuthUIDelegate>)UIDelegate
   callbackMatcher:(FIRAuthURLCallbackMatcher)callbackMatcher
        completion:(FIRAuthURLPresentationCompletion)completion {
  if (_isPresenting) {
    // Unable to start a new presentation on top of another.
    _completion(nil, [FIRAuthErrorUtils webContextAlreadyPresentedErrorWithMessage:nil]);
    return;
  }
  _isPresenting = YES;
  _callbackMatcher = callbackMatcher;
  _completion = completion;
  dispatch_async(dispatch_get_main_queue(), ^() {
    self->_UIDelegate = UIDelegate ?: [FIRAuthDefaultUIDelegate defaultUIDelegate];
    if ([SFSafariViewController class]) {
      self->_safariViewController = [[SFSafariViewController alloc] initWithURL:URL];
      self->_safariViewController.delegate = self;
      [self->_UIDelegate presentViewController:self->_safariViewController
                                      animated:YES
                                    completion:nil];
      return;
    } else {
      self->_webViewController = [[FIRAuthWebViewController alloc] initWithURL:URL delegate:self];
      UINavigationController *navController =
          [[UINavigationController alloc] initWithRootViewController:self->_webViewController];
      [self->_UIDelegate presentViewController:navController animated:YES completion:nil];
    }
  });
}

- (BOOL)canHandleURL:(NSURL *)URL {
  if (_isPresenting && _callbackMatcher && _callbackMatcher(URL)) {
    [self finishPresentationWithURL:URL error:nil];
    return YES;
  }
  return NO;
}

#pragma mark - SFSafariViewControllerDelegate

- (void)safariViewControllerDidFinish:(SFSafariViewController *)controller {
  dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
    if (controller == self->_safariViewController) {
      self->_safariViewController = nil;
      //TODO:Ensure that the SFSafariViewController is actually removed from the screen before
      //invoking finishPresentationWithURL:error:
      [self finishPresentationWithURL:nil
                                error:[FIRAuthErrorUtils webContextCancelledErrorWithMessage:nil]];
    }
  });
}

#pragma mark - FIRAuthwebViewControllerDelegate

- (BOOL)webViewController:(FIRAuthWebViewController *)webViewController canHandleURL:(NSURL *)URL {
  __block BOOL result = NO;
  dispatch_sync(FIRAuthGlobalWorkQueue(), ^() {
    if (webViewController == self->_webViewController) {
      result = [self canHandleURL:URL];
    }
  });
  return result;
}

- (void)webViewControllerDidCancel:(FIRAuthWebViewController *)webViewController {
  dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
    if (webViewController == self->_webViewController) {
      [self finishPresentationWithURL:nil
                                error:[FIRAuthErrorUtils webContextCancelledErrorWithMessage:nil]];
    }
  });
}

- (void)webViewController:(FIRAuthWebViewController *)webViewController
         didFailWithError:(NSError *)error {
  dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
    if (webViewController == self->_webViewController) {
      [self finishPresentationWithURL:nil error:error];
    }
  });
}

#pragma mark - Private methods

/** @fn finishPresentationWithURL:error:
    @brief Finishes the presentation for a given URL, if any.
    @param URL The URL to finish presenting.
    @param error The error with which to finish presenting, if any.
 */
- (void)finishPresentationWithURL:(nullable NSURL *)URL
                            error:(nullable NSError *)error {
  _callbackMatcher = nil;
  id<FIRAuthUIDelegate> UIDelegate = _UIDelegate;
  _UIDelegate = nil;
  FIRAuthURLPresentationCompletion completion = _completion;
  _completion = nil;
  void (^finishBlock)(void) = ^() {
    self->_isPresenting = NO;
    completion(URL, error);
  };
  SFSafariViewController *safariViewController = _safariViewController;
  _safariViewController = nil;
  FIRAuthWebViewController *webViewController = _webViewController;
  _webViewController = nil;
  if (safariViewController || webViewController) {
    dispatch_async(dispatch_get_main_queue(), ^() {
      [UIDelegate dismissViewControllerAnimated:YES completion:^() {
        dispatch_async(FIRAuthGlobalWorkQueue(), finishBlock);
      }];
    });
  } else {
    finishBlock();
  }
}

#pragma clang diagnostic pop  // ignored "-Wunguarded-availability"

@end

NS_ASSUME_NONNULL_END