You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 lines
6.2 KiB

6 years ago
  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 <Foundation/Foundation.h>
  17. @class FIRMessagingPersistentSyncMessage;
  18. // table data handlers
  19. /**
  20. * Handle message stored in the outgoing RMQ messages table.
  21. *
  22. * @param rmqId The rmqID of the message.
  23. * @param tag The message tag.
  24. * @param data The data stored in the message.
  25. */
  26. typedef void(^FCMOutgoingRmqMessagesTableHandler)(int64_t rmqId, int8_t tag, NSData *data);
  27. /// Outgoing messages RMQ table
  28. extern NSString *const kTableOutgoingRmqMessages;
  29. /// Server to device RMQ table
  30. extern NSString *const kTableS2DRmqIds;
  31. @interface FIRMessagingRmq2PersistentStore : NSObject
  32. /**
  33. * Initialize and open the RMQ database on the client.
  34. *
  35. * @param databaseName The name for RMQ database.
  36. *
  37. * @return A store used to persist messages on the client.
  38. */
  39. - (instancetype)initWithDatabaseName:(NSString *)databaseName;
  40. /**
  41. * Save outgoing message in RMQ.
  42. *
  43. * @param rmqId The rmqID for the message.
  44. * @param tag The tag of the message proto.
  45. * @param data The data being sent in the message.
  46. * @param error The error if any while saving the message to the persistent store.
  47. *
  48. * @return YES if the message was successfully saved to the persistent store else NO.
  49. */
  50. - (BOOL)saveMessageWithRmqId:(int64_t)rmqId
  51. tag:(int8_t)tag
  52. data:(NSData *)data
  53. error:(NSError **)error;
  54. /**
  55. * Add unacked server to device message with a given rmqID to the persistent store.
  56. *
  57. * @param rmqId The rmqID of the message that was not acked by the cient.
  58. *
  59. * @return YES if the save was successful else NO.
  60. */
  61. - (BOOL)saveUnackedS2dMessageWithRmqId:(NSString *)rmqId;
  62. /**
  63. * Update the last RMQ ID that was sent by the client.
  64. *
  65. * @param rmqID The latest rmqID sent by the device.
  66. *
  67. * @return YES if the last rmqID was successfully saved else NO.
  68. */
  69. - (BOOL)updateLastOutgoingRmqId:(int64_t)rmqID;
  70. #pragma mark - Query
  71. /**
  72. * Query the highest rmqID saved in the Outgoing messages table.
  73. *
  74. * @return The highest rmqID amongst all the messages in the Outgoing RMQ table. If no message
  75. * was ever persisted return 0.
  76. */
  77. - (int64_t)queryHighestRmqId;
  78. /**
  79. * The last rmqID that was saved on the client.
  80. *
  81. * @return The last rmqID that was saved. If no rmqID was ever persisted return 0.
  82. */
  83. - (int64_t)queryLastRmqId;
  84. /**
  85. * Get a list of all unacked server to device messages stored on the client.
  86. *
  87. * @return List of all unacked s2d messages in the persistent store.
  88. */
  89. - (NSArray *)unackedS2dRmqIds;
  90. /**
  91. * Iterate over all outgoing messages in the RMQ table.
  92. *
  93. * @param handler The handler invoked with each message in the outgoing RMQ table.
  94. */
  95. - (void)scanOutgoingRmqMessagesWithHandler:(FCMOutgoingRmqMessagesTableHandler)handler;
  96. #pragma mark - Delete
  97. /**
  98. * Delete messages with given rmqID's from a table.
  99. *
  100. * @param tableName The table name from which to delete the rmq messages.
  101. * @param rmqIds The rmqID's of the messages to be deleted.
  102. *
  103. * @return The number of messages that were successfully deleted.
  104. */
  105. - (int)deleteMessagesFromTable:(NSString *)tableName
  106. withRmqIds:(NSArray *)rmqIds;
  107. /**
  108. * Remove database from the device.
  109. *
  110. * @param dbName The database name to be deleted.
  111. */
  112. + (void)removeDatabase:(NSString *)dbName;
  113. #pragma mark - Sync Messages
  114. /**
  115. * Save sync message to persistent store to check for duplicates.
  116. *
  117. * @param rmqID The rmqID of the message to save.
  118. * @param expirationTime The expiration time of the message to save.
  119. * @param apnsReceived YES if the message was received via APNS else NO.
  120. * @param mcsReceived YES if the message was received via MCS else NO.
  121. * @param error The error if any while saving the message to store.
  122. *
  123. * @return YES if the message was saved successfully else NO.
  124. */
  125. - (BOOL)saveSyncMessageWithRmqID:(NSString *)rmqID
  126. expirationTime:(int64_t)expirationTime
  127. apnsReceived:(BOOL)apnsReceived
  128. mcsReceived:(BOOL)mcsReceived
  129. error:(NSError **)error;
  130. /**
  131. * Update sync message received via APNS.
  132. *
  133. * @param rmqID The rmqID of the sync message.
  134. * @param error The error if any while updating the sync message in persistence.
  135. *
  136. * @return YES if the update was successful else NO.
  137. */
  138. - (BOOL)updateSyncMessageViaAPNSWithRmqID:(NSString *)rmqID
  139. error:(NSError **)error;
  140. /**
  141. * Update sync message received via MCS.
  142. *
  143. * @param rmqID The rmqID of the sync message.
  144. * @param error The error if any while updating the sync message in persistence.
  145. *
  146. * @return YES if the update was successful else NO.
  147. */
  148. - (BOOL)updateSyncMessageViaMCSWithRmqID:(NSString *)rmqID
  149. error:(NSError **)error;
  150. /**
  151. * Query sync message table for a given rmqID.
  152. *
  153. * @param rmqID The rmqID to search for in SYNC_RMQ.
  154. *
  155. * @return The sync message that was persisted with `rmqID`. If no such message was persisted
  156. * return nil.
  157. */
  158. - (FIRMessagingPersistentSyncMessage *)querySyncMessageWithRmqID:(NSString *)rmqID;
  159. /**
  160. * Delete sync message with rmqID.
  161. *
  162. * @param rmqID The rmqID of the message to delete.
  163. *
  164. * @return YES if a sync message with rmqID was found and deleted successfully else NO.
  165. */
  166. - (BOOL)deleteSyncMessageWithRmqID:(NSString *)rmqID;
  167. /**
  168. * Delete the expired sync messages from persisten store. Also deletes messages that have been
  169. * delivered both via APNS and MCS.
  170. *
  171. * @param error The error if any while deleting the messages.
  172. *
  173. * @return The total number of messages that were deleted from the persistent store.
  174. */
  175. - (int)deleteExpiredOrFinishedSyncMessages:(NSError **)error;
  176. @end