added iOS source code
[wl-app.git] / iOS / Pods / FirebaseMessaging / Firebase / Messaging / FIRMessagingRmqManager.h
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 <Foundation/Foundation.h>
18
19 @class GtalkDataMessageStanza;
20 @class GPBMessage;
21
22 @class FIRMessagingPersistentSyncMessage;
23
24 /**
25  * Called on each raw message.
26  */
27 typedef void(^FIRMessagingRmqMessageHandler)(int64_t rmqId, int8_t tag, NSData *data);
28
29 /**
30  * Called on each DataMessageStanza.
31  */
32 typedef void(^FIRMessagingDataMessageHandler)(int64_t rmqId, GtalkDataMessageStanza *stanza);
33
34 /**
35  *  Used to scan through the rmq and perform actions on messages as required.
36  */
37 @protocol FIRMessagingRmqScanner <NSObject>
38
39 /**
40  *  Scan the RMQ for outgoing messages and process them as required.
41  */
42 - (void)scanWithRmqMessageHandler:(FIRMessagingRmqMessageHandler)rmqMessageHandler
43                dataMessageHandler:(FIRMessagingDataMessageHandler)dataMessageHandler;
44
45 @end
46
47 /**
48  * This manages the RMQ persistent store.
49  *
50  * The store is used to store all the S2D id's that were received by the client and were ACK'ed
51  * by us but the server hasn't confirmed the ACK. We don't delete these id's until the server
52  * ACK's us that they have received them.
53  *
54  * We also store the upstream messages(d2s) that were sent by the client.
55  *
56  * Also store the lastRMQId that was sent by us so that for a new connection being setup we don't
57  * duplicate RMQ Id's for the new messages.
58  */
59 @interface FIRMessagingRmqManager : NSObject <FIRMessagingRmqScanner>
60
61 // designated initializer
62 - (instancetype)initWithDatabaseName:(NSString *)databaseName;
63
64 - (void)loadRmqId;
65
66 /**
67  *  Save an upstream message to RMQ. If the message send fails for some reason we would not
68  *  lose the message since it would be saved in the RMQ.
69  *
70  *  @param message The upstream message to be saved.
71  *  @param error   The error if any while saving the message else nil.
72  *
73  *  @return YES if the message was successfully saved to RMQ else NO.
74  */
75 - (BOOL)saveRmqMessage:(GPBMessage *)message error:(NSError **)error;
76
77 /**
78  *  Save Server to device message with the given RMQ-ID.
79  *
80  *  @param rmqID The rmqID of the s2d message to save.
81  *
82  *  @return YES if the save was successfull else NO.
83  */
84 - (BOOL)saveS2dMessageWithRmqId:(NSString *)rmqID;
85
86 /**
87  *  A list of all unacked Server to device RMQ IDs.
88  *
89  *  @return A list of unacked Server to Device RMQ ID's. All values are Strings.
90  */
91 - (NSArray *)unackedS2dRmqIds;
92
93 /**
94  *  Removes the outgoing message from RMQ store.
95  *
96  *  @param rmqId The rmqID to remove from the store.
97  *
98  *  @return The number of messages deleted successfully.
99  */
100 - (int)removeRmqMessagesWithRmqId:(NSString *)rmqId;
101
102 /**
103  *  Removes the messages with the given rmqIDs from RMQ store.
104  *
105  *  @param rmqIds The lsit of rmqID's to remove from the store.
106  *
107  *  @return The number of messages deleted successfully.
108  */
109 - (int)removeRmqMessagesWithRmqIds:(NSArray *)rmqIds;
110
111 /**
112  *  Removes a list of downstream messages from the RMQ.
113  *
114  *  @param s2dIds The list of messages ACK'ed by the server that we should remove
115  *                from the RMQ store.
116  */
117 - (void)removeS2dIds:(NSArray *)s2dIds;
118
119 #pragma mark - Sync Messages
120
121 /**
122  *  Get persisted sync message with rmqID.
123  *
124  *  @param rmqID The rmqID of the persisted sync message.
125  *
126  *  @return A valid persistent sync message with the given rmqID if found in the RMQ else nil.
127  */
128 - (FIRMessagingPersistentSyncMessage *)querySyncMessageWithRmqID:(NSString *)rmqID;
129
130 /**
131  *  Delete sync message with rmqID.
132  *
133  *  @param rmqID The rmqID of the persisted sync message.
134  *
135  *  @return YES if the message was successfully deleted else NO.
136  */
137 - (BOOL)deleteSyncMessageWithRmqID:(NSString *)rmqID;
138
139 /**
140  *  Delete the expired sync messages from persisten store. Also deletes messages that have been
141  *  delivered both via APNS and MCS.
142  *
143  *  @param error The error if any while deleting the messages.
144  *
145  *  @return The total number of messages that were deleted from the persistent store.
146  */
147 - (int)deleteExpiredOrFinishedSyncMessages:(NSError **)error;
148
149 /**
150  *  Save sync message received by the device.
151  *
152  *  @param rmqID          The rmqID of the message received.
153  *  @param expirationTime The expiration time of the sync message received.
154  *  @param apnsReceived   YES if the message was received via APNS else NO.
155  *  @param mcsReceived    YES if the message was received via MCS else NO.
156  *  @param error          The error if any while saving the sync message to persistent store.
157  *
158  *  @return YES if the message save was successful else NO.
159  */
160 - (BOOL)saveSyncMessageWithRmqID:(NSString *)rmqID
161                   expirationTime:(int64_t)expirationTime
162                     apnsReceived:(BOOL)apnsReceived
163                      mcsReceived:(BOOL)mcsReceived
164                            error:(NSError **)error;
165
166 /**
167  *  Update sync message received via APNS.
168  *
169  *  @param rmqID The rmqID of the received message.
170  *  @param error The error if any while updating the sync message.
171  *
172  *  @return YES if the persistent sync message was successfully updated else NO.
173  */
174 - (BOOL)updateSyncMessageViaAPNSWithRmqID:(NSString *)rmqID error:(NSError **)error;
175
176 /**
177  *  Update sync message received via MCS.
178  *
179  *  @param rmqID The rmqID of the received message.
180  *  @param error The error if any while updating the sync message.
181  *
182  *  @return YES if the persistent sync message was successfully updated else NO.
183  */
184 - (BOOL)updateSyncMessageViaMCSWithRmqID:(NSString *)rmqID error:(NSError **)error;
185
186 #pragma mark - Testing
187
188 + (void)removeDatabaseWithName:(NSString *)dbName;
189
190 @end