FunctionsComponent.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 FirebaseCore
  16. import FirebaseCoreInternal
  17. @objc(FIRFunctionsProvider)
  18. protocol FunctionsProvider {
  19. @objc func functions(for app: FirebaseApp,
  20. region: String?,
  21. customDomain: String?,
  22. type: AnyClass) -> Functions
  23. // TODO: See if we can avoid the `type` parameter by either making it a `Functions` argument to
  24. // allow subclasses, or avoid it entirely and fix tests. This was done for FunctionsCombineUnit,
  25. // although we may be able to now port to using `@testable` instead of using the mock.
  26. }
  27. @objc(FIRFunctionsComponent) class FunctionsComponent: NSObject, Library, FunctionsProvider {
  28. // MARK: - Private Variables
  29. /// The app associated with all functions instances in this container.
  30. private let app: FirebaseApp
  31. /// A map of active instances, grouped by app. Keys are FirebaseApp names and values are arrays
  32. /// containing all instances of Functions associated with the given app.
  33. private var instances: [String: [Functions]] = [:]
  34. /// Lock to manage access to the instances array to avoid race conditions.
  35. private var instancesLock: os_unfair_lock = .init()
  36. // MARK: - Initializers
  37. required init(app: FirebaseApp) {
  38. self.app = app
  39. }
  40. // MARK: - Library conformance
  41. static func componentsToRegister() -> [Component] {
  42. // TODO: We don't actually add a dependency on FCM or AppCheck here even though it's used.. is
  43. // that okay?
  44. // TODO: Get Auth Interop Working, we need the type.
  45. // let authInterop = Dependency(with: AuthProvider.self, isRequired: false)
  46. return [Component(FunctionsProvider.self,
  47. instantiationTiming: .lazy,
  48. dependencies: [ /* authInterop */ ]) { container, isCacheable in
  49. guard let app = container.app else { return nil }
  50. isCacheable.pointee = true
  51. return self.init(app: app)
  52. }]
  53. }
  54. // MARK: - FunctionsProvider conformance
  55. func functions(for app: FirebaseApp,
  56. region: String?,
  57. customDomain: String?,
  58. type: AnyClass) -> Functions {
  59. os_unfair_lock_lock(&instancesLock)
  60. // Unlock before the function returns.
  61. defer { os_unfair_lock_unlock(&instancesLock) }
  62. if let associatedInstances = instances[app.name] {
  63. for instance in associatedInstances {
  64. // Domains may be nil, so handle with care.
  65. var equalDomains = false
  66. if let instanceCustomDomain = instance.customDomain {
  67. equalDomains = instanceCustomDomain == customDomain
  68. } else {
  69. equalDomains = customDomain == nil
  70. }
  71. // Check if it's a match.
  72. if instance.region == region, equalDomains {
  73. return instance
  74. }
  75. }
  76. }
  77. let newInstance = Functions(app: app,
  78. region: region ?? FunctionsConstants.defaultRegion,
  79. customDomain: customDomain)
  80. let existingInstances = instances[app.name, default: []]
  81. instances[app.name] = existingInstances + [newInstance]
  82. return newInstance
  83. }
  84. }