RCNConfigDBManager.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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/Sources/RCNConfigContent.h"
  18. typedef NS_ENUM(NSInteger, RCNUpdateOption) {
  19. RCNUpdateOptionApplyTime,
  20. RCNUpdateOptionDefaultTime,
  21. RCNUpdateOptionFetchStatus,
  22. };
  23. /// Column names in metadata table
  24. static NSString *const RCNKeyBundleIdentifier = @"bundle_identifier";
  25. static NSString *const RCNKeyNamespace = @"namespace";
  26. static NSString *const RCNKeyFetchTime = @"fetch_time";
  27. static NSString *const RCNKeyDigestPerNamespace = @"digest_per_ns";
  28. static NSString *const RCNKeyDeviceContext = @"device_context";
  29. static NSString *const RCNKeyAppContext = @"app_context";
  30. static NSString *const RCNKeySuccessFetchTime = @"success_fetch_time";
  31. static NSString *const RCNKeyFailureFetchTime = @"failure_fetch_time";
  32. static NSString *const RCNKeyLastFetchStatus = @"last_fetch_status";
  33. static NSString *const RCNKeyLastFetchError = @"last_fetch_error";
  34. static NSString *const RCNKeyLastApplyTime = @"last_apply_time";
  35. static NSString *const RCNKeyLastSetDefaultsTime = @"last_set_defaults_time";
  36. /// Persist config data in sqlite database on device. Managing data read/write from/to database.
  37. @interface RCNConfigDBManager : NSObject
  38. /// Shared Singleton Instance
  39. + (instancetype)sharedInstance;
  40. /// Database Operation Completion callback.
  41. /// @param success Decide whether the DB operation succeeds.
  42. /// @param result Return operation result data.
  43. typedef void (^RCNDBCompletion)(BOOL success, NSDictionary *result);
  44. /// Database Load Operation Completion callback.
  45. /// @param success Decide whether the DB operation succeeds.
  46. /// @param fetchedConfig Return fetchedConfig loaded from DB
  47. /// @param activeConfig Return activeConfig loaded from DB
  48. /// @param defaultConfig Return defaultConfig loaded from DB
  49. /// @param rolloutMetadata Return fetched and active RolloutMetadata loaded from DB
  50. typedef void (^RCNDBLoadCompletion)(BOOL success,
  51. NSDictionary *fetchedConfig,
  52. NSDictionary *activeConfig,
  53. NSDictionary *defaultConfig,
  54. NSDictionary *rolloutMetadata);
  55. /// Returns the current version of the Remote Config database.
  56. + (NSString *)remoteConfigPathForDatabase;
  57. /// Load config content from main table to cached memory during app start.
  58. - (void)loadMainWithBundleIdentifier:(NSString *)bundleIdentifier
  59. completionHandler:(RCNDBLoadCompletion)handler;
  60. /// Load config settings for a given namespace from metadata table to cached memory during app
  61. /// start. Config settings include success/failure fetch times, device contenxt, app context, etc.
  62. - (NSDictionary *)loadMetadataWithBundleIdentifier:(NSString *)bundleIdentifier
  63. namespace:(NSString *)namespace;
  64. /// Load internal metadata from internal metadata table, such as customized HTTP connection/read
  65. /// timeout, throttling time interval and number limit of throttling, etc.
  66. /// This call needs to be blocking to ensure throttling works during apps starts.
  67. - (NSDictionary *)loadInternalMetadataTable;
  68. /// Load experiment from experiment table.
  69. /// @param handler The callback when reading from DB is complete.
  70. - (void)loadExperimentWithCompletionHandler:(RCNDBCompletion)handler;
  71. /// Load Personalization from table.
  72. /// @param handler The callback when reading from DB is complete.
  73. - (void)loadPersonalizationWithCompletionHandler:(RCNDBLoadCompletion)handler;
  74. /// Insert a record in metadata table.
  75. /// @param columnNameToValue The column name and its value to be inserted in metadata table.
  76. /// @param handler The callback.
  77. - (void)insertMetadataTableWithValues:(NSDictionary *)columnNameToValue
  78. completionHandler:(RCNDBCompletion)handler;
  79. /// Insert a record in main table.
  80. /// @param values Values to be inserted.
  81. - (void)insertMainTableWithValues:(NSArray *)values
  82. fromSource:(RCNDBSource)source
  83. completionHandler:(RCNDBCompletion)handler;
  84. /// Insert a record in internal metadata table.
  85. /// @param values Values to be inserted.
  86. - (void)insertInternalMetadataTableWithValues:(NSArray *)values
  87. completionHandler:(RCNDBCompletion)handler;
  88. /// Insert experiment data in experiment table.
  89. /// @param key The key of experiment data belongs to, which are defined in
  90. /// RCNConfigDefines.h.
  91. /// @param value The value that experiment.
  92. /// @param handler The callback.
  93. - (void)insertExperimentTableWithKey:(NSString *)key
  94. value:(NSData *)value
  95. completionHandler:(RCNDBCompletion)handler;
  96. - (void)updateMetadataWithOption:(RCNUpdateOption)option
  97. namespace:(NSString *)namespace
  98. values:(NSArray *)values
  99. completionHandler:(RCNDBCompletion)handler;
  100. /// Insert or update the data in Personalization config.
  101. - (BOOL)insertOrUpdatePersonalizationConfig:(NSDictionary *)metadata fromSource:(RCNDBSource)source;
  102. /// Insert rollout metadata in rollout table.
  103. /// @param key Key indicating whether rollout metadata is fetched or active and defined in
  104. /// RCNConfigDefines.h.
  105. /// @param metadataList The metadata info for each rollout entry .
  106. /// @param handler The callback.
  107. - (void)insertOrUpdateRolloutTableWithKey:(NSString *)key
  108. value:(NSArray<NSDictionary *> *)metadataList
  109. completionHandler:(RCNDBCompletion)handler;
  110. /// Clear the record of given namespace and package name
  111. /// before updating the table.
  112. - (void)deleteRecordFromMainTableWithNamespace:(NSString *)namespace_p
  113. bundleIdentifier:(NSString *)bundleIdentifier
  114. fromSource:(RCNDBSource)source;
  115. /// Remove all the records of given package name and namespace from metadata/internal metadata DB
  116. /// before updating new values from response.
  117. - (void)deleteRecordWithBundleIdentifier:(NSString *)bundlerIdentifier
  118. namespace:(NSString *)namespace
  119. isInternalDB:(BOOL)isInternalDB;
  120. /// Remove all the records from a config content table.
  121. - (void)deleteAllRecordsFromTableWithSource:(RCNDBSource)source;
  122. /// Remove all the records from experiment table with given key.
  123. /// @param key The key of experiment data belongs to, which are defined in RCNConfigDefines.h.
  124. - (void)deleteExperimentTableForKey:(NSString *)key;
  125. /// Returns true if this a new install of the Config database.
  126. - (BOOL)isNewDatabase;
  127. @end