RemoteSettings.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. /// Extends ApplicationInfoProtocol to string-format a combined appDisplayVersion and appBuildVersion
  17. extension ApplicationInfoProtocol {
  18. var synthesizedVersion: String { return "\(appDisplayVersion) (\(appBuildVersion))" }
  19. }
  20. class RemoteSettings: SettingsProvider {
  21. private static let cacheDurationSecondsDefault: TimeInterval = 60 * 60
  22. private static let flagSessionsEnabled = "sessions_enabled"
  23. private static let flagSamplingRate = "sampling_rate"
  24. private static let flagSessionTimeout = "session_timeout_seconds"
  25. private static let flagCacheDuration = "cache_duration"
  26. private static let flagSessionsCache = "app_quality"
  27. private let appInfo: ApplicationInfoProtocol
  28. private let downloader: SettingsDownloadClient
  29. private var cache: SettingsCacheClient
  30. private var cacheDurationSeconds: TimeInterval {
  31. guard let duration = cache.cacheContent[RemoteSettings.flagCacheDuration] as? Double else {
  32. return RemoteSettings.cacheDurationSecondsDefault
  33. }
  34. return duration
  35. }
  36. private var sessionsCache: [String: Any] {
  37. return cache.cacheContent[RemoteSettings.flagSessionsCache] as? [String: Any] ?? [:]
  38. }
  39. init(appInfo: ApplicationInfoProtocol,
  40. downloader: SettingsDownloadClient,
  41. cache: SettingsCacheClient = SettingsCache()) {
  42. self.appInfo = appInfo
  43. self.cache = cache
  44. self.downloader = downloader
  45. }
  46. private func fetchAndCacheSettings(currentTime: Date) {
  47. // Only fetch if cache is expired, otherwise do nothing
  48. guard isCacheExpired(time: currentTime) else {
  49. Logger.logDebug("[Settings] Cache is not expired, no fetch will be made.")
  50. return
  51. }
  52. downloader.fetch { result in
  53. switch result {
  54. case let .success(dictionary):
  55. // Saves all newly fetched Settings to cache
  56. self.cache.cacheContent = dictionary
  57. // Saves a "cache-key" which carries TTL metadata about current cache
  58. self.cache.cacheKey = CacheKey(
  59. createdAt: currentTime,
  60. googleAppID: self.appInfo.appID,
  61. appVersion: self.appInfo.synthesizedVersion
  62. )
  63. case let .failure(error):
  64. Logger.logError("[Settings] Fetching newest settings failed with error: \(error)")
  65. }
  66. }
  67. }
  68. }
  69. typealias RemoteSettingsConfigurations = RemoteSettings
  70. extension RemoteSettingsConfigurations {
  71. var sessionsEnabled: Bool? {
  72. return sessionsCache[RemoteSettings.flagSessionsEnabled] as? Bool
  73. }
  74. var samplingRate: Double? {
  75. return sessionsCache[RemoteSettings.flagSamplingRate] as? Double
  76. }
  77. var sessionTimeout: TimeInterval? {
  78. return sessionsCache[RemoteSettings.flagSessionTimeout] as? Double
  79. }
  80. }
  81. typealias RemoteSettingsProvider = RemoteSettings
  82. extension RemoteSettingsConfigurations {
  83. func updateSettings(currentTime: Date) {
  84. fetchAndCacheSettings(currentTime: currentTime)
  85. }
  86. func updateSettings() {
  87. updateSettings(currentTime: Date())
  88. }
  89. func isSettingsStale() -> Bool {
  90. return isCacheExpired(time: Date())
  91. }
  92. private func isCacheExpired(time: Date) -> Bool {
  93. guard !cache.cacheContent.isEmpty else {
  94. cache.removeCache()
  95. return true
  96. }
  97. guard let cacheKey = cache.cacheKey else {
  98. Logger.logError("[Settings] Could not load settings cache key")
  99. cache.removeCache()
  100. return true
  101. }
  102. guard cacheKey.googleAppID == appInfo.appID else {
  103. Logger
  104. .logDebug("[Settings] Cache expired because Google App ID changed")
  105. cache.removeCache()
  106. return true
  107. }
  108. if time.timeIntervalSince(cacheKey.createdAt) > cacheDurationSeconds {
  109. Logger.logDebug("[Settings] Cache TTL expired")
  110. return true
  111. }
  112. if appInfo.synthesizedVersion != cacheKey.appVersion {
  113. Logger.logDebug("[Settings] Cache expired because app version changed")
  114. return true
  115. }
  116. return false
  117. }
  118. }