FIRInstallationsStore.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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/InstallationsStore/FIRInstallationsStore.h"
  17. #import <GoogleUtilities/GULUserDefaults.h>
  18. #if __has_include(<FBLPromises/FBLPromises.h>)
  19. #import <FBLPromises/FBLPromises.h>
  20. #else
  21. #import "FBLPromises.h"
  22. #endif
  23. #import <GoogleUtilities/GULKeychainStorage.h>
  24. #import "FirebaseInstallations/Source/Library/Errors/FIRInstallationsErrorUtil.h"
  25. #import "FirebaseInstallations/Source/Library/FIRInstallationsItem.h"
  26. #import "FirebaseInstallations/Source/Library/InstallationsStore/FIRInstallationsStoredItem.h"
  27. NSString *const kFIRInstallationsStoreUserDefaultsID = @"com.firebase.FIRInstallations";
  28. @interface FIRInstallationsStore ()
  29. @property(nonatomic, readonly) GULKeychainStorage *secureStorage;
  30. @property(nonatomic, readonly, nullable) NSString *accessGroup;
  31. @property(nonatomic, readonly) dispatch_queue_t queue;
  32. @property(nonatomic, readonly) GULUserDefaults *userDefaults;
  33. @end
  34. @implementation FIRInstallationsStore
  35. - (instancetype)initWithSecureStorage:(GULKeychainStorage *)storage
  36. accessGroup:(NSString *)accessGroup {
  37. self = [super init];
  38. if (self) {
  39. _secureStorage = storage;
  40. _accessGroup = [accessGroup copy];
  41. _queue = dispatch_queue_create("com.firebase.FIRInstallationsStore", DISPATCH_QUEUE_SERIAL);
  42. NSString *userDefaultsSuiteName = _accessGroup ?: kFIRInstallationsStoreUserDefaultsID;
  43. _userDefaults = [[GULUserDefaults alloc] initWithSuiteName:userDefaultsSuiteName];
  44. }
  45. return self;
  46. }
  47. - (FBLPromise<FIRInstallationsItem *> *)installationForAppID:(NSString *)appID
  48. appName:(NSString *)appName {
  49. NSString *itemID = [FIRInstallationsItem identifierWithAppID:appID appName:appName];
  50. return [self installationExistsForAppID:appID appName:appName]
  51. .then(^id(id result) {
  52. return [FBLPromise
  53. wrapObjectOrErrorCompletion:^(FBLPromiseObjectOrErrorCompletion _Nonnull handler) {
  54. [self.secureStorage getObjectForKey:itemID
  55. objectClass:[FIRInstallationsStoredItem class]
  56. accessGroup:self.accessGroup
  57. completionHandler:handler];
  58. }];
  59. })
  60. .then(^id(FIRInstallationsStoredItem *_Nullable storedItem) {
  61. if (storedItem == nil) {
  62. return [FIRInstallationsErrorUtil installationItemNotFoundForAppID:appID appName:appName];
  63. }
  64. FIRInstallationsItem *item = [[FIRInstallationsItem alloc] initWithAppID:appID
  65. firebaseAppName:appName];
  66. [item updateWithStoredItem:storedItem];
  67. return item;
  68. });
  69. }
  70. - (FBLPromise<NSNull *> *)saveInstallation:(FIRInstallationsItem *)installationItem {
  71. FIRInstallationsStoredItem *storedItem = [installationItem storedItem];
  72. NSString *identifier = [installationItem identifier];
  73. return
  74. [FBLPromise wrapObjectOrErrorCompletion:^(
  75. FBLPromiseObjectOrErrorCompletion _Nonnull handler) {
  76. [self.secureStorage setObject:storedItem
  77. forKey:identifier
  78. accessGroup:self.accessGroup
  79. completionHandler:handler];
  80. }].then(^id(id __unused unusedResult) {
  81. return [self setInstallationExists:YES forItemWithIdentifier:identifier];
  82. });
  83. }
  84. - (FBLPromise<NSNull *> *)removeInstallationForAppID:(NSString *)appID appName:(NSString *)appName {
  85. NSString *identifier = [FIRInstallationsItem identifierWithAppID:appID appName:appName];
  86. return
  87. [FBLPromise wrapErrorCompletion:^(FBLPromiseErrorCompletion _Nonnull handler) {
  88. [self.secureStorage removeObjectForKey:identifier
  89. accessGroup:self.accessGroup
  90. completionHandler:handler];
  91. }].then(^id(id __unused result) {
  92. return [self setInstallationExists:NO forItemWithIdentifier:identifier];
  93. });
  94. }
  95. #pragma mark - User defaults
  96. - (FBLPromise<NSNull *> *)installationExistsForAppID:(NSString *)appID appName:(NSString *)appName {
  97. NSString *identifier = [FIRInstallationsItem identifierWithAppID:appID appName:appName];
  98. return [FBLPromise onQueue:self.queue
  99. do:^id _Nullable {
  100. return [[self userDefaults] objectForKey:identifier] != nil
  101. ? [NSNull null]
  102. : [FIRInstallationsErrorUtil
  103. installationItemNotFoundForAppID:appID
  104. appName:appName];
  105. }];
  106. }
  107. - (FBLPromise<NSNull *> *)setInstallationExists:(BOOL)exists
  108. forItemWithIdentifier:(NSString *)identifier {
  109. return [FBLPromise onQueue:self.queue
  110. do:^id _Nullable {
  111. if (exists) {
  112. [[self userDefaults] setBool:YES forKey:identifier];
  113. } else {
  114. [[self userDefaults] removeObjectForKey:identifier];
  115. }
  116. return [NSNull null];
  117. }];
  118. }
  119. @end