| 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 | 
              | 
        
        
            | 
            | 
           22 | 
           #import "FirebaseInAppMessaging/Sources/FIRCore+InAppMessaging.h"
  | 
        
        
            | 
            | 
           23 | 
           #import "FirebaseInAppMessaging/Sources/Private/Flows/FIRIAMServerMsgFetchStorage.h"
  | 
        
        
            | 
            | 
           24 | 
           @implementation FIRIAMServerMsgFetchStorage
  | 
        
        
            | 
            | 
           25 | 
           - (NSString *)determineCacheFilePath {
  | 
        
        
            | 
            | 
           26 | 
             NSString *cachePath =
  | 
        
        
            | 
            | 
           27 | 
                 NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
  | 
        
        
            | 
            | 
           28 | 
             NSString *filePath = [NSString stringWithFormat:@"%@/firebase-iam-messages-cache", cachePath];
  | 
        
        
            | 
            | 
           29 | 
             FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM150004",
  | 
        
        
            | 
            | 
           30 | 
                         @"Persistent file path for fetch response data is %@", filePath);
  | 
        
        
            | 
            | 
           31 | 
             return filePath;
  | 
        
        
            | 
            | 
           32 | 
           }
  | 
        
        
            | 
            | 
           33 | 
              | 
        
        
            | 
            | 
           34 | 
           - (void)saveResponseDictionary:(NSDictionary *)response
  | 
        
        
            | 
            | 
           35 | 
                           withCompletion:(void (^)(BOOL success))completion {
  | 
        
        
            | 
            | 
           36 | 
             dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul), ^{
  | 
        
        
            | 
            | 
           37 | 
               if ([response writeToFile:[self determineCacheFilePath] atomically:YES]) {
  | 
        
        
            | 
            | 
           38 | 
                 completion(YES);
  | 
        
        
            | 
            | 
           39 | 
               } else {
  | 
        
        
            | 
            | 
           40 | 
                 completion(NO);
  | 
        
        
            | 
            | 
           41 | 
               }
  | 
        
        
            | 
            | 
           42 | 
             });
  | 
        
        
            | 
            | 
           43 | 
           }
  | 
        
        
            | 
            | 
           44 | 
              | 
        
        
            | 
            | 
           45 | 
           - (void)readResponseDictionary:(void (^)(NSDictionary *response, BOOL success))completion {
  | 
        
        
            | 
            | 
           46 | 
             dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul), ^{
  | 
        
        
            | 
            | 
           47 | 
               NSString *storageFilePath = [self determineCacheFilePath];
  | 
        
        
            | 
            | 
           48 | 
               if ([[NSFileManager defaultManager] fileExistsAtPath:storageFilePath]) {
  | 
        
        
            | 
            | 
           49 | 
                 NSDictionary *dictFromFile =
  | 
        
        
            | 
            | 
           50 | 
                     [[NSMutableDictionary dictionaryWithContentsOfFile:[self determineCacheFilePath]] copy];
  | 
        
        
            | 
            | 
           51 | 
                 if (dictFromFile) {
  | 
        
        
            | 
            | 
           52 | 
                   FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM150001",
  | 
        
        
            | 
            | 
           53 | 
                               @"Loaded response from fetch storage successfully.");
  | 
        
        
            | 
            | 
           54 | 
                   completion(dictFromFile, YES);
  | 
        
        
            | 
            | 
           55 | 
                 } else {
  | 
        
        
            | 
            | 
           56 | 
                   FIRLogWarning(kFIRLoggerInAppMessaging, @"I-IAM150002",
  | 
        
        
            | 
            | 
           57 | 
                                 @"Not able to read response from fetch storage.");
  | 
        
        
            | 
            | 
           58 | 
                   completion(dictFromFile, NO);
  | 
        
        
            | 
            | 
           59 | 
                 }
  | 
        
        
            | 
            | 
           60 | 
               } else {
  | 
        
        
            | 
            | 
           61 | 
                 FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM150003",
  | 
        
        
            | 
            | 
           62 | 
                             @"Local fetch storage file not existent yet: first time launch of the app.");
  | 
        
        
            | 
            | 
           63 | 
                 completion(nil, YES);
  | 
        
        
            | 
            | 
           64 | 
               }
  | 
        
        
            | 
            | 
           65 | 
             });
  | 
        
        
            | 
            | 
           66 | 
           }
  | 
        
        
            | 
            | 
           67 | 
           @end
  | 
        
        
            | 
            | 
           68 | 
              | 
        
        
            | 
            | 
           69 | 
           #endif  // TARGET_OS_IOS || TARGET_OS_TV
  |