Skip to content

Commit

Permalink
Merge branch 'main' into mariusz/ntp-favorites-autocomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
dus7 committed Sep 27, 2024
2 parents 31505ed + b60b38b commit ea9d374
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,13 @@ open class ContentBlockerRulesUserScript: NSObject, UserScript {
!isFirstParty(requestURL: trackerUrlString, websiteURL: pageUrlStr) else { return }

let entity = currentTrackerData.findEntity(forHost: requestETLDp1) ?? Entity(displayName: requestETLDp1, domains: nil, prevalence: nil)
let isAffiliated = resolver.isPageAffiliatedWithTrackerEntity(pageUrlString: pageUrlStr, trackerEntity: entity)

let thirdPartyRequest = DetectedRequest(url: trackerUrlString,
eTLDplus1: requestETLDp1,
knownTracker: nil,
entity: entity,
state: .allowed(reason: .otherThirdPartyRequest),
state: .allowed(reason: isAffiliated ? .ownedByFirstParty : .otherThirdPartyRequest),
pageUrl: pageUrlStr)
delegate.contentBlockerRulesUserScript(self, detectedThirdPartyRequest: thirdPartyRequest)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public class TrackerResolver {
pageUrl: pageUrlString)
}

private func isPageAffiliatedWithTrackerEntity(pageUrlString: String, trackerEntity: Entity) -> Bool {
public func isPageAffiliatedWithTrackerEntity(pageUrlString: String, trackerEntity: Entity) -> Bool {
guard let pageHost = URL(string: pageUrlString)?.host,
let pageEntity = tds.findEntity(forHost: pageHost)
else { return false }
Expand Down
3 changes: 2 additions & 1 deletion Sources/Suggestions/SuggestionProcessing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ final class SuggestionProcessing {
}

// Get best matches from history and bookmarks
let allLocalSuggestions = localSuggestions(from: history, bookmarks: bookmarks, internalPages: internalPages, openTabs: openTabs, query: query)
let allLocalSuggestions = Array(localSuggestions(from: history, bookmarks: bookmarks, internalPages: internalPages, openTabs: openTabs, query: query)
.prefix(100)) // temporary optimsiation

// Combine HaB and domains into navigational suggestions and remove duplicates
let navigationalSuggestions = allLocalSuggestions + duckDuckGoDomainSuggestions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,16 @@ class ContentBlockerRulesUserScriptsTests: XCTestCase {
"entities": {
"Fake Tracking Inc": {
"domains": [
"tracker.com"
"tracker.com",
"trackeraffiliated.com"
],
"displayName": "Fake Tracking Inc",
"prevalence": 0.1
}
},
"domains": {
"tracker.com": "Fake Tracking Inc"
"tracker.com": "Fake Tracking Inc",
"trackeraffiliated.com": "Fake Tracking Inc"
}
}
"""
Expand All @@ -79,6 +81,7 @@ class ContentBlockerRulesUserScriptsTests: XCTestCase {
var webView: WKWebView?

let nonTrackerURL = URL(string: "test://nontracker.com/1.png")!
let nonTrackerAffiliatedURL = URL(string: "test://trackeraffiliated.com/1.png")!
let trackerURL = URL(string: "test://tracker.com/1.png")!
let subTrackerURL = URL(string: "test://sub.tracker.com/1.png")!

Expand All @@ -89,7 +92,8 @@ class ContentBlockerRulesUserScriptsTests: XCTestCase {

website = MockWebsite(resources: [.init(type: .image, url: nonTrackerURL),
.init(type: .image, url: trackerURL),
.init(type: .image, url: subTrackerURL)])
.init(type: .image, url: subTrackerURL),
.init(type: .image, url: nonTrackerAffiliatedURL)])
}

private func setupWebViewForUserScripTests(trackerData: TrackerData,
Expand Down Expand Up @@ -186,7 +190,11 @@ class ContentBlockerRulesUserScriptsTests: XCTestCase {
let blockedTrackers = Set(self.userScriptDelegateMock.detectedTrackers.filter { $0.isBlocked }.map { $0.domain })
XCTAssertEqual(expectedTrackers, blockedTrackers)

let expectedRequests: Set<URL> = [websiteURL, self.nonTrackerURL]
let expected3rdParty: Set<String> = ["nontracker.com", "trackeraffiliated.com"]
let detected3rdParty = Set(self.userScriptDelegateMock.detectedThirdPartyRequests.map { $0.domain })
XCTAssertEqual(detected3rdParty, expected3rdParty)

let expectedRequests: Set<URL> = [websiteURL, self.nonTrackerURL, self.nonTrackerAffiliatedURL]
XCTAssertEqual(Set(self.schemeHandler.handledRequests), expectedRequests)
}

Expand Down Expand Up @@ -216,11 +224,19 @@ class ContentBlockerRulesUserScriptsTests: XCTestCase {
let detectedTrackers = Set(self.userScriptDelegateMock.detectedTrackers.map { $0.domain })
XCTAssert(detectedTrackers.isEmpty)

let expected3rdParty: Set<String> = ["nontracker.com"]
let expected3rdParty: Set<String> = ["nontracker.com", "trackeraffiliated.com"]
let detected3rdParty = Set(self.userScriptDelegateMock.detectedThirdPartyRequests.map { $0.domain })
XCTAssertEqual(detected3rdParty, expected3rdParty)

let expectedRequests: Set<URL> = [websiteURL, self.nonTrackerURL, self.trackerURL, self.subTrackerURL]
let expectedOwnedBy1stPartyRequests: Set<String> = ["trackeraffiliated.com"]
let detectedOwnedBy1stPartyRequests = Set(self.userScriptDelegateMock.detectedThirdPartyRequests.filter { $0.state == .allowed(reason: .ownedByFirstParty) }.map { $0.domain })
XCTAssertEqual(detectedOwnedBy1stPartyRequests, expectedOwnedBy1stPartyRequests)

let expectedOther3rdPartyRequests: Set<String> = ["nontracker.com"]
let detectedOther3rdPartyRequests = Set(self.userScriptDelegateMock.detectedThirdPartyRequests.filter { $0.state == .allowed(reason: .otherThirdPartyRequest) }.map { $0.domain })
XCTAssertEqual(detectedOther3rdPartyRequests, expectedOther3rdPartyRequests)

let expectedRequests: Set<URL> = [websiteURL, self.nonTrackerURL, self.nonTrackerAffiliatedURL, self.trackerURL, self.subTrackerURL]
XCTAssertEqual(Set(self.schemeHandler.handledRequests), expectedRequests)
}

Expand All @@ -247,10 +263,11 @@ class ContentBlockerRulesUserScriptsTests: XCTestCase {
let blockedTrackers = Set(self.userScriptDelegateMock.detectedTrackers.filter { $0.isBlocked }.map { $0.domain })
XCTAssertEqual(blockedTrackers, expectedTrackers)

let expected3rdParty: Set<String> = ["trackeraffiliated.com"]
let detected3rdParty = Set(self.userScriptDelegateMock.detectedThirdPartyRequests.map { $0.domain })
XCTAssert(detected3rdParty.isEmpty)
XCTAssertEqual(detected3rdParty, expected3rdParty)

let expectedRequests: Set<URL> = [websiteURL, self.nonTrackerURL]
let expectedRequests: Set<URL> = [websiteURL, self.nonTrackerURL, self.nonTrackerAffiliatedURL]
XCTAssertEqual(Set(self.schemeHandler.handledRequests), expectedRequests)
}

Expand Down Expand Up @@ -280,7 +297,7 @@ class ContentBlockerRulesUserScriptsTests: XCTestCase {
let detectedTrackers = Set(self.userScriptDelegateMock.detectedTrackers.map { $0.domain })
XCTAssertEqual(expectedTrackers, detectedTrackers)

let expectedRequests: Set<URL> = [websiteURL, self.nonTrackerURL, self.trackerURL, self.subTrackerURL]
let expectedRequests: Set<URL> = [websiteURL, self.nonTrackerURL, self.nonTrackerAffiliatedURL, self.trackerURL, self.subTrackerURL]
XCTAssertEqual(Set(self.schemeHandler.handledRequests), expectedRequests)
}

Expand Down Expand Up @@ -312,7 +329,7 @@ class ContentBlockerRulesUserScriptsTests: XCTestCase {
let detectedTrackers = Set(self.userScriptDelegateMock.detectedTrackers.map { $0.domain })
XCTAssertEqual(expectedTrackers, detectedTrackers)

let expectedRequests: Set<URL> = [websiteURL, self.nonTrackerURL, self.trackerURL, self.subTrackerURL]
let expectedRequests: Set<URL> = [websiteURL, self.nonTrackerURL, self.nonTrackerAffiliatedURL, self.trackerURL, self.subTrackerURL]
XCTAssertEqual(Set(self.schemeHandler.handledRequests), expectedRequests)
}

Expand All @@ -339,7 +356,7 @@ class ContentBlockerRulesUserScriptsTests: XCTestCase {
let blockedTrackers = Set(self.userScriptDelegateMock.detectedTrackers.filter { $0.isBlocked }.map { $0.domain })
XCTAssertEqual(expectedTrackers, blockedTrackers)

let expectedRequests: Set<URL> = [websiteURL, self.nonTrackerURL]
let expectedRequests: Set<URL> = [websiteURL, self.nonTrackerURL, self.nonTrackerAffiliatedURL]
XCTAssertEqual(Set(self.schemeHandler.handledRequests), expectedRequests)
}

Expand All @@ -366,7 +383,7 @@ class ContentBlockerRulesUserScriptsTests: XCTestCase {
let blockedTrackers = Set(self.userScriptDelegateMock.detectedTrackers.filter { $0.isBlocked }.map { $0.domain })
XCTAssertEqual(expectedTrackers, blockedTrackers)

let expectedRequests: Set<URL> = [websiteURL, self.nonTrackerURL]
let expectedRequests: Set<URL> = [websiteURL, self.nonTrackerURL, self.nonTrackerAffiliatedURL]
XCTAssertEqual(Set(self.schemeHandler.handledRequests), expectedRequests)
}

Expand Down Expand Up @@ -396,7 +413,7 @@ class ContentBlockerRulesUserScriptsTests: XCTestCase {
let detectedTrackers = Set(self.userScriptDelegateMock.detectedTrackers.map { $0.domain })
XCTAssertEqual(expectedTrackers, detectedTrackers)

let expectedRequests: Set<URL> = [websiteURL, self.nonTrackerURL, self.trackerURL, self.subTrackerURL]
let expectedRequests: Set<URL> = [websiteURL, self.nonTrackerURL, self.nonTrackerAffiliatedURL, self.trackerURL, self.subTrackerURL]
XCTAssertEqual(Set(self.schemeHandler.handledRequests), expectedRequests)
}

Expand Down Expand Up @@ -426,7 +443,7 @@ class ContentBlockerRulesUserScriptsTests: XCTestCase {
let detectedTrackers = Set(self.userScriptDelegateMock.detectedTrackers.map { $0.domain })
XCTAssertEqual(expectedTrackers, detectedTrackers)

let expectedRequests: Set<URL> = [websiteURL, self.nonTrackerURL, self.trackerURL, self.subTrackerURL]
let expectedRequests: Set<URL> = [websiteURL, self.nonTrackerURL, self.nonTrackerAffiliatedURL, self.trackerURL, self.subTrackerURL]
XCTAssertEqual(Set(self.schemeHandler.handledRequests), expectedRequests)
}

Expand All @@ -453,7 +470,7 @@ class ContentBlockerRulesUserScriptsTests: XCTestCase {
let blockedTrackers = Set(self.userScriptDelegateMock.detectedTrackers.filter { $0.isBlocked }.map { $0.domain })
XCTAssertEqual(expectedTrackers, blockedTrackers)

let expectedRequests: Set<URL> = [websiteURL, self.nonTrackerURL]
let expectedRequests: Set<URL> = [websiteURL, self.nonTrackerURL, self.nonTrackerAffiliatedURL]
XCTAssertEqual(Set(self.schemeHandler.handledRequests), expectedRequests)
}

Expand Down Expand Up @@ -483,7 +500,7 @@ class ContentBlockerRulesUserScriptsTests: XCTestCase {
let detectedTrackers = Set(self.userScriptDelegateMock.detectedTrackers.map { $0.domain })
XCTAssertEqual(expectedTrackers, detectedTrackers)

let expectedRequests: Set<URL> = [websiteURL, self.nonTrackerURL, self.trackerURL, self.subTrackerURL]
let expectedRequests: Set<URL> = [websiteURL, self.nonTrackerURL, self.nonTrackerAffiliatedURL, self.trackerURL, self.subTrackerURL]
XCTAssertEqual(Set(self.schemeHandler.handledRequests), expectedRequests)
}

Expand Down Expand Up @@ -513,7 +530,7 @@ class ContentBlockerRulesUserScriptsTests: XCTestCase {
let detectedTrackers = Set(self.userScriptDelegateMock.detectedTrackers.map { $0.domain })
XCTAssertEqual(expectedTrackers, detectedTrackers)

let expectedRequests: Set<URL> = [websiteURL, self.nonTrackerURL, self.trackerURL, self.subTrackerURL]
let expectedRequests: Set<URL> = [websiteURL, self.nonTrackerURL, self.nonTrackerAffiliatedURL, self.trackerURL, self.subTrackerURL]
XCTAssertEqual(Set(self.schemeHandler.handledRequests), expectedRequests)
}

Expand Down

0 comments on commit ea9d374

Please sign in to comment.