FIRFirebaseUserAgent.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. #import "FirebaseCore/Sources/FIRFirebaseUserAgent.h"
  17. #import <GoogleUtilities/GULAppEnvironmentUtil.h>
  18. @interface FIRFirebaseUserAgent ()
  19. @property(nonatomic, readonly) NSMutableDictionary<NSString *, NSString *> *valuesByComponent;
  20. @property(nonatomic, readonly) NSDictionary<NSString *, NSString *> *environmentComponents;
  21. @property(nonatomic, readonly) NSString *firebaseUserAgent;
  22. @end
  23. @implementation FIRFirebaseUserAgent
  24. @synthesize firebaseUserAgent = _firebaseUserAgent;
  25. @synthesize environmentComponents = _environmentComponents;
  26. - (instancetype)init {
  27. self = [super init];
  28. if (self) {
  29. _valuesByComponent = [[NSMutableDictionary alloc] init];
  30. }
  31. return self;
  32. }
  33. - (NSString *)firebaseUserAgent {
  34. @synchronized(self) {
  35. if (_firebaseUserAgent == nil) {
  36. NSMutableDictionary<NSString *, NSString *> *allComponents =
  37. [self.valuesByComponent mutableCopy];
  38. [allComponents setValuesForKeysWithDictionary:self.environmentComponents];
  39. __block NSMutableArray<NSString *> *components =
  40. [[NSMutableArray<NSString *> alloc] initWithCapacity:self.valuesByComponent.count];
  41. [allComponents enumerateKeysAndObjectsUsingBlock:^(
  42. NSString *_Nonnull name, NSString *_Nonnull value, BOOL *_Nonnull stop) {
  43. [components addObject:[NSString stringWithFormat:@"%@/%@", name, value]];
  44. }];
  45. [components sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
  46. _firebaseUserAgent = [components componentsJoinedByString:@" "];
  47. }
  48. return _firebaseUserAgent;
  49. }
  50. }
  51. - (void)setValue:(nullable NSString *)value forComponent:(NSString *)componentName {
  52. @synchronized(self) {
  53. self.valuesByComponent[componentName] = value;
  54. // Reset cached user agent string.
  55. _firebaseUserAgent = nil;
  56. }
  57. }
  58. - (void)reset {
  59. @synchronized(self) {
  60. // Reset components.
  61. _valuesByComponent = [[[self class] environmentComponents] mutableCopy];
  62. // Reset cached user agent string.
  63. _firebaseUserAgent = nil;
  64. }
  65. }
  66. #pragma mark - Environment components
  67. - (NSDictionary<NSString *, NSString *> *)environmentComponents {
  68. if (_environmentComponents == nil) {
  69. _environmentComponents = [[self class] environmentComponents];
  70. }
  71. return _environmentComponents;
  72. }
  73. + (NSDictionary<NSString *, NSString *> *)environmentComponents {
  74. NSMutableDictionary<NSString *, NSString *> *components = [NSMutableDictionary dictionary];
  75. NSDictionary<NSString *, id> *info = [[NSBundle mainBundle] infoDictionary];
  76. NSString *xcodeVersion = info[@"DTXcodeBuild"];
  77. NSString *appleSdkVersion = info[@"DTSDKBuild"];
  78. NSString *swiftFlagValue = [GULAppEnvironmentUtil hasSwiftRuntime] ? @"true" : @"false";
  79. NSString *isFromAppstoreFlagValue = [GULAppEnvironmentUtil isFromAppStore] ? @"true" : @"false";
  80. components[@"apple-platform"] = [GULAppEnvironmentUtil applePlatform];
  81. components[@"apple-sdk"] = appleSdkVersion;
  82. components[@"appstore"] = isFromAppstoreFlagValue;
  83. components[@"deploy"] = [GULAppEnvironmentUtil deploymentType];
  84. components[@"device"] = [GULAppEnvironmentUtil deviceModel];
  85. components[@"os-version"] = [GULAppEnvironmentUtil systemVersion];
  86. components[@"swift"] = swiftFlagValue;
  87. components[@"xcode"] = xcodeVersion;
  88. return [components copy];
  89. }
  90. @end