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