FIRMessagingSyncMessageManager.m 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright 2017 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 "FirebaseMessaging/Sources/FIRMessagingSyncMessageManager.h"
  17. #import "FirebaseMessaging/Sources/FIRMessagingConstants.h"
  18. #import "FirebaseMessaging/Sources/FIRMessagingDefines.h"
  19. #import "FirebaseMessaging/Sources/FIRMessagingLogger.h"
  20. #import "FirebaseMessaging/Sources/FIRMessagingPersistentSyncMessage.h"
  21. #import "FirebaseMessaging/Sources/FIRMessagingRmqManager.h"
  22. #import "FirebaseMessaging/Sources/FIRMessagingUtilities.h"
  23. static const int64_t kDefaultSyncMessageTTL = 4 * 7 * 24 * 60 * 60; // 4 weeks
  24. // 4 MB of free space is required to persist Sync messages
  25. static const uint64_t kMinFreeDiskSpaceInMB = 1;
  26. @interface FIRMessagingSyncMessageManager ()
  27. @property(nonatomic, readwrite, strong) FIRMessagingRmqManager *rmqManager;
  28. @end
  29. @implementation FIRMessagingSyncMessageManager
  30. - (instancetype)init {
  31. FIRMessagingInvalidateInitializer();
  32. }
  33. - (instancetype)initWithRmqManager:(FIRMessagingRmqManager *)rmqManager {
  34. self = [super init];
  35. if (self) {
  36. _rmqManager = rmqManager;
  37. }
  38. return self;
  39. }
  40. - (void)removeExpiredSyncMessages {
  41. [self.rmqManager deleteExpiredOrFinishedSyncMessages];
  42. }
  43. - (BOOL)didReceiveAPNSSyncMessage:(NSDictionary *)message {
  44. NSString *rmqID = message[kFIRMessagingMessageIDKey];
  45. if (![rmqID length]) {
  46. FIRMessagingLoggerError(kFIRMessagingMessageCodeSyncMessageManager002,
  47. @"Invalid nil rmqID for sync message.");
  48. return NO;
  49. }
  50. FIRMessagingPersistentSyncMessage *persistentMessage =
  51. [self.rmqManager querySyncMessageWithRmqID:rmqID];
  52. if (!persistentMessage) {
  53. // Do not persist the new message if we don't have enough disk space
  54. uint64_t freeDiskSpace = FIRMessagingGetFreeDiskSpaceInMB();
  55. if (freeDiskSpace < kMinFreeDiskSpaceInMB) {
  56. return NO;
  57. }
  58. int64_t expirationTime = [[self class] expirationTimeForSyncMessage:message];
  59. [self.rmqManager saveSyncMessageWithRmqID:rmqID expirationTime:expirationTime];
  60. return NO;
  61. }
  62. if (!persistentMessage.apnsReceived) {
  63. persistentMessage.apnsReceived = YES;
  64. [self.rmqManager updateSyncMessageViaAPNSWithRmqID:rmqID];
  65. }
  66. // Already received this message either via MCS or APNS.
  67. return YES;
  68. }
  69. + (int64_t)expirationTimeForSyncMessage:(NSDictionary *)message {
  70. int64_t ttl = kDefaultSyncMessageTTL;
  71. if (message[kFIRMessagingMessageSyncMessageTTLKey]) {
  72. ttl = [message[kFIRMessagingMessageSyncMessageTTLKey] longLongValue];
  73. }
  74. int64_t currentTime = FIRMessagingCurrentTimestampInSeconds();
  75. return currentTime + ttl;
  76. }
  77. @end