RCNConfigContent.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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/Sources/Private/FIRRemoteConfig_Private.h"
  18. #import "FirebaseRemoteConfig/Sources/Public/FirebaseRemoteConfig/FIRRemoteConfig.h"
  19. #import "FirebaseRemoteConfig/Sources/RCNConfigConstants.h"
  20. #import "FirebaseRemoteConfig/Sources/RCNConfigDBManager.h"
  21. #import "FirebaseRemoteConfig/Sources/RCNConfigDefines.h"
  22. #import "FirebaseRemoteConfig/Sources/RCNConfigValue_Internal.h"
  23. #import "FirebaseCore/Extension/FirebaseCoreInternal.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. /// Active Personalization metadata that is currently used.
  33. NSDictionary *_activePersonalization;
  34. /// Pending Personalization metadata that is latest data from server that might or might not be
  35. /// applied.
  36. NSDictionary *_fetchedPersonalization;
  37. /// DBManager
  38. RCNConfigDBManager *_DBManager;
  39. /// Current bundle identifier;
  40. NSString *_bundleIdentifier;
  41. /// Blocks all config reads until we have read from the database. This only
  42. /// potentially blocks on the first read. Should be a no-wait for all subsequent reads once we
  43. /// have data read into memory from the database.
  44. dispatch_group_t _dispatch_group;
  45. /// Boolean indicating if initial DB load of fetched,active and default config has succeeded.
  46. BOOL _isConfigLoadFromDBCompleted;
  47. /// Boolean indicating that the load from database has initiated at least once.
  48. BOOL _isDatabaseLoadAlreadyInitiated;
  49. }
  50. /// Default timeout when waiting to read data from database.
  51. const NSTimeInterval kDatabaseLoadTimeoutSecs = 30.0;
  52. /// Singleton instance of RCNConfigContent.
  53. + (instancetype)sharedInstance {
  54. static dispatch_once_t onceToken;
  55. static RCNConfigContent *sharedInstance;
  56. dispatch_once(&onceToken, ^{
  57. sharedInstance =
  58. [[RCNConfigContent alloc] initWithDBManager:[RCNConfigDBManager sharedInstance]];
  59. });
  60. return sharedInstance;
  61. }
  62. - (instancetype)init {
  63. NSAssert(NO, @"Invalid initializer.");
  64. return nil;
  65. }
  66. /// Designated initializer
  67. - (instancetype)initWithDBManager:(RCNConfigDBManager *)DBManager {
  68. self = [super init];
  69. if (self) {
  70. _activeConfig = [[NSMutableDictionary alloc] init];
  71. _fetchedConfig = [[NSMutableDictionary alloc] init];
  72. _defaultConfig = [[NSMutableDictionary alloc] init];
  73. _activePersonalization = [[NSDictionary alloc] init];
  74. _fetchedPersonalization = [[NSDictionary alloc] init];
  75. _bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];
  76. if (!_bundleIdentifier) {
  77. FIRLogNotice(kFIRLoggerRemoteConfig, @"I-RCN000038",
  78. @"Main bundle identifier is missing. Remote Config might not work properly.");
  79. _bundleIdentifier = @"";
  80. }
  81. _DBManager = DBManager;
  82. // Waits for both config and Personalization data to load.
  83. _dispatch_group = dispatch_group_create();
  84. [self loadConfigFromMainTable];
  85. }
  86. return self;
  87. }
  88. // Blocking call that returns true/false once database load completes / times out.
  89. // @return Initialization status.
  90. - (BOOL)initializationSuccessful {
  91. RCN_MUST_NOT_BE_MAIN_THREAD();
  92. BOOL isDatabaseLoadSuccessful = [self checkAndWaitForInitialDatabaseLoad];
  93. return isDatabaseLoadSuccessful;
  94. }
  95. #pragma mark - database
  96. /// This method is only meant to be called at init time. The underlying logic will need to be
  97. /// revaluated if the assumption changes at a later time.
  98. - (void)loadConfigFromMainTable {
  99. if (!_DBManager) {
  100. return;
  101. }
  102. NSAssert(!_isDatabaseLoadAlreadyInitiated, @"Database load has already been initiated");
  103. _isDatabaseLoadAlreadyInitiated = true;
  104. dispatch_group_enter(_dispatch_group);
  105. [_DBManager
  106. loadMainWithBundleIdentifier:_bundleIdentifier
  107. completionHandler:^(BOOL success, NSDictionary *fetchedConfig,
  108. NSDictionary *activeConfig, NSDictionary *defaultConfig) {
  109. self->_fetchedConfig = [fetchedConfig mutableCopy];
  110. self->_activeConfig = [activeConfig mutableCopy];
  111. self->_defaultConfig = [defaultConfig mutableCopy];
  112. dispatch_group_leave(self->_dispatch_group);
  113. }];
  114. // TODO(karenzeng): Refactor personalization to be returned in loadMainWithBundleIdentifier above
  115. dispatch_group_enter(_dispatch_group);
  116. [_DBManager loadPersonalizationWithCompletionHandler:^(
  117. BOOL success, NSDictionary *fetchedPersonalization,
  118. NSDictionary *activePersonalization, NSDictionary *defaultConfig) {
  119. self->_fetchedPersonalization = [fetchedPersonalization copy];
  120. self->_activePersonalization = [activePersonalization copy];
  121. dispatch_group_leave(self->_dispatch_group);
  122. }];
  123. }
  124. /// Update the current config result to main table.
  125. /// @param values Values in a row to write to the table.
  126. /// @param source The source the config data is coming from. It determines which table to write to.
  127. - (void)updateMainTableWithValues:(NSArray *)values fromSource:(RCNDBSource)source {
  128. [_DBManager insertMainTableWithValues:values fromSource:source completionHandler:nil];
  129. }
  130. #pragma mark - update
  131. /// This function is for copying dictionary when user set up a default config or when user clicks
  132. /// activate. For now the DBSource can only be Active or Default.
  133. - (void)copyFromDictionary:(NSDictionary *)fromDict
  134. toSource:(RCNDBSource)DBSource
  135. forNamespace:(NSString *)FIRNamespace {
  136. // Make sure database load has completed.
  137. [self checkAndWaitForInitialDatabaseLoad];
  138. NSMutableDictionary *toDict;
  139. if (!fromDict) {
  140. FIRLogError(kFIRLoggerRemoteConfig, @"I-RCN000007",
  141. @"The source dictionary to copy from does not exist.");
  142. return;
  143. }
  144. FIRRemoteConfigSource source = FIRRemoteConfigSourceRemote;
  145. switch (DBSource) {
  146. case RCNDBSourceDefault:
  147. toDict = _defaultConfig;
  148. source = FIRRemoteConfigSourceDefault;
  149. break;
  150. case RCNDBSourceFetched:
  151. FIRLogWarning(kFIRLoggerRemoteConfig, @"I-RCN000008",
  152. @"This shouldn't happen. Destination dictionary should never be pending type.");
  153. return;
  154. case RCNDBSourceActive:
  155. toDict = _activeConfig;
  156. source = FIRRemoteConfigSourceRemote;
  157. [toDict removeObjectForKey:FIRNamespace];
  158. break;
  159. default:
  160. toDict = _activeConfig;
  161. source = FIRRemoteConfigSourceRemote;
  162. [toDict removeObjectForKey:FIRNamespace];
  163. break;
  164. }
  165. // Completely wipe out DB first.
  166. [_DBManager deleteRecordFromMainTableWithNamespace:FIRNamespace
  167. bundleIdentifier:_bundleIdentifier
  168. fromSource:DBSource];
  169. toDict[FIRNamespace] = [[NSMutableDictionary alloc] init];
  170. NSDictionary *config = fromDict[FIRNamespace];
  171. for (NSString *key in config) {
  172. if (DBSource == FIRRemoteConfigSourceDefault) {
  173. NSObject *value = config[key];
  174. NSData *valueData;
  175. if ([value isKindOfClass:[NSData class]]) {
  176. valueData = (NSData *)value;
  177. } else if ([value isKindOfClass:[NSString class]]) {
  178. valueData = [(NSString *)value dataUsingEncoding:NSUTF8StringEncoding];
  179. } else if ([value isKindOfClass:[NSNumber class]]) {
  180. NSString *strValue = [(NSNumber *)value stringValue];
  181. valueData = [(NSString *)strValue dataUsingEncoding:NSUTF8StringEncoding];
  182. } else if ([value isKindOfClass:[NSDate class]]) {
  183. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  184. [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
  185. NSString *strValue = [dateFormatter stringFromDate:(NSDate *)value];
  186. valueData = [(NSString *)strValue dataUsingEncoding:NSUTF8StringEncoding];
  187. } else if ([value isKindOfClass:[NSArray class]]) {
  188. NSError *error;
  189. valueData = [NSJSONSerialization dataWithJSONObject:value options:0 error:&error];
  190. if (error) {
  191. FIRLogError(kFIRLoggerRemoteConfig, @"I-RCN000076", @"Invalid array value for key '%@'",
  192. key);
  193. }
  194. } else if ([value isKindOfClass:[NSDictionary class]]) {
  195. NSError *error;
  196. valueData = [NSJSONSerialization dataWithJSONObject:value options:0 error:&error];
  197. if (error) {
  198. FIRLogError(kFIRLoggerRemoteConfig, @"I-RCN000077",
  199. @"Invalid dictionary value for key '%@'", key);
  200. }
  201. } else {
  202. continue;
  203. }
  204. toDict[FIRNamespace][key] = [[FIRRemoteConfigValue alloc] initWithData:valueData
  205. source:source];
  206. NSArray *values = @[ _bundleIdentifier, FIRNamespace, key, valueData ];
  207. [self updateMainTableWithValues:values fromSource:DBSource];
  208. } else {
  209. FIRRemoteConfigValue *value = config[key];
  210. toDict[FIRNamespace][key] = [[FIRRemoteConfigValue alloc] initWithData:value.dataValue
  211. source:source];
  212. NSArray *values = @[ _bundleIdentifier, FIRNamespace, key, value.dataValue ];
  213. [self updateMainTableWithValues:values fromSource:DBSource];
  214. }
  215. }
  216. }
  217. - (void)updateConfigContentWithResponse:(NSDictionary *)response
  218. forNamespace:(NSString *)currentNamespace {
  219. // Make sure database load has completed.
  220. [self checkAndWaitForInitialDatabaseLoad];
  221. NSString *state = response[RCNFetchResponseKeyState];
  222. if (!state) {
  223. FIRLogError(kFIRLoggerRemoteConfig, @"I-RCN000049", @"State field in fetch response is nil.");
  224. return;
  225. }
  226. FIRLogDebug(kFIRLoggerRemoteConfig, @"I-RCN000059",
  227. @"Updating config content from Response for namespace:%@ with state: %@",
  228. currentNamespace, response[RCNFetchResponseKeyState]);
  229. if ([state isEqualToString:RCNFetchResponseKeyStateNoChange]) {
  230. [self handleNoChangeStateForConfigNamespace:currentNamespace];
  231. return;
  232. }
  233. /// Handle empty config state
  234. if ([state isEqualToString:RCNFetchResponseKeyStateEmptyConfig]) {
  235. [self handleEmptyConfigStateForConfigNamespace:currentNamespace];
  236. return;
  237. }
  238. /// Handle no template state.
  239. if ([state isEqualToString:RCNFetchResponseKeyStateNoTemplate]) {
  240. [self handleNoTemplateStateForConfigNamespace:currentNamespace];
  241. return;
  242. }
  243. /// Handle update state
  244. if ([state isEqualToString:RCNFetchResponseKeyStateUpdate]) {
  245. [self handleUpdateStateForConfigNamespace:currentNamespace
  246. withEntries:response[RCNFetchResponseKeyEntries]];
  247. [self handleUpdatePersonalization:response[RCNFetchResponseKeyPersonalizationMetadata]];
  248. return;
  249. }
  250. }
  251. - (void)activatePersonalization {
  252. _activePersonalization = _fetchedPersonalization;
  253. [_DBManager insertOrUpdatePersonalizationConfig:_activePersonalization
  254. fromSource:RCNDBSourceActive];
  255. }
  256. #pragma mark State handling
  257. - (void)handleNoChangeStateForConfigNamespace:(NSString *)currentNamespace {
  258. if (!_fetchedConfig[currentNamespace]) {
  259. _fetchedConfig[currentNamespace] = [[NSMutableDictionary alloc] init];
  260. }
  261. }
  262. - (void)handleEmptyConfigStateForConfigNamespace:(NSString *)currentNamespace {
  263. if (_fetchedConfig[currentNamespace]) {
  264. [_fetchedConfig[currentNamespace] removeAllObjects];
  265. } else {
  266. // If namespace has empty status and it doesn't exist in _fetchedConfig, we will
  267. // still add an entry for that namespace. Even if it will not be persisted in database.
  268. // TODO: Add generics for all collection types.
  269. _fetchedConfig[currentNamespace] = [[NSMutableDictionary alloc] init];
  270. }
  271. [_DBManager deleteRecordFromMainTableWithNamespace:currentNamespace
  272. bundleIdentifier:_bundleIdentifier
  273. fromSource:RCNDBSourceFetched];
  274. }
  275. - (void)handleNoTemplateStateForConfigNamespace:(NSString *)currentNamespace {
  276. // Remove the namespace.
  277. [_fetchedConfig removeObjectForKey:currentNamespace];
  278. [_DBManager deleteRecordFromMainTableWithNamespace:currentNamespace
  279. bundleIdentifier:_bundleIdentifier
  280. fromSource:RCNDBSourceFetched];
  281. }
  282. - (void)handleUpdateStateForConfigNamespace:(NSString *)currentNamespace
  283. withEntries:(NSDictionary *)entries {
  284. FIRLogDebug(kFIRLoggerRemoteConfig, @"I-RCN000058", @"Update config in DB for namespace:%@",
  285. currentNamespace);
  286. // Clear before updating
  287. [_DBManager deleteRecordFromMainTableWithNamespace:currentNamespace
  288. bundleIdentifier:_bundleIdentifier
  289. fromSource:RCNDBSourceFetched];
  290. if ([_fetchedConfig objectForKey:currentNamespace]) {
  291. [_fetchedConfig[currentNamespace] removeAllObjects];
  292. } else {
  293. _fetchedConfig[currentNamespace] = [[NSMutableDictionary alloc] init];
  294. }
  295. // Store the fetched config values.
  296. for (NSString *key in entries) {
  297. NSData *valueData = [entries[key] dataUsingEncoding:NSUTF8StringEncoding];
  298. if (!valueData) {
  299. continue;
  300. }
  301. _fetchedConfig[currentNamespace][key] =
  302. [[FIRRemoteConfigValue alloc] initWithData:valueData source:FIRRemoteConfigSourceRemote];
  303. NSArray *values = @[ _bundleIdentifier, currentNamespace, key, valueData ];
  304. [self updateMainTableWithValues:values fromSource:RCNDBSourceFetched];
  305. }
  306. }
  307. - (void)handleUpdatePersonalization:(NSDictionary *)metadata {
  308. if (!metadata) {
  309. return;
  310. }
  311. _fetchedPersonalization = metadata;
  312. [_DBManager insertOrUpdatePersonalizationConfig:metadata fromSource:RCNDBSourceFetched];
  313. }
  314. #pragma mark - getter/setter
  315. - (NSDictionary *)fetchedConfig {
  316. /// If this is the first time reading the fetchedConfig, we might still be reading it from the
  317. /// database.
  318. [self checkAndWaitForInitialDatabaseLoad];
  319. return _fetchedConfig;
  320. }
  321. - (NSDictionary *)activeConfig {
  322. /// If this is the first time reading the activeConfig, we might still be reading it from the
  323. /// database.
  324. [self checkAndWaitForInitialDatabaseLoad];
  325. return _activeConfig;
  326. }
  327. - (NSDictionary *)defaultConfig {
  328. /// If this is the first time reading the fetchedConfig, we might still be reading it from the
  329. /// database.
  330. [self checkAndWaitForInitialDatabaseLoad];
  331. return _defaultConfig;
  332. }
  333. - (NSDictionary *)activePersonalization {
  334. [self checkAndWaitForInitialDatabaseLoad];
  335. return _activePersonalization;
  336. }
  337. - (NSDictionary *)getConfigAndMetadataForNamespace:(NSString *)FIRNamespace {
  338. /// If this is the first time reading the active metadata, we might still be reading it from the
  339. /// database.
  340. [self checkAndWaitForInitialDatabaseLoad];
  341. return @{
  342. RCNFetchResponseKeyEntries : _activeConfig[FIRNamespace],
  343. RCNFetchResponseKeyPersonalizationMetadata : _activePersonalization
  344. };
  345. }
  346. /// We load the database async at init time. Block all further calls to active/fetched/default
  347. /// configs until load is done.
  348. /// @return Database load completion status.
  349. - (BOOL)checkAndWaitForInitialDatabaseLoad {
  350. /// Wait until load is done. This should be a no-op for subsequent calls.
  351. if (!_isConfigLoadFromDBCompleted) {
  352. intptr_t isErrorOrTimeout = dispatch_group_wait(
  353. _dispatch_group,
  354. dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kDatabaseLoadTimeoutSecs * NSEC_PER_SEC)));
  355. if (isErrorOrTimeout) {
  356. FIRLogError(kFIRLoggerRemoteConfig, @"I-RCN000048",
  357. @"Timed out waiting for fetched config to be loaded from DB");
  358. return false;
  359. }
  360. _isConfigLoadFromDBCompleted = true;
  361. }
  362. return true;
  363. }
  364. // Compare fetched config with active config and output what has changed
  365. - (FIRRemoteConfigUpdate *)getConfigUpdateForNamespace:(NSString *)FIRNamespace
  366. withExperimentChanges:
  367. (NSMutableSet<NSString *> *)changedExperimentKeys {
  368. FIRRemoteConfigUpdate *configUpdate;
  369. NSMutableSet<NSString *> *updatedKeys = [[NSMutableSet alloc] init];
  370. updatedKeys = [[updatedKeys setByAddingObjectsFromSet:changedExperimentKeys] mutableCopy];
  371. NSDictionary *fetchedConfig =
  372. _fetchedConfig[FIRNamespace] ? _fetchedConfig[FIRNamespace] : [[NSDictionary alloc] init];
  373. NSDictionary *activeConfig =
  374. _activeConfig[FIRNamespace] ? _activeConfig[FIRNamespace] : [[NSDictionary alloc] init];
  375. NSDictionary *fetchedP13n = _fetchedPersonalization;
  376. NSDictionary *activeP13n = _activePersonalization;
  377. // add new/updated params
  378. for (NSString *key in [fetchedConfig allKeys]) {
  379. if (activeConfig[key] == nil ||
  380. ![[activeConfig[key] stringValue] isEqualToString:[fetchedConfig[key] stringValue]]) {
  381. [updatedKeys addObject:key];
  382. }
  383. }
  384. // add deleted params
  385. for (NSString *key in [activeConfig allKeys]) {
  386. if (fetchedConfig[key] == nil) {
  387. [updatedKeys addObject:key];
  388. }
  389. }
  390. // add params with new/updated p13n metadata
  391. for (NSString *key in [fetchedP13n allKeys]) {
  392. if (activeP13n[key] == nil || ![activeP13n[key] isEqualToDictionary:fetchedP13n[key]]) {
  393. [updatedKeys addObject:key];
  394. }
  395. }
  396. // add params with deleted p13n metadata
  397. for (NSString *key in [activeP13n allKeys]) {
  398. if (fetchedP13n[key] == nil) {
  399. [updatedKeys addObject:key];
  400. }
  401. }
  402. configUpdate = [[FIRRemoteConfigUpdate alloc] initWithUpdatedKeys:updatedKeys];
  403. return configUpdate;
  404. }
  405. @end