FIRIAMBookKeeper.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 <TargetConditionals.h>
  17. #if TARGET_OS_IOS || TARGET_OS_TV
  18. #import "FirebaseCore/Extension/FirebaseCoreInternal.h"
  19. #import "FirebaseInAppMessaging/Sources/FIRCore+InAppMessaging.h"
  20. #import "FirebaseInAppMessaging/Sources/Private/Flows/FIRIAMBookKeeper.h"
  21. NSString *const FIRIAM_UserDefaultsKeyForImpressions = @"firebase-iam-message-impressions";
  22. NSString *const FIRIAM_UserDefaultsKeyForLastImpressionTimestamp =
  23. @"firebase-iam-last-impression-timestamp";
  24. NSString *FIRIAM_UserDefaultsKeyForLastFetchTimestamp = @"firebase-iam-last-fetch-timestamp";
  25. // The two keys used to map FIRIAMImpressionRecord object to a NSDictionary object for
  26. // persistence.
  27. NSString *const FIRIAM_ImpressionDictKeyForID = @"message_id";
  28. NSString *const FIRIAM_ImpressionDictKeyForTimestamp = @"impression_time";
  29. static NSString *const kUserDefaultsKeyForFetchWaitTime = @"firebase-iam-fetch-wait-time";
  30. // 24 hours
  31. static NSTimeInterval kDefaultFetchWaitTimeInSeconds = 24 * 60 * 60;
  32. // 3 days
  33. static NSTimeInterval kMaxFetchWaitTimeInSeconds = 3 * 24 * 60 * 60;
  34. @interface FIRIAMBookKeeperViaUserDefaults ()
  35. @property(nonatomic) double lastDisplayTime;
  36. @property(nonatomic) double lastFetchTime;
  37. @property(nonatomic) double nextFetchWaitTime;
  38. @property(nonatomic, nonnull) NSUserDefaults *defaults;
  39. @end
  40. @interface FIRIAMImpressionRecord ()
  41. - (instancetype)initWithStorageDictionary:(NSDictionary *)dict;
  42. @end
  43. @implementation FIRIAMImpressionRecord
  44. - (instancetype)initWithMessageID:(NSString *)messageID
  45. impressionTimeInSeconds:(long)impressionTime {
  46. if (self = [super init]) {
  47. _messageID = messageID;
  48. _impressionTimeInSeconds = impressionTime;
  49. }
  50. return self;
  51. }
  52. - (instancetype)initWithStorageDictionary:(NSDictionary *)dict {
  53. id timestamp = dict[FIRIAM_ImpressionDictKeyForTimestamp];
  54. id messageID = dict[FIRIAM_ImpressionDictKeyForID];
  55. if (![timestamp isKindOfClass:[NSNumber class]] || ![messageID isKindOfClass:[NSString class]]) {
  56. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM270003",
  57. @"Incorrect data in the dictionary object for creating a FIRIAMImpressionRecord"
  58. " object");
  59. return nil;
  60. } else {
  61. return [self initWithMessageID:messageID
  62. impressionTimeInSeconds:((NSNumber *)timestamp).longValue];
  63. }
  64. }
  65. - (NSString *)description {
  66. return [NSString stringWithFormat:@"%@ impressed at %ld in seconds", self.messageID,
  67. self.impressionTimeInSeconds];
  68. }
  69. @end
  70. @implementation FIRIAMBookKeeperViaUserDefaults
  71. - (instancetype)initWithUserDefaults:(NSUserDefaults *)userDefaults {
  72. if (self = [super init]) {
  73. _defaults = userDefaults;
  74. // ok if it returns 0 due to the entry being absent
  75. _lastDisplayTime = [_defaults doubleForKey:FIRIAM_UserDefaultsKeyForLastImpressionTimestamp];
  76. _lastFetchTime = [_defaults doubleForKey:FIRIAM_UserDefaultsKeyForLastFetchTimestamp];
  77. id fetchWaitTimeEntry = [_defaults objectForKey:kUserDefaultsKeyForFetchWaitTime];
  78. if (![fetchWaitTimeEntry isKindOfClass:NSNumber.class]) {
  79. // This corresponds to the case there is no wait time entry is set in user defaults yet
  80. _nextFetchWaitTime = kDefaultFetchWaitTimeInSeconds;
  81. } else {
  82. _nextFetchWaitTime = ((NSNumber *)fetchWaitTimeEntry).doubleValue;
  83. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM270009",
  84. @"Next fetch wait time loaded from user defaults is %lf", _nextFetchWaitTime);
  85. }
  86. }
  87. return self;
  88. }
  89. // A helper function for reading and verifying the stored array data for impressions
  90. // in UserDefaults. It returns nil if it does not exist or fail to pass the data type
  91. // checking.
  92. - (NSArray *)fetchImpressionArrayFromStorage {
  93. id impressionsData = [self.defaults objectForKey:FIRIAM_UserDefaultsKeyForImpressions];
  94. if (impressionsData && ![impressionsData isKindOfClass:[NSArray class]]) {
  95. FIRLogWarning(kFIRLoggerInAppMessaging, @"I-IAM270007",
  96. @"Found non-array data from impression userdefaults storage with key %@",
  97. FIRIAM_UserDefaultsKeyForImpressions);
  98. return nil;
  99. }
  100. return (NSArray *)impressionsData;
  101. }
  102. - (void)recordNewImpressionForMessage:(NSString *)messageID
  103. withStartTimestampInSeconds:(double)timestamp {
  104. @synchronized(self) {
  105. NSArray *oldImpressions = [self fetchImpressionArrayFromStorage];
  106. // oldImpressions could be nil at the first time
  107. NSMutableArray *newImpressions =
  108. oldImpressions ? [oldImpressions mutableCopy] : [[NSMutableArray alloc] init];
  109. // Two cases
  110. // If a prior impression exists for that messageID, update its impression timestamp
  111. // If a prior impression for that messageID does not exist, add a new entry for the
  112. // messageID.
  113. NSDictionary *newImpressionEntry = @{
  114. FIRIAM_ImpressionDictKeyForID : messageID,
  115. FIRIAM_ImpressionDictKeyForTimestamp : [NSNumber numberWithDouble:timestamp]
  116. };
  117. BOOL oldImpressionRecordFound = NO;
  118. for (int i = 0; i < newImpressions.count; i++) {
  119. if ([newImpressions[i] isKindOfClass:[NSDictionary class]]) {
  120. NSDictionary *currentItem = (NSDictionary *)newImpressions[i];
  121. if ([messageID isEqualToString:currentItem[FIRIAM_ImpressionDictKeyForID]]) {
  122. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM270001",
  123. @"Updating timestamp of existing impression record to be %f for "
  124. "message %@",
  125. timestamp, messageID);
  126. [newImpressions replaceObjectAtIndex:i withObject:newImpressionEntry];
  127. oldImpressionRecordFound = YES;
  128. break;
  129. }
  130. }
  131. }
  132. if (!oldImpressionRecordFound) {
  133. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM270002",
  134. @"Insert the first impression record for message %@ with timestamp in seconds "
  135. "as %f",
  136. messageID, timestamp);
  137. [newImpressions addObject:newImpressionEntry];
  138. }
  139. [self.defaults setObject:newImpressions forKey:FIRIAM_UserDefaultsKeyForImpressions];
  140. [self.defaults setDouble:timestamp forKey:FIRIAM_UserDefaultsKeyForLastImpressionTimestamp];
  141. self.lastDisplayTime = timestamp;
  142. }
  143. }
  144. - (void)clearImpressionsWithMessageList:(NSArray<NSString *> *)messageList {
  145. @synchronized(self) {
  146. NSArray *existingImpressions = [self fetchImpressionArrayFromStorage];
  147. NSSet<NSString *> *messageIDSet = [NSSet setWithArray:messageList];
  148. NSPredicate *notInMessageListPredicate =
  149. [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
  150. if (![evaluatedObject isKindOfClass:[NSDictionary class]]) {
  151. return NO; // unexpected item. Throw it away
  152. }
  153. NSDictionary *impression = (NSDictionary *)evaluatedObject;
  154. return impression[FIRIAM_ImpressionDictKeyForID] &&
  155. ![messageIDSet containsObject:impression[FIRIAM_ImpressionDictKeyForID]];
  156. }];
  157. NSArray<NSDictionary *> *updatedImpressions =
  158. [existingImpressions filteredArrayUsingPredicate:notInMessageListPredicate];
  159. if (existingImpressions.count != updatedImpressions.count) {
  160. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM270004",
  161. @"Updating the impression records after purging %d items based on the "
  162. "server fetch response",
  163. (int)(existingImpressions.count - updatedImpressions.count));
  164. [self.defaults setObject:updatedImpressions forKey:FIRIAM_UserDefaultsKeyForImpressions];
  165. } else {
  166. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM270005",
  167. @"No impression records update due to no change after applying the server "
  168. "message list");
  169. }
  170. }
  171. }
  172. - (NSArray<FIRIAMImpressionRecord *> *)getImpressions {
  173. NSArray<NSDictionary *> *impressionsFromStorage = [self fetchImpressionArrayFromStorage];
  174. NSMutableArray<FIRIAMImpressionRecord *> *resultArray = [[NSMutableArray alloc] init];
  175. for (NSDictionary *next in impressionsFromStorage) {
  176. FIRIAMImpressionRecord *nextImpression =
  177. [[FIRIAMImpressionRecord alloc] initWithStorageDictionary:next];
  178. [resultArray addObject:nextImpression];
  179. }
  180. return resultArray;
  181. }
  182. - (NSArray<NSString *> *)getMessageIDsFromImpressions {
  183. NSArray<NSDictionary *> *impressionsFromStorage = [self fetchImpressionArrayFromStorage];
  184. NSMutableArray<NSString *> *resultArray = [[NSMutableArray alloc] init];
  185. for (NSDictionary *next in impressionsFromStorage) {
  186. [resultArray addObject:next[FIRIAM_ImpressionDictKeyForID]];
  187. }
  188. return resultArray;
  189. }
  190. - (void)recordNewFetchWithFetchCount:(NSInteger)fetchedMsgCount
  191. withTimestampInSeconds:(double)fetchTimestamp
  192. nextFetchWaitTime:(nullable NSNumber *)nextFetchWaitTime;
  193. {
  194. [self.defaults setDouble:fetchTimestamp forKey:FIRIAM_UserDefaultsKeyForLastFetchTimestamp];
  195. self.lastFetchTime = fetchTimestamp;
  196. if (nextFetchWaitTime != nil) {
  197. if (nextFetchWaitTime.doubleValue > kMaxFetchWaitTimeInSeconds) {
  198. FIRLogInfo(kFIRLoggerInAppMessaging, @"I-IAM270006",
  199. @"next fetch wait time %lf is too large. Ignore it.",
  200. nextFetchWaitTime.doubleValue);
  201. } else {
  202. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM270008",
  203. @"Setting next fetch wait time as %lf from fetch response.",
  204. nextFetchWaitTime.doubleValue);
  205. self.nextFetchWaitTime = nextFetchWaitTime.doubleValue;
  206. [self.defaults setObject:nextFetchWaitTime forKey:kUserDefaultsKeyForFetchWaitTime];
  207. }
  208. }
  209. }
  210. - (void)cleanupImpressions {
  211. [self.defaults setObject:@[] forKey:FIRIAM_UserDefaultsKeyForImpressions];
  212. }
  213. - (void)cleanupFetchRecords {
  214. [self.defaults setDouble:0 forKey:FIRIAM_UserDefaultsKeyForLastFetchTimestamp];
  215. self.lastFetchTime = 0;
  216. }
  217. @end
  218. #endif // TARGET_OS_IOS || TARGET_OS_TV