1 |
efrain |
1 |
// Copyright 2019 Google
|
|
|
2 |
//
|
|
|
3 |
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
4 |
// you may not use this file except in compliance with the License.
|
|
|
5 |
// You may obtain a copy of the License at
|
|
|
6 |
//
|
|
|
7 |
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
8 |
//
|
|
|
9 |
// Unless required by applicable law or agreed to in writing, software
|
|
|
10 |
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
12 |
// See the License for the specific language governing permissions and
|
|
|
13 |
// limitations under the License.
|
|
|
14 |
|
|
|
15 |
#import "FirebaseCore/Sources/Private/FIRHeartbeatInfo.h"
|
|
|
16 |
#import <GoogleUtilities/GULHeartbeatDateStorable.h>
|
|
|
17 |
#import <GoogleUtilities/GULHeartbeatDateStorage.h>
|
|
|
18 |
#import <GoogleUtilities/GULHeartbeatDateStorageUserDefaults.h>
|
|
|
19 |
#import <GoogleUtilities/GULLogger.h>
|
|
|
20 |
#import "FirebaseCore/Sources/Private/FIRAppInternal.h"
|
|
|
21 |
|
|
|
22 |
const static long secondsInDay = 86400;
|
|
|
23 |
@implementation FIRHeartbeatInfo : NSObject
|
|
|
24 |
|
|
|
25 |
/** Updates the storage with the heartbeat information corresponding to this tag.
|
|
|
26 |
* @param heartbeatTag Tag which could either be sdk specific tag or the global tag.
|
|
|
27 |
* @return Boolean representing whether the heartbeat needs to be sent for this tag or not.
|
|
|
28 |
*/
|
|
|
29 |
+ (BOOL)updateIfNeededHeartbeatDateForTag:(NSString *)heartbeatTag {
|
|
|
30 |
@synchronized(self) {
|
|
|
31 |
NSString *const kHeartbeatStorageName = @"HEARTBEAT_INFO_STORAGE";
|
|
|
32 |
id<GULHeartbeatDateStorable> dataStorage;
|
|
|
33 |
#if TARGET_OS_TV
|
|
|
34 |
NSUserDefaults *defaults =
|
|
|
35 |
[[NSUserDefaults alloc] initWithSuiteName:kFirebaseCoreDefaultsSuiteName];
|
|
|
36 |
dataStorage =
|
|
|
37 |
[[GULHeartbeatDateStorageUserDefaults alloc] initWithDefaults:defaults
|
|
|
38 |
key:kHeartbeatStorageName];
|
|
|
39 |
#else
|
|
|
40 |
dataStorage = [[GULHeartbeatDateStorage alloc] initWithFileName:kHeartbeatStorageName];
|
|
|
41 |
#endif
|
|
|
42 |
NSDate *heartbeatTime = [dataStorage heartbeatDateForTag:heartbeatTag];
|
|
|
43 |
NSDate *currentDate = [NSDate date];
|
|
|
44 |
if (heartbeatTime != nil) {
|
|
|
45 |
NSTimeInterval secondsBetween = [currentDate timeIntervalSinceDate:heartbeatTime];
|
|
|
46 |
if (secondsBetween < secondsInDay) {
|
|
|
47 |
return false;
|
|
|
48 |
}
|
|
|
49 |
}
|
|
|
50 |
return [dataStorage setHearbeatDate:currentDate forTag:heartbeatTag];
|
|
|
51 |
}
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
+ (FIRHeartbeatInfoCode)heartbeatCodeForTag:(NSString *)heartbeatTag {
|
|
|
55 |
NSString *globalTag = @"GLOBAL";
|
|
|
56 |
BOOL isSdkHeartbeatNeeded = [FIRHeartbeatInfo updateIfNeededHeartbeatDateForTag:heartbeatTag];
|
|
|
57 |
BOOL isGlobalHeartbeatNeeded = [FIRHeartbeatInfo updateIfNeededHeartbeatDateForTag:globalTag];
|
|
|
58 |
if (!isSdkHeartbeatNeeded && !isGlobalHeartbeatNeeded) {
|
|
|
59 |
// Both sdk and global heartbeat not needed.
|
|
|
60 |
return FIRHeartbeatInfoCodeNone;
|
|
|
61 |
} else if (isSdkHeartbeatNeeded && !isGlobalHeartbeatNeeded) {
|
|
|
62 |
// Only SDK heartbeat needed.
|
|
|
63 |
return FIRHeartbeatInfoCodeSDK;
|
|
|
64 |
} else if (!isSdkHeartbeatNeeded && isGlobalHeartbeatNeeded) {
|
|
|
65 |
// Only global heartbeat needed.
|
|
|
66 |
return FIRHeartbeatInfoCodeGlobal;
|
|
|
67 |
} else {
|
|
|
68 |
// Both sdk and global heartbeat are needed.
|
|
|
69 |
return FIRHeartbeatInfoCodeCombined;
|
|
|
70 |
}
|
|
|
71 |
}
|
|
|
72 |
@end
|