FIRIAMClientInfoFetcher.m 5.1 KB

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