FIRHeartbeatInfo.m 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. #import "FirebaseCore/Sources/Private/FIRHeartbeatInfo.h"
  15. #import <GoogleUtilities/GULHeartbeatDateStorable.h>
  16. #import <GoogleUtilities/GULHeartbeatDateStorage.h>
  17. #import <GoogleUtilities/GULHeartbeatDateStorageUserDefaults.h>
  18. #import <GoogleUtilities/GULLogger.h>
  19. #import "FirebaseCore/Sources/Private/FIRAppInternal.h"
  20. const static long secondsInDay = 86400;
  21. @implementation FIRHeartbeatInfo : NSObject
  22. /** Updates the storage with the heartbeat information corresponding to this tag.
  23. * @param heartbeatTag Tag which could either be sdk specific tag or the global tag.
  24. * @return Boolean representing whether the heartbeat needs to be sent for this tag or not.
  25. */
  26. + (BOOL)updateIfNeededHeartbeatDateForTag:(NSString *)heartbeatTag {
  27. @synchronized(self) {
  28. NSString *const kHeartbeatStorageName = @"HEARTBEAT_INFO_STORAGE";
  29. id<GULHeartbeatDateStorable> dataStorage;
  30. #if TARGET_OS_TV
  31. NSUserDefaults *defaults =
  32. [[NSUserDefaults alloc] initWithSuiteName:kFirebaseCoreDefaultsSuiteName];
  33. dataStorage =
  34. [[GULHeartbeatDateStorageUserDefaults alloc] initWithDefaults:defaults
  35. key:kHeartbeatStorageName];
  36. #else
  37. dataStorage = [[GULHeartbeatDateStorage alloc] initWithFileName:kHeartbeatStorageName];
  38. #endif
  39. NSDate *heartbeatTime = [dataStorage heartbeatDateForTag:heartbeatTag];
  40. NSDate *currentDate = [NSDate date];
  41. if (heartbeatTime != nil) {
  42. NSTimeInterval secondsBetween = [currentDate timeIntervalSinceDate:heartbeatTime];
  43. if (secondsBetween < secondsInDay) {
  44. return false;
  45. }
  46. }
  47. return [dataStorage setHearbeatDate:currentDate forTag:heartbeatTag];
  48. }
  49. }
  50. + (FIRHeartbeatInfoCode)heartbeatCodeForTag:(NSString *)heartbeatTag {
  51. NSString *globalTag = @"GLOBAL";
  52. BOOL isSdkHeartbeatNeeded = [FIRHeartbeatInfo updateIfNeededHeartbeatDateForTag:heartbeatTag];
  53. BOOL isGlobalHeartbeatNeeded = [FIRHeartbeatInfo updateIfNeededHeartbeatDateForTag:globalTag];
  54. if (!isSdkHeartbeatNeeded && !isGlobalHeartbeatNeeded) {
  55. // Both sdk and global heartbeat not needed.
  56. return FIRHeartbeatInfoCodeNone;
  57. } else if (isSdkHeartbeatNeeded && !isGlobalHeartbeatNeeded) {
  58. // Only SDK heartbeat needed.
  59. return FIRHeartbeatInfoCodeSDK;
  60. } else if (!isSdkHeartbeatNeeded && isGlobalHeartbeatNeeded) {
  61. // Only global heartbeat needed.
  62. return FIRHeartbeatInfoCodeGlobal;
  63. } else {
  64. // Both sdk and global heartbeat are needed.
  65. return FIRHeartbeatInfoCodeCombined;
  66. }
  67. }
  68. @end