FirebaseSessions.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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: SessionsSettings
  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 appInfo = ApplicationInfo(appID: appID)
  54. let settings = SessionsSettings(
  55. appInfo: appInfo,
  56. installations: installations
  57. )
  58. let initiator = SessionInitiator(settings: settings)
  59. self.init(appID: appID,
  60. identifiers: identifiers,
  61. coordinator: coordinator,
  62. initiator: initiator,
  63. appInfo: appInfo,
  64. settings: settings)
  65. }
  66. // Initializes the SDK and begines the process of listening for lifecycle events and logging events
  67. init(appID: String, identifiers: Identifiers, coordinator: SessionCoordinator,
  68. initiator: SessionInitiator, appInfo: ApplicationInfo, settings: SessionsSettings) {
  69. self.appID = appID
  70. self.identifiers = identifiers
  71. self.coordinator = coordinator
  72. self.initiator = initiator
  73. self.appInfo = appInfo
  74. self.settings = settings
  75. super.init()
  76. self.initiator.beginListening {
  77. // On each session start, first update Settings if expired
  78. self.settings.updateSettings()
  79. self.identifiers.generateNewSessionID()
  80. // Generate a session start event only when session data collection is enabled.
  81. if self.settings.sessionsEnabled {
  82. let event = SessionStartEvent(identifiers: self.identifiers, appInfo: self.appInfo)
  83. DispatchQueue.global().async {
  84. self.coordinator.attemptLoggingSessionStart(event: event) { result in
  85. }
  86. }
  87. } else {
  88. Logger.logDebug("Session logging is disabled by configuration settings.")
  89. }
  90. }
  91. }
  92. // MARK: - Library conformance
  93. static func componentsToRegister() -> [Component] {
  94. return [Component(SessionsProvider.self,
  95. instantiationTiming: .alwaysEager,
  96. dependencies: []) { container, isCacheable in
  97. // Sessions SDK only works for the default app
  98. guard let app = container.app, app.isDefaultApp else { return nil }
  99. isCacheable.pointee = true
  100. let installations = Installations.installations(app: app)
  101. return self.init(appID: app.options.googleAppID, installations: installations)
  102. }]
  103. }
  104. // MARK: - SessionsProvider conformance
  105. static func sessions() {}
  106. }