FIRIAMSDKModeManager.m 4.2 KB

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