FIRFirebaseUserAgent.m 3.3 KB

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