SessionCoordinator.swift 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 installations: InstallationsProtocol
  22. let fireLogger: EventGDTLoggerProtocol
  23. let sampler: SessionSamplerProtocol
  24. init(identifiers: IdentifierProvider,
  25. installations: InstallationsProtocol,
  26. fireLogger: EventGDTLoggerProtocol,
  27. sampler: SessionSamplerProtocol) {
  28. self.identifiers = identifiers
  29. self.installations = installations
  30. self.fireLogger = fireLogger
  31. self.sampler = sampler
  32. }
  33. // Begins the process of logging a SessionStartEvent to FireLog, while taking into account Data Collection, Sampling, and fetching Settings
  34. func attemptLoggingSessionStart(event: SessionStartEvent,
  35. callback: @escaping (Result<Void, Error>) -> Void) {
  36. /// Order of execution
  37. /// 1. Check if the session can be sent. If yes, move to 2. Else, drop the event.
  38. /// 2. Fetch the installations Id. If successful, move to 3. Else, drop sending the event.
  39. /// 3. Log the event. If successful, all is good. Else, log the message with error.
  40. if sampler.shouldSendEventForSession(sessionId: identifiers.sessionID) {
  41. installations.installationID { result in
  42. switch result {
  43. case let .success(fiid):
  44. event.setInstallationID(installationId: fiid)
  45. self.fireLogger.logEvent(event: event) { logResult in
  46. switch logResult {
  47. case .success():
  48. Logger.logInfo("Successfully logged Session Start event to GoogleDataTransport")
  49. callback(.success(()))
  50. case let .failure(error):
  51. Logger
  52. .logError(
  53. "Error logging Session Start event to GoogleDataTransport: \(error)."
  54. )
  55. callback(.failure(error))
  56. }
  57. }
  58. case let .failure(error):
  59. Logger
  60. .logError(
  61. "Error getting Firebase Installation ID: \(error). Skip sending event."
  62. )
  63. callback(.failure(FirebaseSessionsError.SessionInstallationsError))
  64. }
  65. }
  66. } else {
  67. Logger
  68. .logInfo(
  69. "Session event dropped due to sampling."
  70. )
  71. callback(.failure(FirebaseSessionsError.SessionSamplingError))
  72. }
  73. }
  74. }