FIRInstallationsIDController.m 22 KB

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