ApplicationInfo.swift 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 FirebaseCore
  17. @_implementationOnly import GoogleUtilities
  18. /// Development environment for the application.
  19. enum DevEnvironment: String {
  20. case prod // Prod environment
  21. case staging // Staging environment
  22. case autopush // Autopush environment
  23. }
  24. protocol ApplicationInfoProtocol {
  25. /// Google App ID / GMP App ID
  26. var appID: String { get }
  27. /// App's bundle ID / bundle short version
  28. var bundleID: String { get }
  29. /// Version of the Firebase SDK
  30. var sdkVersion: String { get }
  31. /// Crashlytics-specific device / OS filter values.
  32. var osName: String { get }
  33. /// Validated Mobile Country Code and Mobile Network Code
  34. var mccMNC: String { get }
  35. /// Development environment on which the application is running.
  36. var environment: DevEnvironment { get }
  37. }
  38. class ApplicationInfo: ApplicationInfoProtocol {
  39. let appID: String
  40. private let networkInfo: NetworkInfoProtocol
  41. private let envParams: [String: String]
  42. init(appID: String, networkInfo: NetworkInfoProtocol = NetworkInfo(),
  43. envParams: [String: String] = ProcessInfo.processInfo.environment) {
  44. self.appID = appID
  45. self.networkInfo = networkInfo
  46. self.envParams = envParams
  47. }
  48. var bundleID: String {
  49. return Bundle.main.bundleIdentifier ?? ""
  50. }
  51. var sdkVersion: String {
  52. return FirebaseVersion()
  53. }
  54. var osName: String {
  55. // TODO: Update once https://github.com/google/GoogleUtilities/pull/89 is released
  56. // to production, update this to GULAppEnvironmentUtil.appleDevicePlatform() and update
  57. // the podfile to depend on the newest version of GoogleUtilities
  58. return GULAppEnvironmentUtil.applePlatform()
  59. }
  60. var mccMNC: String {
  61. return FIRSESValidateMccMnc(networkInfo.mobileCountryCode, networkInfo.mobileNetworkCode) ?? ""
  62. }
  63. var environment: DevEnvironment {
  64. if let environment = envParams["FirebaseSessionsRunEnvironment"] {
  65. return DevEnvironment(rawValue: environment.trimmingCharacters(in: .whitespaces).lowercased())
  66. ?? DevEnvironment.prod
  67. }
  68. return DevEnvironment.prod
  69. }
  70. }