SessionStartEvent.swift 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 GoogleDataTransport
  17. ///
  18. /// SessionStartEvent is responsible for:
  19. /// 1) Writing fields to the Session proto
  20. /// 2) Synthesizing itself for persisting to disk and logging to GoogleDataTransport
  21. ///
  22. class SessionStartEvent: NSObject, GDTCOREventDataObject {
  23. var proto: firebase_appquality_sessions_SessionEvent
  24. init(identifiers: IdentifierProvider, time: TimeProvider = Time()) {
  25. proto = firebase_appquality_sessions_SessionEvent()
  26. super.init()
  27. proto.event_type = firebase_appquality_sessions_EventType_EVENT_SESSION_START
  28. proto.session_data.session_id = makeProtoString(identifiers.sessionID)
  29. proto.session_data.previous_session_id = makeProtoString(identifiers.previousSessionID)
  30. proto.session_data.event_timestamp_us = time.timestampUS
  31. }
  32. func setInstallationID(identifiers: IdentifierProvider) {
  33. proto.session_data.firebase_installation_id = makeProtoString(identifiers.installationID)
  34. }
  35. private func makeProtoString(_ string: String) -> UnsafeMutablePointer<pb_bytes_array_t>? {
  36. return FIRSESEncodeString(string)
  37. }
  38. // MARK: - GDTCOREventDataObject
  39. func transportBytes() -> Data {
  40. var fields = firebase_appquality_sessions_SessionEvent_fields
  41. var error: NSError?
  42. let data = FIRSESEncodeProto(&fields.0, &proto, &error)
  43. if error != nil {
  44. Logger.logError(error.debugDescription)
  45. }
  46. guard let data = data else {
  47. Logger.logError("Session event generated nil transportBytes. Returning empty data.")
  48. return Data()
  49. }
  50. return data
  51. }
  52. }