ApplicationInfo.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. internal import FirebaseCore
  17. #if SWIFT_PACKAGE
  18. import FirebaseSessionsObjC
  19. #endif // SWIFT_PACKAGE
  20. #if SWIFT_PACKAGE
  21. internal import GoogleUtilities_Environment
  22. #else
  23. internal import GoogleUtilities
  24. #endif // SWIFT_PACKAGE
  25. /// Development environment for the application.
  26. enum DevEnvironment: String {
  27. case prod // Prod environment
  28. case staging // Staging environment
  29. case autopush // Autopush environment
  30. }
  31. protocol ApplicationInfoProtocol: Sendable {
  32. /// Google App ID / GMP App ID
  33. var appID: String { get }
  34. /// Version of the Firebase SDK
  35. var sdkVersion: String { get }
  36. /// Crashlytics-specific device / OS filter values.
  37. var osName: String { get }
  38. /// Model of the device
  39. var deviceModel: String { get }
  40. /// Network information for the application
  41. var networkInfo: NetworkInfoProtocol { get }
  42. /// Development environment on which the application is running.
  43. var environment: DevEnvironment { get }
  44. var appBuildVersion: String { get }
  45. var appDisplayVersion: String { get }
  46. var osBuildVersion: String { get }
  47. var osDisplayVersion: String { get }
  48. }
  49. final class ApplicationInfo: ApplicationInfoProtocol {
  50. let appID: String
  51. private let networkInformation: NetworkInfoProtocol
  52. private let envParams: [String: String]
  53. // Used to hold bundle info, so the `Any` params should also
  54. // be Sendable.
  55. private nonisolated(unsafe) let infoDict: [String: Any]?
  56. init(appID: String, networkInfo: NetworkInfoProtocol = NetworkInfo(),
  57. envParams: [String: String] = ProcessInfo.processInfo.environment,
  58. infoDict: [String: Any]? = Bundle.main.infoDictionary) {
  59. self.appID = appID
  60. networkInformation = networkInfo
  61. self.envParams = envParams
  62. self.infoDict = infoDict
  63. }
  64. var sdkVersion: String {
  65. return FirebaseVersion()
  66. }
  67. var osName: String {
  68. return GULAppEnvironmentUtil.appleDevicePlatform()
  69. }
  70. var deviceModel: String {
  71. return GULAppEnvironmentUtil.deviceSimulatorModel() ?? ""
  72. }
  73. var networkInfo: NetworkInfoProtocol {
  74. return networkInformation
  75. }
  76. var environment: DevEnvironment {
  77. if let environment = envParams["FirebaseSessionsRunEnvironment"] {
  78. return DevEnvironment(rawValue: environment.trimmingCharacters(in: .whitespaces).lowercased())
  79. ?? DevEnvironment.prod
  80. }
  81. return DevEnvironment.prod
  82. }
  83. var appBuildVersion: String {
  84. return infoDict?["CFBundleVersion"] as? String ?? ""
  85. }
  86. var appDisplayVersion: String {
  87. return infoDict?["CFBundleShortVersionString"] as? String ?? ""
  88. }
  89. var osBuildVersion: String {
  90. return FIRSESGetSysctlEntry("kern.osversion") ?? ""
  91. }
  92. var osDisplayVersion: String {
  93. return GULAppEnvironmentUtil.systemVersion()
  94. }
  95. }