FIRIAMSDKModeManager.m 4.4 KB

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