FPRSessionManager.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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/FIRPerformance_Private.h"
  17. #import "FirebasePerformance/Sources/Configurations/FPRConfigurations.h"
  18. #import "FirebasePerformance/Sources/FPRConsoleLogger.h"
  19. #import "FirebasePerformance/Sources/Gauges/FPRGaugeManager.h"
  20. #import <UIKit/UIKit.h>
  21. NSString *const kFPRSessionIdUpdatedNotification = @"kFPRSessionIdUpdatedNotification";
  22. NSString *const kFPRSessionIdNotificationKey = @"kFPRSessionIdNotificationKey";
  23. @interface FPRSessionManager ()
  24. @property(nonatomic, readwrite) NSNotificationCenter *sessionNotificationCenter;
  25. @property(nonatomic) BOOL trackingApplicationStateChanges;
  26. /**
  27. * Creates an instance of FPRSesssionManager with the notification center provided. All the
  28. * notifications from the session manager will sent using this notification center.
  29. *
  30. * @param notificationCenter Notification center with which the session manager with be initialized.
  31. * @return Returns an instance of the session manager.
  32. */
  33. - (instancetype)initWithNotificationCenter:(NSNotificationCenter *)notificationCenter;
  34. @end
  35. @implementation FPRSessionManager
  36. + (FPRSessionManager *)sharedInstance {
  37. static FPRSessionManager *instance;
  38. static dispatch_once_t onceToken;
  39. dispatch_once(&onceToken, ^{
  40. NSNotificationCenter *notificationCenter = [[NSNotificationCenter alloc] init];
  41. instance = [[FPRSessionManager alloc] initWithNotificationCenter:notificationCenter];
  42. });
  43. return instance;
  44. }
  45. - (FPRSessionManager *)initWithNotificationCenter:(NSNotificationCenter *)notificationCenter {
  46. self = [super init];
  47. if (self) {
  48. _sessionNotificationCenter = notificationCenter;
  49. _trackingApplicationStateChanges = NO;
  50. [self updateSessionId:nil];
  51. }
  52. return self;
  53. }
  54. - (void)startTrackingAppStateChanges {
  55. if (!self.trackingApplicationStateChanges) {
  56. // Starts tracking the application life cycle events during which the session Ids change.
  57. [[NSNotificationCenter defaultCenter] addObserver:self
  58. selector:@selector(updateSessionId:)
  59. name:UIApplicationWillEnterForegroundNotification
  60. object:[UIApplication sharedApplication]];
  61. self.trackingApplicationStateChanges = YES;
  62. }
  63. }
  64. - (void)renewSessionIdIfRunningTooLong {
  65. NSUInteger maxSessionLength = [[FPRConfigurations sharedInstance] maxSessionLengthInMinutes];
  66. if (self.sessionDetails.sessionLengthInMinutes > maxSessionLength) {
  67. [self updateSessionId:nil];
  68. }
  69. }
  70. /**
  71. * Updates the sessionId on the arrival of a notification.
  72. *
  73. * @param notification Notification received.
  74. */
  75. - (void)updateSessionId:(NSNotification *)notification {
  76. NSUUID *uuid = [NSUUID UUID];
  77. NSString *sessionIdString = [uuid UUIDString];
  78. sessionIdString = [sessionIdString stringByReplacingOccurrencesOfString:@"-" withString:@""];
  79. sessionIdString = [sessionIdString lowercaseString];
  80. FPRSessionOptions sessionOptions = FPRSessionOptionsNone;
  81. FPRGaugeManager *gaugeManager = [FPRGaugeManager sharedInstance];
  82. if ([self isGaugeCollectionEnabledForSessionId:sessionIdString]) {
  83. [gaugeManager startCollectingGauges:FPRGaugeCPU | FPRGaugeMemory forSessionId:sessionIdString];
  84. sessionOptions = FPRSessionOptionsGauges;
  85. } else {
  86. [gaugeManager stopCollectingGauges:FPRGaugeCPU | FPRGaugeMemory];
  87. }
  88. // Send session id to crashlytics
  89. NSString *isSessionVerbose = @"FALSE";
  90. if (sessionOptions == FPRSessionOptionsGauges) {
  91. isSessionVerbose = @"TRUE";
  92. }
  93. NSDictionary *crashlyticsSessionBreadcrumb = @{
  94. @"source" : @"FirebasePerformance",
  95. @"name" : @"App in foreground",
  96. @"sessionID" : sessionIdString,
  97. @"verbose" : isSessionVerbose,
  98. };
  99. NSError *error;
  100. NSData *crashlyticsSessionJsonBreadcrumb =
  101. [NSJSONSerialization dataWithJSONObject:crashlyticsSessionBreadcrumb options:NSJSONWritingPrettyPrinted error:&error];
  102. if (!crashlyticsSessionJsonBreadcrumb) {
  103. NSLog(@"Got an error: %@", error);
  104. } else {
  105. NSString *jsonString = [[NSString alloc] initWithData:crashlyticsSessionJsonBreadcrumb
  106. encoding:NSUTF8StringEncoding];
  107. // Getting the FirePerf shared instance here. Is there a better way to do that internally?
  108. [[FIRPerformance sharedInstance].crashlytics log:jsonString];
  109. }
  110. FPRLogDebug(kFPRSessionId, @"Session Id generated - %@", sessionIdString);
  111. FPRSessionDetails *sessionInfo = [[FPRSessionDetails alloc] initWithSessionId:sessionIdString
  112. options:sessionOptions];
  113. self.sessionDetails = sessionInfo;
  114. NSMutableDictionary<NSString *, FPRSessionDetails *> *userInfo =
  115. [[NSMutableDictionary alloc] init];
  116. [userInfo setObject:sessionInfo forKey:kFPRSessionIdNotificationKey];
  117. [self.sessionNotificationCenter postNotificationName:kFPRSessionIdUpdatedNotification
  118. object:self
  119. userInfo:[userInfo copy]];
  120. }
  121. /**
  122. * Checks if the provided sessionId can have gauge data collection enabled.
  123. *
  124. * @param sessionId Session Id for which the check is done.
  125. * @return YES if gauge collection is enabled, NO otherwise.
  126. */
  127. - (BOOL)isGaugeCollectionEnabledForSessionId:(NSString *)sessionId {
  128. float_t sessionSamplePercentage = [[FPRConfigurations sharedInstance] sessionsSamplingPercentage];
  129. double randomNumberBetween0And1 = ((double)arc4random() / UINT_MAX);
  130. BOOL sessionsEnabled = randomNumberBetween0And1 * 100 < sessionSamplePercentage;
  131. return sessionsEnabled;
  132. }
  133. - (void)dealloc {
  134. if (self.trackingApplicationStateChanges) {
  135. [[NSNotificationCenter defaultCenter] removeObserver:self
  136. name:UIApplicationDidBecomeActiveNotification
  137. object:[UIApplication sharedApplication]];
  138. }
  139. }
  140. @end