FIRIAMClientInfoFetcher.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright 2017 Google
  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/Private/FirebaseCoreInternal.h"
  17. #import "FirebaseInstallations/Source/Library/Private/FirebaseInstallationsInternal.h"
  18. #import "FIRCore+InAppMessaging.h"
  19. #import "FIRIAMClientInfoFetcher.h"
  20. #import "FIRIAMSDKRuntimeErrorCodes.h"
  21. #import "FIRInAppMessagingPrivate.h"
  22. @interface FIRIAMClientInfoFetcher ()
  23. @property(nonatomic, strong, nullable, readonly) FIRInstallations *installations;
  24. @end
  25. @implementation FIRIAMClientInfoFetcher
  26. - (instancetype)initWithFirebaseInstallations:(FIRInstallations *)installations {
  27. if (self = [super init]) {
  28. _installations = installations;
  29. }
  30. return self;
  31. }
  32. - (void)fetchFirebaseInstallationDataWithProjectNumber:(NSString *)projectNumber
  33. withCompletion:
  34. (void (^)(NSString *_Nullable FID,
  35. NSString *_Nullable FISToken,
  36. NSError *_Nullable error))completion {
  37. if (!self.installations) {
  38. NSString *errorDesc = @"Couldn't generate Firebase Installation info";
  39. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM190010", @"%@", errorDesc);
  40. NSError *error = [NSError errorWithDomain:kFirebaseInAppMessagingErrorDomain
  41. code:FIRIAMSDKRuntimeErrorNoFirebaseInstallationsObject
  42. userInfo:@{NSLocalizedDescriptionKey : errorDesc}];
  43. completion(nil, nil, error);
  44. return;
  45. }
  46. [self.installations authTokenWithCompletion:^(
  47. FIRInstallationsAuthTokenResult *_Nullable tokenResult,
  48. NSError *_Nullable error) {
  49. if (error) {
  50. FIRLogWarning(kFIRLoggerInAppMessaging, @"I-IAM190006", @"Error in fetching FIS token: %@",
  51. error.localizedDescription);
  52. completion(nil, nil, error);
  53. } else {
  54. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM190007", @"Successfully generated FIS token");
  55. [self.installations
  56. installationIDWithCompletion:^(NSString *_Nullable identifier, NSError *_Nullable error) {
  57. if (error) {
  58. FIRLogWarning(kFIRLoggerInAppMessaging, @"I-IAM190008", @"Error in fetching FID: %@",
  59. error.localizedDescription);
  60. completion(nil, tokenResult.authToken, error);
  61. } else {
  62. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM190009",
  63. @"Successfully in fetching both FID as %@ and FIS token as %@",
  64. identifier, tokenResult.authToken);
  65. completion(identifier, tokenResult.authToken, nil);
  66. }
  67. }];
  68. }
  69. }];
  70. }
  71. - (nullable NSString *)getDeviceLanguageCode {
  72. // No caching since it's requested at pretty low frequency and we get the benefit of seeing
  73. // updated info the setting has changed
  74. NSArray<NSString *> *preferredLanguages = [NSLocale preferredLanguages];
  75. return preferredLanguages.firstObject;
  76. }
  77. - (nullable NSString *)getAppVersion {
  78. // Since this won't change, read it once in the whole life-cycle of the app and cache its value
  79. static NSString *appVersion = nil;
  80. static dispatch_once_t onceToken;
  81. dispatch_once(&onceToken, ^{
  82. appVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
  83. });
  84. return appVersion;
  85. }
  86. - (nullable NSString *)getOSVersion {
  87. // Since this won't change, read it once in the whole life-cycle of the app and cache its value
  88. static NSString *OSVersion = nil;
  89. static dispatch_once_t onceToken;
  90. dispatch_once(&onceToken, ^{
  91. NSOperatingSystemVersion systemVersion = [NSProcessInfo processInfo].operatingSystemVersion;
  92. OSVersion = [NSString stringWithFormat:@"%ld.%ld.%ld", (long)systemVersion.majorVersion,
  93. (long)systemVersion.minorVersion,
  94. (long)systemVersion.patchVersion];
  95. });
  96. return OSVersion;
  97. }
  98. - (nullable NSString *)getOSMajorVersion {
  99. NSArray *versionItems = [[self getOSVersion] componentsSeparatedByString:@"."];
  100. if (versionItems.count > 0) {
  101. return (NSString *)versionItems[0];
  102. } else {
  103. return nil;
  104. }
  105. }
  106. - (nullable NSString *)getTimezone {
  107. // No caching to deal with potential changes.
  108. return [NSTimeZone localTimeZone].name;
  109. }
  110. // extract macro value into a C string
  111. #define STR_FROM_MACRO(x) #x
  112. #define STR(x) STR_FROM_MACRO(x)
  113. - (NSString *)getIAMSDKVersion {
  114. // FIRInAppMessaging_LIB_VERSION macro comes from pod definition
  115. return [NSString stringWithUTF8String:STR(FIRInAppMessaging_LIB_VERSION)];
  116. }
  117. @end