StorageComponent.swift 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. }
  24. @objc(FIRStorageComponent) class StorageComponent: NSObject, Library, StorageProvider {
  25. // MARK: - Private Variables
  26. /// The app associated with all Storage instances in this container.
  27. /// This is `unowned` instead of `weak` so it can be used without unwrapping in `storage(...)`
  28. private unowned let app: FirebaseApp
  29. /// A map of active instances, grouped by app. Keys are FirebaseApp names and values are arrays
  30. /// containing all instances of Storage associated with the given app.
  31. private var instances: [String: Storage] = [:]
  32. /// Lock to manage access to the instances array to avoid race conditions.
  33. private var instancesLock: os_unfair_lock = .init()
  34. // MARK: - Initializers
  35. required init(app: FirebaseApp) {
  36. self.app = app
  37. }
  38. // MARK: - Library conformance
  39. static func componentsToRegister() -> [Component] {
  40. let appCheckInterop = Dependency(with: AppCheckInterop.self, isRequired: false)
  41. let authInterop = Dependency(with: AuthInterop.self, isRequired: false)
  42. return [Component(StorageProvider.self,
  43. instantiationTiming: .lazy,
  44. dependencies: [
  45. appCheckInterop,
  46. authInterop,
  47. ]) { container, isCacheable in
  48. guard let app = container.app else { return nil }
  49. isCacheable.pointee = true
  50. return self.init(app: app)
  51. }]
  52. }
  53. // MARK: - StorageProvider conformance
  54. func storage(for bucket: String) -> Storage {
  55. os_unfair_lock_lock(&instancesLock)
  56. // Unlock before the function returns.
  57. defer { os_unfair_lock_unlock(&instancesLock) }
  58. if let instance = instances[bucket] {
  59. return instance
  60. }
  61. let newInstance = FirebaseStorage.Storage(app: app, bucket: bucket)
  62. instances[bucket] = newInstance
  63. return newInstance
  64. }
  65. }