FunctionsComponent.swift 3.8 KB

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