Skip to content

Commit

Permalink
cleanup FB protection state management
Browse files Browse the repository at this point in the history
  • Loading branch information
ladamski committed Mar 7, 2024
1 parent 5a5712a commit f8b5ff8
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions DuckDuckGo/Tab/TabExtensions/FBProtectionTabExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,25 @@ final class FBProtectionTabExtension {

extension FBProtectionTabExtension {

private func toggleFBProtection(for url: URL) {
private func enableFBProtection(for url: URL) {
// Enable/disable FBProtection only after UserScripts are installed (awaitContentBlockingAssetsInstalled)
let privacyConfiguration = privacyConfigurationManager.privacyConfig

let featureEnabled = privacyConfiguration.isFeature(.clickToPlay, enabledForDomain: url.host)
setFBProtection(enabled: featureEnabled)
setFBProtection(enable: featureEnabled)
}

@discardableResult
private func setFBProtection(enabled: Bool) -> Bool {
private func setFBProtection(enable: Bool) -> Bool {
if #unavailable(OSX 11) { // disable CTL for Catalina and earlier
return false
}
guard self.fbBlockingEnabled != enabled else { return false }
guard self.fbBlockingEnabled != enable else { return false }
guard let userContentController else {
assertionFailure("Missing UserContentController")
return false
}
if enabled {
if enable {
do {
try userContentController.enableGlobalContentRuleList(withIdentifier: ContentBlockerRulesLists.Constants.clickToLoadRulesListName)
} catch {
Expand All @@ -87,7 +87,7 @@ extension FBProtectionTabExtension {
return false
}
}
self.fbBlockingEnabled = enabled
self.fbBlockingEnabled = enable

return true
}
Expand All @@ -101,18 +101,22 @@ extension FBProtectionTabExtension: ClickToLoadUserScriptDelegate {
return true
}

if setFBProtection(enabled: false) {
if setFBProtection(enable: false) {
return true
} else {
return false
}
}

}

extension FBProtectionTabExtension: NavigationResponder {

func decidePolicy(for navigationAction: NavigationAction, preferences: inout NavigationPreferences) async -> NavigationActionPolicy? {
toggleFBProtection(for: navigationAction.url)
if navigationAction.navigationType == NavigationType.other && navigationAction.isUserInitiated == false {
return .next
}
enableFBProtection(for: navigationAction.url)
return .next
}

Expand Down

0 comments on commit f8b5ff8

Please sign in to comment.