RCNConfigDBManager.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. typedef void (^RCNDBLoadCompletion)(BOOL success,
  50. NSDictionary *fetchedConfig,
  51. NSDictionary *activeConfig,
  52. NSDictionary *defaultConfig);
  53. /// Returns the current version of the Remote Config database.
  54. + (NSString *)remoteConfigPathForDatabase;
  55. /// Load config content from main table to cached memory during app start.
  56. - (void)loadMainWithBundleIdentifier:(NSString *)bundleIdentifier
  57. completionHandler:(RCNDBLoadCompletion)handler;
  58. /// Load config settings for a given namespace from metadata table to cached memory during app
  59. /// start. Config settings include success/failure fetch times, device contenxt, app context, etc.
  60. - (NSDictionary *)loadMetadataWithBundleIdentifier:(NSString *)bundleIdentifier
  61. namespace:(NSString *)namespace;
  62. /// Load internal metadata from internal metadata table, such as customized HTTP connection/read
  63. /// timeout, throttling time interval and number limit of throttling, etc.
  64. /// This call needs to be blocking to ensure throttling works during apps starts.
  65. - (NSDictionary *)loadInternalMetadataTable;
  66. /// Load experiment from experiment table.
  67. /// @param handler The callback when reading from DB is complete.
  68. - (void)loadExperimentWithCompletionHandler:(RCNDBCompletion)handler;
  69. /// Load Personalization from table.
  70. /// @param handler The callback when reading from DB is complete.
  71. - (void)loadPersonalizationWithCompletionHandler:(RCNDBLoadCompletion)handler;
  72. /// Insert a record in metadata table.
  73. /// @param columnNameToValue The column name and its value to be inserted in metadata table.
  74. /// @param handler The callback.
  75. - (void)insertMetadataTableWithValues:(NSDictionary *)columnNameToValue
  76. completionHandler:(RCNDBCompletion)handler;
  77. /// Insert a record in main table.
  78. /// @param values Values to be inserted.
  79. - (void)insertMainTableWithValues:(NSArray *)values
  80. fromSource:(RCNDBSource)source
  81. completionHandler:(RCNDBCompletion)handler;
  82. /// Insert a record in internal metadata table.
  83. /// @param values Values to be inserted.
  84. - (void)insertInternalMetadataTableWithValues:(NSArray *)values
  85. completionHandler:(RCNDBCompletion)handler;
  86. /// Insert exepriment data in experiment table.
  87. /// @param key The key of experiment data belongs to, which are defined in
  88. /// RCNConfigDefines.h.
  89. /// @param value The value that experiment.
  90. /// @param handler The callback.
  91. - (void)insertExperimentTableWithKey:(NSString *)key
  92. value:(NSData *)value
  93. completionHandler:(RCNDBCompletion)handler;
  94. - (void)updateMetadataWithOption:(RCNUpdateOption)option
  95. namespace:(NSString *)namespace
  96. values:(NSArray *)values
  97. completionHandler:(RCNDBCompletion)handler;
  98. /// Insert or update the data in Personalization config.
  99. - (BOOL)insertOrUpdatePersonalizationConfig:(NSDictionary *)metadata fromSource:(RCNDBSource)source;
  100. /// Clear the record of given namespace and package name
  101. /// before updating the table.
  102. - (void)deleteRecordFromMainTableWithNamespace:(NSString *)namespace_p
  103. bundleIdentifier:(NSString *)bundleIdentifier
  104. fromSource:(RCNDBSource)source;
  105. /// Remove all the records of given package name and namespace from metadata/internal metadata DB
  106. /// before updating new values from response.
  107. - (void)deleteRecordWithBundleIdentifier:(NSString *)bundlerIdentifier
  108. namespace:(NSString *)namespace
  109. isInternalDB:(BOOL)isInternalDB;
  110. /// Remove all the records from a config content table.
  111. - (void)deleteAllRecordsFromTableWithSource:(RCNDBSource)source;
  112. /// Remove all the records from experiment table with given key.
  113. /// @param key The key of experiment data belongs to, which are defined in RCNConfigDefines.h.
  114. - (void)deleteExperimentTableForKey:(NSString *)key;
  115. /// Returns true if this a new install of the Config database.
  116. - (BOOL)isNewDatabase;
  117. @end