RCNConfigSettings.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 accesss. 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. @property(nonatomic, copy) NSString *configInstallationsIdentifier;
  39. /// Installations token.
  40. @property(nonatomic, copy) NSString *configInstallationsToken;
  41. /// A list of successful fetch timestamps in milliseconds.
  42. /// TODO Not used anymore. Safe to remove.
  43. @property(nonatomic, readonly, copy) NSArray *successFetchTimes;
  44. /// A list of failed fetch timestamps in milliseconds.
  45. @property(nonatomic, readonly, copy) NSArray *failureFetchTimes;
  46. /// Custom variable (aka App context digest). This is the pending custom variables request before
  47. /// fetching.
  48. @property(nonatomic, copy) NSDictionary *customVariables;
  49. /// Cached internal metadata from internal metadata table. It contains customized information such
  50. /// as HTTP connection timeout, HTTP read timeout, success/failure throttling rate and time
  51. /// interval. Client has the default value of each parameters, they are only saved in
  52. /// internalMetadata if they have been customize by developers.
  53. @property(nonatomic, readonly, copy) NSDictionary *internalMetadata;
  54. /// Device conditions since last successful fetch from the backend. Device conditions including
  55. /// app
  56. /// version, iOS version, device localte, language, GMP project ID and Game project ID. Used for
  57. /// determing whether to throttle.
  58. @property(nonatomic, readonly, copy) NSDictionary *deviceContext;
  59. /// Bundle Identifier
  60. @property(nonatomic, readonly, copy) NSString *bundleIdentifier;
  61. /// The time of last successful config fetch.
  62. @property(nonatomic, readonly, assign) NSTimeInterval lastFetchTimeInterval;
  63. /// Last fetch status.
  64. @property(nonatomic, readwrite, assign) FIRRemoteConfigFetchStatus lastFetchStatus;
  65. /// The reason that last fetch failed.
  66. @property(nonatomic, readwrite, assign) FIRRemoteConfigError lastFetchError;
  67. /// The time of last apply timestamp.
  68. @property(nonatomic, readwrite, assign) NSTimeInterval lastApplyTimeInterval;
  69. /// The time of last setDefaults timestamp.
  70. @property(nonatomic, readwrite, assign) NSTimeInterval lastSetDefaultsTimeInterval;
  71. /// The latest eTag value stored from the last successful response.
  72. @property(nonatomic, readwrite, assign) NSString *lastETag;
  73. /// The timestamp of the last eTag update.
  74. @property(nonatomic, readwrite, assign) NSTimeInterval lastETagUpdateTime;
  75. #pragma mark Throttling properties
  76. /// Throttling intervals are based on https://cloud.google.com/storage/docs/exponential-backoff
  77. /// Returns true if client has fetched config and has not got back from server. This is used to
  78. /// determine whether there is another config task infight when fetching.
  79. @property(atomic, readwrite, assign) BOOL isFetchInProgress;
  80. /// Returns the current retry interval in seconds set for exponential backoff.
  81. @property(nonatomic, readwrite, assign) double exponentialBackoffRetryInterval;
  82. /// Returns the time in seconds until the next request is allowed while in exponential backoff mode.
  83. @property(nonatomic, readonly, assign) NSTimeInterval exponentialBackoffThrottleEndTime;
  84. #pragma mark Throttling Methods
  85. /// Designated initializer.
  86. - (instancetype)initWithDatabaseManager:(RCNConfigDBManager *)manager
  87. namespace:(NSString *)FIRNamespace
  88. firebaseAppName:(NSString *)appName
  89. googleAppID:(NSString *)googleAppID;
  90. /// Returns a fetch request with the latest device and config change.
  91. /// Whenever user issues a fetch api call, collect the latest request.
  92. /// @param userProperties User properties to set to config request.
  93. /// @return Config fetch request string
  94. - (NSString *)nextRequestWithUserProperties:(NSDictionary *)userProperties;
  95. /// Returns metadata from metadata table.
  96. - (NSDictionary *)loadConfigFromMetadataTable;
  97. /// Updates internal content with the latest successful config response.
  98. - (void)updateInternalContentWithResponse:(NSDictionary *)response;
  99. /// Updates the metadata table with the current fetch status.
  100. /// @param fetchSuccess True if fetch was successful.
  101. - (void)updateMetadataWithFetchSuccessStatus:(BOOL)fetchSuccess;
  102. /// Increases the throttling time. Should only be called if the fetch error indicates a server
  103. /// issue.
  104. - (void)updateExponentialBackoffTime;
  105. /// Returns true if we are in exponential backoff mode and it is not yet the next request time.
  106. - (BOOL)shouldThrottle;
  107. /// Returns true if the last fetch is outside the minimum fetch interval supplied.
  108. - (BOOL)hasMinimumFetchIntervalElapsed:(NSTimeInterval)minimumFetchInterval;
  109. @end