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

[iOS] Remove tab storage on all tab content scripts #27162

Merged
merged 1 commit into from
Jan 10, 2025
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
12 changes: 3 additions & 9 deletions ios/brave-ios/Sources/Brave/Frontend/Browser/BraveGetUA.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ import Shared
import WebKit

class BraveGetUA: TabContentScript {
fileprivate weak var tab: Tab?

required init(tab: Tab) {
self.tab = tab
}

static let scriptName = "BraveGetUA"
static let scriptId = UUID().uuidString
static let messageHandlerName = "\(scriptName)_\(messageUUID)"
Expand All @@ -34,9 +28,9 @@ class BraveGetUA: TabContentScript {
)
}()

func userContentController(
_ userContentController: WKUserContentController,
didReceiveScriptMessage message: WKScriptMessage,
func tab(
_ tab: Tab,
receivedScriptMessage message: WKScriptMessage,
replyHandler: (Any?, String?) -> Void
) {
// 🙀 😭 🏃‍♀️💨
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ extension BrowserViewController: ReaderModeStyleViewControllerDelegate {
as? ReaderModeScriptHandler
{
if readerMode.state == ReaderModeState.active {
readerMode.style = style
readerMode.setStyle(style, in: tab)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -721,8 +721,7 @@ extension BrowserViewController: WKNavigationDelegate {

// We can only show this content in the web view if this web view is not pending
// download via the context menu.
let canShowInWebView = navigationResponse.canShowMIMEType && (webView != pendingDownloadWebView)
let forceDownload = webView == pendingDownloadWebView
let canShowInWebView = navigationResponse.canShowMIMEType

let mimeTypesThatRequireSFSafariViewControllerHandling: [UTType] = [
.textCalendar,
Expand Down Expand Up @@ -815,13 +814,8 @@ extension BrowserViewController: WKNavigationDelegate {
request: request,
response: response,
cookieStore: cookieStore,
canShowInWebView: canShowInWebView,
forceDownload: forceDownload
canShowInWebView: canShowInWebView
) {
// Clear the pending download web view so that subsequent navigations from the same
// web view don't invoke another download.
pendingDownloadWebView = nil

let downloadAlertAction: (HTTPDownload) -> Void = { [weak self] download in
self?.downloadQueue.enqueue(download)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,6 @@ public class BrowserViewController: UIViewController {
// allow us to re-trigger the `URLRequest` if the user requests a file to be downloaded.
var pendingRequests = [String: URLRequest]()

// This is set when the user taps "Download Link" from the context menu. We then force a
// download of the next request through the `WKNavigationDelegate` that matches this web view.
weak var pendingDownloadWebView: WKWebView?
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was never set so I removed it


let downloadQueue = DownloadQueue()

private var cancellables: Set<AnyCancellable> = []
Expand Down Expand Up @@ -2629,43 +2625,44 @@ extension BrowserViewController: TabDelegate {
webView.uiDelegate = self

var injectedScripts: [TabContentScript] = [
ReaderModeScriptHandler(tab: tab),
ReaderModeScriptHandler(),
ErrorPageHelper(certStore: profile.certStore),
SessionRestoreScriptHandler(tab: tab),
BlockedDomainScriptHandler(tab: tab),
HTTPBlockedScriptHandler(tab: tab, tabManager: tabManager),
PrintScriptHandler(browserController: self, tab: tab),
CustomSearchScriptHandler(tab: tab),
DarkReaderScriptHandler(tab: tab),
FocusScriptHandler(tab: tab),
BraveGetUA(tab: tab),
BraveSearchScriptHandler(tab: tab, profile: profile, rewards: rewards),
ResourceDownloadScriptHandler(tab: tab),
DownloadContentScriptHandler(browserController: self, tab: tab),
SessionRestoreScriptHandler(),
BlockedDomainScriptHandler(),
HTTPBlockedScriptHandler(tabManager: tabManager),
PrintScriptHandler(browserController: self),
CustomSearchScriptHandler(),
DarkReaderScriptHandler(),
FocusScriptHandler(),
BraveGetUA(),
BraveSearchScriptHandler(profile: profile, rewards: rewards),
ResourceDownloadScriptHandler(),
DownloadContentScriptHandler(browserController: self),
PlaylistScriptHandler(tab: tab),
PlaylistFolderSharingScriptHandler(tab: tab),
RewardsReportingScriptHandler(rewards: rewards, tab: tab),
AdsMediaReportingScriptHandler(rewards: rewards, tab: tab),
ReadyStateScriptHandler(tab: tab),
DeAmpScriptHandler(tab: tab),
SiteStateListenerScriptHandler(tab: tab),
CosmeticFiltersScriptHandler(tab: tab),
URLPartinessScriptHandler(tab: tab),
FaviconScriptHandler(tab: tab),
Web3NameServiceScriptHandler(tab: tab),
PlaylistFolderSharingScriptHandler(),
RewardsReportingScriptHandler(rewards: rewards),
AdsMediaReportingScriptHandler(rewards: rewards),
ReadyStateScriptHandler(),
DeAmpScriptHandler(),
SiteStateListenerScriptHandler(),
CosmeticFiltersScriptHandler(),
URLPartinessScriptHandler(),
FaviconScriptHandler(),
Web3NameServiceScriptHandler(),
YoutubeQualityScriptHandler(tab: tab),
BraveLeoScriptHandler(tab: tab),
BraveLeoScriptHandler(),
BraveSkusScriptHandler(),

tab.contentBlocker,
tab.requestBlockingContentHelper,
]

injectedScripts.append(BraveTranslateScriptLanguageDetectionHandler(tab: tab))
injectedScripts.append(BraveTranslateScriptHandler(tab: tab))
BraveTranslateScriptLanguageDetectionHandler(),
BraveTranslateScriptHandler(),
]

#if canImport(BraveTalk)
injectedScripts.append(
BraveTalkScriptHandler(
tab: tab,
rewards: rewards,
launchNativeBraveTalk: { [weak self] tab, room, token in
self?.launchNativeBraveTalk(tab: tab, room: room, token: token)
Expand All @@ -2674,17 +2671,13 @@ extension BrowserViewController: TabDelegate {
)
#endif

if let braveSkusHandler = BraveSkusScriptHandler(tab: tab) {
injectedScripts.append(braveSkusHandler)
}

// Only add the logins handler and wallet provider if the tab is NOT a private browsing tab
if !tab.isPrivate {
injectedScripts += [
LoginsScriptHandler(tab: tab, profile: profile, passwordAPI: braveCore.passwordAPI),
EthereumProviderScriptHandler(tab: tab),
SolanaProviderScriptHandler(tab: tab),
BraveSearchResultAdScriptHandler(tab: tab),
LoginsScriptHandler(profile: profile, passwordAPI: braveCore.passwordAPI),
EthereumProviderScriptHandler(),
SolanaProviderScriptHandler(),
BraveSearchResultAdScriptHandler(),
]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ import Foundation
import WebKit

class FingerprintingProtection: TabContentScript {
fileprivate weak var tab: Tab?

init(tab: Tab) {
self.tab = tab
}

static let scriptName = "FingerprintingProtection"
static let scriptId = UUID().uuidString
static let messageHandlerName = "\(scriptName)_\(messageUUID)"
Expand All @@ -34,15 +28,14 @@ class FingerprintingProtection: TabContentScript {
)
}()

func userContentController(
_ userContentController: WKUserContentController,
didReceiveScriptMessage message: WKScriptMessage,
func tab(
_ tab: Tab,
receivedScriptMessage message: WKScriptMessage,
replyHandler: (Any?, String?) -> Void
) {
defer { replyHandler(nil, nil) }
if let stats = self.tab?.contentBlocker.stats {
self.tab?.contentBlocker.stats = stats.adding(fingerprintingCount: 1)
BraveGlobalShieldStats.shared.fpProtection += 1
}
let stats = tab.contentBlocker.stats
tab.contentBlocker.stats = stats.adding(fingerprintingCount: 1)
BraveGlobalShieldStats.shared.fpProtection += 1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ class DownloadHelper: NSObject {
request: URLRequest?,
response: URLResponse,
cookieStore: WKHTTPCookieStore,
canShowInWebView: Bool,
forceDownload: Bool
canShowInWebView: Bool
) {
guard let request = request else {
return nil
Expand All @@ -71,7 +70,7 @@ class DownloadHelper: NSObject {
let isAttachment =
contentDisposition?.starts(with: "attachment") ?? (mimeType == MIMEType.octetStream)

guard isAttachment || !canShowInWebView || forceDownload else {
guard isAttachment || !canShowInWebView else {
return nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,10 @@ class LivePlaylistWebLoader: UIView, PlaylistWebLoader {
static let userScript: WKUserScript? = nil
static let playlistProcessDocumentLoad = PlaylistScriptHandler.playlistProcessDocumentLoad

func userContentController(
_ userContentController: WKUserContentController,
didReceiveScriptMessage message: WKScriptMessage,
replyHandler: (Any?, String?) -> Void
func tab(
_ tab: Tab,
receivedScriptMessage message: WKScriptMessage,
replyHandler: @escaping (Any?, String?) -> Void
) {
if !verifyMessage(message: message) {
assertionFailure("Missing required security token.")
Expand Down
38 changes: 20 additions & 18 deletions ios/brave-ios/Sources/Brave/Frontend/Browser/Tab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ protocol TabContentScript: TabContentScriptLoader {
func verifyMessage(message: WKScriptMessage) -> Bool
func verifyMessage(message: WKScriptMessage, securityToken: String) -> Bool

func userContentController(
_ userContentController: WKUserContentController,
didReceiveScriptMessage message: WKScriptMessage,
@MainActor func tab(
_ tab: Tab,
receivedScriptMessage message: WKScriptMessage,
replyHandler: @escaping (Any?, String?) -> Void
)
}
Expand Down Expand Up @@ -359,7 +359,7 @@ class Tab: NSObject {

// There is no 'available macro' on props, we currently just need to store ownership.
lazy var contentBlocker = ContentBlockerHelper(tab: self)
lazy var requestBlockingContentHelper = RequestBlockingContentScriptHandler(tab: self)
let requestBlockingContentHelper = RequestBlockingContentScriptHandler()

/// The last title shown by this tab. Used by the tab tray to show titles for zombie tabs.
var lastTitle: String?
Expand Down Expand Up @@ -399,7 +399,7 @@ class Tab: NSObject {
// If this tab has been opened from another, its parent will point to the tab from which it was opened
weak var parent: Tab?

fileprivate var contentScriptManager = TabContentScriptManager()
fileprivate let contentScriptManager = TabContentScriptManager()
private var userScripts = Set<UserScriptManager.ScriptType>()
private var customUserScripts = Set<UserScriptType>()

Expand Down Expand Up @@ -453,8 +453,10 @@ class Tab: NSObject {
tabGeneratorAPI: BraveTabGeneratorAPI? = nil
) {
self.configuration = configuration
self.favicon = Favicon.default
self.id = id
self.type = type

self.favicon = Favicon.default
rewardsId = UInt32.random(in: 1...UInt32.max)
nightMode = Preferences.General.nightModeEnabled.value
_syncTab = tabGeneratorAPI?.createBraveSyncTab(isOffTheRecord: type == .private)
Expand All @@ -468,7 +470,8 @@ class Tab: NSObject {
}

super.init()
self.type = type

self.contentScriptManager.tab = self
}

weak var navigationDelegate: WKNavigationDelegate? {
Expand Down Expand Up @@ -1058,6 +1061,7 @@ extension Tab: TabWebViewDelegate {

private class TabContentScriptManager: NSObject, WKScriptMessageHandlerWithReply {
fileprivate var helpers = [String: TabContentScript]()
weak var tab: Tab?

func uninstall(from tab: Tab) {
helpers.forEach {
Expand All @@ -1066,22 +1070,20 @@ private class TabContentScriptManager: NSObject, WKScriptMessageHandlerWithReply
}
}

@objc func userContentController(
func userContentController(
_ userContentController: WKUserContentController,
didReceive message: WKScriptMessage,
replyHandler: @escaping (Any?, String?) -> Void
) {
for helper in helpers.values {
let scriptMessageHandlerName = type(of: helper).messageHandlerName
if scriptMessageHandlerName == message.name {
helper.userContentController(
userContentController,
didReceiveScriptMessage: message,
replyHandler: replyHandler
)
return
}
guard let tab,
let helper = helpers.values.first(where: {
type(of: $0).messageHandlerName == message.name
})
else {
replyHandler(nil, nil)
return
}
helper.tab(tab, receivedScriptMessage: message, replyHandler: replyHandler)
}

func addContentScript(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class ReadabilityOperation: Operation {
self.tab.createWebview()
self.tab.navigationDelegate = self

let readerMode = ReaderModeScriptHandler(tab: self.tab)
let readerMode = ReaderModeScriptHandler()
readerMode.delegate = self
self.tab.addContentScript(
readerMode,
Expand Down
Loading
Loading