FIRHeartbeatLogger.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. - (void)asyncHeaderValueWithCompletionHandler:(void (^)(NSString *_Nullable))completionHandler
  65. API_AVAILABLE(ios(13.0), macosx(10.15), macCatalyst(13.0), tvos(13.0), watchos(6.0)) {
  66. [self flushHeartbeatsIntoPayloadWithCompletionHandler:^(FIRHeartbeatsPayload *payload) {
  67. completionHandler(FIRHeaderValueFromHeartbeatsPayload(payload));
  68. }];
  69. }
  70. - (FIRHeartbeatsPayload *)flushHeartbeatsIntoPayload {
  71. FIRHeartbeatsPayload *payload = [_heartbeatController flush];
  72. return payload;
  73. }
  74. - (void)flushHeartbeatsIntoPayloadWithCompletionHandler:
  75. (void (^)(FIRHeartbeatsPayload *))completionHandler
  76. API_AVAILABLE(ios(13.0), macosx(10.15), macCatalyst(13.0), tvos(13.0), watchos(6.0)) {
  77. [_heartbeatController flushAsyncWithCompletionHandler:^(FIRHeartbeatsPayload *payload) {
  78. completionHandler(payload);
  79. }];
  80. }
  81. #endif // FIREBASE_BUILD_CMAKE
  82. - (FIRDailyHeartbeatCode)heartbeatCodeForToday {
  83. #ifndef FIREBASE_BUILD_CMAKE
  84. FIRHeartbeatsPayload *todaysHeartbeatPayload = [_heartbeatController flushHeartbeatFromToday];
  85. if ([todaysHeartbeatPayload isEmpty]) {
  86. return FIRDailyHeartbeatCodeNone;
  87. } else {
  88. return FIRDailyHeartbeatCodeSome;
  89. }
  90. #else
  91. return FIRDailyHeartbeatCodeNone;
  92. #endif // FIREBASE_BUILD_CMAKE
  93. }
  94. @end