FIRInstallations.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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:FIRInstantiationTimingLazy
  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. return [self initWithAppOptions:appOptions
  73. appName:appName
  74. installationsIDController:IDController
  75. prefetchAuthToken:YES];
  76. }
  77. /// The initializer is supposed to be used by tests to inject `installationsStore`.
  78. - (instancetype)initWithAppOptions:(FIROptions *)appOptions
  79. appName:(NSString *)appName
  80. installationsIDController:(FIRInstallationsIDController *)installationsIDController
  81. prefetchAuthToken:(BOOL)prefetchAuthToken {
  82. self = [super init];
  83. if (self) {
  84. _appOptions = [appOptions copy];
  85. _appName = [appName copy];
  86. _installationsIDController = installationsIDController;
  87. // Pre-fetch auth token.
  88. if (prefetchAuthToken) {
  89. [self authTokenWithCompletion:^(FIRInstallationsAuthTokenResult *_Nullable tokenResult,
  90. NSError *_Nullable error){
  91. }];
  92. }
  93. }
  94. return self;
  95. }
  96. #pragma mark - Public
  97. + (FIRInstallations *)installations {
  98. FIRApp *defaultApp = [FIRApp defaultApp];
  99. if (!defaultApp) {
  100. [NSException raise:NSInternalInconsistencyException
  101. format:@"The default FirebaseApp instance must be configured before the default"
  102. @"FirebaseApp instance can be initialized. One way to ensure that is to "
  103. @"call `[FIRApp configure];` (`FirebaseApp.configure()` in Swift) in the App"
  104. @" Delegate's `application:didFinishLaunchingWithOptions:` "
  105. @"(`application(_:didFinishLaunchingWithOptions:)` in Swift)."];
  106. }
  107. return [self installationsWithApp:defaultApp];
  108. }
  109. + (FIRInstallations *)installationsWithApp:(FIRApp *)app {
  110. id<FIRInstallationsInstanceProvider> installations =
  111. FIR_COMPONENT(FIRInstallationsInstanceProvider, app.container);
  112. return (FIRInstallations *)installations;
  113. }
  114. - (void)installationIDWithCompletion:(FIRInstallationsIDHandler)completion {
  115. [self.installationsIDController getInstallationItem]
  116. .then(^id(FIRInstallationsItem *installation) {
  117. completion(installation.firebaseInstallationID, nil);
  118. return nil;
  119. })
  120. .catch(^(NSError *error) {
  121. completion(nil, [FIRInstallationsErrorUtil publicDomainErrorWithError:error]);
  122. });
  123. }
  124. - (void)authTokenWithCompletion:(FIRInstallationsTokenHandler)completion {
  125. [self authTokenForcingRefresh:NO completion:completion];
  126. }
  127. - (void)authTokenForcingRefresh:(BOOL)forceRefresh
  128. completion:(FIRInstallationsTokenHandler)completion {
  129. [self.installationsIDController getAuthTokenForcingRefresh:forceRefresh]
  130. .then(^FIRInstallationsAuthTokenResult *(FIRInstallationsItem *installation) {
  131. FIRInstallationsAuthTokenResult *result = [[FIRInstallationsAuthTokenResult alloc]
  132. initWithToken:installation.authToken.token
  133. expirationDate:installation.authToken.expirationDate];
  134. return result;
  135. })
  136. .then(^id(FIRInstallationsAuthTokenResult *token) {
  137. completion(token, nil);
  138. return nil;
  139. })
  140. .catch(^void(NSError *error) {
  141. completion(nil, [FIRInstallationsErrorUtil publicDomainErrorWithError:error]);
  142. });
  143. }
  144. - (void)deleteWithCompletion:(void (^)(NSError *__nullable error))completion {
  145. [self.installationsIDController deleteInstallation]
  146. .then(^id(id result) {
  147. completion(nil);
  148. return nil;
  149. })
  150. .catch(^void(NSError *error) {
  151. completion([FIRInstallationsErrorUtil publicDomainErrorWithError:error]);
  152. });
  153. }
  154. @end
  155. NS_ASSUME_NONNULL_END