FIRInstallationsStoredIIDCheckin.m 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 "FIRInstallationsStoredIIDCheckin.h"
  17. #import "FIRInstallationsLogger.h"
  18. NSString *const kFIRInstallationsStoredIIDCheckinDeviceIDKey = @"deviceID";
  19. NSString *const kFIRInstallationsStoredIIDCheckinSecretTokenKey = @"secretToken";
  20. NSString *const kFIRInstallationsStoredIIDCheckinStorageVersionKey = @"storageVersion";
  21. NSInteger const kFIRInstallationsStoredIIDCheckinStorageVersion = 1;
  22. @implementation FIRInstallationsStoredIIDCheckin
  23. - (instancetype)initWithDeviceID:(NSString *)deviceID secretToken:(NSString *)secretToken {
  24. self = [super init];
  25. if (self) {
  26. _deviceID = deviceID;
  27. _secretToken = secretToken;
  28. }
  29. return self;
  30. }
  31. - (NSInteger)storageVersion {
  32. return kFIRInstallationsStoredIIDCheckinStorageVersion;
  33. }
  34. #pragma mark - NSSecureCoding
  35. + (BOOL)supportsSecureCoding {
  36. return YES;
  37. }
  38. - (void)encodeWithCoder:(nonnull NSCoder *)coder {
  39. [coder encodeObject:self.deviceID forKey:kFIRInstallationsStoredIIDCheckinDeviceIDKey];
  40. [coder encodeObject:self.secretToken forKey:kFIRInstallationsStoredIIDCheckinSecretTokenKey];
  41. [coder encodeInteger:self.storageVersion
  42. forKey:kFIRInstallationsStoredIIDCheckinStorageVersionKey];
  43. }
  44. - (nullable instancetype)initWithCoder:(nonnull NSCoder *)coder {
  45. NSInteger storageVersion =
  46. [coder decodeIntegerForKey:kFIRInstallationsStoredIIDCheckinStorageVersionKey];
  47. if (storageVersion > self.storageVersion) {
  48. FIRLogWarning(kFIRLoggerInstallations,
  49. kFIRInstallationsMessageCodeIIDCheckinCoderVersionMismatch,
  50. @"FIRInstallationsStoredItem was encoded by a newer coder version %ld. Current "
  51. @"coder version is %ld. Some installation data may be lost.",
  52. (long)storageVersion, (long)kFIRInstallationsStoredIIDCheckinStorageVersion);
  53. }
  54. NSString *deviceID = [coder decodeObjectOfClass:[NSString class]
  55. forKey:kFIRInstallationsStoredIIDCheckinDeviceIDKey];
  56. NSString *secretToken =
  57. [coder decodeObjectOfClass:[NSString class]
  58. forKey:kFIRInstallationsStoredIIDCheckinSecretTokenKey];
  59. if (deviceID == nil || secretToken == nil) {
  60. FIRLogWarning(kFIRLoggerInstallations, kFIRInstallationsMessageCodeIIDCheckinFailedToDecode,
  61. @"Failed to decode FIRInstallationsStoredIIDCheckin.");
  62. return nil;
  63. }
  64. return [self initWithDeviceID:deviceID secretToken:secretToken];
  65. }
  66. @end