diff --git a/Sources/Crashes/CRCIDManager.swift b/Sources/Crashes/CRCIDManager.swift new file mode 100644 index 000000000..f7fc6a392 --- /dev/null +++ b/Sources/Crashes/CRCIDManager.swift @@ -0,0 +1,63 @@ +// +// CRCIDManager.swift +// DuckDuckGo +// +// Copyright © 2024 DuckDuckGo. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +import Foundation +import Persistence +import os.log + +/// Cohort identifier used exclusively to distinguish systemic crashes, only after the user opts in to send them. +/// Its purpose is strictly limited to improving the reliability of crash reporting and is never used elsewhere. +public class CRCIDManager { + static let crcidKey = "CRCIDManager.crcidKey" + var store: KeyValueStoring + + public init(store: KeyValueStoring = UserDefaults()) { + self.store = store + } + + public func handleCrashSenderResult(result: Result, response: HTTPURLResponse?) { + switch result { + case .success: + Logger.general.debug("Crash Collection - Sending Crash Report: succeeded") + if let receivedCRCID = response?.allHeaderFields[CrashReportSender.httpHeaderCRCID] as? String { + if crcid != receivedCRCID { + Logger.general.debug("Crash Collection - Received new value for CRCID: \(receivedCRCID), setting local crcid value") + crcid = receivedCRCID + } else { + Logger.general.debug("Crash Collection - Received matching value for CRCID: \(receivedCRCID), no update necessary") + } + } else { + Logger.general.debug("Crash Collection - No value for CRCID header: \(CRCIDManager.crcidKey), clearing local crcid value if present") + crcid = "" + } + case .failure(let failure): + Logger.general.debug("Crash Collection - Sending Crash Report: failed (\(failure))") + } + } + + public var crcid: String? { + get { + return self.store.object(forKey: CRCIDManager.crcidKey) as? String + } + + set { + store.set(newValue, forKey: CRCIDManager.crcidKey) + } + } +} diff --git a/Sources/Crashes/CrashCollection.swift b/Sources/Crashes/CrashCollection.swift index 31a95840b..3f1f71384 100644 --- a/Sources/Crashes/CrashCollection.swift +++ b/Sources/Crashes/CrashCollection.swift @@ -209,49 +209,3 @@ public final class CrashCollection { static let firstCrashKey = "CrashCollection.first" } } - -// TODO: This should really be its own file, but adding a new file to BSK and propagating it to iOS and macOS projects is hard. This can be done as a separate PR once the main changes land across all 3 repos -// import Foundation -// import Persistence -// import os.log - -// Cohort identifier used exclusively to distinguish systemic crashes, only after the user opts in to send them. -// Its purpose is strictly limited to improving the reliability of crash reporting and is never used elsewhere. -public class CRCIDManager { - static let crcidKey = "CRCIDManager.crcidKey" - var store: KeyValueStoring - - public init(store: KeyValueStoring = UserDefaults()) { - self.store = store - } - - public func handleCrashSenderResult(result: Result, response: HTTPURLResponse?) { - switch result { - case .success: - Logger.general.debug("Crash Collection - Sending Crash Report: succeeded") - if let receivedCRCID = response?.allHeaderFields[CrashReportSender.httpHeaderCRCID] as? String { - if crcid != receivedCRCID { - Logger.general.debug("Crash Collection - Received new value for CRCID: \(receivedCRCID), setting local crcid value") - crcid = receivedCRCID - } else { - Logger.general.debug("Crash Collection - Received matching value for CRCID: \(receivedCRCID), no update necessary") - } - } else { - Logger.general.debug("Crash Collection - No value for CRCID header: \(CRCIDManager.crcidKey), clearing local crcid value if present") - crcid = "" - } - case .failure(let failure): - Logger.general.debug("Crash Collection - Sending Crash Report: failed (\(failure))") - } - } - - public var crcid: String? { - get { - return self.store.object(forKey: CRCIDManager.crcidKey) as? String - } - - set { - store.set(newValue, forKey: CRCIDManager.crcidKey) - } - } -}