RCNConfigSettings.h 5.9 KB

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