FirebaseSessions.swift 3.6 KB

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