Identifiers.swift 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. //
  2. // Copyright 2022 Google LLC
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. import Foundation
  16. @_implementationOnly import FirebaseInstallations
  17. protocol IdentifierProvider {
  18. var sessionID: String {
  19. get
  20. }
  21. var previousSessionID: String? {
  22. get
  23. }
  24. }
  25. ///
  26. /// Identifiers is responsible for:
  27. /// 1) Getting the Installation ID from Installations
  28. /// 2) Generating the Session ID
  29. /// 3) Persisting and reading the Session ID from the last session
  30. /// (Maybe) 4) Persisting, reading, and incrementing an increasing index
  31. ///
  32. class Identifiers: IdentifierProvider {
  33. private var _sessionID: String?
  34. private var _previousSessionID: String?
  35. // Generates a new Session ID. If there was already a generated Session ID
  36. // from the last session during the app's lifecycle, it will also set the last Session ID
  37. func generateNewSessionID() {
  38. _previousSessionID = _sessionID
  39. _sessionID = UUID().uuidString.replacingOccurrences(of: "-", with: "").lowercased()
  40. }
  41. var sessionID: String {
  42. guard let _sessionID = _sessionID else {
  43. Logger.logError("Error: Sessions SDK did not generate a Session ID")
  44. return ""
  45. }
  46. return _sessionID
  47. }
  48. var previousSessionID: String? {
  49. return _previousSessionID
  50. }
  51. }