FIRInstallations.m 6.6 KB

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