]> git.saurik.com Git - apple/security.git/blob - Analytics/SFAnalyticsActivityTracker.m
Security-58286.270.3.0.1.tar.gz
[apple/security.git] / Analytics / SFAnalyticsActivityTracker.m
1 /*
2 * Copyright (c) 2017 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #if __OBJC2__
25
26 #import "SFAnalyticsActivityTracker.h"
27 #import "SFAnalyticsActivityTracker+Internal.h"
28 #import "SFAnalytics.h"
29 #import <mach/mach_time.h>
30 #import "utilities/debugging.h"
31
32 @implementation SFAnalyticsActivityTracker {
33 dispatch_queue_t _queue;
34 NSString* _name;
35 Class _clientClass;
36 NSNumber* _measurement;
37 uint64_t _start;
38 BOOL _canceled;
39 }
40
41 - (instancetype)initWithName:(NSString*)name clientClass:(Class)className {
42 if (![name isKindOfClass:[NSString class]] || ![className isSubclassOfClass:[SFAnalytics class]] ) {
43 secerror("Cannot instantiate SFActivityTracker without name and client class");
44 return nil;
45 }
46
47 if (self = [super init]) {
48 _queue = dispatch_queue_create("SFAnalyticsActivityTracker queue", DISPATCH_QUEUE_SERIAL_WITH_AUTORELEASE_POOL);
49 _name = name;
50 _clientClass = className;
51 _measurement = nil;
52 _canceled = NO;
53 _start = 0;
54 }
55 return self;
56 }
57
58 - (void)performAction:(void (^)(void))action
59 {
60 [self start];
61 dispatch_sync(_queue, ^{
62 action();
63 });
64 [self stop];
65 }
66
67 - (void)start
68 {
69 if (_canceled) {
70 return;
71 }
72 NSAssert(_start == 0, @"SFAnalyticsActivityTracker user called start twice");
73 _start = mach_absolute_time();
74 }
75
76 - (void)stop
77 {
78 uint64_t end = mach_absolute_time();
79
80 if (_canceled) {
81 _start = 0;
82 return;
83 }
84 NSAssert(_start != 0, @"SFAnalyticsActivityTracker user called stop w/o calling start");
85
86 static mach_timebase_info_data_t sTimebaseInfo;
87 if ( sTimebaseInfo.denom == 0 ) {
88 (void)mach_timebase_info(&sTimebaseInfo);
89 }
90
91 _measurement = @([_measurement doubleValue] + (1.0f * (end - _start) * (1.0f * sTimebaseInfo.numer / sTimebaseInfo.denom)));
92 _start = 0;
93 }
94
95 - (void)cancel
96 {
97 _canceled = YES;
98 }
99
100 - (void)dealloc
101 {
102 if (_start != 0) {
103 [self stop];
104 }
105 if (!_canceled && _measurement != nil) {
106 [[_clientClass logger] logMetric:_measurement withName:_name];
107 }
108 }
109
110 @end
111
112 #endif