diff --git a/Sources/Brave/Frontend/Browser/UserScriptManager.swift b/Sources/Brave/Frontend/Browser/UserScriptManager.swift index ca4c1666195..7192f31f735 100644 --- a/Sources/Brave/Frontend/Browser/UserScriptManager.swift +++ b/Sources/Brave/Frontend/Browser/UserScriptManager.swift @@ -280,16 +280,15 @@ 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 - + let logComponents = [ + userScripts.sorted(by: { $0.rawValue < $1.rawValue}).map { scriptType in + " \(scriptType.debugDescription)" + }.joined(separator: "\n"), + customScripts.sorted(by: { $0.order < $1.order}).map { scriptType in + " #\(scriptType.order) \(scriptType.debugDescription)" + }.joined(separator: "\n") + ] + ContentBlockerManager.log.debug("Loaded \(userScripts.count + customScripts.count) script(s): \n\(logComponents.joined(separator: "\n"))") loadScripts(into: webView, scripts: userScripts) webView.configuration.userContentController.do { scriptController in diff --git a/Sources/Brave/WebFilters/AdblockResourceDownloader.swift b/Sources/Brave/WebFilters/AdblockResourceDownloader.swift index 561c7045f99..83c8f22cdd9 100644 --- a/Sources/Brave/WebFilters/AdblockResourceDownloader.swift +++ b/Sources/Brave/WebFilters/AdblockResourceDownloader.swift @@ -47,7 +47,6 @@ public actor AdblockResourceDownloader: Sendable { // .blockAds is special because it can be replaced by a downloaded file. // Hence we need to first check if it already exists. if await ContentBlockerManager.shared.hasRuleList(for: blocklistType, mode: mode) { - ContentBlockerManager.log.debug("Rule list already compiled for `\(blocklistType.makeIdentifier(for: mode))`") return false } else { return true @@ -154,7 +153,7 @@ public actor AdblockResourceDownloader: Sendable { ) } catch { ContentBlockerManager.log.error( - "Failed to compile downloaded content blocker resource: \(error.localizedDescription)" + "Failed to compile rule lists for `\(blocklistType.debugDescription)`: \(error.localizedDescription)" ) } diff --git a/Sources/Brave/WebFilters/ContentBlocker/ContentBlockerHelper.swift b/Sources/Brave/WebFilters/ContentBlocker/ContentBlockerHelper.swift index 48e4e943378..022f3a6b67b 100644 --- a/Sources/Brave/WebFilters/ContentBlocker/ContentBlockerHelper.swift +++ b/Sources/Brave/WebFilters/ContentBlocker/ContentBlockerHelper.swift @@ -67,29 +67,29 @@ class ContentBlockerHelper { @MainActor func set(ruleLists: Set) { guard ruleLists != setRuleLists else { return } - #if DEBUG - ContentBlockerManager.log.debug("Set rule lists:") - #endif + var addedIds: [String] = [] + var removedIds: [String] = [] // Remove unwanted rule lists for ruleList in setRuleLists.subtracting(ruleLists) { // It's added but we don't want it. So we remove it. tab?.webView?.configuration.userContentController.remove(ruleList) setRuleLists.remove(ruleList) - - #if DEBUG - ContentBlockerManager.log.debug(" - \(ruleList.identifier)") - #endif + removedIds.append(ruleList.identifier) } // Add missing rule lists for ruleList in ruleLists.subtracting(setRuleLists) { tab?.webView?.configuration.userContentController.add(ruleList) setRuleLists.insert(ruleList) - - #if DEBUG - ContentBlockerManager.log.debug(" + \(ruleList.identifier)") - #endif + addedIds.append(ruleList.identifier) } + + let parts = [ + addedIds.sorted(by: { $0 < $1 }).map({ " + \($0)" }).joined(separator: "\n"), + removedIds.sorted(by: { $0 < $1 }).map({ " - \($0)" }).joined(separator: "\n") + ] + + ContentBlockerManager.log.debug("Set rule lists:\n\(parts.joined(separator: "\n"))") } } diff --git a/Sources/Brave/WebFilters/ContentBlocker/ContentBlockerManager.swift b/Sources/Brave/WebFilters/ContentBlocker/ContentBlockerManager.swift index dbd02a78367..98623896cf5 100644 --- a/Sources/Brave/WebFilters/ContentBlocker/ContentBlockerManager.swift +++ b/Sources/Brave/WebFilters/ContentBlocker/ContentBlockerManager.swift @@ -74,7 +74,7 @@ actor ContentBlockerManager { } /// An object representing the type of block list - public enum BlocklistType: Hashable { + public enum BlocklistType: Hashable, CustomDebugStringConvertible { fileprivate static let genericPrifix = "stored-type" fileprivate static let filterListPrefix = "filter-list" fileprivate static let filterListURLPrefix = "filter-list-url" @@ -126,6 +126,10 @@ actor ContentBlockerManager { return [self.identifier, "standard"].joined(separator: "-") } } + + public var debugDescription: String { + return identifier + } } public static var shared = ContentBlockerManager() @@ -176,7 +180,7 @@ actor ContentBlockerManager { let cleanedRuleList: [[String: Any?]] do { - cleanedRuleList = try await process(encodedContentRuleList: encodedContentRuleList, with: options) + cleanedRuleList = try await process(encodedContentRuleList: encodedContentRuleList, for: type, with: options) } catch { for mode in modes { self.cachedRuleLists[type.makeIdentifier(for: mode)] = .failure(error) @@ -193,10 +197,10 @@ actor ContentBlockerManager { do { let ruleList = try await compile(ruleList: moddedRuleList, for: type, mode: mode) self.cachedRuleLists[identifier] = .success(ruleList) - Self.log.debug("Compiled content blockers for `\(identifier)`") + Self.log.debug("Compiled rule list for `\(identifier)`") } catch { self.cachedRuleLists[identifier] = .failure(error) - Self.log.debug("Failed to compile content blockers for `\(identifier)`: \(String(describing: error))") + Self.log.debug("Failed to compile rule list for `\(identifier)`: \(String(describing: error))") foundError = error } } @@ -378,7 +382,9 @@ actor ContentBlockerManager { do { return try await self.ruleList(for: blocklistType, mode: mode) } catch { - Self.log.error("Missing rule list for `\(blocklistType.makeIdentifier(for: mode))`") + // We can't log the error because some rules have empty rules. This is normal + // But on relaunches we try to reload the filter list and this will give us an error. + // Need to find a more graceful way of handling this so error here can be logged properly return nil } })) @@ -406,13 +412,12 @@ actor ContentBlockerManager { } /// Perform operations of the rule list given by the provided options - func process(encodedContentRuleList: String, with options: CompileOptions) async throws -> [[String: Any?]] { + func process(encodedContentRuleList: String, for type: BlocklistType, with options: CompileOptions) async throws -> [[String: Any?]] { var ruleList = try decode(encodedContentRuleList: encodedContentRuleList) if options.isEmpty { return ruleList } #if DEBUG let originalCount = ruleList.count - ContentBlockerManager.log.debug("Cleanining up \(originalCount) rules") #endif if options.contains(.stripContentBlockers) { @@ -425,7 +430,9 @@ actor ContentBlockerManager { #if DEBUG let count = originalCount - ruleList.count - ContentBlockerManager.log.debug("Filtered out \(count) rules") + if count > 0 { + Self.log.debug("Filtered out \(count) rules for `\(type.debugDescription)`") + } #endif return ruleList diff --git a/Sources/Brave/WebFilters/FilterListCustomURLDownloader.swift b/Sources/Brave/WebFilters/FilterListCustomURLDownloader.swift index f96cd767614..b5014d14b7a 100644 --- a/Sources/Brave/WebFilters/FilterListCustomURLDownloader.swift +++ b/Sources/Brave/WebFilters/FilterListCustomURLDownloader.swift @@ -87,26 +87,22 @@ actor FilterListCustomURLDownloader: ObservableObject { /// Handle the download results of a custom filter list. This will process the download by compiling iOS rule lists and adding the rule list to the `AdblockEngineManager`. private func handle(downloadResult: ResourceDownloader.DownloadResult, for filterListCustomURL: FilterListCustomURL) async { let uuid = await filterListCustomURL.setting.uuid + let blocklistType = ContentBlockerManager.BlocklistType.customFilterList(uuid: uuid) // Compile this rule list if we haven't already or if the file has been modified if downloadResult.isModified { do { let filterSet = try String(contentsOf: downloadResult.fileURL, encoding: .utf8) let result = try AdblockEngine.contentBlockerRules(fromFilterSet: filterSet) - let type = ContentBlockerManager.BlocklistType.customFilterList(uuid: uuid) try await ContentBlockerManager.shared.compile( encodedContentRuleList: result.rulesJSON, - for: type, + for: blocklistType, options: .all ) - - ContentBlockerManager.log.debug( - "Loaded custom filter list content blockers: \(String(describing: type))" - ) } catch { ContentBlockerManager.log.error( - "Failed to convert custom filter list to content blockers: \(error.localizedDescription)" + "Failed to compile rule lists for `\(blocklistType.debugDescription)`: \(error.localizedDescription)" ) } } diff --git a/Sources/Brave/WebFilters/FilterListResourceDownloader.swift b/Sources/Brave/WebFilters/FilterListResourceDownloader.swift index e8ec2fcd5d5..a667ad5b1f2 100644 --- a/Sources/Brave/WebFilters/FilterListResourceDownloader.swift +++ b/Sources/Brave/WebFilters/FilterListResourceDownloader.swift @@ -298,13 +298,8 @@ public actor FilterListResourceDownloader { } } catch { ContentBlockerManager.log.error( - "Failed to create content blockers for `\(componentId)` v\(version): \(error)" + "Failed to create content blockers for `\(blocklistType.debugDescription)` v\(version): \(error)" ) - #if DEBUG - ContentBlockerManager.log.debug( - "`\(componentId)`: \(filterListURL.absoluteString)" - ) - #endif } } }