FIRInstallationsIDController.m 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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 "FirebaseInstallations/Source/Library/InstallationsIDController/FIRInstallationsIDController.h"
  17. #if __has_include(<FBLPromises/FBLPromises.h>)
  18. #import <FBLPromises/FBLPromises.h>
  19. #else
  20. #import "FBLPromises.h"
  21. #endif
  22. #import "FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
  23. #import "GoogleUtilities/Environment/Private/GULKeychainStorage.h"
  24. #import "FirebaseInstallations/Source/Library/Errors/FIRInstallationsErrorUtil.h"
  25. #import "FirebaseInstallations/Source/Library/FIRInstallationsItem.h"
  26. #import "FirebaseInstallations/Source/Library/FIRInstallationsLogger.h"
  27. #import "FirebaseInstallations/Source/Library/IIDMigration/FIRInstallationsIIDStore.h"
  28. #import "FirebaseInstallations/Source/Library/IIDMigration/FIRInstallationsIIDTokenStore.h"
  29. #import "FirebaseInstallations/Source/Library/InstallationsAPI/FIRInstallationsAPIService.h"
  30. #import "FirebaseInstallations/Source/Library/InstallationsIDController/FIRInstallationsBackoffController.h"
  31. #import "FirebaseInstallations/Source/Library/InstallationsIDController/FIRInstallationsSingleOperationPromiseCache.h"
  32. #import "FirebaseInstallations/Source/Library/InstallationsStore/FIRInstallationsStore.h"
  33. #import "FirebaseInstallations/Source/Library/Errors/FIRInstallationsHTTPError.h"
  34. #import "FirebaseInstallations/Source/Library/InstallationsStore/FIRInstallationsStoredAuthToken.h"
  35. const NSNotificationName FIRInstallationIDDidChangeNotification =
  36. @"FIRInstallationIDDidChangeNotification";
  37. NSString *const kFIRInstallationIDDidChangeNotificationAppNameKey =
  38. @"FIRInstallationIDDidChangeNotification";
  39. NSTimeInterval const kFIRInstallationsTokenExpirationThreshold = 60 * 60; // 1 hour.
  40. static NSString *const kKeychainService = @"com.firebase.FIRInstallations.installations";
  41. @interface FIRInstallationsIDController ()
  42. @property(nonatomic, readonly) NSString *appID;
  43. @property(nonatomic, readonly) NSString *appName;
  44. @property(nonatomic, readonly) FIRInstallationsStore *installationsStore;
  45. @property(nonatomic, readonly) FIRInstallationsIIDStore *IIDStore;
  46. @property(nonatomic, readonly) FIRInstallationsIIDTokenStore *IIDTokenStore;
  47. @property(nonatomic, readonly) FIRInstallationsAPIService *APIService;
  48. @property(nonatomic, readonly) id<FIRInstallationsBackoffControllerProtocol> backoffController;
  49. @property(nonatomic, readonly) FIRInstallationsSingleOperationPromiseCache<FIRInstallationsItem *>
  50. *getInstallationPromiseCache;
  51. @property(nonatomic, readonly)
  52. FIRInstallationsSingleOperationPromiseCache<FIRInstallationsItem *> *authTokenPromiseCache;
  53. @property(nonatomic, readonly) FIRInstallationsSingleOperationPromiseCache<FIRInstallationsItem *>
  54. *authTokenForcingRefreshPromiseCache;
  55. @property(nonatomic, readonly)
  56. FIRInstallationsSingleOperationPromiseCache<NSNull *> *deleteInstallationPromiseCache;
  57. @end
  58. @implementation FIRInstallationsIDController
  59. - (instancetype)initWithGoogleAppID:(NSString *)appID
  60. appName:(NSString *)appName
  61. APIKey:(NSString *)APIKey
  62. projectID:(NSString *)projectID
  63. GCMSenderID:(NSString *)GCMSenderID
  64. accessGroup:(nullable NSString *)accessGroup {
  65. NSString *serviceName = [FIRInstallationsIDController keychainServiceWithAppID:appID];
  66. GULKeychainStorage *secureStorage = [[GULKeychainStorage alloc] initWithService:serviceName];
  67. FIRInstallationsStore *installationsStore =
  68. [[FIRInstallationsStore alloc] initWithSecureStorage:secureStorage accessGroup:accessGroup];
  69. // Use `GCMSenderID` as project identifier when `projectID` is not available.
  70. NSString *APIServiceProjectID = (projectID.length > 0) ? projectID : GCMSenderID;
  71. FIRInstallationsAPIService *apiService =
  72. [[FIRInstallationsAPIService alloc] initWithAPIKey:APIKey projectID:APIServiceProjectID];
  73. FIRInstallationsIIDStore *IIDStore = [[FIRInstallationsIIDStore alloc] init];
  74. FIRInstallationsIIDTokenStore *IIDCheckingStore =
  75. [[FIRInstallationsIIDTokenStore alloc] initWithGCMSenderID:GCMSenderID];
  76. FIRInstallationsBackoffController *backoffController =
  77. [[FIRInstallationsBackoffController alloc] init];
  78. return [self initWithGoogleAppID:appID
  79. appName:appName
  80. installationsStore:installationsStore
  81. APIService:apiService
  82. IIDStore:IIDStore
  83. IIDTokenStore:IIDCheckingStore
  84. backoffController:backoffController];
  85. }
  86. /// The initializer is supposed to be used by tests to inject `installationsStore`.
  87. - (instancetype)initWithGoogleAppID:(NSString *)appID
  88. appName:(NSString *)appName
  89. installationsStore:(FIRInstallationsStore *)installationsStore
  90. APIService:(FIRInstallationsAPIService *)APIService
  91. IIDStore:(FIRInstallationsIIDStore *)IIDStore
  92. IIDTokenStore:(FIRInstallationsIIDTokenStore *)IIDTokenStore
  93. backoffController:
  94. (id<FIRInstallationsBackoffControllerProtocol>)backoffController {
  95. self = [super init];
  96. if (self) {
  97. _appID = appID;
  98. _appName = appName;
  99. _installationsStore = installationsStore;
  100. _APIService = APIService;
  101. _IIDStore = IIDStore;
  102. _IIDTokenStore = IIDTokenStore;
  103. _backoffController = backoffController;
  104. __weak FIRInstallationsIDController *weakSelf = self;
  105. _getInstallationPromiseCache = [[FIRInstallationsSingleOperationPromiseCache alloc]
  106. initWithNewOperationHandler:^FBLPromise *_Nonnull {
  107. FIRInstallationsIDController *strongSelf = weakSelf;
  108. return [strongSelf createGetInstallationItemPromise];
  109. }];
  110. _authTokenPromiseCache = [[FIRInstallationsSingleOperationPromiseCache alloc]
  111. initWithNewOperationHandler:^FBLPromise *_Nonnull {
  112. FIRInstallationsIDController *strongSelf = weakSelf;
  113. return [strongSelf installationWithValidAuthTokenForcingRefresh:NO];
  114. }];
  115. _authTokenForcingRefreshPromiseCache = [[FIRInstallationsSingleOperationPromiseCache alloc]
  116. initWithNewOperationHandler:^FBLPromise *_Nonnull {
  117. FIRInstallationsIDController *strongSelf = weakSelf;
  118. return [strongSelf installationWithValidAuthTokenForcingRefresh:YES];
  119. }];
  120. _deleteInstallationPromiseCache = [[FIRInstallationsSingleOperationPromiseCache alloc]
  121. initWithNewOperationHandler:^FBLPromise *_Nonnull {
  122. FIRInstallationsIDController *strongSelf = weakSelf;
  123. return [strongSelf createDeleteInstallationPromise];
  124. }];
  125. }
  126. return self;
  127. }
  128. #pragma mark - Get Installation.
  129. - (FBLPromise<FIRInstallationsItem *> *)getInstallationItem {
  130. return [self.getInstallationPromiseCache getExistingPendingOrCreateNewPromise];
  131. }
  132. - (FBLPromise<FIRInstallationsItem *> *)createGetInstallationItemPromise {
  133. FIRLogDebug(kFIRLoggerInstallations,
  134. kFIRInstallationsMessageCodeNewGetInstallationOperationCreated, @"%s, appName: %@",
  135. __PRETTY_FUNCTION__, self.appName);
  136. FBLPromise<FIRInstallationsItem *> *installationItemPromise =
  137. [self getStoredInstallation].recover(^id(NSError *error) {
  138. return [self createAndSaveFID];
  139. });
  140. // Initiate registration process on success if needed, but return the installation without waiting
  141. // for it.
  142. installationItemPromise.then(^id(FIRInstallationsItem *installation) {
  143. [self getAuthTokenForcingRefresh:NO];
  144. return nil;
  145. });
  146. return installationItemPromise;
  147. }
  148. - (FBLPromise<FIRInstallationsItem *> *)getStoredInstallation {
  149. return [self.installationsStore installationForAppID:self.appID appName:self.appName].validate(
  150. ^BOOL(FIRInstallationsItem *installation) {
  151. BOOL isValid = NO;
  152. switch (installation.registrationStatus) {
  153. case FIRInstallationStatusUnregistered:
  154. case FIRInstallationStatusRegistered:
  155. isValid = YES;
  156. break;
  157. case FIRInstallationStatusUnknown:
  158. isValid = NO;
  159. break;
  160. }
  161. return isValid;
  162. });
  163. }
  164. - (FBLPromise<FIRInstallationsItem *> *)createAndSaveFID {
  165. return [self migrateOrGenerateInstallation]
  166. .then(^FBLPromise<FIRInstallationsItem *> *(FIRInstallationsItem *installation) {
  167. return [self saveInstallation:installation];
  168. })
  169. .then(^FIRInstallationsItem *(FIRInstallationsItem *installation) {
  170. [self postFIDDidChangeNotification];
  171. return installation;
  172. });
  173. }
  174. - (FBLPromise<FIRInstallationsItem *> *)saveInstallation:(FIRInstallationsItem *)installation {
  175. return [self.installationsStore saveInstallation:installation].then(
  176. ^FIRInstallationsItem *(NSNull *result) {
  177. return installation;
  178. });
  179. }
  180. /**
  181. * Tries to migrate IID data stored by FirebaseInstanceID SDK or generates a new Installation ID if
  182. * not found.
  183. */
  184. - (FBLPromise<FIRInstallationsItem *> *)migrateOrGenerateInstallation {
  185. if (![self isDefaultApp]) {
  186. // Existing IID should be used only for default FirebaseApp.
  187. FIRInstallationsItem *installation =
  188. [self createInstallationWithFID:[FIRInstallationsItem generateFID] IIDDefaultToken:nil];
  189. return [FBLPromise resolvedWith:installation];
  190. }
  191. return [[[FBLPromise
  192. all:@[ [self.IIDStore existingIID], [self.IIDTokenStore existingIIDDefaultToken] ]]
  193. then:^id _Nullable(NSArray *_Nullable results) {
  194. NSString *existingIID = results[0];
  195. NSString *IIDDefaultToken = results[1];
  196. return [self createInstallationWithFID:existingIID IIDDefaultToken:IIDDefaultToken];
  197. }] recover:^id _Nullable(NSError *_Nonnull error) {
  198. return [self createInstallationWithFID:[FIRInstallationsItem generateFID] IIDDefaultToken:nil];
  199. }];
  200. }
  201. - (FIRInstallationsItem *)createInstallationWithFID:(NSString *)FID
  202. IIDDefaultToken:(nullable NSString *)IIDDefaultToken {
  203. FIRInstallationsItem *installation = [[FIRInstallationsItem alloc] initWithAppID:self.appID
  204. firebaseAppName:self.appName];
  205. installation.firebaseInstallationID = FID;
  206. installation.IIDDefaultToken = IIDDefaultToken;
  207. installation.registrationStatus = FIRInstallationStatusUnregistered;
  208. return installation;
  209. }
  210. #pragma mark - FID registration
  211. - (FBLPromise<FIRInstallationsItem *> *)registerInstallationIfNeeded:
  212. (FIRInstallationsItem *)installation {
  213. switch (installation.registrationStatus) {
  214. case FIRInstallationStatusRegistered:
  215. // Already registered. Do nothing.
  216. return [FBLPromise resolvedWith:installation];
  217. case FIRInstallationStatusUnknown:
  218. case FIRInstallationStatusUnregistered:
  219. // Registration required. Proceed.
  220. break;
  221. }
  222. // Check for backoff.
  223. if (![self.backoffController isNextRequestAllowed]) {
  224. return [FIRInstallationsErrorUtil
  225. rejectedPromiseWithError:[FIRInstallationsErrorUtil backoffIntervalWaitError]];
  226. }
  227. return [self.APIService registerInstallation:installation]
  228. .catch(^(NSError *_Nonnull error) {
  229. [self updateBackoffWithSuccess:NO APIError:error];
  230. if ([self doesRegistrationErrorRequireConfigChange:error]) {
  231. FIRLogError(kFIRLoggerInstallations,
  232. kFIRInstallationsMessageCodeInvalidFirebaseConfiguration,
  233. @"Firebase Installation registration failed for app with name: %@, error:\n"
  234. @"%@\nPlease make sure you use valid GoogleService-Info.plist",
  235. self.appName, error.userInfo[NSLocalizedFailureReasonErrorKey]);
  236. }
  237. })
  238. .then(^id(FIRInstallationsItem *registeredInstallation) {
  239. [self updateBackoffWithSuccess:YES APIError:nil];
  240. return [self saveInstallation:registeredInstallation];
  241. })
  242. .then(^FIRInstallationsItem *(FIRInstallationsItem *registeredInstallation) {
  243. // Server may respond with a different FID if the sent one cannot be accepted.
  244. if (![registeredInstallation.firebaseInstallationID
  245. isEqualToString:installation.firebaseInstallationID]) {
  246. [self postFIDDidChangeNotification];
  247. }
  248. return registeredInstallation;
  249. });
  250. }
  251. - (BOOL)doesRegistrationErrorRequireConfigChange:(NSError *)error {
  252. FIRInstallationsHTTPError *HTTPError = (FIRInstallationsHTTPError *)error;
  253. if (![HTTPError isKindOfClass:[FIRInstallationsHTTPError class]]) {
  254. return NO;
  255. }
  256. switch (HTTPError.HTTPResponse.statusCode) {
  257. // These are the errors that require Firebase configuration change.
  258. case FIRInstallationsRegistrationHTTPCodeInvalidArgument:
  259. case FIRInstallationsRegistrationHTTPCodeAPIKeyToProjectIDMismatch:
  260. case FIRInstallationsRegistrationHTTPCodeProjectNotFound:
  261. return YES;
  262. default:
  263. return NO;
  264. }
  265. }
  266. #pragma mark - Auth Token
  267. - (FBLPromise<FIRInstallationsItem *> *)getAuthTokenForcingRefresh:(BOOL)forceRefresh {
  268. if (forceRefresh || [self.authTokenForcingRefreshPromiseCache getExistingPendingPromise] != nil) {
  269. return [self.authTokenForcingRefreshPromiseCache getExistingPendingOrCreateNewPromise];
  270. } else {
  271. return [self.authTokenPromiseCache getExistingPendingOrCreateNewPromise];
  272. }
  273. }
  274. - (FBLPromise<FIRInstallationsItem *> *)installationWithValidAuthTokenForcingRefresh:
  275. (BOOL)forceRefresh {
  276. FIRLogDebug(kFIRLoggerInstallations, kFIRInstallationsMessageCodeNewGetAuthTokenOperationCreated,
  277. @"-[FIRInstallationsIDController installationWithValidAuthTokenForcingRefresh:%@], "
  278. @"appName: %@",
  279. @(forceRefresh), self.appName);
  280. return [self getInstallationItem]
  281. .then(^FBLPromise<FIRInstallationsItem *> *(FIRInstallationsItem *installation) {
  282. return [self registerInstallationIfNeeded:installation];
  283. })
  284. .then(^id(FIRInstallationsItem *registeredInstallation) {
  285. BOOL isTokenExpiredOrExpiresSoon =
  286. [registeredInstallation.authToken.expirationDate timeIntervalSinceDate:[NSDate date]] <
  287. kFIRInstallationsTokenExpirationThreshold;
  288. if (forceRefresh || isTokenExpiredOrExpiresSoon) {
  289. return [self refreshAuthTokenForInstallation:registeredInstallation];
  290. } else {
  291. return registeredInstallation;
  292. }
  293. })
  294. .recover(^id(NSError *error) {
  295. return [self regenerateFIDOnRefreshTokenErrorIfNeeded:error];
  296. });
  297. }
  298. - (FBLPromise<FIRInstallationsItem *> *)refreshAuthTokenForInstallation:
  299. (FIRInstallationsItem *)installation {
  300. // Check for backoff.
  301. if (![self.backoffController isNextRequestAllowed]) {
  302. return [FIRInstallationsErrorUtil
  303. rejectedPromiseWithError:[FIRInstallationsErrorUtil backoffIntervalWaitError]];
  304. }
  305. return [[[self.APIService refreshAuthTokenForInstallation:installation]
  306. then:^id _Nullable(FIRInstallationsItem *_Nullable refreshedInstallation) {
  307. [self updateBackoffWithSuccess:YES APIError:nil];
  308. return [self saveInstallation:refreshedInstallation];
  309. }] recover:^id _Nullable(NSError *_Nonnull error) {
  310. // Pass the error to the backoff controller.
  311. [self updateBackoffWithSuccess:NO APIError:error];
  312. return error;
  313. }];
  314. }
  315. - (id)regenerateFIDOnRefreshTokenErrorIfNeeded:(NSError *)error {
  316. if (![error isKindOfClass:[FIRInstallationsHTTPError class]]) {
  317. // No recovery possible. Return the same error.
  318. return error;
  319. }
  320. FIRInstallationsHTTPError *HTTPError = (FIRInstallationsHTTPError *)error;
  321. switch (HTTPError.HTTPResponse.statusCode) {
  322. case FIRInstallationsAuthTokenHTTPCodeInvalidAuthentication:
  323. case FIRInstallationsAuthTokenHTTPCodeFIDNotFound:
  324. // The stored installation was damaged or blocked by the server.
  325. // Delete the stored installation then generate and register a new one.
  326. return [self getInstallationItem]
  327. .then(^FBLPromise<NSNull *> *(FIRInstallationsItem *installation) {
  328. return [self deleteInstallationLocally:installation];
  329. })
  330. .then(^FBLPromise<FIRInstallationsItem *> *(id result) {
  331. return [self installationWithValidAuthTokenForcingRefresh:NO];
  332. });
  333. default:
  334. // No recovery possible. Return the same error.
  335. return error;
  336. }
  337. }
  338. #pragma mark - Delete FID
  339. - (FBLPromise<NSNull *> *)deleteInstallation {
  340. return [self.deleteInstallationPromiseCache getExistingPendingOrCreateNewPromise];
  341. }
  342. - (FBLPromise<NSNull *> *)createDeleteInstallationPromise {
  343. FIRLogDebug(kFIRLoggerInstallations,
  344. kFIRInstallationsMessageCodeNewDeleteInstallationOperationCreated, @"%s, appName: %@",
  345. __PRETTY_FUNCTION__, self.appName);
  346. // Check for ongoing requests first, if there is no a request, then check local storage for
  347. // existing installation.
  348. FBLPromise<FIRInstallationsItem *> *currentInstallationPromise =
  349. [self mostRecentInstallationOperation] ?: [self getStoredInstallation];
  350. return currentInstallationPromise
  351. .then(^id(FIRInstallationsItem *installation) {
  352. return [self sendDeleteInstallationRequestIfNeeded:installation];
  353. })
  354. .then(^id(FIRInstallationsItem *installation) {
  355. // Remove the installation from the local storage.
  356. return [self deleteInstallationLocally:installation];
  357. });
  358. }
  359. - (FBLPromise<NSNull *> *)deleteInstallationLocally:(FIRInstallationsItem *)installation {
  360. return [self.installationsStore removeInstallationForAppID:installation.appID
  361. appName:installation.firebaseAppName]
  362. .then(^FBLPromise<NSNull *> *(NSNull *result) {
  363. return [self deleteExistingIIDIfNeeded];
  364. })
  365. .then(^NSNull *(NSNull *result) {
  366. [self postFIDDidChangeNotification];
  367. return result;
  368. });
  369. }
  370. - (FBLPromise<FIRInstallationsItem *> *)sendDeleteInstallationRequestIfNeeded:
  371. (FIRInstallationsItem *)installation {
  372. switch (installation.registrationStatus) {
  373. case FIRInstallationStatusUnknown:
  374. case FIRInstallationStatusUnregistered:
  375. // The installation is not registered, so it is safe to be deleted as is, so return early.
  376. return [FBLPromise resolvedWith:installation];
  377. break;
  378. case FIRInstallationStatusRegistered:
  379. // Proceed to de-register the installation on the server.
  380. break;
  381. }
  382. return [self.APIService deleteInstallation:installation].recover(^id(NSError *APIError) {
  383. if ([FIRInstallationsErrorUtil isAPIError:APIError withHTTPCode:404]) {
  384. // The installation was not found on the server.
  385. // Return success.
  386. return installation;
  387. } else {
  388. // Re-throw the error otherwise.
  389. return APIError;
  390. }
  391. });
  392. }
  393. - (FBLPromise<NSNull *> *)deleteExistingIIDIfNeeded {
  394. if ([self isDefaultApp]) {
  395. return [self.IIDStore deleteExistingIID];
  396. } else {
  397. return [FBLPromise resolvedWith:[NSNull null]];
  398. }
  399. }
  400. - (nullable FBLPromise<FIRInstallationsItem *> *)mostRecentInstallationOperation {
  401. return [self.authTokenForcingRefreshPromiseCache getExistingPendingPromise]
  402. ?: [self.authTokenPromiseCache getExistingPendingPromise]
  403. ?: [self.getInstallationPromiseCache getExistingPendingPromise];
  404. }
  405. #pragma mark - Backoff
  406. - (void)updateBackoffWithSuccess:(BOOL)success APIError:(nullable NSError *)APIError {
  407. if (success) {
  408. [self.backoffController registerEvent:FIRInstallationsBackoffEventSuccess];
  409. } else if ([APIError isKindOfClass:[FIRInstallationsHTTPError class]]) {
  410. FIRInstallationsHTTPError *HTTPResponseError = (FIRInstallationsHTTPError *)APIError;
  411. NSInteger statusCode = HTTPResponseError.HTTPResponse.statusCode;
  412. if (statusCode == FIRInstallationsAuthTokenHTTPCodeInvalidAuthentication ||
  413. statusCode == FIRInstallationsAuthTokenHTTPCodeFIDNotFound) {
  414. // These errors are explicitly excluded because they are handled by FIS SDK itself so don't
  415. // require backoff.
  416. } else if (statusCode == 400 || statusCode == 403) { // Explicitly unrecoverable errors.
  417. [self.backoffController registerEvent:FIRInstallationsBackoffEventUnrecoverableFailure];
  418. } else if (statusCode == 429 ||
  419. (statusCode >= 500 && statusCode < 600)) { // Explicitly recoverable errors.
  420. [self.backoffController registerEvent:FIRInstallationsBackoffEventRecoverableFailure];
  421. } else { // Treat all unknown errors as recoverable.
  422. [self.backoffController registerEvent:FIRInstallationsBackoffEventRecoverableFailure];
  423. }
  424. }
  425. // If the error class is not `FIRInstallationsHTTPError` it indicates a connection error. Such
  426. // errors should not change backoff interval.
  427. }
  428. #pragma mark - Notifications
  429. - (void)postFIDDidChangeNotification {
  430. [[NSNotificationCenter defaultCenter]
  431. postNotificationName:FIRInstallationIDDidChangeNotification
  432. object:nil
  433. userInfo:@{kFIRInstallationIDDidChangeNotificationAppNameKey : self.appName}];
  434. }
  435. #pragma mark - Default App
  436. - (BOOL)isDefaultApp {
  437. return [self.appName isEqualToString:kFIRDefaultAppName];
  438. }
  439. #pragma mark - Keychain
  440. + (NSString *)keychainServiceWithAppID:(NSString *)appID {
  441. #if TARGET_OS_MACCATALYST || TARGET_OS_OSX
  442. // We need to keep service name unique per application on macOS.
  443. // Applications on macOS may request access to Keychain items stored by other applications. It
  444. // means that when the app looks up for a relevant Keychain item in the service scope it will
  445. // request user password to grant access to the Keychain if there are other Keychain items from
  446. // other applications stored under the same Keychain Service.
  447. return [kKeychainService stringByAppendingFormat:@".%@", appID];
  448. #else
  449. // Use a constant Keychain service for non-macOS because:
  450. // 1. Keychain items cannot be shared between apps until configured specifically so the service
  451. // name collisions are not a concern
  452. // 2. We don't want to change the service name to avoid doing a migration.
  453. return kKeychainService;
  454. #endif
  455. }
  456. @end