diff --git a/App/iOS/Delegates/AppDelegate.swift b/App/iOS/Delegates/AppDelegate.swift index b86bed64ed2..7c7310b82a5 100644 --- a/App/iOS/Delegates/AppDelegate.swift +++ b/App/iOS/Delegates/AppDelegate.swift @@ -107,6 +107,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate { } SystemUtils.onFirstRun() + + // Clean Logger for Secure content state + DebugLogger.cleanLogger(for: .secureState) + return true } diff --git a/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+ShareActivity.swift b/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+ShareActivity.swift index 256fc0c5618..b476e496187 100644 --- a/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+ShareActivity.swift +++ b/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+ShareActivity.swift @@ -268,7 +268,9 @@ extension BrowserViewController { } // Display Certificate Activity - if let secureState = tabManager.selectedTab?.secureContentState, secureState != .missingSSL && secureState != .unknown { + if let selectedTab = tabManager.selectedTab, selectedTab.secureContentState != .missingSSL && selectedTab.secureContentState != .unknown { + logSecureContentState(tab: selectedTab, details: "Display Certificate Activity Settings") + activities.append( BasicMenuActivity( title: Strings.displayCertificate, diff --git a/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+WKNavigationDelegate.swift b/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+WKNavigationDelegate.swift index 0a58c67f48e..30507b3dd6b 100644 --- a/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+WKNavigationDelegate.swift +++ b/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+WKNavigationDelegate.swift @@ -73,6 +73,8 @@ extension BrowserViewController: WKNavigationDelegate { selectedTab.sslPinningError = nil selectedTab.sslPinningTrust = nil selectedTab.secureContentState = .unknown + logSecureContentState(tab: selectedTab, details: "DidStartProvisionalNavigation - Reset secure content state to unknown until page can be evaluated") + updateToolbarSecureContentState(.unknown) } } @@ -715,6 +717,8 @@ extension BrowserViewController: WKNavigationDelegate { // However, WebKit does NOT trigger the `serverTrust` observer when the URL changes, but the trust has not. // WebKit also does NOT trigger the `serverTrust` observer when the page is actually insecure (non-https). // So manually trigger it with the current trust. + logSecureContentState(tab: tab, details: "ObserveValue trigger in didCommit") + observeValue(forKeyPath: KVOConstants.serverTrust.keyPath, of: webView, change: [.newKey: webView.serverTrust as Any, .kindKey: 1], @@ -823,6 +827,8 @@ extension BrowserViewController: WKNavigationDelegate { // Also, when Chromium cert validation passes, BUT Apple cert validation fails, the request is cancelled automatically by WebKit // In such a case, the webView.serverTrust is `nil`. The only time we have a valid trust is when we received the challenge // so we need to update the URL-Bar to show that serverTrust when WebKit's is nil. + logSecureContentState(tab: tab, details: "ObserveValue trigger in didFailProvisionalNavigation") + observeValue(forKeyPath: KVOConstants.serverTrust.keyPath, of: webView, change: [.newKey: webView.serverTrust ?? tab.sslPinningTrust as Any, .kindKey: 1], diff --git a/Sources/Brave/Frontend/Browser/BrowserViewController/BrowserViewController.swift b/Sources/Brave/Frontend/Browser/BrowserViewController/BrowserViewController.swift index 4c9f461078b..eccb6af36a6 100644 --- a/Sources/Brave/Frontend/Browser/BrowserViewController/BrowserViewController.swift +++ b/Sources/Brave/Frontend/Browser/BrowserViewController/BrowserViewController.swift @@ -1834,6 +1834,7 @@ public class BrowserViewController: UIViewController { } tab.secureContentState = .mixedContent + logSecureContentState(tab: tab, path: path) } if let url = tab.webView?.url, @@ -1844,18 +1845,22 @@ public class BrowserViewController: UIViewController { if ErrorPageHelper.certificateError(for: url) != 0 { // Cert validation takes precedence over all other errors tab.secureContentState = .invalidCert + logSecureContentState(tab: tab, path: path, details: "Cert validation takes precedence over all other errors") } else if NetworkErrorPageHandler.isNetworkError(errorCode: ErrorPageHelper.errorCode(for: url)) { // Network error takes precedence over missing cert // Because we cannot determine if a cert is missing yet, if we cannot connect to the server // Our network interstitial page shows tab.secureContentState = .localhost + logSecureContentState(tab: tab, path: path, details: "Network error takes precedence over missing cert") } else { // Since it's not a cert error explicitly, and it's not a network error, and the cert is missing (no serverTrust), // then we display .missingSSL tab.secureContentState = .missingSSL + logSecureContentState(tab: tab, path: path, details: "Certificate is missing (no serverTrust)") } } else if url.isReaderModeURL || InternalURL.isValid(url: url) { tab.secureContentState = .localhost + logSecureContentState(tab: tab, path: path, details: "Reader Mode or Internal URL") } } @@ -1868,7 +1873,8 @@ public class BrowserViewController: UIViewController { } tab.secureContentState = .unknown - + logSecureContentState(tab: tab, path: path) + guard let serverTrust = tab.webView?.serverTrust else { if let url = tab.webView?.url ?? tab.url { if InternalURL.isValid(url: url), @@ -1876,6 +1882,8 @@ public class BrowserViewController: UIViewController { (internalUrl.isAboutURL || internalUrl.isAboutHomeURL) { tab.secureContentState = .localhost + logSecureContentState(tab: tab, path: path, details: "Internal URL aboutURL or is aboutHomeURL") + if tabManager.selectedTab === tab { updateToolbarSecureContentState(.localhost) } @@ -1889,15 +1897,20 @@ public class BrowserViewController: UIViewController { if ErrorPageHelper.certificateError(for: url) != 0 { // Cert validation takes precedence over all other errors tab.secureContentState = .invalidCert + + logSecureContentState(tab: tab, path: path, details: "Cert validation takes precedence over all other errors") } else if NetworkErrorPageHandler.isNetworkError(errorCode: ErrorPageHelper.errorCode(for: url)) { // Network error takes precedence over missing cert // Because we cannot determine if a cert is missing yet, if we cannot connect to the server // Our network interstitial page shows tab.secureContentState = .localhost + + logSecureContentState(tab: tab, path: path, details: "Network error takes precedence over missing cert") } else { // Since it's not a cert error explicitly, and it's not a network error, and the cert is missing (no serverTrust), // then we display .missingSSL tab.secureContentState = .missingSSL + logSecureContentState(tab: tab, path: path, details: "Certificate is missing (no serverTrust)") } if tabManager.selectedTab === tab { @@ -1908,6 +1921,8 @@ public class BrowserViewController: UIViewController { if url.isReaderModeURL || InternalURL.isValid(url: url) { tab.secureContentState = .localhost + logSecureContentState(tab: tab, path: path, details: "Reader Mode or Internal URL") + if tabManager.selectedTab === tab { updateToolbarSecureContentState(.localhost) } @@ -1916,9 +1931,13 @@ public class BrowserViewController: UIViewController { // All our checks failed, we show the page as insecure tab.secureContentState = .missingSSL + + logSecureContentState(tab: tab, path: path, details: "All our checks failed, we show the page as insecure") } else { // When there is no URL, it's likely a new tab. tab.secureContentState = .localhost + + logSecureContentState(tab: tab, path: path, details: "When there is no URL, it's likely a new tab") } if tabManager.selectedTab === tab { @@ -1930,6 +1949,8 @@ public class BrowserViewController: UIViewController { guard let scheme = tab.webView?.url?.scheme, let host = tab.webView?.url?.host else { tab.secureContentState = .unknown + logSecureContentState(tab: tab, path: path, details: "No webview URL host scheme)") + self.updateURLBar() return } @@ -1950,16 +1971,24 @@ public class BrowserViewController: UIViewController { // Cert is valid! if result == 0 { tab.secureContentState = .secure + + logSecureContentState(tab: tab, path: path, details: "Cert is valid!") } else if result == Int32.min { // Cert is valid but should be validated by the system // Let the system handle it and we'll show an error if the system cannot validate it try await BraveCertificateUtils.evaluateTrust(serverTrust, for: host) tab.secureContentState = .secure + + logSecureContentState(tab: tab, path: path, details: "Cert is valid but should be validated by the system") } else { tab.secureContentState = .invalidCert + + logSecureContentState(tab: tab, path: path, details: "Invalid Cert") } } catch { tab.secureContentState = .invalidCert + + logSecureContentState(tab: tab, path: path, details: "Verify Trust Error") } self.updateURLBar() @@ -1971,6 +2000,24 @@ public class BrowserViewController: UIViewController { } } + func logSecureContentState(tab: Tab, path: KVOConstants? = nil, details: String? = nil) { + var text = """ + Tab URL: \(tab.url?.absoluteString ?? "Empty Tab URL") + Tab VebView URL: \(tab.webView?.url?.absoluteString ?? "Empty Webview URL") + Secure State: \(tab.secureContentState.rawValue) + """ + + if let keyPath = path?.keyPath { + text.append("\n Value Observed: \(keyPath)\n") + } + + if let extraDetails = details { + text.append("\n Extra Details: \(extraDetails)\n") + } + + DebugLogger.log(for: .secureState, text: text) + } + func updateForwardStatusIfNeeded(webView: WKWebView) { if let forwardListItem = webView.backForwardList.forwardList.first, forwardListItem.url.isReaderModeURL { navigationToolbar.updateForwardStatus(false) diff --git a/Sources/Brave/Frontend/Browser/Tab.swift b/Sources/Brave/Frontend/Browser/Tab.swift index 23e0e385035..bbea8623061 100644 --- a/Sources/Brave/Frontend/Browser/Tab.swift +++ b/Sources/Brave/Frontend/Browser/Tab.swift @@ -57,14 +57,14 @@ protocol URLChangeDelegate { func tab(_ tab: Tab, urlDidChangeTo url: URL) } -enum TabSecureContentState { - case unknown - case localhost - case secure - case invalidCert - case missingSSL - case mixedContent - +enum TabSecureContentState: String { + case unknown = "Unknown" + case localhost = "Localhost" + case secure = "Secure" + case invalidCert = "InvalidCertificate" + case missingSSL = "SSL Certificate Missing" + case mixedContent = "Mixed Content" + var shouldDisplayWarning: Bool { switch self { case .unknown, .invalidCert, .missingSSL, .mixedContent: diff --git a/Sources/Brave/Frontend/Settings/SettingsViewController.swift b/Sources/Brave/Frontend/Settings/SettingsViewController.swift index 716990f832a..b953f50ea34 100644 --- a/Sources/Brave/Frontend/Settings/SettingsViewController.swift +++ b/Sources/Brave/Frontend/Settings/SettingsViewController.swift @@ -809,10 +809,15 @@ class SettingsViewController: TableViewController { let vc = AdblockDebugMenuTableViewController(style: .grouped) self.navigationController?.pushViewController(vc, animated: true) }, accessory: .disclosureIndicator, cellClass: MultilineValue1Cell.self), + Row( + text: "Secure Content State Debug", + selection: { [unowned self] in + self.navigationController?.pushViewController(DebugLogViewController(type: .secureState), animated: true) + }, accessory: .disclosureIndicator, cellClass: MultilineValue1Cell.self), Row( text: "View URP Logs", selection: { [unowned self] in - self.navigationController?.pushViewController(UrpLogsViewController(), animated: true) + self.navigationController?.pushViewController(DebugLogViewController(type: .urp), animated: true) }, accessory: .disclosureIndicator, cellClass: MultilineValue1Cell.self), Row(text: "URP Code: \(UserReferralProgram.getReferralCode() ?? "--")"), Row( diff --git a/Sources/BraveShared/DebugLogger.swift b/Sources/BraveShared/DebugLogger.swift new file mode 100644 index 00000000000..da4e42190a8 --- /dev/null +++ b/Sources/BraveShared/DebugLogger.swift @@ -0,0 +1,111 @@ +// Copyright 2024 The Brave Authors. All rights reserved. +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +import UIKit + +public enum LoggerType { + case urp, secureState + + var prefsKey: String { + switch self { + case .urp: + return "urpLogs" + case .secureState: + return "secureStateLogs" + } + } +} + +public struct DebugLogger { + public static func log(for type: LoggerType, text: String) { + var logs = UserDefaults.standard.string(forKey: type.prefsKey) ?? "" + + let date = Date() + let calendar = Calendar.current + let components = calendar.dateComponents([.year, .month, .day, .hour, .minute], from: date) + + guard let year = components.year, let month = components.month, let day = components.day, let hour = components.hour, let minute = components.minute else { + return + } + let time = "\(year)-\(month)-\(day) \(hour):\(minute)" + + switch type { + case .secureState: + logs.append("------------------------------------\n\n") + logs.append(" [\(time)]\n\n \(text)\n") + case .urp: + logs.append("[\(time)] \(text)\n") + } + + UserDefaults.standard.set(logs, forKey: type.prefsKey) + } + + public static func cleanLogger(for type: LoggerType) { + UserDefaults.standard.removeObject(forKey: type.prefsKey) + } +} + +public class DebugLogViewController: UIViewController { + var loggerType: LoggerType + + public init(type: LoggerType) { + loggerType = type + + super.init(nibName: nil, bundle: nil) + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + public override func viewDidLoad() { + super.viewDidLoad() + + switch loggerType { + case .urp: + title = "URP Logs" + case .secureState: + title = "Secure Content State" + } + + let rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(shareButtonTapped)) + navigationItem.rightBarButtonItem = rightBarButtonItem + + let textView = UITextView() + textView.translatesAutoresizingMaskIntoConstraints = false + textView.isEditable = false + + view.addSubview(textView) + + NSLayoutConstraint.activate([ + textView.topAnchor.constraint( + equalTo: view.topAnchor, constant: 8), + textView.leadingAnchor.constraint( + equalTo: view.leadingAnchor, constant: 8), + textView.trailingAnchor.constraint( + equalTo: view.trailingAnchor, constant: -8), + textView.bottomAnchor.constraint( + equalTo: view.bottomAnchor, constant: -8) + ]) + + guard let logs = UserDefaults.standard.string(forKey: loggerType.prefsKey) else { return } + let title = "Secure Content Logs\n\n" + + textView.text = title + logs + } + + @objc func shareButtonTapped() { + guard let logs = UserDefaults.standard.string(forKey: loggerType.prefsKey) else { return } + + // Create an activity view controller with the text to share + let activityViewController = UIActivityViewController(activityItems: [logs], applicationActivities: nil) + + // Present the activity view controller + if let popoverController = activityViewController.popoverPresentationController { + popoverController.barButtonItem = navigationItem.rightBarButtonItem + } + present(activityViewController, animated: true, completion: nil) + } +} diff --git a/Sources/Growth/DAU.swift b/Sources/Growth/DAU.swift index 0a40e461c93..17e00fc1c1f 100644 --- a/Sources/Growth/DAU.swift +++ b/Sources/Growth/DAU.swift @@ -5,6 +5,7 @@ import Shared import BraveCore import os.log import Preferences +import BraveShared public class DAU { @@ -92,7 +93,7 @@ public class DAU { @objc private func sendPingToServerInternal() { guard let paramsAndPrefs = paramsAndPrefsSetup(for: Date()) else { Logger.module.debug("dau, no changes detected, no server ping") - UrpLog.log("dau, no changes detected, no server ping") + DebugLogger.log(for: .urp, text: "dau, no changes detected, no server ping") return } @@ -112,8 +113,8 @@ public class DAU { } Logger.module.debug("send ping to server, url: \(pingRequestUrl)") - UrpLog.log("send ping to server, url: \(pingRequestUrl)") - + DebugLogger.log(for: .urp, text: "send ping to server, url: \(pingRequestUrl)") + var request = URLRequest(url: pingRequestUrl) for (key, value) in paramsAndPrefs.headers { request.setValue(value, forHTTPHeaderField: key) @@ -126,7 +127,7 @@ public class DAU { if let e = error { Logger.module.error("status update error: \(e.localizedDescription)") - UrpLog.log("status update error: \(e)") + DebugLogger.log(for: .urp, text: "status update error: \(e)") return } @@ -212,7 +213,7 @@ public class DAU { if let referralCode = UserReferralProgram.getReferralCode() { params.append(URLQueryItem(name: "ref", value: referralCode)) - UrpLog.log("DAU ping with added ref, params: \(params)") + DebugLogger.log(for: .urp, text: "DAU ping with added ref, params: \(params)") } let lastPingTimestamp = [Int((date).timeIntervalSince1970)] diff --git a/Sources/Growth/URP/AdAttributionReportData.swift b/Sources/Growth/URP/AdAttributionReportData.swift index 6eaacdac635..5283b4e3091 100644 --- a/Sources/Growth/URP/AdAttributionReportData.swift +++ b/Sources/Growth/URP/AdAttributionReportData.swift @@ -5,6 +5,7 @@ import os.log import Foundation +import BraveShared public enum SerializationError: Error { case missing(String) @@ -64,14 +65,14 @@ public struct AdAttributionData { // They will indicate if the Apple Searhs Ads is clicked and for which campaign guard let attribution = json["attribution"] as? Bool else { Logger.module.error("Failed to unwrap json to Ad Attribution property.") - UrpLog.log("Failed to unwrap json to Ad Attribution property. \(json)") + DebugLogger.log(for: .urp, text: "Failed to unwrap json to Ad Attribution property. \(json)") throw SerializationError.missing("Attribution Context") } guard let campaignId = json["campaignId"] as? Int else { Logger.module.error("Failed to unwrap json to Campaign Id property.") - UrpLog.log("Failed to unwrap json to Campaign Id property. \(json)") + DebugLogger.log(for: .urp, text: "Failed to unwrap json to Campaign Id property. \(json)") throw SerializationError.missing("Campaign Id") } diff --git a/Sources/Growth/URP/ReferralData.swift b/Sources/Growth/URP/ReferralData.swift index ae8e4332222..58684fe840b 100644 --- a/Sources/Growth/URP/ReferralData.swift +++ b/Sources/Growth/URP/ReferralData.swift @@ -4,6 +4,7 @@ import Foundation import Shared import SwiftyJSON import os.log +import BraveShared struct ReferralData { @@ -25,7 +26,7 @@ struct ReferralData { init?(json: JSON) { guard let downloadId = json["download_id"].string, let code = json["referral_code"].string else { Logger.module.error("Failed to unwrap json to Referral struct.") - UrpLog.log("Failed to unwrap json to Referral struct. \(json)") + DebugLogger.log(for: .urp, text: "Failed to unwrap json to Referral struct. \(json)") return nil } diff --git a/Sources/Growth/URP/UrpLogsViewController.swift b/Sources/Growth/URP/UrpLogsViewController.swift deleted file mode 100644 index ac7ed0e5868..00000000000 --- a/Sources/Growth/URP/UrpLogsViewController.swift +++ /dev/null @@ -1,46 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -import UIKit -import SnapKit - -public struct UrpLog { - static let prefsKey = "urpLogs" - - public static func log(_ text: String) { - var logs = UserDefaults.standard.string(forKey: prefsKey) ?? "" - - let date = Date() - let calendar = Calendar.current - let components = calendar.dateComponents([.year, .month, .day, .hour, .minute], from: date) - - guard let year = components.year, let month = components.month, let day = components.day, let hour = components.hour, let minute = components.minute else { - return - } - - let time = "\(year)-\(month)-\(day) \(hour):\(minute)" - logs.append("[\(time)] \(text)\n") - - UserDefaults.standard.set(logs, forKey: prefsKey) - } -} - -public class UrpLogsViewController: UIViewController { - lazy var logsTextView: UITextView = { - let textView = UITextView() - textView.isEditable = false - return textView - }() - - public override func viewDidLoad() { - super.viewDidLoad() - - view.addSubview(logsTextView) - logsTextView.snp.makeConstraints { make in - make.top.equalTo(self.view.safeAreaLayoutGuide.snp.top).offset(8) - make.left.right.bottom.equalTo(self.view).inset(8) - } - - guard let logs = UserDefaults.standard.string(forKey: UrpLog.prefsKey) else { return } - logsTextView.text = logs - } -} diff --git a/Sources/Growth/URP/UrpService.swift b/Sources/Growth/URP/UrpService.swift index beccac3efb2..2be57df8aa0 100644 --- a/Sources/Growth/URP/UrpService.swift +++ b/Sources/Growth/URP/UrpService.swift @@ -44,7 +44,7 @@ struct UrpService { func referralCodeLookup(refCode: String?, completion: @escaping (ReferralData?, UrpError?) -> Void) { guard var endPoint = URL(string: host) else { completion(nil, .endpointError) - UrpLog.log("Host not a url: \(host)") + DebugLogger.log(for: .urp, text: "Host not a url: \(host)") return } @@ -65,16 +65,16 @@ struct UrpService { Logger.module.debug("Referral code lookup response: \(String(data: data, encoding: .utf8) ?? "nil")") } - UrpLog.log("Referral code lookup response: \(data)") - + DebugLogger.log(for: .urp, text: "Referral code lookup response: \(data)") + let json = JSON(data) let referral = ReferralData(json: json) completion(referral, nil) case .failure(let error): Logger.module.error("Referral code lookup response: \(error.localizedDescription)") - UrpLog.log("Referral code lookup response: \(error.localizedDescription)") - + DebugLogger.log(for: .urp, text: "Referral code lookup response: \(error.localizedDescription)") + completion(nil, .endpointError) } } @@ -94,7 +94,7 @@ struct UrpService { rawData: attributionDataToken, isRetryEnabled: isRetryEnabled, timeout: timeout) - UrpLog.log("Ad Attribution response: \(result)") + DebugLogger.log(for: .urp, text: "Ad Attribution response: \(result)") if let resultData = result as? Data { let jsonResponse = try JSONSerialization.jsonObject(with: resultData, options: []) as? [String: Any] @@ -122,7 +122,7 @@ struct UrpService { do { let (result, _) = try await sessionManager.adGroupsReportApiRequest(endPoint: endPoint) - UrpLog.log("Ad Groups Report response: \(result)") + DebugLogger.log(for: .urp, text: "Ad Groups Report response: \(result)") if let resultData = result as? Data { let adGroupsReportData = try AdGroupReportData(data: resultData, keywordId: keywordId) diff --git a/Sources/Growth/URP/UserReferralProgram.swift b/Sources/Growth/URP/UserReferralProgram.swift index f43a64a5994..1990a5c717f 100644 --- a/Sources/Growth/URP/UserReferralProgram.swift +++ b/Sources/Growth/URP/UserReferralProgram.swift @@ -8,6 +8,7 @@ import Preferences import WebKit import os.log import AdServices +import BraveShared public class UserReferralProgram { @@ -47,20 +48,20 @@ public class UserReferralProgram { let urpService = UrpService(host: host, apiKey: apiKey, adServicesURL: adServicesURLString, adReportsURL: adReportsURLString) - UrpLog.log("URP init, host: \(host)") - + DebugLogger.log(for: .urp, text: "URP init, host: \(host)") + self.service = urpService } /// Looks for referral and returns its landing page if possible. public func referralLookup(refCode: String? = nil, completion: @escaping (_ refCode: String?, _ offerUrl: String?) -> Void) { - UrpLog.log("first run referral lookup") - + DebugLogger.log(for: .urp, text: "first run referral lookup") + let referralBlock: (ReferralData?, UrpError?) -> Void = { [weak self] referral, error in guard let self = self else { return } if error == Growth.UrpError.endpointError { - UrpLog.log("URP look up had endpoint error, will retry on next launch.") + DebugLogger.log(for: .urp, text: "URP look up had endpoint error, will retry on next launch.") self.referralLookupRetry.timer?.invalidate() self.referralLookupRetry.timer = nil @@ -85,14 +86,14 @@ public class UserReferralProgram { Preferences.URP.referralLookupOutstanding.value = false guard let ref = referral else { Logger.module.info("No referral code found") - UrpLog.log("No referral code found") + DebugLogger.log(for: .urp, text: "No referral code found") completion(nil, nil) return } if ref.isExtendedUrp() { completion(ref.referralCode, ref.offerPage) - UrpLog.log("Extended referral code found, opening landing page: \(ref.offerPage ?? "404")") + DebugLogger.log(for: .urp, text: "Extended referral code found, opening landing page: \(ref.offerPage ?? "404")") // We do not want to persist referral data for extended URPs return } @@ -103,7 +104,7 @@ public class UserReferralProgram { self.referralLookupRetry.timer?.invalidate() self.referralLookupRetry.timer = nil - UrpLog.log("Found referral: downloadId: \(ref.downloadId), code: \(ref.referralCode)") + DebugLogger.log(for: .urp, text: "Found referral: downloadId: \(ref.downloadId), code: \(ref.referralCode)") // In case of network errors or getting `isFinalized = false`, we retry the api call. self.initRetryPingConnection(numberOfTimes: 30) @@ -169,13 +170,13 @@ public class UserReferralProgram { public func pingIfEnoughTimePassed() { if !DeviceInfo.hasConnectivity() { - UrpLog.log("No internet connection, not sending update ping.") + DebugLogger.log(for: .urp, text: "No internet connection, not sending update ping.") return } guard let downloadId = Preferences.URP.downloadId.value else { Logger.module.info("Could not retrieve download id model from preferences.") - UrpLog.log("Update ping, no download id found.") + DebugLogger.log(for: .urp, text: "Update ping, no download id found.") return } @@ -188,11 +189,11 @@ public class UserReferralProgram { if todayInSeconds <= checkDate { Logger.module.debug("Not enough time has passed for referral ping.") - UrpLog.log("Not enough time has passed for referral ping.") + DebugLogger.log(for: .urp, text: "Not enough time has passed for referral ping.") return } - UrpLog.log("Update ping") + DebugLogger.log(for: .urp, text: "Update ping") service.checkIfAuthorizedForGrant(with: downloadId) { initialized, error in guard let counter = Preferences.URP.retryCountdown.value else { Logger.module.error("Could not retrieve retry countdown from preferences.") @@ -202,29 +203,29 @@ public class UserReferralProgram { var shouldRemoveData = false if error == .downloadIdNotFound { - UrpLog.log("Download id not found on server.") + DebugLogger.log(for: .urp, text: "Download id not found on server.") shouldRemoveData = true } if initialized == true { - UrpLog.log("Got initialized = true from server.") + DebugLogger.log(for: .urp, text: "Got initialized = true from server.") shouldRemoveData = true } // Last retry attempt if counter <= 1 { - UrpLog.log("Last retry and failed to get data from server.") + DebugLogger.log(for: .urp, text: "Last retry and failed to get data from server.") shouldRemoveData = true } if shouldRemoveData { - UrpLog.log("Removing all referral data from device") - + DebugLogger.log(for: .urp, text: "Removing all referral data from device") + Preferences.URP.downloadId.value = nil Preferences.URP.nextCheckDate.value = nil Preferences.URP.retryCountdown.value = nil } else { - UrpLog.log("Network error or isFinalized returned false, decrementing retry counter and trying again next time.") + DebugLogger.log(for: .urp, text: "Network error or isFinalized returned false, decrementing retry counter and trying again next time.") // Decrement counter, next retry happens on next day Preferences.URP.retryCountdown.value = counter - 1 Preferences.URP.nextCheckDate.value = checkDate + 1.days @@ -238,13 +239,13 @@ public class UserReferralProgram { Date().timeIntervalSince1970 >= referralCodeDeleteDate { Preferences.URP.referralCode.value = nil Preferences.URP.referralCodeDeleteDate.value = nil - UrpLog.log("Enough time has passed, removing referral code data") + DebugLogger.log(for: .urp, text: "Enough time has passed, removing referral code data") return nil } else if let referralCode = Preferences.URP.referralCode.value { // Appending ref code to dau ping if user used installed the app via // user referral program or apple search ad if Preferences.URP.referralCodeDeleteDate.value == nil { - UrpLog.log("Setting new date for deleting referral code.") + DebugLogger.log(for: .urp, text: "Setting new date for deleting referral code.") let timeToDelete = AppConstants.buildChannel.isPublic ? 90.days : 20.minutes Preferences.URP.referralCodeDeleteDate.value = Date().timeIntervalSince1970 + timeToDelete }