FunctionsComponent.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright 2022 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import Foundation
  15. import FirebaseAppCheckInterop
  16. import FirebaseAuthInterop
  17. import FirebaseCore
  18. import FirebaseMessagingInterop
  19. // Avoids exposing internal FirebaseCore APIs to Swift users.
  20. @_implementationOnly import FirebaseCoreExtension
  21. @objc(FIRFunctionsProvider)
  22. protocol FunctionsProvider {
  23. @objc func functions(for app: FirebaseApp,
  24. region: String?,
  25. customDomain: String?,
  26. type: AnyClass) -> Functions
  27. // TODO: See if we can avoid the `type` parameter by either making it a `Functions` argument to
  28. // allow subclasses, or avoid it entirely and fix tests. This was done for FunctionsCombineUnit,
  29. // although we may be able to now port to using `@testable` instead of using the mock.
  30. }
  31. @objc(FIRFunctionsComponent) class FunctionsComponent: NSObject, Library, FunctionsProvider {
  32. // MARK: - Private Variables
  33. /// The app associated with all functions instances in this container.
  34. private let app: FirebaseApp
  35. /// A map of active instances, grouped by app. Keys are FirebaseApp names and values are arrays
  36. /// containing all instances of Functions associated with the given app.
  37. private var instances: [String: [Functions]] = [:]
  38. /// Lock to manage access to the instances array to avoid race conditions.
  39. private var instancesLock: os_unfair_lock = .init()
  40. // MARK: - Initializers
  41. required init(app: FirebaseApp) {
  42. self.app = app
  43. }
  44. // MARK: - Library conformance
  45. static func componentsToRegister() -> [Component] {
  46. let appCheckInterop = Dependency(with: AppCheckInterop.self, isRequired: false)
  47. let authInterop = Dependency(with: AuthInterop.self, isRequired: false)
  48. let messagingInterop = Dependency(with: MessagingInterop.self, isRequired: false)
  49. return [Component(FunctionsProvider.self,
  50. instantiationTiming: .lazy,
  51. dependencies: [
  52. appCheckInterop,
  53. authInterop,
  54. messagingInterop,
  55. ]) { container, isCacheable in
  56. guard let app = container.app else { return nil }
  57. isCacheable.pointee = true
  58. return self.init(app: app)
  59. }]
  60. }
  61. // MARK: - FunctionsProvider conformance
  62. func functions(for app: FirebaseApp,
  63. region: String?,
  64. customDomain: String?,
  65. type: AnyClass) -> Functions {
  66. os_unfair_lock_lock(&instancesLock)
  67. // Unlock before the function returns.
  68. defer { os_unfair_lock_unlock(&instancesLock) }
  69. if let associatedInstances = instances[app.name] {
  70. for instance in associatedInstances {
  71. // Domains may be nil, so handle with care.
  72. var equalDomains = false
  73. if let instanceCustomDomain = instance.customDomain {
  74. equalDomains = instanceCustomDomain == customDomain
  75. } else {
  76. equalDomains = customDomain == nil
  77. }
  78. // Check if it's a match.
  79. if instance.region == region, equalDomains {
  80. return instance
  81. }
  82. }
  83. }
  84. let newInstance = Functions(app: app,
  85. region: region ?? FunctionsConstants.defaultRegion,
  86. customDomain: customDomain)
  87. let existingInstances = instances[app.name, default: []]
  88. instances[app.name] = existingInstances + [newInstance]
  89. return newInstance
  90. }
  91. }