No Description

FIRHeartbeatInfo.m 2.5KB

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