Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor FXIOS-5607 [v111] Removed Deferred from TPStatsBlocklistChecker #13055

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ extension TabContentBlocker {
components.scheme = "http"
guard let url = components.url else { return }

TPStatsBlocklistChecker.shared.isBlocked(url: url, mainDocumentURL: mainDocumentUrl).uponQueue(.main) { listItem in
TPStatsBlocklistChecker.shared.isBlocked(url: url, mainDocumentURL: mainDocumentUrl) { listItem in
if let listItem = listItem {
self.stats = self.stats.create(matchingBlocklist: listItem, host: url.host ?? "")
}
Expand Down
15 changes: 6 additions & 9 deletions content-blocker-lib-ios/src/TrackingProtectionPageStats.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,34 +35,31 @@ class TPStatsBlocklistChecker {
// Initialized async, is non-nil when ready to be used.
private var blockLists: TPStatsBlocklists?

func isBlocked(url: URL, mainDocumentURL: URL) -> Deferred<BlocklistCategory?> {
let deferred = Deferred<BlocklistCategory?>()

func isBlocked(url: URL, mainDocumentURL: URL, completionHandler: @escaping (BlocklistCategory?) -> Void) {
guard let blockLists = blockLists,
let host = url.host,
!host.isEmpty
else {
// TP Stats init isn't complete yet
deferred.fill(nil)
return deferred
completionHandler(nil)
return
}

guard let domain = url.baseDomain,
let docDomain = mainDocumentURL.baseDomain,
domain != docDomain
else {
deferred.fill(nil)
return deferred
completionHandler(nil)
return
}

// Make a copy on the main thread
let safelistRegex = ContentBlocker.shared.safelistedDomains.domainRegex

DispatchQueue.global().async {
// Return true in the Deferred if the domain could potentially be blocked
deferred.fill(blockLists.urlIsInList(url, mainDocumentURL: mainDocumentURL, safelistedDomains: safelistRegex))
completionHandler(blockLists.urlIsInList(url, mainDocumentURL: mainDocumentURL, safelistedDomains: safelistRegex))
}
return deferred
}

func startup() {
Expand Down