FIRIAMServerMsgFetchStorage.m 2.7 KB

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