Skip to content

Commit

Permalink
Add content scope code sharing
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanKingston committed Jan 5, 2022
1 parent 5091379 commit 9422d1e
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 156 deletions.
12 changes: 2 additions & 10 deletions DuckDuckGo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
objects = {

/* Begin PBXBuildFile section */
0230C09F271F53000018F728 /* GPCUserScript.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0230C09E271F53000018F728 /* GPCUserScript.swift */; };
0230C0A1271F53670018F728 /* gpc.js in Resources */ = {isa = PBXBuildFile; fileRef = 0230C0A0271F53670018F728 /* gpc.js */; };
0230C0A3272080090018F728 /* KeyedCodingExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0230C0A2272080090018F728 /* KeyedCodingExtension.swift */; };
0230C0A52721F3750018F728 /* GPCRequestFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0230C0A42721F3750018F728 /* GPCRequestFactory.swift */; };
026ADE1426C3010C002518EE /* macos-config.json in Resources */ = {isa = PBXBuildFile; fileRef = 026ADE1326C3010C002518EE /* macos-config.json */; };
Expand Down Expand Up @@ -650,8 +648,6 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
0230C09E271F53000018F728 /* GPCUserScript.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GPCUserScript.swift; sourceTree = "<group>"; };
0230C0A0271F53670018F728 /* gpc.js */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.javascript; path = gpc.js; sourceTree = "<group>"; };
0230C0A2272080090018F728 /* KeyedCodingExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeyedCodingExtension.swift; sourceTree = "<group>"; };
0230C0A42721F3750018F728 /* GPCRequestFactory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GPCRequestFactory.swift; sourceTree = "<group>"; };
026ADE1326C3010C002518EE /* macos-config.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "macos-config.json"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1324,9 +1320,7 @@
0230C09D271F52D50018F728 /* GPC */ = {
isa = PBXGroup;
children = (
0230C09E271F53000018F728 /* GPCUserScript.swift */,
0230C0A42721F3750018F728 /* GPCRequestFactory.swift */,
0230C0A0271F53670018F728 /* gpc.js */,
);
path = GPC;
sourceTree = "<group>";
Expand Down Expand Up @@ -3564,7 +3558,6 @@
AA80EC6D256C469C007083E7 /* NavigationBar.storyboard in Resources */,
4B0511C6262CAA5A00F6079C /* DefaultBrowserTableCellView.xib in Resources */,
85B7185027677EA400B4277F /* Onboarding.xcassets in Resources */,
0230C0A1271F53670018F728 /* gpc.js in Resources */,
4B677433255DBEB800025BD8 /* httpsMobileV2Bloom.bin in Resources */,
AA34397B2754D55100B241FA /* trackers-3.json in Resources */,
026ADE1426C3010C002518EE /* macos-config.json in Resources */,
Expand Down Expand Up @@ -3986,7 +3979,6 @@
85378DA2274E7F25007C5CBF /* EmailManagerRequestDelegate.swift in Sources */,
B68172A9269C487D006D1092 /* PrivacyDashboardUserScript.swift in Sources */,
858A797F26A79EAA00A75A42 /* UserText+PasswordManager.swift in Sources */,
0230C09F271F53000018F728 /* GPCUserScript.swift in Sources */,
B693954E26F04BEB0015B914 /* ProgressView.swift in Sources */,
B69B503C2726A12500758A2B /* StatisticsStore.swift in Sources */,
B693955426F04BEC0015B914 /* ColorView.swift in Sources */,
Expand Down Expand Up @@ -5143,8 +5135,8 @@
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/duckduckgo/BrowserServicesKit.git";
requirement = {
kind = revision;
revision = be84a3b281920cdd2805398960eff43fe9267607;
branch = "jkt/build-step-content-scope";
kind = branch;
};
};
85FF55C625F82E4F00E2AB99 /* XCRemoteSwiftPackageReference "lottie-ios" */ = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"package": "BrowserServicesKit",
"repositoryURL": "https://github.com/duckduckgo/BrowserServicesKit.git",
"state": {
"branch": null,
"revision": "be84a3b281920cdd2805398960eff43fe9267607",
"branch": "jkt/build-step-content-scope",
"revision": "e17097af41e475bffbe7f76aeddd87f5f46c4456",
"version": null
}
},
Expand Down Expand Up @@ -63,15 +63,6 @@
"revision": "6b2aa2748a7881eebb9f84fb10c01293e15b52ca",
"version": "0.5.0"
}
},
{
"package": "TrackerRadarKit",
"repositoryURL": "https://github.com/duckduckgo/TrackerRadarKit.git",
"state": {
"branch": null,
"revision": "5f4caf35b8418700a48c64c7c61eb43308c8dacc",
"version": "1.0.3"
}
}
]
},
Expand Down
8 changes: 5 additions & 3 deletions DuckDuckGo/BrowserTab/Model/UserScripts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,16 @@ final class UserScripts {
let contentBlockerRulesScript: ContentBlockerRulesUserScript
let surrogatesScript: SurrogatesUserScript
let navigatorCredentialsUserScript: NavigatorCredentialsUserScript
let gpcScript: GPCUserScript
let contentScopeUserScript: ContentScopeUserScript

init(with sourceProvider: ScriptSourceProviding) {

contentBlockerRulesScript = ContentBlockerRulesUserScript(configuration: sourceProvider.contentBlockerRulesConfig!)
surrogatesScript = SurrogatesUserScript(configuration: sourceProvider.surrogatesConfig!)
navigatorCredentialsUserScript = NavigatorCredentialsUserScript(scriptSource: sourceProvider)
gpcScript = GPCUserScript(scriptSource: sourceProvider)
let privSettings = PrivacySecurityPreferences()
let prefs = ContentScopeProperties.init(gpcEnabled: privSettings.gpcEnabled, sessionKey: sourceProvider.sessionKey)
contentScopeUserScript = ContentScopeUserScript(sourceProvider.privacyConfigurationManager, preferences: prefs)
}

lazy var userScripts: [UserScript] = [
Expand All @@ -54,8 +56,8 @@ final class UserScripts {
pageObserverScript,
printingUserScript,
hoverUserScript,
gpcScript,
navigatorCredentialsUserScript,
contentScopeUserScript,
autofillScript
]

Expand Down
27 changes: 8 additions & 19 deletions DuckDuckGo/ContentBlocker/ScriptSourceProviding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ protocol ScriptSourceProviding {
func reload(knownChanges: ContentBlockerRulesIdentifier.Difference?)
var contentBlockerRulesConfig: ContentBlockerUserScriptConfig? { get }
var surrogatesConfig: SurrogatesUserScriptConfig? { get }
var gpcSource: String { get }
var navigatorCredentialsSource: String { get }

var privacyConfigurationManager: PrivacyConfigurationManager { get }
var sessionKey: String { get }
var sourceUpdatedPublisher: AnyPublisher<ContentBlockerRulesIdentifier.Difference?, Never> { get }

}
Expand All @@ -38,8 +38,8 @@ final class DefaultScriptSourceProvider: ScriptSourceProviding {

private(set) var contentBlockerRulesConfig: ContentBlockerUserScriptConfig?
private(set) var surrogatesConfig: SurrogatesUserScriptConfig?
private(set) var gpcSource: String = ""
private(set) var navigatorCredentialsSource: String = ""
private(set) var sessionKey: String = ""

private let sourceUpdatedSubject = PassthroughSubject<ContentBlockerRulesIdentifier.Difference?, Never>()
var sourceUpdatedPublisher: AnyPublisher<ContentBlockerRulesIdentifier.Difference?, Never> {
Expand Down Expand Up @@ -77,11 +77,15 @@ final class DefaultScriptSourceProvider: ScriptSourceProviding {
func reload(knownChanges: ContentBlockerRulesIdentifier.Difference?) {
contentBlockerRulesConfig = buildContentBlockerRulesConfig()
surrogatesConfig = buildSurrogatesConfig()
gpcSource = buildGPCSource()
navigatorCredentialsSource = buildNavigatorCredentialsSource()
sessionKey = generateSessionKey()
sourceUpdatedSubject.send( knownChanges )
}

private func generateSessionKey() -> String {
return UUID().uuidString
}

private func buildContentBlockerRulesConfig() -> ContentBlockerUserScriptConfig {
return DefaultContentBlockerUserScriptConfig(privacyConfiguration: privacyConfigurationManager.privacyConfig,
trackerData: contentBlockingManager.currentRules?.trackerData)
Expand All @@ -104,20 +108,6 @@ final class DefaultScriptSourceProvider: ScriptSourceProviding {
encodedSurrogateTrackerData: rules?.encodedTrackerData,
isDebugBuild: isDebugBuild)
}

private func buildGPCSource() -> String {
let privacyConfiguration = privacyConfigurationManager.privacyConfig
let exceptions = privacyConfiguration.tempUnprotectedDomains +
privacyConfiguration.exceptionsList(forFeature: .gpc)
let privSettings = PrivacySecurityPreferences()
let localUnprotectedDomains = privacyConfiguration.userUnprotectedDomains.joined(separator: "\n")

return GPCUserScript.loadJS("gpc", from: .main, withReplacements: [
"$GPC_ENABLED$": privacyConfiguration.isEnabled(featureKey: .gpc) && privSettings.gpcEnabled ? "true" : "false",
"$GPC_EXCEPTIONS$": exceptions.joined(separator: "\n"),
"$USER_UNPROTECTED_DOMAINS$": localUnprotectedDomains
])
}

private func buildNavigatorCredentialsSource() -> String {
let privacyConfiguration = privacyConfigurationManager.privacyConfig
Expand All @@ -131,5 +121,4 @@ final class DefaultScriptSourceProvider: ScriptSourceProviding {
"$CREDENTIALS_EXCEPTIONS$": (unprotectedDomains + contentBlockingExceptions).joined(separator: "\n")
])
}

}
35 changes: 0 additions & 35 deletions DuckDuckGo/GPC/GPCUserScript.swift

This file was deleted.

78 changes: 0 additions & 78 deletions DuckDuckGo/GPC/gpc.js

This file was deleted.

0 comments on commit 9422d1e

Please sign in to comment.