2 * Copyright 2017 Google
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #import <Foundation/Foundation.h>
19 @class FIRMessagingPersistentSyncMessage;
21 // table data handlers
23 * Handle message stored in the outgoing RMQ messages table.
25 * @param rmqId The rmqID of the message.
26 * @param tag The message tag.
27 * @param data The data stored in the message.
29 typedef void(^FCMOutgoingRmqMessagesTableHandler)(int64_t rmqId, int8_t tag, NSData *data);
31 /// Outgoing messages RMQ table
32 extern NSString *const kTableOutgoingRmqMessages;
33 /// Server to device RMQ table
34 extern NSString *const kTableS2DRmqIds;
36 @interface FIRMessagingRmq2PersistentStore : NSObject
39 * Initialize and open the RMQ database on the client.
41 * @param databaseName The name for RMQ database.
43 * @return A store used to persist messages on the client.
45 - (instancetype)initWithDatabaseName:(NSString *)databaseName;
48 * Save outgoing message in RMQ.
50 * @param rmqId The rmqID for the message.
51 * @param tag The tag of the message proto.
52 * @param data The data being sent in the message.
53 * @param error The error if any while saving the message to the persistent store.
55 * @return YES if the message was successfully saved to the persistent store else NO.
57 - (BOOL)saveMessageWithRmqId:(int64_t)rmqId
60 error:(NSError **)error;
63 * Add unacked server to device message with a given rmqID to the persistent store.
65 * @param rmqId The rmqID of the message that was not acked by the cient.
67 * @return YES if the save was successful else NO.
69 - (BOOL)saveUnackedS2dMessageWithRmqId:(NSString *)rmqId;
72 * Update the last RMQ ID that was sent by the client.
74 * @param rmqID The latest rmqID sent by the device.
76 * @return YES if the last rmqID was successfully saved else NO.
78 - (BOOL)updateLastOutgoingRmqId:(int64_t)rmqID;
83 * Query the highest rmqID saved in the Outgoing messages table.
85 * @return The highest rmqID amongst all the messages in the Outgoing RMQ table. If no message
86 * was ever persisted return 0.
88 - (int64_t)queryHighestRmqId;
91 * The last rmqID that was saved on the client.
93 * @return The last rmqID that was saved. If no rmqID was ever persisted return 0.
95 - (int64_t)queryLastRmqId;
98 * Get a list of all unacked server to device messages stored on the client.
100 * @return List of all unacked s2d messages in the persistent store.
102 - (NSArray *)unackedS2dRmqIds;
105 * Iterate over all outgoing messages in the RMQ table.
107 * @param handler The handler invoked with each message in the outgoing RMQ table.
109 - (void)scanOutgoingRmqMessagesWithHandler:(FCMOutgoingRmqMessagesTableHandler)handler;
111 #pragma mark - Delete
114 * Delete messages with given rmqID's from a table.
116 * @param tableName The table name from which to delete the rmq messages.
117 * @param rmqIds The rmqID's of the messages to be deleted.
119 * @return The number of messages that were successfully deleted.
121 - (int)deleteMessagesFromTable:(NSString *)tableName
122 withRmqIds:(NSArray *)rmqIds;
125 * Remove database from the device.
127 * @param dbName The database name to be deleted.
129 + (void)removeDatabase:(NSString *)dbName;
131 #pragma mark - Sync Messages
134 * Save sync message to persistent store to check for duplicates.
136 * @param rmqID The rmqID of the message to save.
137 * @param expirationTime The expiration time of the message to save.
138 * @param apnsReceived YES if the message was received via APNS else NO.
139 * @param mcsReceived YES if the message was received via MCS else NO.
140 * @param error The error if any while saving the message to store.
142 * @return YES if the message was saved successfully else NO.
144 - (BOOL)saveSyncMessageWithRmqID:(NSString *)rmqID
145 expirationTime:(int64_t)expirationTime
146 apnsReceived:(BOOL)apnsReceived
147 mcsReceived:(BOOL)mcsReceived
148 error:(NSError **)error;
151 * Update sync message received via APNS.
153 * @param rmqID The rmqID of the sync message.
154 * @param error The error if any while updating the sync message in persistence.
156 * @return YES if the update was successful else NO.
158 - (BOOL)updateSyncMessageViaAPNSWithRmqID:(NSString *)rmqID
159 error:(NSError **)error;
162 * Update sync message received via MCS.
164 * @param rmqID The rmqID of the sync message.
165 * @param error The error if any while updating the sync message in persistence.
167 * @return YES if the update was successful else NO.
169 - (BOOL)updateSyncMessageViaMCSWithRmqID:(NSString *)rmqID
170 error:(NSError **)error;
173 * Query sync message table for a given rmqID.
175 * @param rmqID The rmqID to search for in SYNC_RMQ.
177 * @return The sync message that was persisted with `rmqID`. If no such message was persisted
180 - (FIRMessagingPersistentSyncMessage *)querySyncMessageWithRmqID:(NSString *)rmqID;
183 * Delete sync message with rmqID.
185 * @param rmqID The rmqID of the message to delete.
187 * @return YES if a sync message with rmqID was found and deleted successfully else NO.
189 - (BOOL)deleteSyncMessageWithRmqID:(NSString *)rmqID;
192 * Delete the expired sync messages from persisten store. Also deletes messages that have been
193 * delivered both via APNS and MCS.
195 * @param error The error if any while deleting the messages.
197 * @return The total number of messages that were deleted from the persistent store.
199 - (int)deleteExpiredOrFinishedSyncMessages:(NSError **)error;