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