SessionCoordinator.swift 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. protocol SessionCoordinatorProtocol: Sendable {
  16. func attemptLoggingSessionStart(event: SessionStartEvent,
  17. callback: @escaping @Sendable (Result<Void,
  18. FirebaseSessionsError>) -> Void)
  19. }
  20. ///
  21. /// SessionCoordinator is responsible for coordinating the systems in this SDK
  22. /// involved with sending a Session Start event.
  23. ///
  24. final class SessionCoordinator: SessionCoordinatorProtocol {
  25. let installations: InstallationsProtocol
  26. let fireLogger: EventGDTLoggerProtocol
  27. init(installations: InstallationsProtocol,
  28. fireLogger: EventGDTLoggerProtocol) {
  29. self.installations = installations
  30. self.fireLogger = fireLogger
  31. }
  32. /// Begins the process of logging a SessionStartEvent to FireLog after
  33. /// it has been approved for sending
  34. func attemptLoggingSessionStart(event: SessionStartEvent,
  35. callback: @escaping @Sendable (Result<
  36. Void,
  37. FirebaseSessionsError
  38. >)
  39. -> Void) {
  40. /// Order of execution
  41. /// 1. Fetch the installations Id. Regardless of success, move to step 2
  42. /// 2. Log the event. If successful, all is good. Else, log the message with error.
  43. /// 3. If there was a FireLog error, expose it to the callback. Otherwise expose the FIID
  44. /// error if it exists. Otherwise, success.
  45. fillInFIID(event: event) { fiidResult in
  46. self.fireLogger.logEvent(event: event) { logResult in
  47. switch logResult {
  48. case .success():
  49. Logger.logDebug("Successfully logged Session Start event to GoogleDataTransport")
  50. switch fiidResult {
  51. case .success(()):
  52. callback(.success(()))
  53. case let .failure(error):
  54. callback(.failure(error))
  55. }
  56. case let .failure(error):
  57. callback(.failure(FirebaseSessionsError.DataTransportError(error)))
  58. }
  59. }
  60. }
  61. }
  62. private func fillInFIID(event: SessionStartEvent,
  63. callback: @escaping (Result<Void, FirebaseSessionsError>)
  64. -> Void) {
  65. installations.installationID { result in
  66. switch result {
  67. case let .success(installationsInfo):
  68. event.setInstallationID(installationId: installationsInfo.0)
  69. event.setAuthenticationToken(authenticationToken: installationsInfo.1)
  70. callback(.success(()))
  71. case let .failure(error):
  72. event.setInstallationID(installationId: "")
  73. event.setAuthenticationToken(authenticationToken: "")
  74. callback(.failure(FirebaseSessionsError.SessionInstallationsError(error)))
  75. }
  76. }
  77. }
  78. }