From 49f4e946e52eb49977cd6ef674b84e2efc31e684 Mon Sep 17 00:00:00 2001 From: Jacob Sikorski Date: Thu, 5 Oct 2023 14:52:59 -0600 Subject: [PATCH 1/3] Fix unwanted removal of content blockers upon launch --- .../Browser/Helpers/LaunchHelper.swift | 29 +++++++++++++++---- 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/Sources/Brave/Frontend/Browser/Helpers/LaunchHelper.swift b/Sources/Brave/Frontend/Browser/Helpers/LaunchHelper.swift index 22b92af0c19..254477c3178 100644 --- a/Sources/Brave/Frontend/Browser/Helpers/LaunchHelper.swift +++ b/Sources/Brave/Frontend/Browser/Helpers/LaunchHelper.swift @@ -109,13 +109,9 @@ public actor LaunchHelper { /// Get all possible types of blocklist types available in this app, this includes actual and potential types /// This is used to delete old filter lists so that we clean up old stuff @MainActor private func getAllValidBlocklistTypes() -> Set { - return FilterListStorage.shared.filterLists + return FilterListStorage.shared // All filter lists blocklist types - .reduce(Set()) { partialResult, filterList in - return partialResult.union([ - .filterList(componentId: filterList.entry.componentId, isAlwaysAggressive: filterList.isAlwaysAggressive) - ]) - } + .validBlocklistTypes // All generic types .union( ContentBlockerManager.GenericBlocklistType.allCases.map { .generic($0) } @@ -127,6 +123,27 @@ public actor LaunchHelper { } } +private extension FilterListStorage { + /// Return all the blocklist types that are valid for filter lists. + var validBlocklistTypes: Set { + if filterLists.isEmpty { + /// If we don't have filter lists yet loaded, use the settings + return allFilterListSettings.reduce(Set()) { partialResult, setting in + guard let componentId = setting.componentId else { return partialResult } + return partialResult.union([ + .filterList(componentId: componentId, isAlwaysAggressive: setting.isAlwaysAggressive) + ]) + } + } else { + /// If we do have filter lists yet loaded, use them as they are always the most up to date and accurate + return filterLists.reduce(Set()) { partialResult, filterList in + return partialResult.union([ + .filterList(componentId: filterList.entry.componentId, isAlwaysAggressive: filterList.isAlwaysAggressive) + ]) + } + } + } +} private extension ShieldLevel { /// Return a list of first launch content blocker modes that MUST be precompiled during launch var firstLaunchBlockingModes: Set { From 8367076ecc44a66e3ee1a5cce5954cdc9e9bbb3c Mon Sep 17 00:00:00 2001 From: Jacob Sikorski Date: Wed, 11 Oct 2023 09:16:02 -0600 Subject: [PATCH 2/3] Use mutable reduce --- .../Frontend/Browser/Helpers/LaunchHelper.swift | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/Sources/Brave/Frontend/Browser/Helpers/LaunchHelper.swift b/Sources/Brave/Frontend/Browser/Helpers/LaunchHelper.swift index 254477c3178..aa6c0338863 100644 --- a/Sources/Brave/Frontend/Browser/Helpers/LaunchHelper.swift +++ b/Sources/Brave/Frontend/Browser/Helpers/LaunchHelper.swift @@ -127,17 +127,18 @@ private extension FilterListStorage { /// Return all the blocklist types that are valid for filter lists. var validBlocklistTypes: Set { if filterLists.isEmpty { - /// If we don't have filter lists yet loaded, use the settings - return allFilterListSettings.reduce(Set()) { partialResult, setting in - guard let componentId = setting.componentId else { return partialResult } - return partialResult.union([ + // If we don't have filter lists yet loaded, use the settings + return allFilterListSettings.reduce(into: Set(), { partialResult, setting in + guard let componentId = setting.componentId else { return } + + partialResult.formUnion([ .filterList(componentId: componentId, isAlwaysAggressive: setting.isAlwaysAggressive) ]) - } + }) } else { - /// If we do have filter lists yet loaded, use them as they are always the most up to date and accurate - return filterLists.reduce(Set()) { partialResult, filterList in - return partialResult.union([ + // If we do have filter lists yet loaded, use them as they are always the most up to date and accurate + return filterLists.reduce(into: Set()) { partialResult, filterList in + partialResult.formUnion([ .filterList(componentId: filterList.entry.componentId, isAlwaysAggressive: filterList.isAlwaysAggressive) ]) } From 2b7985edad56159acbd3df7567beba700f2d10a1 Mon Sep 17 00:00:00 2001 From: Jacob Sikorski Date: Wed, 11 Oct 2023 10:07:33 -0600 Subject: [PATCH 3/3] Change reduce to a map --- .../Browser/Helpers/LaunchHelper.swift | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/Sources/Brave/Frontend/Browser/Helpers/LaunchHelper.swift b/Sources/Brave/Frontend/Browser/Helpers/LaunchHelper.swift index aa6c0338863..d1deaa96a21 100644 --- a/Sources/Brave/Frontend/Browser/Helpers/LaunchHelper.swift +++ b/Sources/Brave/Frontend/Browser/Helpers/LaunchHelper.swift @@ -128,20 +128,21 @@ private extension FilterListStorage { var validBlocklistTypes: Set { if filterLists.isEmpty { // If we don't have filter lists yet loaded, use the settings - return allFilterListSettings.reduce(into: Set(), { partialResult, setting in - guard let componentId = setting.componentId else { return } - - partialResult.formUnion([ - .filterList(componentId: componentId, isAlwaysAggressive: setting.isAlwaysAggressive) - ]) + return Set(allFilterListSettings.compactMap { setting in + guard let componentId = setting.componentId else { return nil } + return .filterList( + componentId: componentId, + isAlwaysAggressive: setting.isAlwaysAggressive + ) }) } else { // If we do have filter lists yet loaded, use them as they are always the most up to date and accurate - return filterLists.reduce(into: Set()) { partialResult, filterList in - partialResult.formUnion([ - .filterList(componentId: filterList.entry.componentId, isAlwaysAggressive: filterList.isAlwaysAggressive) - ]) - } + return Set(filterLists.map { filterList in + return .filterList( + componentId: filterList.entry.componentId, + isAlwaysAggressive: filterList.isAlwaysAggressive + ) + }) } } }