AppManager.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 "AppManager.h"
  17. #import <FirebaseCore/FIRApp.h>
  18. #import "FIRPhoneAuthProvider.h"
  19. #import "FirebaseAuth.h"
  20. NS_ASSUME_NONNULL_BEGIN
  21. // Declares a private method of FIRInstanceID to work around a bug.
  22. @interface FIRInstanceID : NSObject
  23. + (void)notifyTokenRefresh;
  24. @end
  25. @implementation AppManager {
  26. /** @var _createdAppNames
  27. @brief The set of names of live (created but not deleted) app, to avoid iCore warnings.
  28. */
  29. NSMutableSet<NSString *> *_liveAppNames;
  30. }
  31. - (instancetype)init {
  32. self = [super init];
  33. if (self) {
  34. _count = 2;
  35. _liveAppNames = [[NSMutableSet<NSString *> alloc] initWithCapacity:_count - 1];
  36. }
  37. return self;
  38. }
  39. - (nullable FIRApp *)appAtIndex:(int)index {
  40. if (index == 0) {
  41. return [FIRApp defaultApp];
  42. }
  43. NSString *name = [self appNameWithIndex:index];
  44. if ([_liveAppNames containsObject:name]) {
  45. return [FIRApp appNamed:[self appNameWithIndex:index]];
  46. }
  47. return nil;
  48. }
  49. - (void)recreateAppAtIndex:(int)index
  50. withOptions:(nullable FIROptions *)options
  51. completion:(void (^)())completion {
  52. [self deleteAppAtIndex:index completion:^() {
  53. if (index == 0) {
  54. [FIRInstanceID notifyTokenRefresh]; // b/28967043
  55. if (options) {
  56. [FIRApp configureWithOptions:options];
  57. }
  58. } else {
  59. NSString *name = [self appNameWithIndex:index];
  60. if (options) {
  61. [FIRApp configureWithName:name options:options];
  62. [_liveAppNames addObject:name];
  63. } else {
  64. [_liveAppNames removeObject:name];
  65. }
  66. }
  67. completion();
  68. }];
  69. }
  70. + (instancetype)sharedInstance {
  71. static dispatch_once_t onceToken;
  72. static AppManager *sharedInstance;
  73. dispatch_once(&onceToken, ^{
  74. sharedInstance = [[self alloc] init];
  75. });
  76. return sharedInstance;
  77. }
  78. + (FIRApp *)app {
  79. AppManager *manager = [self sharedInstance];
  80. return [manager appAtIndex:manager.active];
  81. }
  82. + (FIRAuth *)auth {
  83. return [FIRAuth authWithApp:[self app]];
  84. }
  85. + (FIRPhoneAuthProvider *)phoneAuthProvider {
  86. return [FIRPhoneAuthProvider providerWithAuth:[self auth]];
  87. }
  88. #pragma mark - Helpers
  89. /** @fn appNameWithIndex:
  90. @brief Gets the app name for the given index.
  91. @param index The index of the app managed by this instance.
  92. @returns The app name for the FIRApp instance.
  93. */
  94. - (NSString *)appNameWithIndex:(int)index {
  95. return [NSString stringWithFormat:@"APP_%02d", index];
  96. }
  97. /** @fn deleteAppAtIndex:withOptions:completion:
  98. @brief Deletes the app at the given index.
  99. @param index The index of the app to be deleted, 0 being the default app.
  100. @param completion The block to call when completes.
  101. */
  102. - (void)deleteAppAtIndex:(int)index
  103. completion:(void (^)())completion {
  104. FIRApp *app = [self appAtIndex:index];
  105. if (app) {
  106. [app deleteApp:^(BOOL success) {
  107. if (success) {
  108. completion();
  109. } else {
  110. NSLog(@"Failed to delete app '%@'.", app.name);
  111. }
  112. }];
  113. } else {
  114. completion();
  115. }
  116. }
  117. @end
  118. NS_ASSUME_NONNULL_END