StorageComponent.swift 2.8 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. import FirebaseCoreExtension
  19. @objc(FIRStorageProvider)
  20. protocol StorageProvider {
  21. @objc func storage(for bucket: String) -> Storage
  22. // TODO: See if we can avoid the `type` parameter by either making it a `Storage` argument to
  23. // allow subclasses, or avoid it entirely and fix tests. This was done for StorageCombineUnit,
  24. // although we may be able to now port to using `@testable` instead of using the mock.
  25. }
  26. @objc(FIRStorageComponent) class StorageComponent: NSObject, Library, StorageProvider {
  27. // MARK: - Private Variables
  28. /// The app associated with all Storage instances in this container.
  29. private let app: FirebaseApp
  30. /// A map of active instances, grouped by app. Keys are FirebaseApp names and values are arrays
  31. /// containing all instances of Storage associated with the given app.
  32. private var instances: [String: Storage] = [:]
  33. /// Lock to manage access to the instances array to avoid race conditions.
  34. private var instancesLock: os_unfair_lock = .init()
  35. // MARK: - Initializers
  36. required init(app: FirebaseApp) {
  37. self.app = app
  38. }
  39. // MARK: - Library conformance
  40. static func componentsToRegister() -> [Component] {
  41. let appCheckInterop = Dependency(with: AppCheckInterop.self, isRequired: false)
  42. let authInterop = Dependency(with: AuthInterop.self, isRequired: false)
  43. return [Component(StorageProvider.self,
  44. instantiationTiming: .lazy,
  45. dependencies: [
  46. appCheckInterop,
  47. authInterop,
  48. ]) { 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: - StorageProvider conformance
  55. func storage(for bucket: String) -> Storage {
  56. os_unfair_lock_lock(&instancesLock)
  57. // Unlock before the function returns.
  58. defer { os_unfair_lock_unlock(&instancesLock) }
  59. if let instance = instances[bucket] {
  60. return instance
  61. }
  62. let newInstance = FirebaseStorage.Storage(app: app, bucket: bucket)
  63. instances[bucket] = newInstance
  64. return newInstance
  65. }
  66. }