FirebaseSessions.swift 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 FirebaseCore
  16. import FirebaseInstallations
  17. // Avoids exposing internal FirebaseCore APIs to Swift users.
  18. @_implementationOnly import FirebaseCoreExtension
  19. @objc(FIRSessionsProvider)
  20. protocol SessionsProvider {
  21. @objc static func sessions() -> Void
  22. }
  23. @objc(FIRSessions) final class Sessions: NSObject, Library, SessionsProvider {
  24. // MARK: - Private Variables
  25. /// The Firebase App ID associated with Sessions.
  26. private let appID: String
  27. /// Top-level Classes in the Sessions SDK
  28. private let coordinator: SessionCoordinator
  29. private let initiator: SessionInitiator
  30. private let identifiers: Identifiers
  31. // MARK: - Initializers
  32. required convenience init(appID: String, installations: InstallationsProtocol) {
  33. let identifiers = Identifiers(installations: installations)
  34. let coordinator = SessionCoordinator(identifiers: identifiers)
  35. let initiator = SessionInitiator()
  36. self.init(appID: appID,
  37. identifiers: identifiers,
  38. coordinator: coordinator,
  39. initiator: initiator)
  40. }
  41. init(appID: String, identifiers: Identifiers, coordinator: SessionCoordinator,
  42. initiator: SessionInitiator) {
  43. self.appID = appID
  44. self.identifiers = identifiers
  45. self.coordinator = coordinator
  46. self.initiator = initiator
  47. super.init()
  48. self.initiator.beginListening {
  49. self.identifiers.generateNewSessionID()
  50. self.coordinator.runMain()
  51. }
  52. }
  53. // MARK: - Library conformance
  54. static func componentsToRegister() -> [Component] {
  55. return [Component(SessionsProvider.self,
  56. instantiationTiming: .alwaysEager,
  57. dependencies: []) { container, isCacheable in
  58. // Sessions SDK only works for the default app
  59. guard let app = container.app, app.isDefaultApp else { return nil }
  60. isCacheable.pointee = true
  61. let installations = Installations.installations(app: app)
  62. return self.init(appID: app.options.googleAppID, installations: installations)
  63. }]
  64. }
  65. // MARK: - SessionsProvider conformance
  66. static func sessions() {}
  67. }