SessionCoordinator.swift 2.4 KB

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