FIRInstallationsStore.m 5.0 KB

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