FIRHeartbeatLogger.m 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. - (FIRHeartbeatsPayload *)flushHeartbeatsIntoPayload {
  62. FIRHeartbeatsPayload *payload = [_heartbeatController flush];
  63. return payload;
  64. }
  65. #endif // FIREBASE_BUILD_CMAKE
  66. - (FIRHeartbeatInfoCode)heartbeatCodeForToday {
  67. #ifndef FIREBASE_BUILD_CMAKE
  68. FIRHeartbeatsPayload *todaysHeartbeatPayload = [_heartbeatController flushHeartbeatFromToday];
  69. if ([todaysHeartbeatPayload isEmpty]) {
  70. return FIRHeartbeatInfoCodeNone;
  71. } else {
  72. return FIRHeartbeatInfoCodeGlobal;
  73. }
  74. #else
  75. return FIRHeartbeatInfoCodeNone;
  76. #endif // FIREBASE_BUILD_CMAKE
  77. }
  78. @end