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

Fix #8556: Add GPC toggle #8593

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ var braveTarget: PackageDescription.Target = .target(
.copy("Frontend/UserContent/UserScripts/Scripts_Dynamic/Scripts/DomainSpecific/Paged/FrameCheckWrapper.js"),
.copy("Frontend/UserContent/UserScripts/Scripts_Dynamic/Scripts/Paged/CookieControlScript.js"),
.copy("Frontend/UserContent/UserScripts/Scripts_Dynamic/Scripts/Paged/FarblingProtectionScript.js"),
.copy("Frontend/UserContent/UserScripts/Scripts_Dynamic/Scripts/Paged/gpc.js"),
.copy("Frontend/UserContent/UserScripts/Scripts_Dynamic/Scripts/Paged/MediaBackgroundingScript.js"),
.copy("Frontend/UserContent/UserScripts/Scripts_Dynamic/Scripts/Paged/PlaylistScript.js"),
.copy("Frontend/UserContent/UserScripts/Scripts_Dynamic/Scripts/Paged/PlaylistSwizzlerScript.js"),
Expand Down
5 changes: 4 additions & 1 deletion Sources/Brave/Frontend/Browser/PageData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Data
import BraveShared
import BraveCore
import Shared
import BraveShields

/// The data for the current web-page which is needed for loading and executing privacy scripts
///
Expand Down Expand Up @@ -71,7 +72,9 @@ struct PageData {

/// Return all the user script types for this page. The number of script types grows as more frames are loaded.
@MainActor func makeUserScriptTypes(domain: Domain) async -> Set<UserScriptType> {
var userScriptTypes: Set<UserScriptType> = [.siteStateListener]
var userScriptTypes: Set<UserScriptType> = [
.siteStateListener, .gpc(ShieldPreferences.enableGPC.value)
]

// Handle dynamic domain level scripts on the main document.
// These are scripts that change depending on the domain and the main document
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ class ScriptFactory {
let source = try makeScriptSource(of: .nacl)
resultingScript = WKUserScript(source: source, injectionTime: .atDocumentStart, forMainFrameOnly: false, in: .page)

case .gpc(let isEnabled):
let source = try makeScriptSource(of: .gpc)
.replacingOccurrences(of: "$<is_enabled>", with: isEnabled ? "true" : "false")
resultingScript = WKUserScript(source: source, injectionTime: .atDocumentStart, forMainFrameOnly: false, in: .page)

case .domainUserScript(let domainUserScript):
resultingScript = try self.makeScript(for: domainUserScript)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@ enum ScriptSourceType {
/// This script is a modification of the android and desktop script found here:
/// https://github.com/brave/brave-core/blob/master/components/cosmetic_filters/resources/data/content_cosmetic.ts
case selectorsPoller
/// Global Privacy Control script
case gpc

var fileName: String {
switch self {
case .nacl: return "nacl.min"
case .farblingProtection: return "FarblingProtectionScript"
case .frameCheckWrapper: return "FrameCheckWrapper"
case .selectorsPoller: return "SelectorsPollerScript"
case .gpc: return "gpc"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ enum UserScriptType: Hashable {
/// Selectors poller script (aka cosmetic filtering script) is responsible for hiding and unhiding css elements as dictated by the ad-block engines.
/// This script is actually executed rather than injected and this type is solely used for the creation rather than the injection of the script.
case selectorsPoller(SelectorsPollerSetup)
/// Global Privacy Control (GPC) script
case gpc(Bool)

/// The order in which we want to inject the scripts
var order: Int {
Expand All @@ -72,7 +74,8 @@ enum UserScriptType: Hashable {
case .domainUserScript: return 2
case .siteStateListener: return 3
case .selectorsPoller: return 4
case .engineScript(let configuration): return 5 + configuration.order
case .gpc: return 5
case .engineScript(let configuration): return 6 + configuration.order
}
}
}
Expand All @@ -88,6 +91,8 @@ extension UserScriptType: CustomDebugStringConvertible {
return "farblingProtection(\(etld))"
case .nacl:
return "nacl"
case .gpc(let isEnabled):
return "gpc(\(isEnabled)"
case .siteStateListener:
return "siteStateListener"
case .selectorsPoller:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ struct DefaultShieldsViewView: View {
option: Preferences.Shields.fingerprintingProtection
)

OptionToggleView(
title: Strings.Shields.enableGPCLabel,
subtitle: Strings.Shields.enableGPCDescription,
option: ShieldPreferences.enableGPC
)

ShieldToggleView(
title: Strings.blockCookieConsentNotices,
subtitle: nil,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2023 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

'use strict'

Object.defineProperty(navigator, 'globalPrivacyControl', {
enumerable: false,
configurable: false,
writable: false,
value: $<is_enabled>
})
6 changes: 6 additions & 0 deletions Sources/BraveShields/ShieldPreferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,10 @@ public class ShieldPreferences {
get { ShieldLevel(rawValue: blockAdsAndTrackingLevelRaw.value) ?? defaultBlockAdsAndTrackingLevel }
set { blockAdsAndTrackingLevelRaw.value = newValue.rawValue }
}

/// A boolean value inidicating if GPC is enabled
public static var enableGPC = Preferences.Option<Bool>(
key: "shields.enable-gpc",
default: true
)
}
17 changes: 17 additions & 0 deletions Sources/BraveShields/ShieldStrings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,20 @@ public extension Strings.Shields {
comment: "A button that ignores the brave player"
)
}

// MARK: - Shields
public extension Strings.Shields {
/// A label of the GPC toggle
static let enableGPCLabel = NSLocalizedString(
"EnableGPCLabel", tableName: "BraveShared", bundle: .module,
value: "Enable Global Privacy Control",
comment: "A label of the GPC toggle"
)

/// A description of what the Enable GPC toggle does
static let enableGPCDescription = NSLocalizedString(
"EnableGPCDescription", tableName: "BraveShared", bundle: .module,
value: "Enable the Global Privacy Control JS API",
comment: "A description of what the Enable GPC toggle does"
)
}
6 changes: 3 additions & 3 deletions Tests/ClientTests/PageDataTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import XCTest
import WebKit
import BraveShields
@testable import Brave

final class PageDataTests: XCTestCase {
Expand All @@ -14,7 +15,6 @@ final class PageDataTests: XCTestCase {
let mainFrameURL = URL(string: "http://example.com")!
let subFrameURL = URL(string: "http://example.com/1p/subframe")!
let upgradedMainFrameURL = URL(string: "https://example.com")!
let upgradedSubFrameURL = URL(string: "https://example.com/1p/subframe")!
var pageData = PageData(mainFrameURL: mainFrameURL, adBlockStats: AdBlockStats())
let expectation = expectation(description: "")

Expand All @@ -28,7 +28,7 @@ final class PageDataTests: XCTestCase {
// We get only entries of the main frame
// NOTE: If we were to add some engines we might see additional types
let expectedMainFrameTypes: Set<UserScriptType> = [
.siteStateListener, .nacl, .farblingProtection(etld: "example.com")
.siteStateListener, .nacl, .farblingProtection(etld: "example.com"), .gpc(ShieldPreferences.enableGPC.value)
]
XCTAssertEqual(mainFrameRequestTypes, expectedMainFrameTypes)

Expand All @@ -50,7 +50,7 @@ final class PageDataTests: XCTestCase {
// If we were to add some engines we might see additional types
let addedSubFrameFrameRequestTypes = await pageData.makeUserScriptTypes(domain: domain)
let expectedMainAndSubFrameTypes: Set<UserScriptType> = [
.siteStateListener, .nacl, .farblingProtection(etld: "example.com")
.siteStateListener, .nacl, .farblingProtection(etld: "example.com"), .gpc(ShieldPreferences.enableGPC.value)
]
XCTAssertEqual(expectedMainAndSubFrameTypes, addedSubFrameFrameRequestTypes)

Expand Down