RCNConfigSettings.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright 2019 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 <Foundation/Foundation.h>
  17. #import <FirebaseRemoteConfig/FIRRemoteConfig.h>
  18. @class RCNConfigDBManager;
  19. /// This internal class contains a set of variables that are unique among all the config instances.
  20. /// It also handles all metadata and internal metadata. This class is not thread safe and does not
  21. /// inherently allow for synchronized access. Callers are responsible for synchronization
  22. /// (currently using serial dispatch queues).
  23. @interface RCNConfigSettings : NSObject
  24. /// The time interval that config data stays fresh.
  25. @property(nonatomic, readwrite, assign) NSTimeInterval minimumFetchInterval;
  26. /// The timeout to set for outgoing fetch requests.
  27. @property(nonatomic, readwrite, assign) NSTimeInterval fetchTimeout;
  28. // The Google App ID of the configured FIRApp.
  29. @property(nonatomic, readwrite, copy) NSString *googleAppID;
  30. #pragma mark - Data required by config request.
  31. /// Device authentication ID required by config request.
  32. @property(nonatomic, copy) NSString *deviceAuthID;
  33. /// Secret Token required by config request.
  34. @property(nonatomic, copy) NSString *secretToken;
  35. /// Device data version of checkin information.
  36. @property(nonatomic, copy) NSString *deviceDataVersion;
  37. /// InstallationsID.
  38. /// @note The property is atomic because it is accessed across multiple threads.
  39. @property(atomic, copy) NSString *configInstallationsIdentifier;
  40. /// Installations token.
  41. /// @note The property is atomic because it is accessed across multiple threads.
  42. @property(atomic, copy) NSString *configInstallationsToken;
  43. /// A list of successful fetch timestamps in milliseconds.
  44. /// TODO Not used anymore. Safe to remove.
  45. @property(nonatomic, readonly, copy) NSArray *successFetchTimes;
  46. /// A list of failed fetch timestamps in milliseconds.
  47. @property(nonatomic, readonly, copy) NSArray *failureFetchTimes;
  48. /// Custom variable (aka App context digest). This is the pending custom variables request before
  49. /// fetching.
  50. @property(nonatomic, copy) NSDictionary *customVariables;
  51. /// Device conditions since last successful fetch from the backend. Device conditions including
  52. /// app
  53. /// version, iOS version, device localte, language, GMP project ID and Game project ID. Used for
  54. /// determing whether to throttle.
  55. @property(nonatomic, readonly, copy) NSDictionary *deviceContext;
  56. /// Bundle Identifier
  57. @property(nonatomic, readonly, copy) NSString *bundleIdentifier;
  58. /// The time of last successful config fetch.
  59. @property(nonatomic, readonly, assign) NSTimeInterval lastFetchTimeInterval;
  60. /// Last fetch status.
  61. @property(nonatomic, readwrite, assign) FIRRemoteConfigFetchStatus lastFetchStatus;
  62. /// The reason that last fetch failed.
  63. @property(nonatomic, readwrite, assign) FIRRemoteConfigError lastFetchError;
  64. /// The time of last apply timestamp.
  65. @property(nonatomic, readwrite, assign) NSTimeInterval lastApplyTimeInterval;
  66. /// The time of last setDefaults timestamp.
  67. @property(nonatomic, readwrite, assign) NSTimeInterval lastSetDefaultsTimeInterval;
  68. /// The latest eTag value stored from the last successful response.
  69. @property(nonatomic, readwrite, assign) NSString *lastETag;
  70. /// The timestamp of the last eTag update.
  71. @property(nonatomic, readwrite, assign) NSTimeInterval lastETagUpdateTime;
  72. /// Last fetched template version.
  73. @property(nonatomic, readwrite, assign) NSString *lastFetchedTemplateVersion;
  74. /// Last active template version.
  75. @property(nonatomic, readwrite, assign) NSString *lastActiveTemplateVersion;
  76. #pragma mark - Custom Signals
  77. /// A dictionary to hold custom signals that are set by the developer.
  78. @property(nonatomic, readwrite, strong) NSDictionary<NSString *, NSString *> *customSignals;
  79. #pragma mark Throttling properties
  80. /// Throttling intervals are based on https://cloud.google.com/storage/docs/exponential-backoff
  81. /// Returns true if client has fetched config and has not got back from server. This is used to
  82. /// determine whether there is another config task infight when fetching.
  83. @property(atomic, readwrite, assign) BOOL isFetchInProgress;
  84. /// Returns the current retry interval in seconds set for exponential backoff.
  85. @property(nonatomic, readwrite, assign) double exponentialBackoffRetryInterval;
  86. /// Returns the time in seconds until the next request is allowed while in exponential backoff mode.
  87. @property(nonatomic, readonly, assign) NSTimeInterval exponentialBackoffThrottleEndTime;
  88. /// Returns the current retry interval in seconds set for exponential backoff for the Realtime
  89. /// service.
  90. @property(nonatomic, readwrite, assign) double realtimeExponentialBackoffRetryInterval;
  91. /// Returns the time in seconds until the next request is allowed while in exponential backoff mode
  92. /// for the Realtime service.
  93. @property(nonatomic, readonly, assign) NSTimeInterval realtimeExponentialBackoffThrottleEndTime;
  94. /// Realtime connection attempts.
  95. @property(nonatomic, readwrite, assign) int realtimeRetryCount;
  96. #pragma mark Throttling Methods
  97. /// Designated initializer.
  98. - (instancetype)initWithDatabaseManager:(RCNConfigDBManager *)manager
  99. namespace:(NSString *)FIRNamespace
  100. firebaseAppName:(NSString *)appName
  101. googleAppID:(NSString *)googleAppID;
  102. /// Returns a fetch request with the latest device and config change.
  103. /// Whenever user issues a fetch api call, collect the latest request.
  104. /// @param userProperties User properties to set to config request.
  105. /// @return Config fetch request string
  106. - (NSString *)nextRequestWithUserProperties:(NSDictionary *)userProperties;
  107. /// Returns metadata from metadata table.
  108. - (NSDictionary *)loadConfigFromMetadataTable;
  109. /// Updates the metadata table with the current fetch status.
  110. /// @param fetchSuccess True if fetch was successful.
  111. - (void)updateMetadataWithFetchSuccessStatus:(BOOL)fetchSuccess
  112. templateVersion:(NSString *)templateVersion;
  113. /// Increases the throttling time. Should only be called if the fetch error indicates a server
  114. /// issue.
  115. - (void)updateExponentialBackoffTime;
  116. /// Increases the throttling time for Realtime. Should only be called if the Realtime error
  117. /// indicates a server issue.
  118. - (void)updateRealtimeExponentialBackoffTime;
  119. /// Update last active template version from last fetched template version.
  120. - (void)updateLastActiveTemplateVersion;
  121. /// Returns the difference between the Realtime backoff end time and the current time in a
  122. /// NSTimeInterval format.
  123. - (NSTimeInterval)getRealtimeBackoffInterval;
  124. /// Returns true if we are in exponential backoff mode and it is not yet the next request time.
  125. - (BOOL)shouldThrottle;
  126. /// Returns true if the last fetch is outside the minimum fetch interval supplied.
  127. - (BOOL)hasMinimumFetchIntervalElapsed:(NSTimeInterval)minimumFetchInterval;
  128. @end