Skip to content

Commit

Permalink
Fix brave#1096: Get currentTabUrl before going before going to backgr…
Browse files Browse the repository at this point in the history
…ound thread.
  • Loading branch information
iccub committed May 22, 2019
1 parent 4e1eccf commit ddef174
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
12 changes: 10 additions & 2 deletions Client/WebFilters/ContentBlocker/TrackingProtectionPageStats.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,17 @@ class TPStatsBlocklistChecker {
return deferred
}

// Getting this domain inside of asyncrhonous clousre below can cause thread locks(#1094)
// Getting this domain and current tab urls before going into asynchronous closure
// to avoid threading problems(#1094, #1096)
assertIsMainThread("Getting enabled blocklists should happen on main thread")
let domainBlockLists = BlocklistName.blocklists(forDomain: domain).on

guard let delegate = UIApplication.shared.delegate as? AppDelegate else {
deferred.fill(nil)
return deferred
}
let currentTabUrl = delegate.browserViewController.tabManager.selectedTab?.url

DispatchQueue.global().async {
let enabledLists = domainBlockLists

Expand All @@ -84,7 +91,8 @@ class TPStatsBlocklistChecker {

let isAdOrTrackerListEnabled = enabledLists.contains(.ad) || enabledLists.contains(.tracker)

if isAdOrTrackerListEnabled && AdBlockStats.shared.shouldBlock(request) {
if isAdOrTrackerListEnabled && AdBlockStats.shared.shouldBlock(request,
currentTabUrl: currentTabUrl) {
deferred.fill(BlocklistName.ad)
return
}
Expand Down
9 changes: 1 addition & 8 deletions Client/WebFilters/ShieldStats/Adblock/AdBlockStats.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,11 @@ class AdBlockStats: LocalAdblockResourceProtocol {
adblockStatsResources[currentLocaleCode] = ABPFilterLibWrapper()
}

func shouldBlock(_ request: URLRequest) -> Bool {
func shouldBlock(_ request: URLRequest, currentTabUrl: URL?) -> Bool {
guard let url = request.url else {
return false
}

var currentTabUrl: URL?

DispatchQueue.main.async {
guard let delegate = UIApplication.shared.delegate as? AppDelegate else { return }
currentTabUrl = delegate.browserViewController.tabManager.selectedTab?.url
}

// Do not block main frame urls
// e.g. user clicked on an ad intentionally (adblock could block redirect to requested site)
if url == currentTabUrl { return false }
Expand Down

0 comments on commit ddef174

Please # to comment.