FIRHeartbeatLogger.m 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. #import "FirebaseCore/Extension/FIRHeartbeatLogger.h"
  16. @import FirebaseCoreInternal;
  17. #import "FirebaseCore/Extension/FIRAppInternal.h"
  18. NSString *_Nullable FIRHeaderValueFromHeartbeatsPayload(FIRHeartbeatsPayload *heartbeatsPayload) {
  19. if ([heartbeatsPayload isEmpty]) {
  20. return nil;
  21. }
  22. return [heartbeatsPayload headerValue];
  23. }
  24. @interface FIRHeartbeatLogger ()
  25. @property(nonatomic, readonly) FIRHeartbeatController *heartbeatController;
  26. @property(copy, readonly) NSString * (^userAgentProvider)(void);
  27. @end
  28. @implementation FIRHeartbeatLogger
  29. - (instancetype)initWithAppID:(NSString *)appID {
  30. return [self initWithAppID:appID userAgentProvider:[[self class] currentUserAgentProvider]];
  31. }
  32. - (instancetype)initWithAppID:(NSString *)appID
  33. userAgentProvider:(NSString * (^)(void))userAgentProvider {
  34. self = [super init];
  35. if (self) {
  36. _heartbeatController = [[FIRHeartbeatController alloc] initWithId:[appID copy]];
  37. _userAgentProvider = [userAgentProvider copy];
  38. }
  39. return self;
  40. }
  41. + (NSString * (^)(void))currentUserAgentProvider {
  42. return ^NSString * {
  43. return [FIRApp firebaseUserAgent];
  44. };
  45. }
  46. - (void)log {
  47. NSString *userAgent = _userAgentProvider();
  48. [_heartbeatController log:userAgent];
  49. }
  50. - (FIRHeartbeatsPayload *)flushHeartbeatsIntoPayload {
  51. FIRHeartbeatsPayload *payload = [_heartbeatController flush];
  52. return payload;
  53. }
  54. // TODO: Rename to `heartbeatCodeForToday` in future PR's.
  55. - (FIRHeartbeatInfoCode)heartbeatCode {
  56. FIRHeartbeatsPayload *todaysHeartbeatPayload = [_heartbeatController flushHeartbeatFromToday];
  57. if ([todaysHeartbeatPayload isEmpty]) {
  58. return FIRHeartbeatInfoCodeNone;
  59. } else {
  60. return FIRHeartbeatInfoCodeGlobal;
  61. }
  62. }
  63. @end