FIRRemoteConfigComponent.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import Foundation
  2. import FirebaseCore
  3. class FIRRemoteConfigComponent :NSObject, FIRRemoteConfigProvider, FIRRemoteConfigInterop {
  4. static var componentInstances = [String : FIRRemoteConfigComponent]()
  5. var app : FIRApp?
  6. var instances : NSMutableDictionary<NSString,FIRRemoteConfig> = [:]
  7. static func getComponent(app: FIRApp) -> FIRRemoteConfigComponent? {
  8. @synchronized(componentInstances) {
  9. if (componentInstances.isEmpty) {
  10. componentInstances = [String : FIRRemoteConfigComponent]()
  11. }
  12. if (componentInstances[app.name] == nil) {
  13. componentInstances[app.name] = FIRRemoteConfigComponent(app: app)
  14. }
  15. return componentInstances[app.name]
  16. }
  17. return nil
  18. }
  19. static func clearAllComponentInstances() {
  20. @synchronized(componentInstances) {
  21. componentInstances.removeAll()
  22. }
  23. }
  24. func remoteConfigForNamespace(firebaseNamespace: String) -> FIRRemoteConfig? {
  25. if firebaseNamespace.isEmpty {
  26. return nil
  27. }
  28. // Validate the required information is available.
  29. guard let options = self.app?.options else {
  30. fatalError("The 'options' property was not available for this configuration")
  31. }
  32. if options.googleAppID.isEmpty {
  33. fatalError("Firebase Remote Config is missing the required googleAppID property from the " +
  34. "configured FirebaseApp and will not be able to function properly. Please " +
  35. "fix this issue to ensure that Firebase is correctly configured.")
  36. }
  37. if options.GCMSenderID.isEmpty {
  38. fatalError("Firebase Remote Config is missing the required GCMSenderID property from the " +
  39. "configured FirebaseApp and will not be able to function properly. Please " +
  40. "fix this issue to ensure that Firebase is correctly configured.")
  41. }
  42. if options.projectID.isEmpty {
  43. fatalError("Firebase Remote Config is missing the required projectID property from the " +
  44. "configured FirebaseApp and will not be able to function properly. Please " +
  45. "fix this issue to ensure that Firebase is correctly configured.")
  46. }
  47. var instance : FIRRemoteConfig? = self.instances[firebaseNamespace]
  48. if (instance == nil) {
  49. let appName : String = self.app?.name ?? ""
  50. let dbManager : RCNConfigDBManager = RCNConfigDBManager.sharedInstance()
  51. let configContent = RCNConfigContent.sharedInstance()
  52. let googleAppID = options.googleAppID ?? ""
  53. instance = FIRRemoteConfig(appName: appName,
  54. FIROptions: options,
  55. namespace: FIRNamespace, DBManager: dbManager,
  56. configContent: configContent, analytics: nil)
  57. self.instances[firebaseNamespace] = instance;
  58. }
  59. return instance
  60. }
  61. init(app: FIRApp) {
  62. self.app = app;
  63. self.instances = [:]
  64. }
  65. static func load() {
  66. // Register as an internal library to be part of the initialization process. The name comes
  67. // from go/firebase-sdk-platform-info.
  68. FIRApp.registerInternalLibrary(self, withName: "fire-rc")
  69. }
  70. static func componentsToRegister() -> [FIRComponent] {
  71. let rcProvider = FIRComponent(protocol: FIRRemoteConfigProvider.self,
  72. instantiationTiming: .alwaysEager) {
  73. (container, isCacheable) -> AnyObject in
  74. // Cache the component so instances of Remote Config are cached.
  75. var isCacheable = true
  76. return FIRRemoteConfigComponent.getComponent(app: container.app) as Any
  77. }
  78. let rcInterop = FIRComponent(protocol: FIRRemoteConfigInterop.self,
  79. instantiationTiming: .alwaysEager) {
  80. (container, isCacheable) -> AnyObject in
  81. // Cache the component so instances of Remote Config are cached.
  82. var isCacheable = true
  83. return FIRRemoteConfigComponent.getComponent(app: container.app) as Any
  84. }
  85. return [rcProvider, rcInterop]
  86. }
  87. // MARK: - Remote Config Interop Protocol
  88. func registerRolloutsStateSubscriber(subscriber: FIRRolloutsStateSubscriber, for namespace: String) {
  89. let instance : FIRRemoteConfig? = self.remoteConfigForNamespace(firebaseNamespace: namespace)
  90. guard let instance = instance else {
  91. return
  92. }
  93. [instance addRemoteConfigInteropSubscriber:subscriber];
  94. }
  95. }