FPRSessionManager.m 5.2 KB

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