FirebaseSessions.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. private let appInfo: ApplicationInfo
  36. private let settings: SettingsProtocol
  37. // MARK: - Initializers
  38. // Initializes the SDK and top-level classes
  39. required convenience init(appID: String, installations: InstallationsProtocol) {
  40. let googleDataTransport = GDTCORTransport(
  41. mappingID: GoogleDataTransportConfig.sessionsLogSource,
  42. transformers: nil,
  43. target: GoogleDataTransportConfig.sessionsTarget
  44. )
  45. let fireLogger = EventGDTLogger(googleDataTransport: googleDataTransport!)
  46. let identifiers = Identifiers()
  47. let coordinator = SessionCoordinator(
  48. identifiers: identifiers,
  49. installations: installations,
  50. fireLogger: fireLogger,
  51. sampler: SessionSampler()
  52. )
  53. let initiator = SessionInitiator()
  54. let appInfo = ApplicationInfo(appID: appID)
  55. let settings = Settings(appInfo: appInfo)
  56. self.init(appID: appID,
  57. identifiers: identifiers,
  58. coordinator: coordinator,
  59. initiator: initiator,
  60. appInfo: appInfo,
  61. settings: settings)
  62. }
  63. // Initializes the SDK and begines the process of listening for lifecycle events and logging events
  64. init(appID: String, identifiers: Identifiers, coordinator: SessionCoordinator,
  65. initiator: SessionInitiator, appInfo: ApplicationInfo, settings: SettingsProtocol) {
  66. self.appID = appID
  67. self.identifiers = identifiers
  68. self.coordinator = coordinator
  69. self.initiator = initiator
  70. self.appInfo = appInfo
  71. self.settings = settings
  72. super.init()
  73. self.initiator.beginListening {
  74. self.identifiers.generateNewSessionID()
  75. let event = SessionStartEvent(identifiers: self.identifiers, appInfo: self.appInfo)
  76. DispatchQueue.global().async {
  77. self.coordinator.attemptLoggingSessionStart(event: event) { result in
  78. }
  79. }
  80. }
  81. }
  82. // MARK: - Library conformance
  83. static func componentsToRegister() -> [Component] {
  84. return [Component(SessionsProvider.self,
  85. instantiationTiming: .alwaysEager,
  86. dependencies: []) { container, isCacheable in
  87. // Sessions SDK only works for the default app
  88. guard let app = container.app, app.isDefaultApp else { return nil }
  89. isCacheable.pointee = true
  90. let installations = Installations.installations(app: app)
  91. return self.init(appID: app.options.googleAppID, installations: installations)
  92. }]
  93. }
  94. // MARK: - SessionsProvider conformance
  95. static func sessions() {}
  96. }