ApplicationInfo.swift 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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" // Prod environment
  21. case staging = "staging" // Staging environment
  22. case autopush = "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(), envParams: [String : String] = ProcessInfo.processInfo.environment) {
  43. self.appID = appID
  44. self.networkInfo = networkInfo
  45. self.envParams = envParams
  46. }
  47. var bundleID: String {
  48. return Bundle.main.bundleIdentifier ?? ""
  49. }
  50. var sdkVersion: String {
  51. return FirebaseVersion()
  52. }
  53. var osName: String {
  54. // TODO: Update once https://github.com/google/GoogleUtilities/pull/89 is released
  55. // to production, update this to GULAppEnvironmentUtil.appleDevicePlatform() and update
  56. // the podfile to depend on the newest version of GoogleUtilities
  57. return GULAppEnvironmentUtil.applePlatform()
  58. }
  59. var mccMNC: String {
  60. return FIRSESValidateMccMnc(networkInfo.mobileCountryCode, networkInfo.mobileNetworkCode) ?? ""
  61. }
  62. var environment: DevEnvironment {
  63. let environment = envParams["FIREBASE_RUN_ENVIRONMENT"]
  64. if (environment != nil) {
  65. return DevEnvironment(rawValue: environment!.trimmingCharacters(in: .whitespaces).lowercased()) ?? DevEnvironment.prod
  66. }
  67. return DevEnvironment.prod
  68. }
  69. }