SessionCoordinator.swift 2.9 KB

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