FirebaseSessions.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. // Avoids exposing internal FirebaseCore APIs to Swift users.
  16. @_implementationOnly import FirebaseCoreExtension
  17. @_implementationOnly import FirebaseInstallations
  18. @_implementationOnly import GoogleDataTransport
  19. private enum GoogleDataTransportConfig {
  20. static let sessionsLogSource = "1974"
  21. static let sessionsTarget = GDTCORTarget.FLL
  22. }
  23. @objc(FIRSessionsProvider)
  24. protocol SessionsProvider {
  25. @objc static func sessions() -> Void
  26. }
  27. @objc(FIRSessions) final class Sessions: NSObject, Library, SessionsProvider {
  28. // MARK: - Private Variables
  29. /// The Firebase App ID associated with Sessions.
  30. private let appID: String
  31. /// Top-level Classes in the Sessions SDK
  32. private let coordinator: SessionCoordinator
  33. private let initiator: SessionInitiator
  34. private let identifiers: Identifiers
  35. // MARK: - Initializers
  36. // Initializes the SDK and top-level classes
  37. required convenience init(appID: String, installations: InstallationsProtocol) {
  38. let googleDataTransport = GDTCORTransport(
  39. mappingID: GoogleDataTransportConfig.sessionsLogSource,
  40. transformers: nil,
  41. target: GoogleDataTransportConfig.sessionsTarget
  42. )
  43. let fireLogger = EventGDTLogger(googleDataTransport: googleDataTransport!)
  44. let identifiers = Identifiers(installations: installations)
  45. let coordinator = SessionCoordinator(identifiers: identifiers, fireLogger: fireLogger)
  46. let initiator = SessionInitiator()
  47. self.init(appID: appID,
  48. identifiers: identifiers,
  49. coordinator: coordinator,
  50. initiator: initiator)
  51. }
  52. // Initializes the SDK and begines the process of listening for lifecycle events and logging events
  53. init(appID: String, identifiers: Identifiers, coordinator: SessionCoordinator,
  54. initiator: SessionInitiator) {
  55. self.appID = appID
  56. self.identifiers = identifiers
  57. self.coordinator = coordinator
  58. self.initiator = initiator
  59. super.init()
  60. self.initiator.beginListening {
  61. self.identifiers.generateNewSessionID()
  62. let event = SessionStartEvent(identifiers: self.identifiers)
  63. DispatchQueue.global().async {
  64. self.coordinator.attemptLoggingSessionStart(event: event) { result in
  65. }
  66. }
  67. }
  68. }
  69. // MARK: - Library conformance
  70. static func componentsToRegister() -> [Component] {
  71. return [Component(SessionsProvider.self,
  72. instantiationTiming: .alwaysEager,
  73. dependencies: []) { container, isCacheable in
  74. // Sessions SDK only works for the default app
  75. guard let app = container.app, app.isDefaultApp else { return nil }
  76. isCacheable.pointee = true
  77. let installations = Installations.installations(app: app)
  78. return self.init(appID: app.options.googleAppID, installations: installations)
  79. }]
  80. }
  81. // MARK: - SessionsProvider conformance
  82. static func sessions() {}
  83. }