Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Add better ad-block logs
Browse files Browse the repository at this point in the history
  • Loading branch information
cuba committed Mar 24, 2023
1 parent f3e4c44 commit f7ca8e8
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 19 deletions.
9 changes: 9 additions & 0 deletions Sources/Brave/Frontend/Browser/Helpers/LaunchHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down
35 changes: 35 additions & 0 deletions Sources/Brave/Frontend/Browser/UserScriptManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
36 changes: 17 additions & 19 deletions Sources/Brave/WebFilters/AdBlockEngineManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Void, Error>]) {
let resourcesString = compiledResults.sorted(by: { $0.key.order < $1.key.order })
.map { (resourceWithVersion, compileResult) -> String in
func debug(compiledResults: [ResourceWithVersion: Result<Void, Error>]) {
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: " ")
}
}

Expand Down

0 comments on commit f7ca8e8

Please sign in to comment.