1 |
efrain |
1 |
/*
|
|
|
2 |
* Copyright 2018 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 <TargetConditionals.h>
|
|
|
18 |
#if TARGET_OS_IOS || TARGET_OS_TV
|
|
|
19 |
|
|
|
20 |
#import <UIKit/UIKit.h>
|
|
|
21 |
|
|
|
22 |
#import "FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
|
|
|
23 |
|
|
|
24 |
#import "FirebaseInAppMessaging/Sources/Analytics/FIRIAMClearcutLogStorage.h"
|
|
|
25 |
#import "FirebaseInAppMessaging/Sources/FIRCore+InAppMessaging.h"
|
|
|
26 |
#import "FirebaseInAppMessaging/Sources/Private/Util/FIRIAMTimeFetcher.h"
|
|
|
27 |
|
|
|
28 |
@implementation FIRIAMClearcutLogRecord
|
|
|
29 |
static NSString *const kEventTimestampKey = @"event_ts_seconds";
|
|
|
30 |
static NSString *const kEventExtensionJson = @"extension_js";
|
|
|
31 |
|
|
|
32 |
+ (BOOL)supportsSecureCoding {
|
|
|
33 |
return YES;
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
- (instancetype)initWithExtensionJsonString:(NSString *)jsonString
|
|
|
37 |
eventTimestampInSeconds:(NSInteger)eventTimestampInSeconds {
|
|
|
38 |
self = [super init];
|
|
|
39 |
if (self != nil) {
|
|
|
40 |
_eventTimestampInSeconds = eventTimestampInSeconds;
|
|
|
41 |
_eventExtensionJsonString = jsonString;
|
|
|
42 |
}
|
|
|
43 |
return self;
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
- (id)initWithCoder:(NSCoder *)decoder {
|
|
|
47 |
self = [super init];
|
|
|
48 |
if (self != nil) {
|
|
|
49 |
_eventTimestampInSeconds = [decoder decodeIntegerForKey:kEventTimestampKey];
|
|
|
50 |
_eventExtensionJsonString = [decoder decodeObjectOfClass:[NSString class]
|
|
|
51 |
forKey:kEventExtensionJson];
|
|
|
52 |
}
|
|
|
53 |
return self;
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
- (void)encodeWithCoder:(NSCoder *)encoder {
|
|
|
57 |
[encoder encodeInteger:self.eventTimestampInSeconds forKey:kEventTimestampKey];
|
|
|
58 |
[encoder encodeObject:self.eventExtensionJsonString forKey:kEventExtensionJson];
|
|
|
59 |
}
|
|
|
60 |
@end
|
|
|
61 |
|
|
|
62 |
@interface FIRIAMClearcutLogStorage ()
|
|
|
63 |
@property(nonatomic) NSInteger recordExpiresInSeconds;
|
|
|
64 |
@property(nonatomic) NSMutableArray<FIRIAMClearcutLogRecord *> *records;
|
|
|
65 |
@property(nonatomic) id<FIRIAMTimeFetcher> timeFetcher;
|
|
|
66 |
@end
|
|
|
67 |
|
|
|
68 |
// We keep all the records in memory and flush them into files upon receiving
|
|
|
69 |
// applicationDidEnterBackground notifications.
|
|
|
70 |
@implementation FIRIAMClearcutLogStorage
|
|
|
71 |
|
|
|
72 |
+ (NSString *)determineCacheFilePath {
|
|
|
73 |
static NSString *logCachePath;
|
|
|
74 |
static dispatch_once_t onceToken;
|
|
|
75 |
|
|
|
76 |
dispatch_once(&onceToken, ^{
|
|
|
77 |
NSString *libraryDirPath =
|
|
|
78 |
NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
|
|
|
79 |
logCachePath =
|
|
|
80 |
[NSString stringWithFormat:@"%@/firebase-iam-clearcut-retry-records", libraryDirPath];
|
|
|
81 |
FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM230001",
|
|
|
82 |
@"Persistent file path for clearcut log records is %@", logCachePath);
|
|
|
83 |
});
|
|
|
84 |
return logCachePath;
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
- (instancetype)initWithExpireAfterInSeconds:(NSInteger)expireInSeconds
|
|
|
88 |
withTimeFetcher:(id<FIRIAMTimeFetcher>)timeFetcher
|
|
|
89 |
cachePath:(nullable NSString *)cachePath {
|
|
|
90 |
if (self = [super init]) {
|
|
|
91 |
_records = [[NSMutableArray alloc] init];
|
|
|
92 |
_timeFetcher = timeFetcher;
|
|
|
93 |
_recordExpiresInSeconds = expireInSeconds;
|
|
|
94 |
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
95 |
selector:@selector(appWillBecomeInactive:)
|
|
|
96 |
name:UIApplicationWillResignActiveNotification
|
|
|
97 |
object:nil];
|
|
|
98 |
#if defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
|
|
|
99 |
if (@available(iOS 13.0, tvOS 13.0, *)) {
|
|
|
100 |
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
101 |
selector:@selector(appWillBecomeInactive:)
|
|
|
102 |
name:UISceneWillDeactivateNotification
|
|
|
103 |
object:nil];
|
|
|
104 |
}
|
|
|
105 |
#endif // defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
|
|
|
106 |
|
|
|
107 |
@try {
|
|
|
108 |
[self loadFromCachePath:cachePath];
|
|
|
109 |
} @catch (NSException *exception) {
|
|
|
110 |
FIRLogWarning(kFIRLoggerInAppMessaging, @"I-IAM230004",
|
|
|
111 |
@"Non-fatal exception in loading persisted clearcut log records: %@.",
|
|
|
112 |
exception);
|
|
|
113 |
}
|
|
|
114 |
}
|
|
|
115 |
return self;
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
- (instancetype)initWithExpireAfterInSeconds:(NSInteger)expireInSeconds
|
|
|
119 |
withTimeFetcher:(id<FIRIAMTimeFetcher>)timeFetcher {
|
|
|
120 |
return [self initWithExpireAfterInSeconds:expireInSeconds
|
|
|
121 |
withTimeFetcher:timeFetcher
|
|
|
122 |
cachePath:nil];
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
- (void)appWillBecomeInactive:(NSNotification *)notification {
|
|
|
126 |
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul), ^{
|
|
|
127 |
[self saveIntoCacheWithPath:nil];
|
|
|
128 |
});
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
- (void)dealloc {
|
|
|
132 |
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
- (void)pushRecords:(NSArray<FIRIAMClearcutLogRecord *> *)newRecords {
|
|
|
136 |
@synchronized(self) {
|
|
|
137 |
[self.records addObjectsFromArray:newRecords];
|
|
|
138 |
}
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
- (NSArray<FIRIAMClearcutLogRecord *> *)popStillValidRecordsForUpTo:(NSInteger)upTo {
|
|
|
142 |
NSMutableArray<FIRIAMClearcutLogRecord *> *resultArray = [[NSMutableArray alloc] init];
|
|
|
143 |
NSInteger nowInSeconds = (NSInteger)[self.timeFetcher currentTimestampInSeconds];
|
|
|
144 |
|
|
|
145 |
NSInteger next = 0;
|
|
|
146 |
|
|
|
147 |
@synchronized(self) {
|
|
|
148 |
while (resultArray.count < upTo && next < self.records.count) {
|
|
|
149 |
FIRIAMClearcutLogRecord *nextRecord = self.records[next++];
|
|
|
150 |
if (nextRecord.eventTimestampInSeconds > nowInSeconds - self.recordExpiresInSeconds) {
|
|
|
151 |
// record not expired yet
|
|
|
152 |
[resultArray addObject:nextRecord];
|
|
|
153 |
}
|
|
|
154 |
}
|
|
|
155 |
|
|
|
156 |
[self.records removeObjectsInRange:NSMakeRange(0, next)];
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM230005",
|
|
|
160 |
@"Returning %d clearcut retry records from popStillValidRecords",
|
|
|
161 |
(int)resultArray.count);
|
|
|
162 |
return resultArray;
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
- (void)loadFromCachePath:(NSString *)cacheFilePath {
|
|
|
166 |
NSString *filePath = cacheFilePath == nil ? [self.class determineCacheFilePath] : cacheFilePath;
|
|
|
167 |
|
|
|
168 |
NSTimeInterval start = [self.timeFetcher currentTimestampInSeconds];
|
|
|
169 |
#pragma clang diagnostic push
|
|
|
170 |
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
|
|
171 |
id fetchedClearcutRetryRecords = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
|
|
|
172 |
#pragma clang diagnostic pop
|
|
|
173 |
if (fetchedClearcutRetryRecords) {
|
|
|
174 |
@synchronized(self) {
|
|
|
175 |
self.records = (NSMutableArray<FIRIAMClearcutLogRecord *> *)fetchedClearcutRetryRecords;
|
|
|
176 |
}
|
|
|
177 |
FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM230002",
|
|
|
178 |
@"Loaded %d clearcut log records from file in %lf seconds", (int)self.records.count,
|
|
|
179 |
(double)[self.timeFetcher currentTimestampInSeconds] - start);
|
|
|
180 |
}
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
- (BOOL)saveIntoCacheWithPath:(NSString *)cacheFilePath {
|
|
|
184 |
NSString *filePath = cacheFilePath == nil ? [self.class determineCacheFilePath] : cacheFilePath;
|
|
|
185 |
@synchronized(self) {
|
|
|
186 |
#pragma clang diagnostic push
|
|
|
187 |
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
|
|
188 |
BOOL saveResult = [NSKeyedArchiver archiveRootObject:self.records toFile:filePath];
|
|
|
189 |
#pragma clang diagnostic pop
|
|
|
190 |
FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM230003",
|
|
|
191 |
@"Saving %d clearcut log records into file is %@", (int)self.records.count,
|
|
|
192 |
saveResult ? @"successful" : @"failure");
|
|
|
193 |
|
|
|
194 |
return saveResult;
|
|
|
195 |
}
|
|
|
196 |
}
|
|
|
197 |
@end
|
|
|
198 |
|
|
|
199 |
#endif // TARGET_OS_IOS || TARGET_OS_TV
|