FIRIAMSDKModeManager.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright 2018 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 || TARGET_OS_VISION
  18. #import <GoogleUtilities/GULUserDefaults.h>
  19. #import "FirebaseCore/Extension/FirebaseCoreInternal.h"
  20. #import "FirebaseInAppMessaging/Sources/FIRCore+InAppMessaging.h"
  21. #import "FirebaseInAppMessaging/Sources/Private/Runtime/FIRIAMSDKModeManager.h"
  22. NSString *FIRIAMDescriptionStringForSDKMode(FIRIAMSDKMode mode) {
  23. switch (mode) {
  24. case FIRIAMSDKModeTesting:
  25. return @"Testing Instance";
  26. case FIRIAMSDKModeRegular:
  27. return @"Regular";
  28. case FIRIAMSDKModeNewlyInstalled:
  29. return @"Newly Installed";
  30. default:
  31. FIRLogWarning(kFIRLoggerInAppMessaging, @"I-IAM290003", @"Unknown sdk mode value %d",
  32. (int)mode);
  33. return @"Unknown";
  34. }
  35. }
  36. @interface FIRIAMSDKModeManager ()
  37. @property(nonatomic, nonnull, readonly) GULUserDefaults *userDefaults;
  38. // Make it weak so that we don't depend on its existence to avoid circular reference.
  39. @property(nonatomic, readonly, weak) id<FIRIAMTestingModeListener> testingModeListener;
  40. @end
  41. NSString *const kFIRIAMUserDefaultKeyForSDKMode = @"firebase-iam-sdk-mode";
  42. NSString *const kFIRIAMUserDefaultKeyForServerFetchCount = @"firebase-iam-server-fetch-count";
  43. NSInteger const kFIRIAMMaxFetchInNewlyInstalledMode = 5;
  44. @implementation FIRIAMSDKModeManager {
  45. FIRIAMSDKMode _sdkMode;
  46. NSInteger _fetchCount;
  47. }
  48. - (instancetype)initWithUserDefaults:(GULUserDefaults *)userDefaults
  49. testingModeListener:(id<FIRIAMTestingModeListener>)testingModeListener {
  50. if (self = [super init]) {
  51. _userDefaults = userDefaults;
  52. _testingModeListener = testingModeListener;
  53. id modeEntry = [_userDefaults objectForKey:kFIRIAMUserDefaultKeyForSDKMode];
  54. if (modeEntry == nil) {
  55. // no entry yet, it's a newly installed sdk instance
  56. _sdkMode = FIRIAMSDKModeNewlyInstalled;
  57. // initialize the mode and fetch count in the persistent storage
  58. [_userDefaults setObject:[NSNumber numberWithInteger:_sdkMode]
  59. forKey:kFIRIAMUserDefaultKeyForSDKMode];
  60. [_userDefaults setInteger:0 forKey:kFIRIAMUserDefaultKeyForServerFetchCount];
  61. } else {
  62. _sdkMode = [(NSNumber *)modeEntry integerValue];
  63. _fetchCount = [_userDefaults integerForKey:kFIRIAMUserDefaultKeyForServerFetchCount];
  64. }
  65. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM290001",
  66. @"SDK is in mode of %@ and has seen %d fetches.",
  67. FIRIAMDescriptionStringForSDKMode(_sdkMode), (int)_fetchCount);
  68. }
  69. return self;
  70. }
  71. // inform the manager that one more fetch is done. This is to allow
  72. // the manager to potentially graduate from the newly installed mode.
  73. - (void)registerOneMoreFetch {
  74. // we only care about the fetch count when sdk is in newly installed mode (so that it may
  75. // graduate from that after certain number of fetches).
  76. if (_sdkMode == FIRIAMSDKModeNewlyInstalled) {
  77. if (++_fetchCount >= kFIRIAMMaxFetchInNewlyInstalledMode) {
  78. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM290002",
  79. @"Coming out of newly installed mode since there have been %d fetches",
  80. (int)_fetchCount);
  81. _sdkMode = FIRIAMSDKModeRegular;
  82. [_userDefaults setObject:[NSNumber numberWithInteger:_sdkMode]
  83. forKey:kFIRIAMUserDefaultKeyForSDKMode];
  84. } else {
  85. [_userDefaults setInteger:_fetchCount forKey:kFIRIAMUserDefaultKeyForServerFetchCount];
  86. }
  87. }
  88. }
  89. - (void)becomeTestingInstance {
  90. _sdkMode = FIRIAMSDKModeTesting;
  91. [_userDefaults setObject:[NSNumber numberWithInteger:_sdkMode]
  92. forKey:kFIRIAMUserDefaultKeyForSDKMode];
  93. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM290004",
  94. @"Test mode enabled, notifying test mode listener.");
  95. [self.testingModeListener testingModeSwitchedOn];
  96. }
  97. // returns the current SDK mode
  98. - (FIRIAMSDKMode)currentMode {
  99. return _sdkMode;
  100. }
  101. @end
  102. #endif // TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_VISION