1 |
efrain |
1 |
/*
|
|
|
2 |
* Copyright 2020 Google LLC
|
|
|
3 |
*
|
|
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
5 |
* you may not use this file except in compliance with the License.
|
|
|
6 |
* You may obtain a copy of the License at
|
|
|
7 |
*
|
|
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
9 |
*
|
|
|
10 |
* Unless required by applicable law or agreed to in writing, software
|
|
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
13 |
* See the License for the specific language governing permissions and
|
|
|
14 |
* limitations under the License.
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
#import "FirebaseCore/Sources/FIRFirebaseUserAgent.h"
|
|
|
18 |
|
|
|
19 |
#import <GoogleUtilities/GULAppEnvironmentUtil.h>
|
|
|
20 |
|
|
|
21 |
@interface FIRFirebaseUserAgent ()
|
|
|
22 |
|
|
|
23 |
@property(nonatomic, readonly) NSMutableDictionary<NSString *, NSString *> *valuesByComponent;
|
|
|
24 |
@property(nonatomic, readonly) NSDictionary<NSString *, NSString *> *environmentComponents;
|
|
|
25 |
@property(nonatomic, readonly) NSString *firebaseUserAgent;
|
|
|
26 |
|
|
|
27 |
@end
|
|
|
28 |
|
|
|
29 |
@implementation FIRFirebaseUserAgent
|
|
|
30 |
|
|
|
31 |
@synthesize firebaseUserAgent = _firebaseUserAgent;
|
|
|
32 |
@synthesize environmentComponents = _environmentComponents;
|
|
|
33 |
|
|
|
34 |
- (instancetype)init {
|
|
|
35 |
self = [super init];
|
|
|
36 |
if (self) {
|
|
|
37 |
_valuesByComponent = [[NSMutableDictionary alloc] init];
|
|
|
38 |
}
|
|
|
39 |
return self;
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
- (NSString *)firebaseUserAgent {
|
|
|
43 |
@synchronized(self) {
|
|
|
44 |
if (_firebaseUserAgent == nil) {
|
|
|
45 |
NSMutableDictionary<NSString *, NSString *> *allComponents =
|
|
|
46 |
[self.valuesByComponent mutableCopy];
|
|
|
47 |
[allComponents setValuesForKeysWithDictionary:self.environmentComponents];
|
|
|
48 |
|
|
|
49 |
__block NSMutableArray<NSString *> *components =
|
|
|
50 |
[[NSMutableArray<NSString *> alloc] initWithCapacity:self.valuesByComponent.count];
|
|
|
51 |
[allComponents enumerateKeysAndObjectsUsingBlock:^(
|
|
|
52 |
NSString *_Nonnull name, NSString *_Nonnull value, BOOL *_Nonnull stop) {
|
|
|
53 |
[components addObject:[NSString stringWithFormat:@"%@/%@", name, value]];
|
|
|
54 |
}];
|
|
|
55 |
[components sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
|
|
|
56 |
_firebaseUserAgent = [components componentsJoinedByString:@" "];
|
|
|
57 |
}
|
|
|
58 |
return _firebaseUserAgent;
|
|
|
59 |
}
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
- (void)setValue:(nullable NSString *)value forComponent:(NSString *)componentName {
|
|
|
63 |
@synchronized(self) {
|
|
|
64 |
self.valuesByComponent[componentName] = value;
|
|
|
65 |
// Reset cached user agent string.
|
|
|
66 |
_firebaseUserAgent = nil;
|
|
|
67 |
}
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
- (void)reset {
|
|
|
71 |
@synchronized(self) {
|
|
|
72 |
// Reset components.
|
|
|
73 |
_valuesByComponent = [[[self class] environmentComponents] mutableCopy];
|
|
|
74 |
// Reset cached user agent string.
|
|
|
75 |
_firebaseUserAgent = nil;
|
|
|
76 |
}
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
#pragma mark - Environment components
|
|
|
80 |
|
|
|
81 |
- (NSDictionary<NSString *, NSString *> *)environmentComponents {
|
|
|
82 |
if (_environmentComponents == nil) {
|
|
|
83 |
_environmentComponents = [[self class] environmentComponents];
|
|
|
84 |
}
|
|
|
85 |
return _environmentComponents;
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
+ (NSDictionary<NSString *, NSString *> *)environmentComponents {
|
|
|
89 |
NSMutableDictionary<NSString *, NSString *> *components = [NSMutableDictionary dictionary];
|
|
|
90 |
|
|
|
91 |
NSDictionary<NSString *, id> *info = [[NSBundle mainBundle] infoDictionary];
|
|
|
92 |
NSString *xcodeVersion = info[@"DTXcodeBuild"];
|
|
|
93 |
NSString *appleSdkVersion = info[@"DTSDKBuild"];
|
|
|
94 |
NSString *isFromAppstoreFlagValue = [GULAppEnvironmentUtil isFromAppStore] ? @"true" : @"false";
|
|
|
95 |
|
|
|
96 |
components[@"apple-platform"] = [GULAppEnvironmentUtil applePlatform];
|
|
|
97 |
components[@"apple-sdk"] = appleSdkVersion;
|
|
|
98 |
components[@"appstore"] = isFromAppstoreFlagValue;
|
|
|
99 |
components[@"deploy"] = [GULAppEnvironmentUtil deploymentType];
|
|
|
100 |
components[@"device"] = [GULAppEnvironmentUtil deviceModel];
|
|
|
101 |
components[@"os-version"] = [GULAppEnvironmentUtil systemVersion];
|
|
|
102 |
components[@"xcode"] = xcodeVersion;
|
|
|
103 |
|
|
|
104 |
return [components copy];
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
@end
|