Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Fix #3700: Move ScriptSourceType and UserScriptType to own files
Browse files Browse the repository at this point in the history
  • Loading branch information
cuba committed Mar 30, 2022
1 parent 11f4c85 commit 087da5e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 109 deletions.
8 changes: 8 additions & 0 deletions Client.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,8 @@
8CB0FE5427EB570D00707490 /* JSDataType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8CB0FE5327EB570D00707490 /* JSDataType.swift */; };
8CB0FE5627EB596B00707490 /* RandomConfigurationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8CB0FE5527EB596B00707490 /* RandomConfigurationTests.swift */; };
8CB0FE5827EB5F5100707490 /* FarblingProtectionHelperTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8CB0FE5727EB5F5000707490 /* FarblingProtectionHelperTests.swift */; };
8CFEF62427F220E700CF3235 /* ScriptSourceType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8CFEF62327F220E700CF3235 /* ScriptSourceType.swift */; };
8CFEF62627F2213A00CF3235 /* UserScriptType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8CFEF62527F2213900CF3235 /* UserScriptType.swift */; };
A104E199210A384400D2323E /* ShieldsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A104E198210A384400D2323E /* ShieldsViewController.swift */; };
A134B88A20DA98BB00A581D0 /* ClientPreferences.swift in Sources */ = {isa = PBXBuildFile; fileRef = A134B88920DA98BB00A581D0 /* ClientPreferences.swift */; };
A13AC72520EC12360040D4BB /* Migration.swift in Sources */ = {isa = PBXBuildFile; fileRef = A13AC72420EC12360040D4BB /* Migration.swift */; };
Expand Down Expand Up @@ -2941,6 +2943,8 @@
8CB0FE5327EB570D00707490 /* JSDataType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JSDataType.swift; sourceTree = "<group>"; };
8CB0FE5527EB596B00707490 /* RandomConfigurationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RandomConfigurationTests.swift; sourceTree = "<group>"; };
8CB0FE5727EB5F5000707490 /* FarblingProtectionHelperTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FarblingProtectionHelperTests.swift; sourceTree = "<group>"; };
8CFEF62327F220E700CF3235 /* ScriptSourceType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScriptSourceType.swift; sourceTree = "<group>"; };
8CFEF62527F2213900CF3235 /* UserScriptType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserScriptType.swift; sourceTree = "<group>"; };
A104E198210A384400D2323E /* ShieldsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShieldsViewController.swift; sourceTree = "<group>"; };
A134B88920DA98BB00A581D0 /* ClientPreferences.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClientPreferences.swift; sourceTree = "<group>"; };
A13AC72420EC12360040D4BB /* Migration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Migration.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -5777,6 +5781,8 @@
8CB0FE4F27EB528200707490 /* RandomConfiguration.swift */,
8CB0FE5127EB56A600707490 /* ARC4RandomNumberGenerator.swift */,
8CB0FE5327EB570D00707490 /* JSDataType.swift */,
8CFEF62327F220E700CF3235 /* ScriptSourceType.swift */,
8CFEF62527F2213900CF3235 /* UserScriptType.swift */,
);
path = "User Scripts";
sourceTree = "<group>";
Expand Down Expand Up @@ -8411,6 +8417,7 @@
4422D4B921BFFB7600BF1855 /* crc32c.cc in Sources */,
0A39FE932486604D00290ABC /* GRDVPNHelper.m in Sources */,
592F521E2217327C0078395E /* HttpCookieExtension.swift in Sources */,
8CFEF62427F220E700CF3235 /* ScriptSourceType.swift in Sources */,
4422D4D721BFFB7600BF1855 /* table.cc in Sources */,
0A4BEF62221B26610005551A /* LocalAdblockResourceProtocol.swift in Sources */,
27C626CB25BA198700418F40 /* WalletTransferExpiredViewController.swift in Sources */,
Expand Down Expand Up @@ -8919,6 +8926,7 @@
0A1DF48424487AA000541FE4 /* GRDGatewayAPIResponse.m in Sources */,
4422D4B421BFFB7600BF1855 /* hash.cc in Sources */,
2F1A835A274C35340089A8A9 /* RetentionPreferencesDebugMenuViewController.swift in Sources */,
8CFEF62627F2213A00CF3235 /* UserScriptType.swift in Sources */,
0A5CBD3823D4905D00362CC8 /* NTPNotificationView.swift in Sources */,
27D114D62358FCA400166534 /* SettingsRowViews.swift in Sources */,
4422D56621BFFB7F00BF1855 /* tostring.cc in Sources */,
Expand Down
109 changes: 0 additions & 109 deletions Client/Frontend/Browser/User Scripts/ScriptFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,115 +11,6 @@ enum ScriptLoadFailure: Error {
case notFound
}

/// An enum representing the unmodified local scripts stored in the application.
///
/// - Warning: Some of these scripts are not usable "as-is". Rather, you should be using `UserScriptType`.
enum ScriptSourceType {
/// A simple encryption library found here:
/// https://www.npmjs.com/package/tweetnacl
case nacl

/// This script farbles certian system methods to output slightly randomized output.
/// This script has a dependency on `nacl`.
///
/// This script has the following dynamic variables:
/// - `$<fudge_factor>`: A random value between 0.99 and 1
/// - `$<fake_plugin_data>`: A randomized array of fake plugin information
/// - `$<fake_voice_name>`: A randomized name used as a voice in the speech synthesizer
/// - `$<random_voice_index_scale>`: A random value between 0 and 1 used to get a random object in an array of an unknown size.
case farblingProtection

/// A YouTube ad blocking script
case youtubeAdBlock
case archive
case braveSearchHelper
case braveTalkHelper

fileprivate var fileName: String {
switch self {
case .nacl: return "nacl.min"
case .farblingProtection: return "FarblingProtection"
case .youtubeAdBlock: return "YoutubeAdblock"
case .archive: return "ArchiveIsCompat"
case .braveSearchHelper: return "BraveSearchHelper"
case .braveTalkHelper: return "BraveTalkHelper"
}
}

fileprivate func loadScript() throws -> String {
guard let path = Bundle.main.path(forResource: fileName, ofType: "js") else {
assertionFailure("Cannot load script. This should not happen as it's part of the codebase")
throw ScriptLoadFailure.notFound
}

return try String(contentsOfFile: path)
}
}

/// An enum representing a specific (modified) variation of a local script replacing any dynamic variables.
enum UserScriptType: Hashable {
/// This type does farbling protection and is customized for the provided eTLD+1
/// Has a dependency on `nacl`
case farblingProtection(etld: String)
/// Scripts specific to certain domains
case domainUserScript(DomainUserScript)
/// A symple encryption library to be used by other scripts
case nacl

/// Return a source typ for this script type
var sourceType: ScriptSourceType {
switch self {
case .farblingProtection:
return .farblingProtection
case .domainUserScript(let domainUserScript):
switch domainUserScript {
case .youtubeAdBlock:
return .youtubeAdBlock
case .archive:
return .archive
case .braveSearchHelper:
return .braveSearchHelper
case .braveTalkHelper:
return .braveTalkHelper
}
case .nacl:
return .nacl
}
}

/// The order in which we want to inject the scripts
var order: Int {
switch self {
case .nacl: return 0
case .farblingProtection: return 1
case .domainUserScript: return 2
}
}

var injectionTime: WKUserScriptInjectionTime {
switch self {
case .farblingProtection, .domainUserScript, .nacl:
return .atDocumentStart
}
}

var forMainFrameOnly: Bool {
switch self {
case .farblingProtection, .domainUserScript, .nacl:
return false
}
}

var contentWorld: WKContentWorld {
switch self {
case .farblingProtection, .domainUserScript:
return .page
case .nacl:
return .page
}
}
}

/// A class that helps in the aid of creating and caching scripts.
class ScriptFactory {
/// A shared instance to be shared throughout the app.
Expand Down

0 comments on commit 087da5e

Please # to comment.