RCNConfigContent.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 "FirebaseRemoteConfig/Sources/RCNConfigContent.h"
  17. #import <FirebaseRemoteConfig/FIRRemoteConfig.h>
  18. #import "FirebaseRemoteConfig/Sources/RCNConfigConstants.h"
  19. #import "FirebaseRemoteConfig/Sources/RCNConfigDBManager.h"
  20. #import "FirebaseRemoteConfig/Sources/RCNConfigDefines.h"
  21. #import "FirebaseRemoteConfig/Sources/RCNConfigValue_Internal.h"
  22. #import <FirebaseCore/FIRApp.h>
  23. #import <FirebaseCore/FIRLogger.h>
  24. @implementation RCNConfigContent {
  25. /// Active config data that is currently used.
  26. NSMutableDictionary *_activeConfig;
  27. /// Pending config (aka Fetched config) data that is latest data from server that might or might
  28. /// not be applied.
  29. NSMutableDictionary *_fetchedConfig;
  30. /// Default config provided by user.
  31. NSMutableDictionary *_defaultConfig;
  32. /// DBManager
  33. RCNConfigDBManager *_DBManager;
  34. /// Current bundle identifier;
  35. NSString *_bundleIdentifier;
  36. /// Dispatch semaphore to block all config reads until we have read from the database. This only
  37. /// potentially blocks on the first read. Should be a no-wait for all subsequent reads once we
  38. /// have data read into memory from the database.
  39. dispatch_semaphore_t _configLoadFromDBSemaphore;
  40. /// Boolean indicating if initial DB load of fetched,active and default config has succeeded.
  41. BOOL _isConfigLoadFromDBCompleted;
  42. /// Boolean indicating that the load from database has initiated at least once.
  43. BOOL _isDatabaseLoadAlreadyInitiated;
  44. }
  45. /// Default timeout when waiting to read data from database.
  46. static const NSTimeInterval kDatabaseLoadTimeoutSecs = 30.0;
  47. /// Singleton instance of RCNConfigContent.
  48. + (instancetype)sharedInstance {
  49. static dispatch_once_t onceToken;
  50. static RCNConfigContent *sharedInstance;
  51. dispatch_once(&onceToken, ^{
  52. sharedInstance =
  53. [[RCNConfigContent alloc] initWithDBManager:[RCNConfigDBManager sharedInstance]];
  54. });
  55. return sharedInstance;
  56. }
  57. - (instancetype)init {
  58. NSAssert(NO, @"Invalid initializer.");
  59. return nil;
  60. }
  61. /// Designated initializer
  62. - (instancetype)initWithDBManager:(RCNConfigDBManager *)DBManager {
  63. self = [super init];
  64. if (self) {
  65. _activeConfig = [[NSMutableDictionary alloc] init];
  66. _fetchedConfig = [[NSMutableDictionary alloc] init];
  67. _defaultConfig = [[NSMutableDictionary alloc] init];
  68. _bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];
  69. if (!_bundleIdentifier) {
  70. FIRLogNotice(kFIRLoggerRemoteConfig, @"I-RCN000038",
  71. @"Main bundle identifier is missing. Remote Config might not work properly.");
  72. _bundleIdentifier = @"";
  73. }
  74. _DBManager = DBManager;
  75. _configLoadFromDBSemaphore = dispatch_semaphore_create(0);
  76. [self loadConfigFromMainTable];
  77. }
  78. return self;
  79. }
  80. // Blocking call that returns true/false once database load completes / times out.
  81. // @return Initialization status.
  82. - (BOOL)initializationSuccessful {
  83. RCN_MUST_NOT_BE_MAIN_THREAD();
  84. BOOL isDatabaseLoadSuccessful = [self checkAndWaitForInitialDatabaseLoad];
  85. return isDatabaseLoadSuccessful;
  86. }
  87. #pragma mark - update
  88. /// This function is for copying dictionary when user set up a default config or when user clicks
  89. /// activate. For now the DBSource can only be Active or Default.
  90. - (void)copyFromDictionary:(NSDictionary *)fromDict
  91. toSource:(RCNDBSource)DBSource
  92. forNamespace:(NSString *)FIRNamespace {
  93. // Make sure database load has completed.
  94. [self checkAndWaitForInitialDatabaseLoad];
  95. NSMutableDictionary *toDict;
  96. if (!fromDict) {
  97. FIRLogError(kFIRLoggerRemoteConfig, @"I-RCN000007",
  98. @"The source dictionary to copy from does not exist.");
  99. return;
  100. }
  101. FIRRemoteConfigSource source = FIRRemoteConfigSourceRemote;
  102. switch (DBSource) {
  103. case RCNDBSourceDefault:
  104. toDict = _defaultConfig;
  105. source = FIRRemoteConfigSourceDefault;
  106. break;
  107. case RCNDBSourceFetched:
  108. FIRLogWarning(kFIRLoggerRemoteConfig, @"I-RCN000008",
  109. @"This shouldn't happen. Destination dictionary should never be pending type.");
  110. return;
  111. case RCNDBSourceActive:
  112. toDict = _activeConfig;
  113. source = FIRRemoteConfigSourceRemote;
  114. [toDict removeObjectForKey:FIRNamespace];
  115. break;
  116. default:
  117. toDict = _activeConfig;
  118. source = FIRRemoteConfigSourceRemote;
  119. [toDict removeObjectForKey:FIRNamespace];
  120. break;
  121. }
  122. // Completely wipe out DB first.
  123. [_DBManager deleteRecordFromMainTableWithNamespace:FIRNamespace
  124. bundleIdentifier:_bundleIdentifier
  125. fromSource:DBSource];
  126. toDict[FIRNamespace] = [[NSMutableDictionary alloc] init];
  127. NSDictionary *config = fromDict[FIRNamespace];
  128. for (NSString *key in config) {
  129. if (DBSource == FIRRemoteConfigSourceDefault) {
  130. NSObject *value = config[key];
  131. NSData *valueData;
  132. if ([value isKindOfClass:[NSData class]]) {
  133. valueData = (NSData *)value;
  134. } else if ([value isKindOfClass:[NSString class]]) {
  135. valueData = [(NSString *)value dataUsingEncoding:NSUTF8StringEncoding];
  136. } else if ([value isKindOfClass:[NSNumber class]]) {
  137. NSString *strValue = [(NSNumber *)value stringValue];
  138. valueData = [(NSString *)strValue dataUsingEncoding:NSUTF8StringEncoding];
  139. } else if ([value isKindOfClass:[NSDate class]]) {
  140. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  141. [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
  142. NSString *strValue = [dateFormatter stringFromDate:(NSDate *)value];
  143. valueData = [(NSString *)strValue dataUsingEncoding:NSUTF8StringEncoding];
  144. } else {
  145. continue;
  146. }
  147. toDict[FIRNamespace][key] = [[FIRRemoteConfigValue alloc] initWithData:valueData
  148. source:source];
  149. NSArray *values = @[ _bundleIdentifier, FIRNamespace, key, valueData ];
  150. [self updateMainTableWithValues:values fromSource:DBSource];
  151. } else {
  152. FIRRemoteConfigValue *value = config[key];
  153. toDict[FIRNamespace][key] = [[FIRRemoteConfigValue alloc] initWithData:value.dataValue
  154. source:source];
  155. NSArray *values = @[ _bundleIdentifier, FIRNamespace, key, value.dataValue ];
  156. [self updateMainTableWithValues:values fromSource:DBSource];
  157. }
  158. }
  159. }
  160. - (void)updateConfigContentWithResponse:(NSDictionary *)response
  161. forNamespace:(NSString *)currentNamespace {
  162. // Make sure database load has completed.
  163. [self checkAndWaitForInitialDatabaseLoad];
  164. NSString *state = response[RCNFetchResponseKeyState];
  165. if (!state) {
  166. FIRLogError(kFIRLoggerRemoteConfig, @"I-RCN000049", @"State field in fetch response is nil.");
  167. return;
  168. }
  169. FIRLogDebug(kFIRLoggerRemoteConfig, @"I-RCN000059",
  170. @"Updating config content from Response for namespace:%@ with state: %@",
  171. currentNamespace, response[RCNFetchResponseKeyState]);
  172. if ([state isEqualToString:RCNFetchResponseKeyStateNoChange]) {
  173. [self handleNoChangeStateForConfigNamespace:currentNamespace];
  174. return;
  175. }
  176. /// Handle empty config state
  177. if ([state isEqualToString:RCNFetchResponseKeyStateEmptyConfig]) {
  178. [self handleEmptyConfigStateForConfigNamespace:currentNamespace];
  179. return;
  180. }
  181. /// Handle no template state.
  182. if ([state isEqualToString:RCNFetchResponseKeyStateNoTemplate]) {
  183. [self handleNoTemplateStateForConfigNamespace:currentNamespace];
  184. return;
  185. }
  186. /// Handle update state
  187. if ([state isEqualToString:RCNFetchResponseKeyStateUpdate]) {
  188. [self handleUpdateStateForConfigNamespace:currentNamespace
  189. withEntries:response[RCNFetchResponseKeyEntries]];
  190. return;
  191. }
  192. }
  193. #pragma mark State handling
  194. - (void)handleNoChangeStateForConfigNamespace:(NSString *)currentNamespace {
  195. if (!_fetchedConfig[currentNamespace]) {
  196. _fetchedConfig[currentNamespace] = [[NSMutableDictionary alloc] init];
  197. }
  198. }
  199. - (void)handleEmptyConfigStateForConfigNamespace:(NSString *)currentNamespace {
  200. if (_fetchedConfig[currentNamespace]) {
  201. [_fetchedConfig[currentNamespace] removeAllObjects];
  202. } else {
  203. // If namespace has empty status and it doesn't exist in _fetchedConfig, we will
  204. // still add an entry for that namespace. Even if it will not be persisted in database.
  205. // TODO: Add generics for all collection types.
  206. _fetchedConfig[currentNamespace] = [[NSMutableDictionary alloc] init];
  207. }
  208. [_DBManager deleteRecordFromMainTableWithNamespace:currentNamespace
  209. bundleIdentifier:_bundleIdentifier
  210. fromSource:RCNDBSourceFetched];
  211. }
  212. - (void)handleNoTemplateStateForConfigNamespace:(NSString *)currentNamespace {
  213. // Remove the namespace.
  214. [_fetchedConfig removeObjectForKey:currentNamespace];
  215. [_DBManager deleteRecordFromMainTableWithNamespace:currentNamespace
  216. bundleIdentifier:_bundleIdentifier
  217. fromSource:RCNDBSourceFetched];
  218. }
  219. - (void)handleUpdateStateForConfigNamespace:(NSString *)currentNamespace
  220. withEntries:(NSDictionary *)entries {
  221. FIRLogDebug(kFIRLoggerRemoteConfig, @"I-RCN000058", @"Update config in DB for namespace:%@",
  222. currentNamespace);
  223. // Clear before updating
  224. [_DBManager deleteRecordFromMainTableWithNamespace:currentNamespace
  225. bundleIdentifier:_bundleIdentifier
  226. fromSource:RCNDBSourceFetched];
  227. if ([_fetchedConfig objectForKey:currentNamespace]) {
  228. [_fetchedConfig[currentNamespace] removeAllObjects];
  229. } else {
  230. _fetchedConfig[currentNamespace] = [[NSMutableDictionary alloc] init];
  231. }
  232. // Store the fetched config values.
  233. for (NSString *key in entries) {
  234. NSData *valueData = [entries[key] dataUsingEncoding:NSUTF8StringEncoding];
  235. if (!valueData) {
  236. continue;
  237. }
  238. _fetchedConfig[currentNamespace][key] =
  239. [[FIRRemoteConfigValue alloc] initWithData:valueData source:FIRRemoteConfigSourceRemote];
  240. NSArray *values = @[ _bundleIdentifier, currentNamespace, key, valueData ];
  241. [self updateMainTableWithValues:values fromSource:RCNDBSourceFetched];
  242. }
  243. }
  244. #pragma mark - database
  245. /// This method is only meant to be called at init time. The underlying logic will need to be
  246. /// revaluated if the assumption changes at a later time.
  247. - (void)loadConfigFromMainTable {
  248. if (!_DBManager) {
  249. return;
  250. }
  251. NSAssert(!_isDatabaseLoadAlreadyInitiated, @"Database load has already been initiated");
  252. _isDatabaseLoadAlreadyInitiated = true;
  253. [_DBManager
  254. loadMainWithBundleIdentifier:_bundleIdentifier
  255. completionHandler:^(BOOL success, NSDictionary *fetchedConfig,
  256. NSDictionary *activeConfig, NSDictionary *defaultConfig) {
  257. self->_fetchedConfig = [fetchedConfig mutableCopy];
  258. self->_activeConfig = [activeConfig mutableCopy];
  259. self->_defaultConfig = [defaultConfig mutableCopy];
  260. dispatch_semaphore_signal(self->_configLoadFromDBSemaphore);
  261. }];
  262. }
  263. /// Update the current config result to main table.
  264. /// @param values Values in a row to write to the table.
  265. /// @param source The source the config data is coming from. It determines which table to write to.
  266. - (void)updateMainTableWithValues:(NSArray *)values fromSource:(RCNDBSource)source {
  267. [_DBManager insertMainTableWithValues:values fromSource:source completionHandler:nil];
  268. }
  269. #pragma mark - getter/setter
  270. - (NSDictionary *)fetchedConfig {
  271. /// If this is the first time reading the fetchedConfig, we might still be reading it from the
  272. /// database.
  273. [self checkAndWaitForInitialDatabaseLoad];
  274. return _fetchedConfig;
  275. }
  276. - (NSDictionary *)activeConfig {
  277. /// If this is the first time reading the activeConfig, we might still be reading it from the
  278. /// database.
  279. [self checkAndWaitForInitialDatabaseLoad];
  280. return _activeConfig;
  281. }
  282. - (NSDictionary *)defaultConfig {
  283. /// If this is the first time reading the fetchedConfig, we might still be reading it from the
  284. /// database.
  285. [self checkAndWaitForInitialDatabaseLoad];
  286. return _defaultConfig;
  287. }
  288. /// We load the database async at init time. Block all further calls to active/fetched/default
  289. /// configs until load is done.
  290. /// @return Database load completion status.
  291. - (BOOL)checkAndWaitForInitialDatabaseLoad {
  292. /// Wait on semaphore until done. This should be a no-op for subsequent calls.
  293. if (!_isConfigLoadFromDBCompleted) {
  294. long result = dispatch_semaphore_wait(
  295. _configLoadFromDBSemaphore,
  296. dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kDatabaseLoadTimeoutSecs * NSEC_PER_SEC)));
  297. if (result != 0) {
  298. FIRLogError(kFIRLoggerRemoteConfig, @"I-RCN000048",
  299. @"Timed out waiting for fetched config to be loaded from DB");
  300. return false;
  301. }
  302. _isConfigLoadFromDBCompleted = true;
  303. }
  304. return true;
  305. }
  306. @end