FIRIAMBookKeeper.m 10 KB

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