From f7ca8e80c2b018820219336269be998a686ee0e8 Mon Sep 17 00:00:00 2001 From: Jacob Sikorski Date: Wed, 22 Mar 2023 16:59:34 +0100 Subject: [PATCH] Add better ad-block logs --- .../Browser/Helpers/LaunchHelper.swift | 9 +++++ .../Frontend/Browser/UserScriptManager.swift | 35 ++++++++++++++++++ .../WebFilters/AdBlockEngineManager.swift | 36 +++++++++---------- 3 files changed, 61 insertions(+), 19 deletions(-) diff --git a/Sources/Brave/Frontend/Browser/Helpers/LaunchHelper.swift b/Sources/Brave/Frontend/Browser/Helpers/LaunchHelper.swift index 987eb337eb6..02e4f6df029 100644 --- a/Sources/Brave/Frontend/Browser/Helpers/LaunchHelper.swift +++ b/Sources/Brave/Frontend/Browser/Helpers/LaunchHelper.swift @@ -27,6 +27,10 @@ public actor LaunchHelper { // Otherwise prepare the services and await the task let task = Task { + #if DEBUG + let startTime = CFAbsoluteTimeGetCurrent() + #endif + // Load cached data // This is done first because compileResources and loadCachedRuleLists need their results async let loadBundledResources: Void = ContentBlockerManager.shared.loadBundledResources() @@ -39,6 +43,11 @@ public actor LaunchHelper { async let cachedRuleListLoad: Void = ContentBlockerManager.shared.loadCachedRuleLists() _ = await (compiledResourcesCompile, cachedRuleListLoad) + #if DEBUG + let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime + ContentBlockerManager.log.debug("Adblock loaded: \(timeElapsed)s") + #endif + // This one is non-blocking performPostLoadTasks(adBlockService: adBlockService) markAdBlockReady() diff --git a/Sources/Brave/Frontend/Browser/UserScriptManager.swift b/Sources/Brave/Frontend/Browser/UserScriptManager.swift index 9d6ccb31e77..8f7a5ac8d7d 100644 --- a/Sources/Brave/Frontend/Browser/UserScriptManager.swift +++ b/Sources/Brave/Frontend/Browser/UserScriptManager.swift @@ -192,6 +192,16 @@ class UserScriptManager { return } + #if DEBUG + ContentBlockerManager.log.debug("Loaded \(userScripts.count + customScripts.count) scripts:") + userScripts.sorted(by: { $0.rawValue < $1.rawValue}).forEach { scriptType in + ContentBlockerManager.log.debug(" \(scriptType.debugDescription)") + } + customScripts.sorted(by: { $0.order < $1.order}).forEach { scriptType in + ContentBlockerManager.log.debug(" #\(scriptType.order) \(scriptType.debugDescription)") + } + #endif + loadScripts(into: tab, scripts: userScripts) webView.configuration.userContentController.do { scriptController in @@ -309,3 +319,28 @@ class UserScriptManager { } } } + +#if DEBUG +extension UserScriptType: CustomDebugStringConvertible { + var debugDescription: String { + switch self { + case .domainUserScript(let domainUserScript): + return "domainUserScript(\(domainUserScript.associatedDomains.joined(separator: ", ")))" + case .engineScript(let configuration): + return "engineScript(\(configuration.frameURL))" + case .farblingProtection(let etld): + return "farblingProtection(\(etld))" + case .nacl: + return "nacl" + case .siteStateListener: + return "siteStateListener" + } + } +} + +extension UserScriptManager.ScriptType: CustomDebugStringConvertible { + var debugDescription: String { + return rawValue + } +} +#endif diff --git a/Sources/Brave/WebFilters/AdBlockEngineManager.swift b/Sources/Brave/WebFilters/AdBlockEngineManager.swift index 002fd80cf20..268b20cbccf 100644 --- a/Sources/Brave/WebFilters/AdBlockEngineManager.swift +++ b/Sources/Brave/WebFilters/AdBlockEngineManager.swift @@ -184,41 +184,39 @@ public actor AdBlockEngineManager: Sendable { } #if DEBUG -extension AdBlockEngineManager { +private extension AdBlockEngineManager { /// A method that logs info on the given resources - fileprivate func debug(compiledResults: [ResourceWithVersion: Result]) { - let resourcesString = compiledResults.sorted(by: { $0.key.order < $1.key.order }) - .map { (resourceWithVersion, compileResult) -> String in + func debug(compiledResults: [ResourceWithVersion: Result]) { + log.debug("Loaded \(compiledResults.count) (total) engine resources:") + + compiledResults.sorted(by: { $0.key.order < $1.key.order }) + .forEach { (resourceWithVersion, compileResult) in let resultString: String switch compileResult { case .success: - resultString = "success" + resultString = "✔︎" case .failure(let error): - resultString = error.localizedDescription + resultString = "\(error)" } let sourceDebugString = [ - resourceWithVersion.debugDescription, - "result: \(resultString)", - ].joined(separator: ", ") + "", resourceWithVersion.debugDescription, + "\(resultString)", + ].joined(separator: " ") - return ["{", sourceDebugString, "}"].joined() - }.joined(separator: ", ") - - log.debug("Loaded \(self.enabledResources.count, privacy: .public) (total) engine resources: \(resourcesString, privacy: .public)") + log.debug("\(sourceDebugString)") + } } } extension AdBlockEngineManager.ResourceWithVersion: CustomDebugStringConvertible { public var debugDescription: String { return [ - "order: \(order)", - "fileName: \(fileURL.lastPathComponent)", - "source: \(resource.source.debugDescription)", - "version: \(version ?? "nil")", - "type: \(resource.type.debugDescription)" - ].joined(separator: ", ") + "#\(order)", + "\(resource.source.debugDescription).\(resource.type.debugDescription)", + "v\(version ?? "nil")", + ].joined(separator: " ") } }