FPRSessionManager.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Copyright 2020 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 "FirebasePerformance/Sources/AppActivity/FPRSessionManager.h"
  15. #import "FirebasePerformance/Sources/AppActivity/FPRSessionManager+Private.h"
  16. #import "FirebasePerformance/Sources/Configurations/FPRConfigurations.h"
  17. #import "FirebasePerformance/Sources/FPRConsoleLogger.h"
  18. #import "FirebasePerformance/Sources/Gauges/FPRGaugeManager.h"
  19. #import <UIKit/UIKit.h>
  20. NSString *const kFPRSessionIdUpdatedNotification = @"kFPRSessionIdUpdatedNotification";
  21. NSString *const kFPRSessionIdNotificationKey = @"kFPRSessionIdNotificationKey";
  22. @interface FPRSessionManager ()
  23. @property(nonatomic, readwrite) NSNotificationCenter *sessionNotificationCenter;
  24. @property(nonatomic) BOOL trackingApplicationStateChanges;
  25. /**
  26. * Creates an instance of FPRSesssionManager with the notification center provided. All the
  27. * notifications from the session manager will sent using this notification center.
  28. *
  29. * @param notificationCenter Notification center with which the session manager with be initialized.
  30. * @return Returns an instance of the session manager.
  31. */
  32. - (instancetype)initWithNotificationCenter:(NSNotificationCenter *)notificationCenter;
  33. @end
  34. @implementation FPRSessionManager
  35. + (FPRSessionManager *)sharedInstance {
  36. static FPRSessionManager *instance;
  37. static dispatch_once_t onceToken;
  38. dispatch_once(&onceToken, ^{
  39. NSNotificationCenter *notificationCenter = [[NSNotificationCenter alloc] init];
  40. instance = [[FPRSessionManager alloc] initWithNotificationCenter:notificationCenter];
  41. });
  42. return instance;
  43. }
  44. - (FPRSessionManager *)initWithNotificationCenter:(NSNotificationCenter *)notificationCenter {
  45. self = [super init];
  46. if (self) {
  47. _sessionNotificationCenter = notificationCenter;
  48. _trackingApplicationStateChanges = NO;
  49. [self updateSessionId:nil];
  50. }
  51. return self;
  52. }
  53. - (void)startTrackingAppStateChanges {
  54. if (!self.trackingApplicationStateChanges) {
  55. // Starts tracking the application life cycle events during which the session Ids change.
  56. [[NSNotificationCenter defaultCenter] addObserver:self
  57. selector:@selector(updateSessionId:)
  58. name:UIApplicationWillEnterForegroundNotification
  59. object:[UIApplication sharedApplication]];
  60. self.trackingApplicationStateChanges = YES;
  61. }
  62. }
  63. - (void)renewSessionIdIfRunningTooLong {
  64. NSUInteger maxSessionLength = [[FPRConfigurations sharedInstance] maxSessionLengthInMinutes];
  65. if (self.sessionDetails.sessionLengthInMinutes > maxSessionLength) {
  66. [self updateSessionId:nil];
  67. }
  68. }
  69. /**
  70. * Updates the sessionId on the arrival of a notification.
  71. *
  72. * @param notification Notification received.
  73. */
  74. - (void)updateSessionId:(NSNotification *)notification {
  75. NSUUID *uuid = [NSUUID UUID];
  76. NSString *sessionIdString = [uuid UUIDString];
  77. sessionIdString = [sessionIdString stringByReplacingOccurrencesOfString:@"-" withString:@""];
  78. sessionIdString = [sessionIdString lowercaseString];
  79. FPRSessionOptions sessionOptions = FPRSessionOptionsNone;
  80. FPRGaugeManager *gaugeManager = [FPRGaugeManager sharedInstance];
  81. if ([self isGaugeCollectionEnabledForSessionId:sessionIdString]) {
  82. [gaugeManager startCollectingGauges:FPRGaugeCPU | FPRGaugeMemory forSessionId:sessionIdString];
  83. sessionOptions = FPRSessionOptionsGauges;
  84. } else {
  85. [gaugeManager stopCollectingGauges:FPRGaugeCPU | FPRGaugeMemory];
  86. }
  87. FPRLogDebug(kFPRSessionId, @"Session Id generated - %@", sessionIdString);
  88. FPRSessionDetails *sessionInfo = [[FPRSessionDetails alloc] initWithSessionId:sessionIdString
  89. options:sessionOptions];
  90. self.sessionDetails = sessionInfo;
  91. NSMutableDictionary<NSString *, FPRSessionDetails *> *userInfo =
  92. [[NSMutableDictionary alloc] init];
  93. [userInfo setObject:sessionInfo forKey:kFPRSessionIdNotificationKey];
  94. [self.sessionNotificationCenter postNotificationName:kFPRSessionIdUpdatedNotification
  95. object:self
  96. userInfo:[userInfo copy]];
  97. }
  98. /**
  99. * Checks if the provided sessionId can have gauge data collection enabled.
  100. *
  101. * @param sessionId Session Id for which the check is done.
  102. * @return YES if gauge collection is enabled, NO otherwise.
  103. */
  104. - (BOOL)isGaugeCollectionEnabledForSessionId:(NSString *)sessionId {
  105. float_t sessionSamplePercentage = [[FPRConfigurations sharedInstance] sessionsSamplingPercentage];
  106. double randomNumberBetween0And1 = ((double)arc4random() / UINT_MAX);
  107. BOOL sessionsEnabled = randomNumberBetween0And1 * 100 < sessionSamplePercentage;
  108. return sessionsEnabled;
  109. }
  110. - (void)dealloc {
  111. if (self.trackingApplicationStateChanges) {
  112. [[NSNotificationCenter defaultCenter] removeObserver:self
  113. name:UIApplicationDidBecomeActiveNotification
  114. object:[UIApplication sharedApplication]];
  115. }
  116. }
  117. @end