FIRInstallationsIDController.m 22 KB

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