RCNConfigContent.m 15 KB

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