FIRInstallations.m 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright 2019 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 "FIRInstallations.h"
  17. #if __has_include(<FBLPromises/FBLPromises.h>)
  18. #import <FBLPromises/FBLPromises.h>
  19. #else
  20. #import "FBLPromises.h"
  21. #endif
  22. #import <FirebaseCore/FIRAppInternal.h>
  23. #import <FirebaseCore/FIRComponent.h>
  24. #import <FirebaseCore/FIRComponentContainer.h>
  25. #import <FirebaseCore/FIRLibrary.h>
  26. #import <FirebaseCore/FIRLogger.h>
  27. #import <FirebaseCore/FIROptions.h>
  28. #import "FIRInstallationsAuthTokenResultInternal.h"
  29. #import "FIRInstallationsErrorUtil.h"
  30. #import "FIRInstallationsIDController.h"
  31. #import "FIRInstallationsItem.h"
  32. #import "FIRInstallationsStoredAuthToken.h"
  33. #import "FIRInstallationsVersion.h"
  34. NS_ASSUME_NONNULL_BEGIN
  35. @protocol FIRInstallationsInstanceProvider <FIRLibrary>
  36. @end
  37. @interface FIRInstallations () <FIRInstallationsInstanceProvider>
  38. @property(nonatomic, readonly) FIROptions *appOptions;
  39. @property(nonatomic, readonly) NSString *appName;
  40. @property(nonatomic, readonly) FIRInstallationsIDController *installationsIDController;
  41. @end
  42. @implementation FIRInstallations
  43. #pragma mark - Firebase component
  44. + (void)load {
  45. [FIRApp registerInternalLibrary:(Class<FIRLibrary>)self
  46. withName:@"fire-install"
  47. withVersion:[NSString stringWithUTF8String:FIRInstallationsVersionStr]];
  48. }
  49. + (nonnull NSArray<FIRComponent *> *)componentsToRegister {
  50. FIRComponentCreationBlock creationBlock =
  51. ^id _Nullable(FIRComponentContainer *container, BOOL *isCacheable) {
  52. *isCacheable = YES;
  53. FIRInstallations *installations = [[FIRInstallations alloc] initWithApp:container.app];
  54. return installations;
  55. };
  56. FIRComponent *installationsProvider =
  57. [FIRComponent componentWithProtocol:@protocol(FIRInstallationsInstanceProvider)
  58. instantiationTiming:FIRInstantiationTimingAlwaysEager
  59. dependencies:@[]
  60. creationBlock:creationBlock];
  61. return @[ installationsProvider ];
  62. }
  63. - (instancetype)initWithApp:(FIRApp *)app {
  64. return [self initWitAppOptions:app.options appName:app.name];
  65. }
  66. - (instancetype)initWitAppOptions:(FIROptions *)appOptions appName:(NSString *)appName {
  67. FIRInstallationsIDController *IDController =
  68. [[FIRInstallationsIDController alloc] initWithGoogleAppID:appOptions.googleAppID
  69. appName:appName
  70. APIKey:appOptions.APIKey
  71. projectID:appOptions.projectID
  72. GCMSenderID:appOptions.GCMSenderID];
  73. return [self initWithAppOptions:appOptions
  74. appName:appName
  75. installationsIDController:IDController
  76. prefetchAuthToken:YES];
  77. }
  78. /// The initializer is supposed to be used by tests to inject `installationsStore`.
  79. - (instancetype)initWithAppOptions:(FIROptions *)appOptions
  80. appName:(NSString *)appName
  81. installationsIDController:(FIRInstallationsIDController *)installationsIDController
  82. prefetchAuthToken:(BOOL)prefetchAuthToken {
  83. self = [super init];
  84. if (self) {
  85. [[self class] assertCompatibleIIDVersion];
  86. _appOptions = [appOptions copy];
  87. _appName = [appName copy];
  88. _installationsIDController = installationsIDController;
  89. // Pre-fetch auth token.
  90. if (prefetchAuthToken) {
  91. [self authTokenWithCompletion:^(FIRInstallationsAuthTokenResult *_Nullable tokenResult,
  92. NSError *_Nullable error){
  93. }];
  94. }
  95. }
  96. return self;
  97. }
  98. #pragma mark - Public
  99. + (FIRInstallations *)installations {
  100. FIRApp *defaultApp = [FIRApp defaultApp];
  101. if (!defaultApp) {
  102. [NSException raise:NSInternalInconsistencyException
  103. format:@"The default FirebaseApp instance must be configured before the default"
  104. @"FirebaseApp instance can be initialized. One way to ensure that is to "
  105. @"call `[FIRApp configure];` (`FirebaseApp.configure()` in Swift) in the App"
  106. @" Delegate's `application:didFinishLaunchingWithOptions:` "
  107. @"(`application(_:didFinishLaunchingWithOptions:)` in Swift)."];
  108. }
  109. return [self installationsWithApp:defaultApp];
  110. }
  111. + (FIRInstallations *)installationsWithApp:(FIRApp *)app {
  112. id<FIRInstallationsInstanceProvider> installations =
  113. FIR_COMPONENT(FIRInstallationsInstanceProvider, app.container);
  114. return (FIRInstallations *)installations;
  115. }
  116. - (void)installationIDWithCompletion:(FIRInstallationsIDHandler)completion {
  117. [self.installationsIDController getInstallationItem]
  118. .then(^id(FIRInstallationsItem *installation) {
  119. completion(installation.firebaseInstallationID, nil);
  120. return nil;
  121. })
  122. .catch(^(NSError *error) {
  123. completion(nil, [FIRInstallationsErrorUtil publicDomainErrorWithError:error]);
  124. });
  125. }
  126. - (void)authTokenWithCompletion:(FIRInstallationsTokenHandler)completion {
  127. [self authTokenForcingRefresh:NO completion:completion];
  128. }
  129. - (void)authTokenForcingRefresh:(BOOL)forceRefresh
  130. completion:(FIRInstallationsTokenHandler)completion {
  131. [self.installationsIDController getAuthTokenForcingRefresh:forceRefresh]
  132. .then(^FIRInstallationsAuthTokenResult *(FIRInstallationsItem *installation) {
  133. FIRInstallationsAuthTokenResult *result = [[FIRInstallationsAuthTokenResult alloc]
  134. initWithToken:installation.authToken.token
  135. expirationDate:installation.authToken.expirationDate];
  136. return result;
  137. })
  138. .then(^id(FIRInstallationsAuthTokenResult *token) {
  139. completion(token, nil);
  140. return nil;
  141. })
  142. .catch(^void(NSError *error) {
  143. completion(nil, [FIRInstallationsErrorUtil publicDomainErrorWithError:error]);
  144. });
  145. }
  146. - (void)deleteWithCompletion:(void (^)(NSError *__nullable error))completion {
  147. [self.installationsIDController deleteInstallation]
  148. .then(^id(id result) {
  149. completion(nil);
  150. return nil;
  151. })
  152. .catch(^void(NSError *error) {
  153. completion([FIRInstallationsErrorUtil publicDomainErrorWithError:error]);
  154. });
  155. }
  156. #pragma mark - IID version compatibility
  157. + (void)assertCompatibleIIDVersion {
  158. // We use this flag to disable IID compatibility exception for unit tests.
  159. #ifdef FIR_INSTALLATIONS_ALLOWS_INCOMPATIBLE_IID_VERSION
  160. return;
  161. #else
  162. if (![self isIIDVersionCompatible]) {
  163. [NSException raise:NSInternalInconsistencyException
  164. format:@"FirebaseInstallations will not work correctly with current version of "
  165. @"Firebase Instance ID. Please update your Firebase Instance ID version."];
  166. }
  167. #endif
  168. }
  169. + (BOOL)isIIDVersionCompatible {
  170. Class IIDClass = NSClassFromString(@"FIRInstanceID");
  171. if (IIDClass == nil) {
  172. // It is OK if there is no IID at all.
  173. return YES;
  174. }
  175. // We expect a compatible version having the method `+[FIRInstanceID usesFIS]` defined.
  176. BOOL isCompatibleVersion = [IIDClass respondsToSelector:NSSelectorFromString(@"usesFIS")];
  177. return isCompatibleVersion;
  178. }
  179. @end
  180. NS_ASSUME_NONNULL_END