Proyectos de Subversion Iphone Microlearning

Rev

| Ultima modificación | Ver Log |

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