FunctionsComponent.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 FirebaseAppCheckInterop
  15. import FirebaseAuthInterop
  16. import FirebaseCore
  17. import FirebaseMessagingInterop
  18. import Foundation
  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. /// This is `unowned` instead of `weak` so it can be used without unwrapping in `functions()`
  35. private unowned let app: FirebaseApp
  36. /// A map of active instances, grouped by app. Keys are FirebaseApp names and values are arrays
  37. /// containing all instances of Functions associated with the given app.
  38. private var instances: [String: [Functions]] = [:]
  39. /// Lock to manage access to the instances array to avoid race conditions.
  40. private var instancesLock: os_unfair_lock = .init()
  41. // MARK: - Initializers
  42. required init(app: FirebaseApp) {
  43. self.app = app
  44. }
  45. // MARK: - Library conformance
  46. static func componentsToRegister() -> [Component] {
  47. let appCheckInterop = Dependency(with: AppCheckInterop.self, isRequired: false)
  48. let authInterop = Dependency(with: AuthInterop.self, isRequired: false)
  49. let messagingInterop = Dependency(with: MessagingInterop.self, isRequired: false)
  50. return [Component(FunctionsProvider.self,
  51. instantiationTiming: .lazy,
  52. dependencies: [
  53. appCheckInterop,
  54. authInterop,
  55. messagingInterop,
  56. ]) { container, isCacheable in
  57. guard let app = container.app else { return nil }
  58. isCacheable.pointee = true
  59. return self.init(app: app)
  60. }]
  61. }
  62. // MARK: - FunctionsProvider conformance
  63. func functions(for app: FirebaseApp,
  64. region: String?,
  65. customDomain: String?,
  66. type: AnyClass) -> Functions {
  67. os_unfair_lock_lock(&instancesLock)
  68. // Unlock before the function returns.
  69. defer { os_unfair_lock_unlock(&instancesLock) }
  70. if let associatedInstances = instances[app.name] {
  71. for instance in associatedInstances {
  72. // Domains may be nil, so handle with care.
  73. var equalDomains = false
  74. if let instanceCustomDomain = instance.customDomain {
  75. equalDomains = instanceCustomDomain == customDomain
  76. } else {
  77. equalDomains = customDomain == nil
  78. }
  79. // Check if it's a match.
  80. if instance.region == region, equalDomains {
  81. return instance
  82. }
  83. }
  84. }
  85. let newInstance = Functions(app: app,
  86. region: region ?? FunctionsConstants.defaultRegion,
  87. customDomain: customDomain)
  88. let existingInstances = instances[app.name, default: []]
  89. instances[app.name] = existingInstances + [newInstance]
  90. return newInstance
  91. }
  92. }