FirebaseSessions.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 appInfo = ApplicationInfo(appID: appID)
  48. let settings = Settings(
  49. appInfo: appInfo,
  50. downloader: SettingsDownloader(
  51. appInfo: appInfo,
  52. identifiers: identifiers,
  53. installations: installations
  54. )
  55. )
  56. let coordinator = SessionCoordinator(
  57. identifiers: identifiers,
  58. installations: installations,
  59. fireLogger: fireLogger,
  60. sampler: SessionSampler(sessionSamplingRate: settings.samplingRate),
  61. loggingEnabled: settings.sessionsEnabled
  62. )
  63. let initiator = SessionInitiator(sessionTimeout: settings.sessionTimeout)
  64. self.init(appID: appID,
  65. identifiers: identifiers,
  66. coordinator: coordinator,
  67. initiator: initiator,
  68. appInfo: appInfo,
  69. settings: settings)
  70. }
  71. // Initializes the SDK and begines the process of listening for lifecycle events and logging events
  72. init(appID: String, identifiers: Identifiers, coordinator: SessionCoordinator,
  73. initiator: SessionInitiator, appInfo: ApplicationInfo, settings: SettingsProtocol) {
  74. self.appID = appID
  75. self.identifiers = identifiers
  76. self.coordinator = coordinator
  77. self.initiator = initiator
  78. self.appInfo = appInfo
  79. self.settings = settings
  80. super.init()
  81. self.initiator.beginListening {
  82. // On each session start, first update Settings if expired
  83. self.settings.fetchAndCacheSettings()
  84. self.identifiers.generateNewSessionID()
  85. let event = SessionStartEvent(identifiers: self.identifiers, appInfo: self.appInfo)
  86. DispatchQueue.global().async {
  87. self.coordinator.attemptLoggingSessionStart(event: event) { result in
  88. }
  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. }