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

update ClickToLoad to use shared C-S-S #2162

Merged
merged 62 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
01c8a53
swap embedded user script with CSS one
ladamski Feb 6, 2024
cf561f1
fire "displayCLickToLoadPlaceholders" on load completion
ladamski Feb 7, 2024
98cb9c5
disable old CTL user script
ladamski Feb 12, 2024
8a3f6d6
test - trigger CTL on FB tracker match
ladamski Feb 12, 2024
367f031
switch to isolated scope, hook up overlay trigger
ladamski Feb 13, 2024
e19f103
use v6 TDS, updated config
ladamski Mar 2, 2024
ae481dc
move CTL code to subfolder
ladamski Mar 2, 2024
50f1411
update CTL rule management to use main TDS
ladamski Mar 2, 2024
b603ed7
WIP rule splitting for custom actions
ladamski Mar 5, 2024
f971774
cleanup FB protection state management
ladamski Mar 7, 2024
daf57ad
update embedded config and TDS
ladamski Mar 16, 2024
0d2c7a4
remove dead code
ladamski Mar 16, 2024
3a0c7fd
switch back to separate blocking CTL rule set
ladamski Mar 18, 2024
7156d78
switch back to previous CTL rules, part 2
ladamski Mar 20, 2024
fd7287e
combine rules for surrogates user script
ladamski Mar 21, 2024
7025ba1
cleanup surrogate code to remove need for vars
ladamski Apr 5, 2024
d3d0c87
update embeds
ladamski Apr 11, 2024
4879680
update config clickToPlay -> clickToLoad
ladamski Apr 11, 2024
909e53b
burn with fire (dead code)
ladamski Apr 11, 2024
be1a4ea
dead code cleanup part 2
ladamski Apr 15, 2024
12cce92
clarify method naming
ladamski Apr 15, 2024
7ed7200
handle empty ruleset in in surrogate generation (unit tests)
ladamski Apr 16, 2024
b249af6
use fallback TDS for CTL test vs old embedded CTL TDS
ladamski Apr 17, 2024
a5e1287
bump TDS test to v6
ladamski Apr 17, 2024
a93d49d
remove remapping of custom action .blockCtlFB to .block
ladamski Apr 19, 2024
5069df9
remove dead fonts, post-merge cleanup
ladamski Apr 23, 2024
f77d0c7
TEMP disable CTL assertions while testing
ladamski Apr 24, 2024
7c3f672
address @MainActor warnings, remove debug spam
ladamski Apr 24, 2024
1bc59ed
lint nit
ladamski Apr 24, 2024
841de53
try special casing NavigationType.backForward for CTL
ladamski Apr 24, 2024
838d6f8
remove MD5 func
ladamski Apr 30, 2024
abdb4f0
move FB entity string to const in FBProtectionTabExtension
ladamski Apr 30, 2024
1999c4a
update splitter logic per PR review, fix test
ladamski May 1, 2024
3ca418b
Update CTL rule splitting logic with alternate path (#2733)
ladamski May 1, 2024
95a798c
update rule combining logic per last rule splitting update
ladamski May 2, 2024
1856498
remove dead ClickToLoadModel code
ladamski May 21, 2024
4a05050
Update ClickToLoadRulesSplitter.swift
ladamski May 21, 2024
0072d09
code review nits
ladamski May 21, 2024
2a61134
Merge branch 'main' into la/update-ctl
ladamski May 21, 2024
d19a19f
cleanup FIXME
ladamski May 21, 2024
223b434
update to BSK WIP branch
ladamski May 21, 2024
cb42dd6
Update proj file and BSK ref
jaceklyp May 21, 2024
098696e
remove unused var
ladamski May 22, 2024
b01f13e
update CTL action to blockCTLFB
ladamski May 23, 2024
084d24d
add CTL test for rules splitting
ladamski May 23, 2024
2a2cf71
test nits
ladamski May 23, 2024
85cc309
move ClickToLoadRulesSpliter to BSK
ladamski May 31, 2024
3673f70
Update ClickToLoadTDSTests.swift
ladamski May 31, 2024
6ca5394
move ClickToLoadRulesSplitter to BSK
ladamski May 31, 2024
d55102c
review nits
ladamski May 31, 2024
77993dd
Update ClickToLoadTDSTests.swift
ladamski Jun 3, 2024
13af034
test tweaks
ladamski Jun 7, 2024
f5c56fd
Update ClickToLoadTDSTests.swift
ladamski Jun 8, 2024
24566fd
remove forward/back nav exception in FB protections
ladamski Jun 12, 2024
98c095b
revert config URL to prod and update embeds
ladamski Jun 13, 2024
2d04a7c
release BSK
ladamski Jun 14, 2024
0479655
update BSK dep
ladamski Jun 14, 2024
84d76eb
Merge branch 'main' into la/update-ctl
ladamski Jun 14, 2024
ef74977
Restore NetP Package.swift
bwaresiak Jun 14, 2024
388d73e
version bump to 1.93.0
ladamski Jun 14, 2024
a0ab778
Revert "version bump to 1.93.0"
ladamski Jun 14, 2024
5e5feb1
nit
ladamski Jun 14, 2024
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
60 changes: 2 additions & 58 deletions DuckDuckGo.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/duckduckgo/BrowserServicesKit",
"state" : {
"revision" : "ddb0eb213c081a5d6d3f78f4df5dff11bc974218",
"version" : "155.0.0"
"revision" : "e9e239fe5dfeab87dcacbf5a31c8f555623a4ce2",
"version" : "156.0.0"
}
},
{
Expand Down Expand Up @@ -176,8 +176,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/duckduckgo/TrackerRadarKit",
"state" : {
"revision" : "c01e6a59d000356b58ec77053e0a99d538be56a5",
"version" : "2.1.1"
"revision" : "1403e17eeeb8493b92fb9d11eb8c846bb9776581",
"version" : "2.1.2"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion DuckDuckGo/Application/AppConfigurationURLProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ struct AppConfigurationURLProvider: ConfigurationURLProviding {
case .bloomFilterExcludedDomains: return URL(string: "https://staticcdn.duckduckgo.com/https/https-mobile-v2-false-positives.json")!
case .privacyConfiguration: return customPrivacyConfigurationUrl ?? URL(string: "https://staticcdn.duckduckgo.com/trackerblocking/config/v4/macos-config.json")!
case .surrogates: return URL(string: "https://staticcdn.duckduckgo.com/surrogates.txt")!
case .trackerDataSet: return URL(string: "https://staticcdn.duckduckgo.com/trackerblocking/v5/current/macos-tds.json")!
case .trackerDataSet: return URL(string: "https://staticcdn.duckduckgo.com/trackerblocking/v6/current/macos-tds.json")!
// In archived repo, to be refactored shortly (https://staticcdn.duckduckgo.com/useragents/social_ctp_configuration.json)
case .FBConfig: return URL(string: "https://staticcdn.duckduckgo.com/useragents/")!
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import BrowserServicesKit
final class AppPrivacyConfigurationDataProvider: EmbeddedDataProvider {

public struct Constants {
public static let embeddedDataETag = "\"c46f529760ac695b143fb664350ad760\""
public static let embeddedDataSHA = "00383f1d5e5e8b5da067c1527ed88027093422c85f362fcfa95cda12c5870987"
public static let embeddedDataETag = "\"d05f1997ae293869ab8c448e4cbc3e39\""
public static let embeddedDataSHA = "0ea4b7874c38d81d5e24505585d8b941069e7e7893a05a7cea9f1745adc4fa52"
}

var embeddedDataEtag: String {
Expand Down
4 changes: 2 additions & 2 deletions DuckDuckGo/ContentBlocker/AppTrackerDataSetProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import BrowserServicesKit
final class AppTrackerDataSetProvider: EmbeddedDataProvider {

public struct Constants {
public static let embeddedDataETag = "\"ea184137cdaa19ca5de76352215a9e0e\""
public static let embeddedDataSHA = "faa4dfbef4903710374b153c9a87e09b713fc19d64fa0bcfd1fd392fff93af21"
public static let embeddedDataETag = "\"6fa664462bd29b66c1b13eed51072afa\""
public static let embeddedDataSHA = "52d29aa8942e518db24a5adbcca63148aac9c01a8a28c3f2c4bc517cb80b18ff"
}

var embeddedDataEtag: String {
Expand Down
55 changes: 0 additions & 55 deletions DuckDuckGo/ContentBlocker/ClickToLoadModel.swift

This file was deleted.

121 changes: 66 additions & 55 deletions DuckDuckGo/ContentBlocker/ClickToLoadUserScript.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,77 +17,88 @@
//

import WebKit
import Common
import UserScript
import BrowserServicesKit

protocol ClickToLoadUserScriptDelegate: AnyObject {

func clickToLoadUserScriptAllowFB(_ script: UserScript, replyHandler: @escaping (Bool) -> Void)
func clickToLoadUserScriptAllowFB() -> Bool
}

final class ClickToLoadUserScript: NSObject, UserScript, WKScriptMessageHandlerWithReply {
final class ClickToLoadUserScript: NSObject, WKNavigationDelegate, Subfeature {
weak var broker: UserScriptMessageBroker?
weak var webView: WKWebView?

var injectionTime: WKUserScriptInjectionTime { .atDocumentEnd }
var forMainFrameOnly: Bool { false }
var messageNames: [String] { ["getImage", "enableFacebook", "initClickToLoad" ] }
let source: String
weak var delegate: ClickToLoadUserScriptDelegate?

#if DEBUG
var devMode: Bool = true
#else
var devMode: Bool = false
#endif

// this isn't an issue to be set to 'all' because the page
public let messageOriginPolicy: MessageOriginPolicy = .all
public let featureName: String = "clickToLoad"

init(scriptSourceProvider: ScriptSourceProviding) {
self.source = scriptSourceProvider.clickToLoadSource
// MARK: - Subfeature

public func with(broker: UserScriptMessageBroker) {
self.broker = broker
}

weak var delegate: ClickToLoadUserScriptDelegate?
// MARK: - MessageNames

@MainActor
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage,
replyHandler: @escaping (Any?, String?) -> Void) {
if message.name == "initClickToLoad" {
let host = message.body as? String
let controller = userContentController as? UserContentController
let privacyConfigurationManager = controller!.privacyConfigurationManager
let privacyConfiguration = privacyConfigurationManager.privacyConfig

let locallyProtected = privacyConfiguration.isProtected(domain: host)
let featureEnabled = privacyConfiguration.isFeature(.clickToPlay, enabledForDomain: host)
if locallyProtected && featureEnabled {
replyHandler(true, nil)
} else {
replyHandler(false, nil)
}
return
}
if message.name == "enableFacebook" {
guard let delegate = delegate else { return }
delegate.clickToLoadUserScriptAllowFB(self) { _ in
guard let isLogin = message.body as? Bool else {
replyHandler(nil, nil)
return
}

replyHandler(isLogin, nil)
}

return
enum MessageNames: String, CaseIterable {
case getClickToLoadState
case unblockClickToLoadContent
case updateFacebookCTLBreakageFlags
case addDebugFlag
}

public func handler(forMethodNamed methodName: String) -> Handler? {
switch MessageNames(rawValue: methodName) {
case .getClickToLoadState:
return handleGetClickToLoadState
case .unblockClickToLoadContent:
return handleUnblockClickToLoadContent
case .updateFacebookCTLBreakageFlags:
return handleDebugFlagsMock
case .addDebugFlag:
return handleDebugFlagsMock
default:
assertionFailure("ClickToLoadUserScript: Failed to parse User Script message: \(methodName)")
return nil
}
}

var image: String
@MainActor
private func handleGetClickToLoadState(params: Any, message: UserScriptMessage) -> Encodable? {
webView = message.messageWebView
return [
"devMode": devMode,
"youtubePreviewsEnabled": false
]
}

guard let arg = message.body as? String else {
replyHandler(nil, nil)
return
}
if message.name == "getImage" {
image = ClickToLoadModel.getImage[arg]!
} else {
assertionFailure("Uknown message type")
replyHandler(nil, nil)
return
}
replyHandler(image, nil)
@MainActor
private func handleUnblockClickToLoadContent(params: Any, message: UserScriptMessage) -> Encodable? {
guard let delegate = delegate else { return false }

// only worry about CTL FB for now
return delegate.clickToLoadUserScriptAllowFB()
}

func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
assertionFailure("SHOULDN'T BE HERE!")
@MainActor
private func handleDebugFlagsMock(params: Any, message: UserScriptMessage) -> Encodable? {
// breakage flags not supported on Mac yet
return nil
}

@MainActor
public func displayClickToLoadPlaceholders() {
guard let webView else { return }

broker?.push(method: "displayClickToLoadPlaceholders", params: ["ruleAction": ["block"]], for: self, into: webView)
}
}
50 changes: 12 additions & 38 deletions DuckDuckGo/ContentBlocker/ContentBlockerRulesLists.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,6 @@ import CryptoKit

final class ContentBlockerRulesLists: DefaultContentBlockerRulesListsSource {

enum Constants {
static let clickToLoadRulesListName = "ClickToLoad"
}

static var fbTrackerDataFile: Data = {
do {
let url = Bundle.main.url(forResource: "fb-tds", withExtension: "json")!
return try Data(contentsOf: url)
} catch {
fatalError("Failed to load FB-TDS")
}
}()

static var fbTrackerDataSet: TrackerRadarKit.TrackerData = {
do {
return try JSONDecoder().decode(TrackerData.self, from: fbTrackerDataFile)
} catch {
fatalError("Failed to JSON decode FB-TDS")
}
}()

func MD5(data: Data) -> String {
let digest = Insecure.MD5.hash(data: data)

return digest.map {
String(format: "%02hhx", $0)
}.joined()
}

private let adClickAttribution: AdClickAttributing

init(trackerDataManager: TrackerDataManager, adClickAttribution: AdClickAttributing) {
Expand All @@ -66,21 +37,24 @@ final class ContentBlockerRulesLists: DefaultContentBlockerRulesListsSource {
let tdsRulesIndex = result.firstIndex(where: { $0.name == DefaultContentBlockerRulesListsSource.Constants.trackerDataSetRulesListName }) {
let tdsRules = result[tdsRulesIndex]
let allowlistedTrackerNames = adClickAttribution.allowlist.map { $0.entity }
let splitter = AdClickAttributionRulesSplitter(rulesList: tdsRules, allowlistedTrackerNames: allowlistedTrackerNames)
if let splitRules = splitter.split() {
let adSplitter = AdClickAttributionRulesSplitter(rulesList: tdsRules, allowlistedTrackerNames: allowlistedTrackerNames)
if let splitRules = adSplitter.split() {
result.remove(at: tdsRulesIndex)
result.append(splitRules.0)
result.append(splitRules.1)
}
}

// Add new ones
let etag = MD5(data: Self.fbTrackerDataFile)
ladamski marked this conversation as resolved.
Show resolved Hide resolved
let dataSet: TrackerDataManager.DataSet = TrackerDataManager.DataSet(Self.fbTrackerDataSet, etag)
let CTLRulesList = ContentBlockerRulesList(name: Constants.clickToLoadRulesListName,
trackerData: nil,
fallbackTrackerData: dataSet)
result.append(CTLRulesList)
// split CTL rules so they can be managed separately from the main list when user clicks through a CTL dialog
if let tdsRulesIndex = result.firstIndex(where: { $0.name == DefaultContentBlockerRulesListsSource.Constants.trackerDataSetRulesListName }) {
let tdsRules = result[tdsRulesIndex]
let ctlSplitter = ClickToLoadRulesSplitter(rulesList: tdsRules)
if let splitRules = ctlSplitter.split() {
result.remove(at: tdsRulesIndex)
result.append(splitRules.withoutBlockCTL)
result.append(splitRules.withBlockCTL)
}
}

return result
}
Expand Down
2 changes: 1 addition & 1 deletion DuckDuckGo/ContentBlocker/ContentBlocking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ final class AppContentBlocking {
switch listName {
case defaultTDSListName:
listType = .tds
case ContentBlockerRulesLists.Constants.clickToLoadRulesListName:
case DefaultContentBlockerRulesListsSource.Constants.clickToLoadRulesListName:
listType = .clickToLoad
case AdClickAttributionRulesSplitter.blockingAttributionRuleListName(forListNamed: defaultTDSListName):
listType = .blockingAttribution
Expand Down
Loading
Loading