FIRHeartbeatLogger.m 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2021 Google LLC
  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 <Foundation/Foundation.h>
  15. #ifndef FIREBASE_BUILD_CMAKE
  16. @import FirebaseCoreInternal;
  17. #endif // FIREBASE_BUILD_CMAKE
  18. #import "FirebaseCore/Extension/FIRAppInternal.h"
  19. #import "FirebaseCore/Extension/FIRHeartbeatLogger.h"
  20. #ifndef FIREBASE_BUILD_CMAKE
  21. NSString *_Nullable FIRHeaderValueFromHeartbeatsPayload(FIRHeartbeatsPayload *heartbeatsPayload) {
  22. if ([heartbeatsPayload isEmpty]) {
  23. return nil;
  24. }
  25. return [heartbeatsPayload headerValue];
  26. }
  27. #endif // FIREBASE_BUILD_CMAKE
  28. @interface FIRHeartbeatLogger ()
  29. #ifndef FIREBASE_BUILD_CMAKE
  30. @property(nonatomic, readonly) FIRHeartbeatController *heartbeatController;
  31. #endif // FIREBASE_BUILD_CMAKE
  32. @property(copy, readonly) NSString * (^userAgentProvider)(void);
  33. @end
  34. @implementation FIRHeartbeatLogger
  35. - (instancetype)initWithAppID:(NSString *)appID {
  36. return [self initWithAppID:appID userAgentProvider:[[self class] currentUserAgentProvider]];
  37. }
  38. - (instancetype)initWithAppID:(NSString *)appID
  39. userAgentProvider:(NSString * (^)(void))userAgentProvider {
  40. self = [super init];
  41. if (self) {
  42. #ifndef FIREBASE_BUILD_CMAKE
  43. _heartbeatController = [[FIRHeartbeatController alloc] initWithId:[appID copy]];
  44. #endif // FIREBASE_BUILD_CMAKE
  45. _userAgentProvider = [userAgentProvider copy];
  46. }
  47. return self;
  48. }
  49. + (NSString * (^)(void))currentUserAgentProvider {
  50. return ^NSString * {
  51. return [FIRApp firebaseUserAgent];
  52. };
  53. }
  54. - (void)log {
  55. NSString *userAgent = _userAgentProvider();
  56. #ifndef FIREBASE_BUILD_CMAKE
  57. [_heartbeatController log:userAgent];
  58. #endif // FIREBASE_BUILD_CMAKE
  59. }
  60. #ifndef FIREBASE_BUILD_CMAKE
  61. - (NSString *_Nullable)headerValue {
  62. return FIRHeaderValueFromHeartbeatsPayload([self flushHeartbeatsIntoPayload]);
  63. }
  64. - (FIRHeartbeatsPayload *)flushHeartbeatsIntoPayload {
  65. FIRHeartbeatsPayload *payload = [_heartbeatController flush];
  66. return payload;
  67. }
  68. #endif // FIREBASE_BUILD_CMAKE
  69. - (FIRDailyHeartbeatCode)heartbeatCodeForToday {
  70. #ifndef FIREBASE_BUILD_CMAKE
  71. FIRHeartbeatsPayload *todaysHeartbeatPayload = [_heartbeatController flushHeartbeatFromToday];
  72. if ([todaysHeartbeatPayload isEmpty]) {
  73. return FIRDailyHeartbeatCodeNone;
  74. } else {
  75. return FIRDailyHeartbeatCodeSome;
  76. }
  77. #else
  78. return FIRDailyHeartbeatCodeNone;
  79. #endif // FIREBASE_BUILD_CMAKE
  80. }
  81. @end