FIRInstallationsIDController.m 22 KB

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