StorageComponent.swift 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. // Avoids exposing internal FirebaseCore APIs to Swift users.
  19. @_implementationOnly import FirebaseCoreExtension
  20. @objc(FIRStorageProvider)
  21. protocol StorageProvider {
  22. @objc func storage(for bucket: String) -> Storage
  23. // TODO: See if we can avoid the `type` parameter by either making it a `Storage` argument to
  24. // allow subclasses, or avoid it entirely and fix tests. This was done for StorageCombineUnit,
  25. // although we may be able to now port to using `@testable` instead of using the mock.
  26. }
  27. @objc(FIRStorageComponent) class StorageComponent: NSObject, Library, StorageProvider {
  28. // MARK: - Private Variables
  29. /// The app associated with all Storage 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 Storage associated with the given app.
  33. private var instances: [String: Storage] = [:]
  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. let appCheckInterop = Dependency(with: AppCheckInterop.self, isRequired: false)
  43. let authInterop = Dependency(with: AuthInterop.self, isRequired: false)
  44. return [Component(StorageProvider.self,
  45. instantiationTiming: .lazy,
  46. dependencies: [
  47. appCheckInterop,
  48. authInterop,
  49. ]) { container, isCacheable in
  50. guard let app = container.app else { return nil }
  51. isCacheable.pointee = true
  52. return self.init(app: app)
  53. }]
  54. }
  55. // MARK: - StorageProvider conformance
  56. func storage(for bucket: String) -> Storage {
  57. os_unfair_lock_lock(&instancesLock)
  58. // Unlock before the function returns.
  59. defer { os_unfair_lock_unlock(&instancesLock) }
  60. if let instance = instances[bucket] {
  61. return instance
  62. }
  63. let newInstance = FirebaseStorage.Storage(app: app, bucket: bucket)
  64. instances[bucket] = newInstance
  65. return newInstance
  66. }
  67. }