Skip to content

Commit

Permalink
Refactor FXIOS-5607 [v111] Removed Deferred from TPStatsBlocklistChec…
Browse files Browse the repository at this point in the history
…ker (#13055)
  • Loading branch information
TwizzyIndy authored Jan 30, 2023
1 parent 95940a3 commit a71918e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
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

0 comments on commit a71918e

Please sign in to comment.