FIRInstallationsStoredRegistrationError.m 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 "FIRInstallationsStoredRegistrationError.h"
  17. #import "FIRInstallationsHTTPError.h"
  18. #import "FIRInstallationsStoredRegistrationParameters.h"
  19. #import "FIRInstallationsLogger.h"
  20. NSString *const kFIRInstallationsStoredRegistrationErrorRegistrationParametersKey =
  21. @"registrationParameters";
  22. NSString *const kFIRInstallationsStoredRegistrationErrorAPIErrorKey = @"APIError";
  23. NSString *const kFIRInstallationsStoredRegistrationErrorStorageVersionKey = @"storageVersion";
  24. NSString *const kFIRInstallationsStoredRegistrationErrorDateKey = @"date";
  25. NSInteger const kFIRInstallationsStoredRegistrationErrorStorageVersion = 1;
  26. @implementation FIRInstallationsStoredRegistrationError
  27. - (instancetype)initWithRegistrationParameters:
  28. (FIRInstallationsStoredRegistrationParameters *)registrationParameters
  29. date:(NSDate *)date
  30. APIError:(NSError *)error {
  31. self = [super init];
  32. if (self) {
  33. _registrationParameters = registrationParameters;
  34. _date = date;
  35. _APIError = error;
  36. }
  37. return self;
  38. }
  39. - (NSInteger)storageVersion {
  40. return kFIRInstallationsStoredRegistrationErrorStorageVersion;
  41. }
  42. #pragma mark - NSSecureCoding
  43. + (BOOL)supportsSecureCoding {
  44. return YES;
  45. }
  46. - (void)encodeWithCoder:(nonnull NSCoder *)coder {
  47. [coder encodeObject:self.registrationParameters
  48. forKey:kFIRInstallationsStoredRegistrationErrorRegistrationParametersKey];
  49. [coder encodeObject:self.date forKey:kFIRInstallationsStoredRegistrationErrorDateKey];
  50. [coder encodeObject:self.APIError forKey:kFIRInstallationsStoredRegistrationErrorAPIErrorKey];
  51. [coder encodeInteger:kFIRInstallationsStoredRegistrationErrorStorageVersion
  52. forKey:kFIRInstallationsStoredRegistrationErrorStorageVersionKey];
  53. }
  54. - (nullable instancetype)initWithCoder:(nonnull NSCoder *)coder {
  55. NSInteger storageVersion =
  56. [coder decodeIntegerForKey:kFIRInstallationsStoredRegistrationErrorStorageVersionKey];
  57. if (storageVersion > self.storageVersion) {
  58. FIRLogWarning(kFIRLoggerInstallations,
  59. kFIRInstallationsMessageCodeRegistrationErrorCoderVersionMismatch,
  60. @"FIRInstallationsStoredRegistrationError was encoded by a newer coder version "
  61. @"%ld. Current coder version is %ld. Some installation data may be lost.",
  62. (long)storageVersion, (long)self.storageVersion);
  63. }
  64. FIRInstallationsStoredRegistrationParameters *registrationParameters =
  65. [coder decodeObjectOfClass:[FIRInstallationsStoredRegistrationParameters class]
  66. forKey:kFIRInstallationsStoredRegistrationErrorRegistrationParametersKey];
  67. NSDate *date = [coder decodeObjectOfClass:[NSDate class]
  68. forKey:kFIRInstallationsStoredRegistrationErrorDateKey];
  69. NSSet<Class> *allowedErrorClasses =
  70. [NSSet setWithArray:@ [[FIRInstallationsHTTPError class], [NSError class]]];
  71. NSError *APIError =
  72. [coder decodeObjectOfClasses:allowedErrorClasses
  73. forKey:kFIRInstallationsStoredRegistrationErrorAPIErrorKey];
  74. if (registrationParameters == nil || APIError == nil) {
  75. FIRLogWarning(kFIRLoggerInstallations,
  76. kFIRInstallationsMessageCodeRegistrationErrorFailedToDecode,
  77. @"Failed to decode FIRInstallationsStoredRegistrationError.");
  78. return nil;
  79. }
  80. return [self initWithRegistrationParameters:registrationParameters date:date APIError:APIError];
  81. }
  82. @end