SessionCoordinator.swift 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. ///
  16. /// SessionCoordinator is responsible for coordinating the systems in this SDK
  17. /// involved with sending a Session Start event.
  18. ///
  19. class SessionCoordinator {
  20. let identifiers: IdentifierProvider
  21. let fireLogger: EventGDTLoggerProtocol
  22. let sampler: SessionSamplerProtocol
  23. init(identifiers: IdentifierProvider, fireLogger: EventGDTLoggerProtocol,
  24. sampler: SessionSamplerProtocol) {
  25. self.identifiers = identifiers
  26. self.fireLogger = fireLogger
  27. self.sampler = sampler
  28. }
  29. // Begins the process of logging a SessionStartEvent to FireLog, while taking into account Data Collection, Sampling, and fetching Settings
  30. func attemptLoggingSessionStart(event: SessionStartEvent,
  31. callback: @escaping (Result<Void, Error>) -> Void) {
  32. if sampler.shouldSendEventForSession(sessionId: identifiers.sessionID) {
  33. event.setInstallationID(identifiers: identifiers)
  34. fireLogger.logEvent(event: event) { result in
  35. switch result {
  36. case .success():
  37. Logger.logInfo("Successfully logged Session Start event to GoogleDataTransport")
  38. callback(.success(()))
  39. case let .failure(error):
  40. Logger
  41. .logError(
  42. "Error logging Session Start event to GoogleDataTransport: \(error)."
  43. )
  44. callback(.failure(error))
  45. }
  46. }
  47. } else {
  48. Logger
  49. .logInfo(
  50. "Session event dropped due to sampling."
  51. )
  52. callback(.failure(FirebaseSessionsError.SessionSamplingError))
  53. }
  54. }
  55. }