diff --git a/ios/brave-ios/Sources/Brave/Frontend/Browser/BraveGetUA.swift b/ios/brave-ios/Sources/Brave/Frontend/Browser/BraveGetUA.swift index a9b068677aa3..5ba0a0a46ae0 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/Browser/BraveGetUA.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/Browser/BraveGetUA.swift @@ -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)" @@ -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 ) { // 🙀 😭 🏃‍♀️💨 diff --git a/ios/brave-ios/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+ReaderMode.swift b/ios/brave-ios/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+ReaderMode.swift index 93842de7b0a7..08f97ff3315c 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+ReaderMode.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+ReaderMode.swift @@ -54,7 +54,7 @@ extension BrowserViewController: ReaderModeStyleViewControllerDelegate { as? ReaderModeScriptHandler { if readerMode.state == ReaderModeState.active { - readerMode.style = style + readerMode.setStyle(style, in: tab) } } } diff --git a/ios/brave-ios/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+WKNavigationDelegate.swift b/ios/brave-ios/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+WKNavigationDelegate.swift index b1f04f104549..3f330c72ad6f 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+WKNavigationDelegate.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+WKNavigationDelegate.swift @@ -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, @@ -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) } diff --git a/ios/brave-ios/Sources/Brave/Frontend/Browser/BrowserViewController/BrowserViewController.swift b/ios/brave-ios/Sources/Brave/Frontend/Browser/BrowserViewController/BrowserViewController.swift index 18bd7232f631..8fba27dec6e3 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/Browser/BrowserViewController/BrowserViewController.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/Browser/BrowserViewController/BrowserViewController.swift @@ -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? - let downloadQueue = DownloadQueue() private var cancellables: Set = [] @@ -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) @@ -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(), ] } diff --git a/ios/brave-ios/Sources/Brave/Frontend/Browser/FingerprintingProtection.swift b/ios/brave-ios/Sources/Brave/Frontend/Browser/FingerprintingProtection.swift index bf07db9d86a3..669d07899c0b 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/Browser/FingerprintingProtection.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/Browser/FingerprintingProtection.swift @@ -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)" @@ -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 } } diff --git a/ios/brave-ios/Sources/Brave/Frontend/Browser/Helpers/OpenInHelper.swift b/ios/brave-ios/Sources/Brave/Frontend/Browser/Helpers/OpenInHelper.swift index e9029ffce9cb..c4afc72d3d64 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/Browser/Helpers/OpenInHelper.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/Browser/Helpers/OpenInHelper.swift @@ -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 @@ -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 } diff --git a/ios/brave-ios/Sources/Brave/Frontend/Browser/Playlist/Managers & Cache/PlaylistCacheLoader.swift b/ios/brave-ios/Sources/Brave/Frontend/Browser/Playlist/Managers & Cache/PlaylistCacheLoader.swift index 39b1b3a727fc..3799d79b6767 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/Browser/Playlist/Managers & Cache/PlaylistCacheLoader.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/Browser/Playlist/Managers & Cache/PlaylistCacheLoader.swift @@ -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.") diff --git a/ios/brave-ios/Sources/Brave/Frontend/Browser/Tab.swift b/ios/brave-ios/Sources/Brave/Frontend/Browser/Tab.swift index d98f18a6eb09..ef8a8b146842 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/Browser/Tab.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/Browser/Tab.swift @@ -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 ) } @@ -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? @@ -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() private var customUserScripts = Set() @@ -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) @@ -468,7 +470,8 @@ class Tab: NSObject { } super.init() - self.type = type + + self.contentScriptManager.tab = self } weak var navigationDelegate: WKNavigationDelegate? { @@ -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 { @@ -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( diff --git a/ios/brave-ios/Sources/Brave/Frontend/Reader/ReadabilityService.swift b/ios/brave-ios/Sources/Brave/Frontend/Reader/ReadabilityService.swift index de668968f30e..3a743f9af77b 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/Reader/ReadabilityService.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/Reader/ReadabilityService.swift @@ -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, diff --git a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Internal/BlockedDomainScriptHandler.swift b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Internal/BlockedDomainScriptHandler.swift index bfdac0f56e41..26dbd0df2b39 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Internal/BlockedDomainScriptHandler.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Internal/BlockedDomainScriptHandler.swift @@ -8,21 +8,15 @@ import Shared import WebKit class BlockedDomainScriptHandler: TabContentScript { - private weak var tab: Tab? - - required init(tab: Tab) { - self.tab = tab - } - static let scriptName = "BlockedDomainScript" static let scriptId = UUID().uuidString static let messageHandlerName = "\(scriptName)_\(messageUUID)" static let scriptSandbox: WKContentWorld = .page static let userScript: WKUserScript? = nil - func userContentController( - _ userContentController: WKUserContentController, - didReceiveScriptMessage message: WKScriptMessage, + func tab( + _ tab: Tab, + receivedScriptMessage message: WKScriptMessage, replyHandler: (Any?, String?) -> Void ) { defer { replyHandler(nil, nil) } @@ -41,16 +35,16 @@ class BlockedDomainScriptHandler: TabContentScript { switch action { case "didProceed": - blockedDomainDidProceed() + blockedDomainDidProceed(tab: tab) case "didGoBack": - blockedDomainDidGoBack() + blockedDomainDidGoBack(tab: tab) default: assertionFailure("Unhandled action `\(action)`") } } - private func blockedDomainDidProceed() { - guard let url = tab?.url?.strippedInternalURL, let etldP1 = url.baseDomain else { + private func blockedDomainDidProceed(tab: Tab) { + guard let url = tab.url?.strippedInternalURL, let etldP1 = url.baseDomain else { assertionFailure( "There should be no way this method can be triggered if the tab is not on an internal url" ) @@ -58,27 +52,27 @@ class BlockedDomainScriptHandler: TabContentScript { } let request = URLRequest(url: url) - tab?.proceedAnywaysDomainList.insert(etldP1) - tab?.loadRequest(request) + tab.proceedAnywaysDomainList.insert(etldP1) + tab.loadRequest(request) } - private func blockedDomainDidGoBack() { - guard let url = tab?.url?.strippedInternalURL else { + private func blockedDomainDidGoBack(tab: Tab) { + guard let url = tab.url?.strippedInternalURL else { assertionFailure( "There should be no way this method can be triggered if the tab is not on an internal url" ) return } - guard let listItem = tab?.backList?.reversed().first(where: { $0.url != url }) else { + guard let listItem = tab.backList?.reversed().first(where: { $0.url != url }) else { // How is this even possible? // All testing indicates no, so we will not handle. // If we find it is, then we need to disable or hide the "Go Back" button in these cases. // But this would require heavy changes or ugly mechanisms to InternalSchemeHandler. - tab?.goBack() + tab.goBack() return } - tab?.goToBackForwardListItem(listItem) + tab.goToBackForwardListItem(listItem) } } diff --git a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Internal/ErrorPageScriptHandler.swift b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Internal/ErrorPageScriptHandler.swift index 5ca06d61e87a..84bc919fedcd 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Internal/ErrorPageScriptHandler.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Internal/ErrorPageScriptHandler.swift @@ -17,10 +17,10 @@ extension ErrorPageHelper: TabContentScript { static let scriptSandbox: WKContentWorld = .page static let userScript: WKUserScript? = nil - func userContentController( - _ userContentController: WKUserContentController, - didReceiveScriptMessage message: WKScriptMessage, - replyHandler: (Any?, String?) -> Void + func tab( + _ tab: Tab, + receivedScriptMessage message: WKScriptMessage, + replyHandler: @escaping (Any?, String?) -> Void ) { defer { replyHandler(nil, nil) } diff --git a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Internal/HTTPBlockedScriptHandler.swift b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Internal/HTTPBlockedScriptHandler.swift index 53ada8f4bccb..83061607b5f6 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Internal/HTTPBlockedScriptHandler.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Internal/HTTPBlockedScriptHandler.swift @@ -9,11 +9,9 @@ import Shared import WebKit class HTTPBlockedScriptHandler: TabContentScript { - private weak var tab: Tab? private weak var tabManager: TabManager? - required init(tab: Tab, tabManager: TabManager) { - self.tab = tab + required init(tabManager: TabManager) { self.tabManager = tabManager } @@ -23,10 +21,10 @@ class HTTPBlockedScriptHandler: TabContentScript { static let scriptSandbox: WKContentWorld = .page static let userScript: WKUserScript? = nil - @MainActor func userContentController( - _ userContentController: WKUserContentController, - didReceiveScriptMessage message: WKScriptMessage, - replyHandler: (Any?, String?) -> Void + func tab( + _ tab: Tab, + receivedScriptMessage message: WKScriptMessage, + replyHandler: @escaping (Any?, String?) -> Void ) { defer { replyHandler(nil, nil) } @@ -44,16 +42,16 @@ class HTTPBlockedScriptHandler: TabContentScript { switch action { case "didProceed": - didProceed() + didProceed(tab: tab) case "didGoBack": - didGoBack() + didGoBack(tab: tab) default: assertionFailure("Unhandled action `\(action)`") } } - private func didProceed() { - guard let tab, let url = tab.upgradedHTTPSRequest?.url ?? tab.url?.strippedInternalURL else { + private func didProceed(tab: Tab) { + guard let url = tab.upgradedHTTPSRequest?.url ?? tab.url?.strippedInternalURL else { // assertionFailure( // "There should be no way this method can be triggered if the tab is not on an internal url" // ) @@ -71,8 +69,7 @@ class HTTPBlockedScriptHandler: TabContentScript { tab.loadRequest(request) } - @MainActor private func didGoBack() { - guard let tab else { return } + @MainActor private func didGoBack(tab: Tab) { tab.upgradedHTTPSRequest = nil if tab.backList?.isEmpty == true { // interstitial was opened in a new tab diff --git a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Internal/SessionRestoreScriptHandler.swift b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Internal/SessionRestoreScriptHandler.swift index d859819bba57..16e6ec097864 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Internal/SessionRestoreScriptHandler.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Internal/SessionRestoreScriptHandler.swift @@ -12,11 +12,6 @@ protocol SessionRestoreScriptHandlerDelegate: AnyObject { class SessionRestoreScriptHandler: TabContentScript { weak var delegate: SessionRestoreScriptHandlerDelegate? - fileprivate weak var tab: Tab? - - required init(tab: Tab) { - self.tab = tab - } static let scriptName = "SessionRestoreScript" static let scriptId = UUID().uuidString @@ -38,9 +33,9 @@ class SessionRestoreScriptHandler: TabContentScript { ) }() - func userContentController( - _ userContentController: WKUserContentController, - didReceiveScriptMessage message: WKScriptMessage, + func tab( + _ tab: Tab, + receivedScriptMessage message: WKScriptMessage, replyHandler: (Any?, String?) -> Void ) { defer { replyHandler(nil, nil) } @@ -50,7 +45,7 @@ class SessionRestoreScriptHandler: TabContentScript { return } - if let tab = tab, let params = message.body as? [String: AnyObject] { + if let params = message.body as? [String: AnyObject] { if params["name"] as? String == "didRestoreSession" { DispatchQueue.main.async { self.delegate?.sessionRestore(self, didRestoreSessionForTab: tab) diff --git a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Internal/Web3NameServiceScriptHandler.swift b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Internal/Web3NameServiceScriptHandler.swift index 0c647b46a66b..08b2726c5f59 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Internal/Web3NameServiceScriptHandler.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Internal/Web3NameServiceScriptHandler.swift @@ -26,11 +26,6 @@ class Web3NameServiceScriptHandler: TabContentScript { weak var delegate: Web3NameServiceScriptHandlerDelegate? var originalURL: URL? - fileprivate weak var tab: Tab? - - required init(tab: Tab) { - self.tab = tab - } static let scriptName = "Web3NameServiceScript" static let scriptId = UUID().uuidString @@ -38,9 +33,9 @@ class Web3NameServiceScriptHandler: TabContentScript { static let scriptSandbox: WKContentWorld = .page static let userScript: WKUserScript? = nil - func userContentController( - _ userContentController: WKUserContentController, - didReceiveScriptMessage message: WKScriptMessage, + func tab( + _ tab: Tab, + receivedScriptMessage message: WKScriptMessage, replyHandler: (Any?, String?) -> Void ) { defer { replyHandler(nil, nil) } diff --git a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/BraveSearchResultAdScriptHandler.swift b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/BraveSearchResultAdScriptHandler.swift index d4eb19b4df18..457ba9398606 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/BraveSearchResultAdScriptHandler.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/BraveSearchResultAdScriptHandler.swift @@ -28,12 +28,6 @@ class BraveSearchResultAdScriptHandler: TabContentScript { let creatives: [SearchResultAd] } - fileprivate weak var tab: Tab? - - init(tab: Tab) { - self.tab = tab - } - static let scriptName = "BraveSearchResultAdScript" static let scriptId = UUID().uuidString static let messageHandlerName = "\(scriptName)_\(messageUUID)" @@ -54,9 +48,9 @@ class BraveSearchResultAdScriptHandler: TabContentScript { ) }() - func userContentController( - _ userContentController: WKUserContentController, - didReceiveScriptMessage message: WKScriptMessage, + func tab( + _ tab: Tab, + receivedScriptMessage message: WKScriptMessage, replyHandler: (Any?, String?) -> Void ) { defer { replyHandler(nil, nil) } @@ -66,9 +60,7 @@ class BraveSearchResultAdScriptHandler: TabContentScript { return } - guard let tab = tab, - let braveSearchResultAdManager = tab.braveSearchResultAdManager - else { + guard let braveSearchResultAdManager = tab.braveSearchResultAdManager else { Logger.module.error("Failed to get brave search result ad handler") return } diff --git a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/BraveSearchScriptHandler.swift b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/BraveSearchScriptHandler.swift index e933d4a9744c..06be38e8c2e7 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/BraveSearchScriptHandler.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/BraveSearchScriptHandler.swift @@ -11,7 +11,6 @@ import WebKit import os.log class BraveSearchScriptHandler: TabContentScript { - private weak var tab: Tab? private let profile: Profile private weak var rewards: BraveRewards? @@ -23,8 +22,7 @@ class BraveSearchScriptHandler: TabContentScript { /// How many times user is shown the default browser prompt in total, this does not reset between app launches. private let maxCountOfDefaultBrowserPromptsTotal = 10 - required init(tab: Tab, profile: Profile, rewards: BraveRewards) { - self.tab = tab + required init(profile: Profile, rewards: BraveRewards) { self.profile = profile self.rewards = rewards } @@ -62,9 +60,9 @@ class BraveSearchScriptHandler: TabContentScript { let methodId: Int } - func userContentController( - _ userContentController: WKUserContentController, - didReceiveScriptMessage message: WKScriptMessage, + func tab( + _ tab: Tab, + receivedScriptMessage message: WKScriptMessage, replyHandler: (Any?, String?) -> Void ) { if !verifyMessage(message: message) { @@ -93,7 +91,7 @@ class BraveSearchScriptHandler: TabContentScript { switch method { case Method.canSetBraveSearchAsDefault.rawValue: - handleCanSetBraveSearchAsDefault(replyHandler: replyHandler) + handleCanSetBraveSearchAsDefault(tab: tab, replyHandler: replyHandler) case Method.setBraveSearchDefault.rawValue: handleSetBraveSearchDefault(replyHandler: replyHandler) default: @@ -101,8 +99,8 @@ class BraveSearchScriptHandler: TabContentScript { } } - private func handleCanSetBraveSearchAsDefault(replyHandler: (Any?, String?) -> Void) { - if tab?.isPrivate == true { + private func handleCanSetBraveSearchAsDefault(tab: Tab, replyHandler: (Any?, String?) -> Void) { + if tab.isPrivate == true { Logger.module.debug("Private mode detected, skipping setting Brave Search as a default") replyHandler(false, nil) return diff --git a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/BraveSkusScriptHandler.swift b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/BraveSkusScriptHandler.swift index 572b32e9659a..5ebc6afdae26 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/BraveSkusScriptHandler.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/BraveSkusScriptHandler.swift @@ -15,18 +15,6 @@ import WebKit import os.log class BraveSkusScriptHandler: TabContentScript { - private weak var tab: Tab? - private let skusManager: BraveSkusManager - - required init?(tab: Tab) { - self.tab = tab - guard let skusManager = BraveSkusManager(isPrivateMode: tab.isPrivate) else { - return nil - } - - self.skusManager = skusManager - } - static let scriptName = "BraveSkusScript" static let scriptId = UUID().uuidString static let messageHandlerName = "\(scriptName)_\(messageUUID)" @@ -49,9 +37,9 @@ class BraveSkusScriptHandler: TabContentScript { ) }() - func userContentController( - _ userContentController: WKUserContentController, - didReceiveScriptMessage message: WKScriptMessage, + func tab( + _ tab: Tab, + receivedScriptMessage message: WKScriptMessage, replyHandler: @escaping (Any?, String?) -> Void ) { if !verifyMessage(message: message) { @@ -78,7 +66,12 @@ class BraveSkusScriptHandler: TabContentScript { Task { @MainActor in do { - let result = try await processRequest(message: message, method: method, for: requestHost) + let result = try await processRequest( + tab: tab, + message: message, + method: method, + for: requestHost + ) replyHandler(result, nil) } catch { Logger.module.error("Brave skus error processing request: \(error)") @@ -89,10 +82,15 @@ class BraveSkusScriptHandler: TabContentScript { @MainActor private func processRequest( + tab: Tab, message: WKScriptMessage, method: Method, for skusDomain: String ) async throws -> Any? { + guard let skusManager = BraveSkusManager(isPrivateMode: tab.isPrivate) else { + return nil + } + switch method { case .refreshOrder: let order = try OrderMessage.from(message: message) diff --git a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/BraveTalkScriptHandler.swift b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/BraveTalkScriptHandler.swift index c9fb9f548a93..31a4cdfe30fc 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/BraveTalkScriptHandler.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/BraveTalkScriptHandler.swift @@ -14,23 +14,16 @@ import os.log import BraveTalk class BraveTalkScriptHandler: TabContentScript { - private weak var tab: Tab? private weak var rewards: BraveRewards? private var rewardsEnabledReplyHandler: ((Any?, String?) -> Void)? private let launchNativeBraveTalk: (_ tab: Tab?, _ room: String, _ token: String) -> Void required init( - tab: Tab, rewards: BraveRewards, launchNativeBraveTalk: @escaping (_ tab: Tab?, _ room: String, _ token: String) -> Void ) { - self.tab = tab self.rewards = rewards self.launchNativeBraveTalk = launchNativeBraveTalk - - tab.rewardsEnabledCallback = { [weak self] success in - self?.rewardsEnabledReplyHandler?(success, nil) - } } static let scriptName = "BraveTalkScript" @@ -85,9 +78,9 @@ class BraveTalkScriptHandler: TabContentScript { } } - func userContentController( - _ userContentController: WKUserContentController, - didReceiveScriptMessage message: WKScriptMessage, + func tab( + _ tab: Tab, + receivedScriptMessage message: WKScriptMessage, replyHandler: @escaping (Any?, String?) -> Void ) { if !verifyMessage(message: message) { @@ -114,7 +107,7 @@ class BraveTalkScriptHandler: TabContentScript { switch payload.kind { case .braveRequestAdsEnabled: - handleBraveRequestAdsEnabled(replyHandler) + handleBraveRequestAdsEnabled(tab: tab, replyHandler) case .launchNativeBraveTalk(let url): guard let components = URLComponents(string: url), case let room = String(components.path.dropFirst(1)), @@ -127,8 +120,11 @@ class BraveTalkScriptHandler: TabContentScript { } } - private func handleBraveRequestAdsEnabled(_ replyHandler: @escaping (Any?, String?) -> Void) { - guard let rewards = rewards, tab?.isPrivate != true else { + private func handleBraveRequestAdsEnabled( + tab: Tab, + _ replyHandler: @escaping (Any?, String?) -> Void + ) { + guard let rewards = rewards, tab.isPrivate != true else { replyHandler(false, nil) return } @@ -140,10 +136,11 @@ class BraveTalkScriptHandler: TabContentScript { // If rewards are disabled we show a Rewards panel, // The `rewardsEnabledReplyHandler` will be called from other place. - if let tab = tab { - rewardsEnabledReplyHandler = replyHandler - tab.tabDelegate?.showRequestRewardsPanel(tab) + rewardsEnabledReplyHandler = replyHandler + tab.rewardsEnabledCallback = { [weak self] success in + self?.rewardsEnabledReplyHandler?(success, nil) } + tab.tabDelegate?.showRequestRewardsPanel(tab) } } diff --git a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/ContentBlockerScriptHandler.swift b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/ContentBlockerScriptHandler.swift index c4f4006dfb47..d9c921122e1e 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/ContentBlockerScriptHandler.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/ContentBlockerScriptHandler.swift @@ -54,14 +54,14 @@ extension ContentBlockerHelper: TabContentScript { blockedRequests.removeAll() } - func userContentController( - _ userContentController: WKUserContentController, - didReceiveScriptMessage message: WKScriptMessage, - replyHandler: (Any?, String?) -> Void + func tab( + _ tab: Tab, + receivedScriptMessage message: WKScriptMessage, + replyHandler: @escaping (Any?, String?) -> Void ) { defer { replyHandler(nil, nil) } - guard let currentTabURL = tab?.webView?.url else { + guard let currentTabURL = tab.webView?.url else { assertionFailure("Missing tab or webView") return } @@ -128,7 +128,7 @@ extension ContentBlockerHelper: TabContentScript { if blockedType == .ad, Preferences.PrivacyReports.captureShieldsData.value, let domainURL = URL(string: domainURLString), let blockedResourceHost = requestURL.baseDomain, - tab?.isPrivate != true + !tab.isPrivate { PrivacyReportsManager.pendingBlockedRequests.append( (blockedResourceHost, domainURL, Date()) diff --git a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/CosmeticFiltersScriptHandler.swift b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/CosmeticFiltersScriptHandler.swift index 42cdec13b0ab..abb7e451fc5a 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/CosmeticFiltersScriptHandler.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/CosmeticFiltersScriptHandler.swift @@ -30,15 +30,9 @@ class CosmeticFiltersScriptHandler: TabContentScript { static let scriptSandbox: WKContentWorld = .defaultClient static let userScript: WKUserScript? = nil - private weak var tab: Tab? - - init(tab: Tab) { - self.tab = tab - } - - func userContentController( - _ userContentController: WKUserContentController, - didReceiveScriptMessage message: WKScriptMessage, + func tab( + _ tab: Tab, + receivedScriptMessage message: WKScriptMessage, replyHandler: @escaping (Any?, String?) -> Void ) { if !verifyMessage(message: message) { @@ -59,7 +53,7 @@ class CosmeticFiltersScriptHandler: TabContentScript { Task { @MainActor in let domain = Domain.getOrCreate( forUrl: frameURL, - persistent: self.tab?.isPrivate == true ? false : true + persistent: !tab.isPrivate ) let cachedEngines = AdBlockGroupsManager.shared.cachedEngines(for: domain) @@ -76,7 +70,7 @@ class CosmeticFiltersScriptHandler: TabContentScript { return nil } - return (selectors, cachedEngine.type.isAlwaysAggressive) + return await (selectors, cachedEngine.type.isAlwaysAggressive) } catch { Logger.module.error("\(error.localizedDescription)") return nil diff --git a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/EthereumProviderScriptHandler.swift b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/EthereumProviderScriptHandler.swift index c064519da782..6cca72940ee7 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/EthereumProviderScriptHandler.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/EthereumProviderScriptHandler.swift @@ -19,12 +19,6 @@ class EthereumProviderScriptHandler: TabContentScript { "eth_newPendingTransactionFilter", ] - private weak var tab: Tab? - - init(tab: Tab) { - self.tab = tab - } - static let scriptName = "WalletEthereumProviderScript" static let scriptId = UUID().uuidString static let messageHandlerName = "\(scriptName)_\(messageUUID)" @@ -64,9 +58,9 @@ class EthereumProviderScriptHandler: TabContentScript { } } - @MainActor func userContentController( - _ userContentController: WKUserContentController, - didReceiveScriptMessage message: WKScriptMessage, + func tab( + _ tab: Tab, + receivedScriptMessage message: WKScriptMessage, replyHandler: @escaping (Any?, String?) -> Void ) { if !verifyMessage(message: message) { @@ -74,8 +68,7 @@ class EthereumProviderScriptHandler: TabContentScript { return } - guard let tab = tab, - !tab.isPrivate, + guard !tab.isPrivate, let provider = tab.walletEthProvider, // Fail if there is no last committed URL yet !message.frameInfo.securityOrigin.host.isEmpty, diff --git a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/PlaylistFolderSharingScriptHandler.swift b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/PlaylistFolderSharingScriptHandler.swift index 1a5c61e1ea69..2c4c6c598006 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/PlaylistFolderSharingScriptHandler.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/PlaylistFolderSharingScriptHandler.swift @@ -15,14 +15,8 @@ protocol PlaylistFolderSharingScriptHandlerDelegate: AnyObject { } class PlaylistFolderSharingScriptHandler: NSObject, TabContentScript { - fileprivate weak var tab: Tab? public weak var delegate: PlaylistFolderSharingScriptHandlerDelegate? - init(tab: Tab) { - self.tab = tab - super.init() - } - static let scriptName = "PlaylistFolderSharingScript" static let scriptId = UUID().uuidString static let messageHandlerName = "\(scriptName)_\(messageUUID)" @@ -43,10 +37,10 @@ class PlaylistFolderSharingScriptHandler: NSObject, TabContentScript { ) }() - func userContentController( - _ userContentController: WKUserContentController, - didReceiveScriptMessage message: WKScriptMessage, - replyHandler: (Any?, String?) -> Void + func tab( + _ tab: Tab, + receivedScriptMessage message: WKScriptMessage, + replyHandler: @escaping (Any?, String?) -> Void ) { defer { replyHandler(nil, nil) } diff --git a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/PlaylistScriptHandler.swift b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/PlaylistScriptHandler.swift index fbd305eba9fa..17ad8988db20 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/PlaylistScriptHandler.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/PlaylistScriptHandler.swift @@ -27,7 +27,6 @@ protocol PlaylistScriptHandlerDelegate: NSObject { } class PlaylistScriptHandler: NSObject, TabContentScript { - fileprivate weak var tab: Tab? public weak var delegate: PlaylistScriptHandlerDelegate? private var url: URL? private var urlObserver: NSObjectProtocol? @@ -35,21 +34,20 @@ class PlaylistScriptHandler: NSObject, TabContentScript { private static let queue = DispatchQueue(label: "com.playlisthelper.queue", qos: .userInitiated) init(tab: Tab) { - self.tab = tab self.url = tab.url super.init() urlObserver = tab.webView?.observe( \.url, options: [.new], - changeHandler: { [weak self] _, change in + changeHandler: { [weak self, weak tab] _, change in guard let self = self, let url = change.newValue else { return } if self.url != url { self.url = url self.asset?.cancelLoading() self.asset = nil - self.delegate?.updatePlaylistURLBar(tab: self.tab, state: .none, item: nil) + self.delegate?.updatePlaylistURLBar(tab: tab, state: .none, item: nil) } } ) @@ -63,7 +61,6 @@ class PlaylistScriptHandler: NSObject, TabContentScript { deinit { asset?.cancelLoading() - delegate?.updatePlaylistURLBar(tab: tab, state: .none, item: nil) } static let playlistLongPressed = "playlistLongPressed_\(uniqueID)" @@ -100,10 +97,10 @@ class PlaylistScriptHandler: NSObject, TabContentScript { ) }() - func userContentController( - _ userContentController: WKUserContentController, - didReceiveScriptMessage message: WKScriptMessage, - replyHandler: (Any?, String?) -> Void + func tab( + _ tab: Tab, + receivedScriptMessage message: WKScriptMessage, + replyHandler: @escaping (Any?, String?) -> Void ) { defer { replyHandler(nil, nil) } @@ -122,22 +119,27 @@ class PlaylistScriptHandler: NSObject, TabContentScript { } Self.processPlaylistInfo( + tab: tab, handler: self, item: PlaylistInfo.from(message: message) ) } - private class func processPlaylistInfo(handler: PlaylistScriptHandler, item: PlaylistInfo?) { + private class func processPlaylistInfo( + tab: Tab, + handler: PlaylistScriptHandler, + item: PlaylistInfo? + ) { guard var item = item, !item.src.isEmpty else { DispatchQueue.main.async { - handler.delegate?.updatePlaylistURLBar(tab: handler.tab, state: .none, item: nil) + handler.delegate?.updatePlaylistURLBar(tab: tab, state: .none, item: nil) } return } if handler.url?.baseDomain != "soundcloud.com", item.isInvisible { DispatchQueue.main.async { - handler.delegate?.updatePlaylistURLBar(tab: handler.tab, state: .none, item: nil) + handler.delegate?.updatePlaylistURLBar(tab: tab, state: .none, item: nil) } return } @@ -147,8 +149,8 @@ class PlaylistScriptHandler: NSObject, TabContentScript { item = PlaylistInfo( name: item.name, src: item.src, - pageSrc: handler.tab?.webView?.url?.absoluteString ?? item.pageSrc, - pageTitle: handler.tab?.webView?.title ?? item.pageTitle, + pageSrc: tab.webView?.url?.absoluteString ?? item.pageSrc, + pageTitle: tab.webView?.title ?? item.pageTitle, mimeType: item.mimeType, duration: item.duration, lastPlayedOffset: 0.0, @@ -164,7 +166,7 @@ class PlaylistScriptHandler: NSObject, TabContentScript { if item.duration <= 0.0 && !item.detected || item.src.isEmpty { DispatchQueue.main.async { - handler.delegate?.updatePlaylistURLBar(tab: handler.tab, state: .none, item: nil) + handler.delegate?.updatePlaylistURLBar(tab: tab, state: .none, item: nil) } return } @@ -177,14 +179,14 @@ class PlaylistScriptHandler: NSObject, TabContentScript { if PlaylistItem.itemExists(pageSrc: item.pageSrc) { // Item already exists, so just update the database with new token or URL. - handler.updateItem(item, detected: item.detected) + handler.updateItem(item, detected: item.detected, tab: tab) } else if item.detected { // Automatic Detection - delegate.updatePlaylistURLBar(tab: handler.tab, state: .newItem, item: item) - delegate.showPlaylistOnboarding(tab: handler.tab) + delegate.updatePlaylistURLBar(tab: tab, state: .newItem, item: item) + delegate.showPlaylistOnboarding(tab: tab) } else { // Long-Press - delegate.showPlaylistAlert(tab: handler.tab, state: .newItem, item: item) + delegate.showPlaylistAlert(tab: tab, state: .newItem, item: item) } } } @@ -207,9 +209,9 @@ class PlaylistScriptHandler: NSObject, TabContentScript { return await PlaylistMediaStreamer.loadAssetPlayability(asset: asset) } - private func updateItem(_ item: PlaylistInfo, detected: Bool) { + private func updateItem(_ item: PlaylistInfo, detected: Bool, tab: Tab) { if detected { - self.delegate?.updatePlaylistURLBar(tab: self.tab, state: .existingItem, item: item) + self.delegate?.updatePlaylistURLBar(tab: tab, state: .existingItem, item: item) } PlaylistItem.updateItem(item) { [weak self] in @@ -219,9 +221,9 @@ class PlaylistScriptHandler: NSObject, TabContentScript { if let delegate = self.delegate { if detected { - delegate.updatePlaylistURLBar(tab: self.tab, state: .existingItem, item: item) + delegate.updatePlaylistURLBar(tab: tab, state: .existingItem, item: item) } else { - delegate.showPlaylistToast(tab: self.tab, state: .existingItem, item: item) + delegate.showPlaylistToast(tab: tab, state: .existingItem, item: item) } } } @@ -232,7 +234,7 @@ extension PlaylistScriptHandler: UIGestureRecognizerDelegate { @objc func onLongPressedWebView(_ gestureRecognizer: UILongPressGestureRecognizer) { if gestureRecognizer.state == .began, - let webView = tab?.webView, + let webView = gestureRecognizer.view as? BraveWebView, Preferences.Playlist.enableLongPressAddToPlaylist.value { @@ -277,7 +279,7 @@ extension PlaylistScriptHandler: UIGestureRecognizerDelegate { extension PlaylistScriptHandler { static func getCurrentTime( - webView: WKWebView, + webView: BraveWebView, nodeTag: String, completion: @escaping (Double) -> Void ) { @@ -330,7 +332,7 @@ extension PlaylistScriptHandler { extension PlaylistScriptHandler { static func updatePlaylistTab(tab: Tab, item: PlaylistInfo?) { if let handler = tab.getContentScript(name: Self.scriptName) as? PlaylistScriptHandler { - Self.processPlaylistInfo(handler: handler, item: item) + Self.processPlaylistInfo(tab: tab, handler: handler, item: item) } } } diff --git a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/PrintScriptHandler.swift b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/PrintScriptHandler.swift index 7f6b042025cc..84d3329bb8f6 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/PrintScriptHandler.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/PrintScriptHandler.swift @@ -9,15 +9,13 @@ import os.log class PrintScriptHandler: TabContentScript { private weak var browserController: BrowserViewController? - private weak var tab: Tab? private var isPresentingController = false private var printCounter = 0 private var isBlocking = false private var currentDomain: String? - required init(browserController: BrowserViewController, tab: Tab) { + required init(browserController: BrowserViewController) { self.browserController = browserController - self.tab = tab } static let scriptName = "PrintScript" @@ -40,10 +38,10 @@ class PrintScriptHandler: TabContentScript { ) }() - func userContentController( - _ userContentController: WKUserContentController, - didReceiveScriptMessage message: WKScriptMessage, - replyHandler: (Any?, String?) -> Void + func tab( + _ tab: Tab, + receivedScriptMessage message: WKScriptMessage, + replyHandler: @escaping (Any?, String?) -> Void ) { defer { replyHandler(nil, nil) } @@ -52,7 +50,7 @@ class PrintScriptHandler: TabContentScript { return } - if let tab = tab, let webView = tab.webView, let url = webView.url { + if let webView = tab.webView, let url = webView.url { // If the main-frame's URL has changed if let domain = url.baseDomain, domain != currentDomain, message.frameInfo.isMainFrame { isBlocking = false diff --git a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/ReadyStateScriptHandler.swift b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/ReadyStateScriptHandler.swift index 1cf03c3702ce..68967edd5e4e 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/ReadyStateScriptHandler.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/ReadyStateScriptHandler.swift @@ -45,13 +45,8 @@ struct ReadyState: Codable { } class ReadyStateScriptHandler: TabContentScript { - private weak var tab: Tab? private var debounceTimer: Timer? - required init(tab: Tab) { - self.tab = tab - } - static let scriptName = "ReadyStateScript" static let scriptId = UUID().uuidString static let messageHandlerName = "\(scriptName)_\(messageUUID)" @@ -72,12 +67,11 @@ class ReadyStateScriptHandler: TabContentScript { ) }() - func userContentController( - _ userContentController: WKUserContentController, - didReceiveScriptMessage message: WKScriptMessage, - replyHandler: (Any?, String?) -> Void + func tab( + _ tab: Tab, + receivedScriptMessage message: WKScriptMessage, + replyHandler: @escaping (Any?, String?) -> Void ) { - defer { replyHandler(nil, nil) } if !verifyMessage(message: message) { @@ -90,6 +84,6 @@ class ReadyStateScriptHandler: TabContentScript { return } - tab?.onPageReadyStateChanged?(readyState.state) + tab.onPageReadyStateChanged?(readyState.state) } } diff --git a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/RequestBlockingContentScriptHandler.swift b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/RequestBlockingContentScriptHandler.swift index c017f21713e0..3b2d9e69371f 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/RequestBlockingContentScriptHandler.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/RequestBlockingContentScriptHandler.swift @@ -44,18 +44,12 @@ class RequestBlockingContentScriptHandler: TabContentScript { ) }() - private weak var tab: Tab? - - init(tab: Tab) { - self.tab = tab - } - - func userContentController( - _ userContentController: WKUserContentController, - didReceiveScriptMessage message: WKScriptMessage, + func tab( + _ tab: Tab, + receivedScriptMessage message: WKScriptMessage, replyHandler: @escaping (Any?, String?) -> Void ) { - guard let tab = tab, let currentTabURL = tab.webView?.url else { + guard let currentTabURL = tab.webView?.url else { assertionFailure("Should have a tab set") return } @@ -93,7 +87,7 @@ class RequestBlockingContentScriptHandler: TabContentScript { // We simply check the known subframeURLs on this page. guard tab.url?.baseDomain == windowOriginURL.baseDomain - || self.tab?.currentPageData?.allSubframeURLs.contains(windowOriginURL) == true + || tab.currentPageData?.allSubframeURLs.contains(windowOriginURL) == true else { replyHandler(shouldBlock, nil) return diff --git a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/RewardsReportingScriptHandler.swift b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/RewardsReportingScriptHandler.swift index 64d2fc5227a6..9d6b75c3abb9 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/RewardsReportingScriptHandler.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/RewardsReportingScriptHandler.swift @@ -10,11 +10,9 @@ import os.log class RewardsReportingScriptHandler: TabContentScript { let rewards: BraveRewards - weak var tab: Tab? - init(rewards: BraveRewards, tab: Tab) { + init(rewards: BraveRewards) { self.rewards = rewards - self.tab = tab } static let scriptName = "RewardsReportingScript" @@ -38,10 +36,10 @@ class RewardsReportingScriptHandler: TabContentScript { ) }() - func userContentController( - _ userContentController: WKUserContentController, - didReceiveScriptMessage message: WKScriptMessage, - replyHandler: (Any?, String?) -> Void + func tab( + _ tab: Tab, + receivedScriptMessage message: WKScriptMessage, + replyHandler: @escaping (Any?, String?) -> Void ) { defer { replyHandler(nil, nil) } struct Content: Decodable { @@ -51,7 +49,7 @@ class RewardsReportingScriptHandler: TabContentScript { var referrerUrl: String? } - if tab?.isPrivate == true || !rewards.isEnabled { + if tab.isPrivate || !rewards.isEnabled { return } @@ -69,7 +67,7 @@ class RewardsReportingScriptHandler: TabContentScript { let json = try JSONSerialization.data(withJSONObject: body, options: []) var content = try JSONDecoder().decode(Content.self, from: json) - guard let tab = tab, let tabURL = tab.url else { return } + guard let tabURL = tab.url else { return } if content.url.hasPrefix("//") { content.url = "\(tabURL.scheme ?? "http"):\(content.url)" diff --git a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/SolanaProviderScriptHandler.swift b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/SolanaProviderScriptHandler.swift index ef7d11b4d56f..2a0dacbbcf36 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/SolanaProviderScriptHandler.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/SolanaProviderScriptHandler.swift @@ -52,12 +52,6 @@ class SolanaProviderScriptHandler: TabContentScript { ) }() - private weak var tab: Tab? - - init(tab: Tab) { - self.tab = tab - } - private struct MessageBody: Decodable { enum Method: String, Decodable { case connect @@ -77,9 +71,9 @@ class SolanaProviderScriptHandler: TabContentScript { } } - func userContentController( - _ userContentController: WKUserContentController, - didReceiveScriptMessage message: WKScriptMessage, + func tab( + _ tab: Tab, + receivedScriptMessage message: WKScriptMessage, replyHandler: @escaping (Any?, String?) -> Void ) { if !verifyMessage(message: message) { @@ -87,8 +81,7 @@ class SolanaProviderScriptHandler: TabContentScript { return } - guard let tab = tab, - !tab.isPrivate, + guard !tab.isPrivate, let provider = tab.walletSolProvider, // Fail if there is no last committed URL yet !message.frameInfo.securityOrigin.host.isEmpty, @@ -114,19 +107,19 @@ class SolanaProviderScriptHandler: TabContentScript { Task { @MainActor in switch body.method { case .connect: - let (publicKey, error) = await connect(args: body.args) + let (publicKey, error) = await connect(tab: tab, args: body.args) replyHandler(publicKey, error) if let publicKey = publicKey as? String { - await emitConnectEvent(publicKey: publicKey) + await emitConnectEvent(tab: tab, publicKey: publicKey) } case .disconnect: provider.disconnect() replyHandler("{:}", nil) case .signAndSendTransaction: - let (result, error) = await signAndSendTransaction(args: body.args) + let (result, error) = await signAndSendTransaction(tab: tab, args: body.args) replyHandler(result, error) case .signMessage: - let (result, error) = await signMessage(args: body.args) + let (result, error) = await signMessage(tab: tab, args: body.args) replyHandler(result, error) case .request: guard let args = body.args, @@ -136,26 +129,26 @@ class SolanaProviderScriptHandler: TabContentScript { replyHandler(nil, buildErrorJson(status: .invalidParams, errorMessage: "Invalid args")) return } - let (result, error) = await request(args: body.args) + let (result, error) = await request(tab: tab, args: body.args) replyHandler(result, error) if method == Keys.connect.rawValue, let publicKey = result as? String { - await emitConnectEvent(publicKey: publicKey) + await emitConnectEvent(tab: tab, publicKey: publicKey) } else if method == Keys.disconnect.rawValue { tab.emitSolanaEvent(.disconnect) } case .signTransaction: - let (result, error) = await signTransaction(args: body.args) + let (result, error) = await signTransaction(tab: tab, args: body.args) replyHandler(result, error) case .signAllTransactions: - let (result, error) = await signAllTransactions(args: body.args) + let (result, error) = await signAllTransactions(tab: tab, args: body.args) replyHandler(result, error) } } } /// Given optional args `{onlyIfTrusted: Bool}`, will return the base 58 encoded public key for success or the error dictionary for failures. - @MainActor func connect(args: String?) async -> (Any?, String?) { - guard let tab = tab, let provider = tab.walletSolProvider else { + @MainActor func connect(tab: Tab, args: String?) async -> (Any?, String?) { + guard let provider = tab.walletSolProvider else { return ( nil, buildErrorJson(status: .internalError, errorMessage: Strings.Wallet.internalErrorMessage) @@ -177,12 +170,12 @@ class SolanaProviderScriptHandler: TabContentScript { /// Given args `{serializedMessage: [Uint8], signatures: [Buffer], sendOptions: [:]}`, will return /// dictionary `{publicKey: , signature: }` for success /// or an error dictionary for failures. - @MainActor func signAndSendTransaction(args: String?) async -> (Any?, String?) { + @MainActor func signAndSendTransaction(tab: Tab, args: String?) async -> (Any?, String?) { guard let args = args, let arguments = MojoBase.Value(jsonString: args)?.dictionaryValue, let serializedMessage = arguments[Keys.serializedMessage.rawValue], let signatures = arguments[Keys.signatures.rawValue], - let provider = tab?.walletSolProvider + let provider = tab.walletSolProvider else { return (nil, buildErrorJson(status: .invalidParams, errorMessage: "Invalid args")) } @@ -208,11 +201,11 @@ class SolanaProviderScriptHandler: TabContentScript { /// Given args `{[[UInt8], String]}` (second arg optional), will return /// `{publicKey: , signature: }` for success flow /// or an error dictionary for failures - @MainActor func signMessage(args: String?) async -> (Any?, String?) { + @MainActor func signMessage(tab: Tab, args: String?) async -> (Any?, String?) { guard let args = args, let argsList = MojoBase.Value(jsonString: args)?.listValue, let blobMsg = argsList.first?.numberArray, - let provider = tab?.walletSolProvider + let provider = tab.walletSolProvider else { return (nil, buildErrorJson(status: .invalidParams, errorMessage: "Invalid args")) } @@ -239,11 +232,10 @@ class SolanaProviderScriptHandler: TabContentScript { /// Given a request arg `{method: String, params: {}}`, will encode the response as a json object for success /// or provide an error dictionary for failures - @MainActor func request(args: String?) async -> (Any?, String?) { + @MainActor func request(tab: Tab, args: String?) async -> (Any?, String?) { guard let args = args, var argDict = MojoBase.Value(jsonString: args)?.dictionaryValue, let method = argDict[Keys.method.rawValue]?.stringValue, - let tab = tab, let provider = tab.walletSolProvider else { return (nil, buildErrorJson(status: .invalidParams, errorMessage: "Invalid args")) @@ -279,12 +271,12 @@ class SolanaProviderScriptHandler: TabContentScript { /// Given args `{serializedMessage: Buffer, signatures: {publicKey: String, signature: Buffer}}`, /// will encoded the response as a json object for success or provide an error dictionary for failures - @MainActor func signTransaction(args: String?) async -> (Any?, String?) { + @MainActor func signTransaction(tab: Tab, args: String?) async -> (Any?, String?) { guard let args = args, let arguments = MojoBase.Value(jsonString: args)?.dictionaryValue, let serializedMessage = arguments[Keys.serializedMessage.rawValue], let signatures = arguments[Keys.signatures.rawValue], - let provider = tab?.walletSolProvider + let provider = tab.walletSolProvider else { return (nil, buildErrorJson(status: .invalidParams, errorMessage: "Invalid args")) } @@ -311,10 +303,10 @@ class SolanaProviderScriptHandler: TabContentScript { /// Given args `[{serializedMessage: Buffer, signatures: {publicKey: String, signature: Buffer}}]`, /// will encoded the response as a json object for success or provide an error dictionary for failures - @MainActor func signAllTransactions(args: String?) async -> (Any?, String?) { + @MainActor func signAllTransactions(tab: Tab, args: String?) async -> (Any?, String?) { guard let args = args, let transactions = MojoBase.Value(jsonString: args)?.listValue, - let provider = tab?.walletSolProvider + let provider = tab.walletSolProvider else { return (nil, buildErrorJson(status: .invalidParams, errorMessage: "Invalid args")) } @@ -406,8 +398,8 @@ class SolanaProviderScriptHandler: TabContentScript { ) } - @MainActor private func emitConnectEvent(publicKey: String) async { - if let webView = tab?.webView { + @MainActor private func emitConnectEvent(tab: Tab, publicKey: String) async { + if let webView = tab.webView { let script = "window.solana.emit('connect', new \(UserScriptManager.walletSolanaNameSpace).solanaWeb3.PublicKey('\(publicKey.htmlEntityEncodedString)'))" await webView.evaluateSafeJavaScript( diff --git a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/URLPartinessScriptHandler.swift b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/URLPartinessScriptHandler.swift index f4573a77e3bb..8e289e97a8c3 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/URLPartinessScriptHandler.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/URLPartinessScriptHandler.swift @@ -29,15 +29,9 @@ class URLPartinessScriptHandler: TabContentScript { static let scriptSandbox: WKContentWorld = .defaultClient static let userScript: WKUserScript? = nil - private weak var tab: Tab? - - init(tab: Tab) { - self.tab = tab - } - - func userContentController( - _ userContentController: WKUserContentController, - didReceiveScriptMessage message: WKScriptMessage, + func tab( + _ tab: Tab, + receivedScriptMessage message: WKScriptMessage, replyHandler: @escaping (Any?, String?) -> Void ) { if !verifyMessage(message: message) { diff --git a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/YoutubeQualityScriptHandler.swift b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/YoutubeQualityScriptHandler.swift index cb2397c54718..1be268311bd0 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/YoutubeQualityScriptHandler.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/YoutubeQualityScriptHandler.swift @@ -10,12 +10,10 @@ import Shared import WebKit class YoutubeQualityScriptHandler: NSObject, TabContentScript { - private weak var tab: Tab? private var url: URL? private var urlObserver: NSObjectProtocol? init(tab: Tab) { - self.tab = tab self.url = tab.url super.init() @@ -78,10 +76,10 @@ class YoutubeQualityScriptHandler: NSObject, TabContentScript { ) } - 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.") diff --git a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/AdsMediaReportingScriptHandler.swift b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/AdsMediaReportingScriptHandler.swift index f89070b54ee6..d95ec5d6b5c5 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/AdsMediaReportingScriptHandler.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/AdsMediaReportingScriptHandler.swift @@ -10,11 +10,9 @@ import os.log class AdsMediaReportingScriptHandler: TabContentScript { let rewards: BraveRewards - weak var tab: Tab? - init(rewards: BraveRewards, tab: Tab) { + init(rewards: BraveRewards) { self.rewards = rewards - self.tab = tab } static let scriptName = "AdsMediaReportingScript" @@ -23,10 +21,10 @@ class AdsMediaReportingScriptHandler: TabContentScript { static let scriptSandbox: WKContentWorld = .defaultClient static let userScript: WKUserScript? = nil - func userContentController( - _ userContentController: WKUserContentController, - didReceiveScriptMessage message: WKScriptMessage, - replyHandler: (Any?, String?) -> Void + func tab( + _ tab: Tab, + receivedScriptMessage message: WKScriptMessage, + replyHandler: @escaping (Any?, String?) -> Void ) { defer { replyHandler(nil, nil) } @@ -40,7 +38,6 @@ class AdsMediaReportingScriptHandler: TabContentScript { } if let isPlaying = body["data"] as? Bool { - guard let tab = tab else { return } if isPlaying { rewards.reportMediaStarted(tabId: Int(tab.rewardsId)) } else { diff --git a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/BraveLeoScriptHandler.swift b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/BraveLeoScriptHandler.swift index c8d99dd72610..4cef76c38123 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/BraveLeoScriptHandler.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/BraveLeoScriptHandler.swift @@ -9,13 +9,6 @@ import WebKit import os.log class BraveLeoScriptHandler: NSObject, TabContentScript { - fileprivate weak var tab: Tab? - - init(tab: Tab) { - self.tab = tab - super.init() - } - static let getMainArticle = "getMainArticle\(uniqueID)" static let getPDFDocument = "getPDFDocument\(uniqueID)" @@ -44,10 +37,10 @@ class BraveLeoScriptHandler: NSObject, TabContentScript { ) }() - func userContentController( - _ userContentController: WKUserContentController, - didReceiveScriptMessage message: WKScriptMessage, - replyHandler: (Any?, String?) -> Void + func tab( + _ tab: Tab, + receivedScriptMessage message: WKScriptMessage, + replyHandler: @escaping (Any?, String?) -> Void ) { defer { replyHandler(nil, nil) } diff --git a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/BraveTranslateScriptHandler.swift b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/BraveTranslateScriptHandler.swift index 3d39571b1c39..c832b6be9a08 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/BraveTranslateScriptHandler.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/BraveTranslateScriptHandler.swift @@ -18,14 +18,8 @@ protocol BraveTranslateScriptHandlerDelegate: NSObject { } class BraveTranslateScriptHandler: NSObject, TabContentScript { - private weak var tab: Tab? private static var elementScriptTask: Task = downloadElementScript() - init(tab: Tab) { - self.tab = tab - super.init() - } - static let namespace = "translate_\(uniqueID)" static let scriptName = "BraveTranslateScript" static let scriptId = UUID().uuidString @@ -55,12 +49,11 @@ class BraveTranslateScriptHandler: NSObject, TabContentScript { ) }() - func userContentController( - _ userContentController: WKUserContentController, - didReceiveScriptMessage message: WKScriptMessage, + func tab( + _ tab: Tab, + receivedScriptMessage message: WKScriptMessage, replyHandler: @escaping (Any?, String?) -> Void ) { - // Setup guard let webView = message.webView else { @@ -171,26 +164,19 @@ class BraveTranslateScriptHandler: NSObject, TabContentScript { } class BraveTranslateScriptLanguageDetectionHandler: NSObject, TabContentScript { - private weak var tab: Tab? private static let namespace = "translate_\(uniqueID)" - init(tab: Tab) { - self.tab = tab - super.init() - } - static let scriptName = "BraveTranslateLanguageDetectionScript" static let scriptId = UUID().uuidString static let messageHandlerName = "LanguageDetectionTextCaptured" static let scriptSandbox = WKContentWorld.world(name: "BraveTranslateContentWorld") static let userScript: WKUserScript? = nil - func userContentController( - _ userContentController: WKUserContentController, - didReceiveScriptMessage message: WKScriptMessage, + func tab( + _ tab: Tab, + receivedScriptMessage message: WKScriptMessage, replyHandler: @escaping (Any?, String?) -> Void ) { - // In the future we'd get this from: components/language/ios/browser/language_detection_java_script_feature.mm guard let body = message.body as? [String: Any] else { @@ -199,7 +185,7 @@ class BraveTranslateScriptLanguageDetectionHandler: NSObject, TabContentScript { } guard - let translateHelper = tab?.translateHelper + let translateHelper = tab.translateHelper else { return } diff --git a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/CustomSearchScriptHandler.swift b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/CustomSearchScriptHandler.swift index b4379e26c608..438b70629e85 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/CustomSearchScriptHandler.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/CustomSearchScriptHandler.swift @@ -6,12 +6,6 @@ import Foundation import WebKit class CustomSearchScriptHandler: TabContentScript { - fileprivate weak var tab: Tab? - - required init(tab: Tab) { - self.tab = tab - } - static let scriptName = "CustomSearchHelper" static let scriptId = UUID().uuidString static let messageHandlerName = "\(scriptName)_\(messageUUID)" @@ -32,10 +26,10 @@ class CustomSearchScriptHandler: TabContentScript { ) }() - func userContentController( - _ userContentController: WKUserContentController, - didReceiveScriptMessage message: WKScriptMessage, - replyHandler: (Any?, String?) -> Void + func tab( + _ tab: Tab, + receivedScriptMessage message: WKScriptMessage, + replyHandler: @escaping (Any?, String?) -> Void ) { replyHandler(nil, nil) // We don't listen to messages because the BVC calls the searchHelper script by itself. diff --git a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/DarkReaderScriptHandler.swift b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/DarkReaderScriptHandler.swift index 4520331b6a14..f86e7cf38a19 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/DarkReaderScriptHandler.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/DarkReaderScriptHandler.swift @@ -9,9 +9,6 @@ import Shared import WebKit public class DarkReaderScriptHandler: TabContentScript { - - private weak var tab: Tab? - #if USE_NIGHTMODE_COLOURS private static let configuration = [ "brightness": 100, @@ -22,10 +19,6 @@ public class DarkReaderScriptHandler: TabContentScript { private static let configuration: [String: Int] = [:] #endif - init(tab: Tab) { - self.tab = tab - } - static let scriptName = "DarkReaderScript" static let scriptId = UUID().uuidString static let messageHandlerName = "\(scriptName)_\(messageUUID)" @@ -46,9 +39,9 @@ public class DarkReaderScriptHandler: TabContentScript { ) }() - func userContentController( - _ userContentController: WKUserContentController, - didReceiveScriptMessage message: WKScriptMessage, + func tab( + _ tab: Tab, + receivedScriptMessage message: WKScriptMessage, replyHandler: @escaping (Any?, String?) -> Void ) { // Do nothing. There's no message handling for Dark-Reader diff --git a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/DeAmpScriptHandler.swift b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/DeAmpScriptHandler.swift index 56827be23774..76434b4a9201 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/DeAmpScriptHandler.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/DeAmpScriptHandler.swift @@ -13,12 +13,6 @@ public class DeAmpScriptHandler: TabContentScript { let destURL: URL } - private weak var tab: Tab? - - init(tab: Tab) { - self.tab = tab - } - static let scriptName = "DeAmpScript" static let scriptId = UUID().uuidString static let messageHandlerName = "\(scriptName)_\(messageUUID)" @@ -39,9 +33,9 @@ public class DeAmpScriptHandler: TabContentScript { ) }() - func userContentController( - _ userContentController: WKUserContentController, - didReceiveScriptMessage message: WKScriptMessage, + func tab( + _ tab: Tab, + receivedScriptMessage message: WKScriptMessage, replyHandler: @escaping (Any?, String?) -> Void ) { if !verifyMessage(message: message) { @@ -59,7 +53,7 @@ public class DeAmpScriptHandler: TabContentScript { // Also check that our window url does not match the previously committed url // or that previousURL is nil which indicates as circular loop caused by a server side redirect let shouldRedirect = - dto.destURL != tab?.previousComittedURL && tab?.committedURL != tab?.previousComittedURL + dto.destURL != tab.previousComittedURL && tab.committedURL != tab.previousComittedURL replyHandler(shouldRedirect, nil) } catch { assertionFailure("Invalid type of message. Fix the `RequestBlocking.js` script") diff --git a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/DownloadContentScriptHandler.swift b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/DownloadContentScriptHandler.swift index 6c35b1c5b62c..21c78cc29403 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/DownloadContentScriptHandler.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/DownloadContentScriptHandler.swift @@ -18,13 +18,11 @@ private struct BlobDownloadInfo: Codable { } class DownloadContentScriptHandler: TabContentScript { - private weak var tab: Tab? private weak var browserViewController: BrowserViewController? private static var blobUrlForDownload: URL? - init(browserController: BrowserViewController, tab: Tab) { + init(browserController: BrowserViewController) { self.browserViewController = browserController - self.tab = tab } static let scriptName = "DownloadContentScript" @@ -47,9 +45,9 @@ class DownloadContentScriptHandler: TabContentScript { return true } - func userContentController( - _ userContentController: WKUserContentController, - didReceiveScriptMessage message: WKScriptMessage, + func tab( + _ tab: Tab, + receivedScriptMessage message: WKScriptMessage, replyHandler: @escaping (Any?, String?) -> Void ) { defer { replyHandler(nil, nil) } @@ -73,7 +71,6 @@ class DownloadContentScriptHandler: TabContentScript { } defer { - browserViewController?.pendingDownloadWebView = nil Self.blobUrlForDownload = nil } diff --git a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/FaviconScriptHandler.swift b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/FaviconScriptHandler.swift index 9c4d79d9dbc3..cb35d10a59bb 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/FaviconScriptHandler.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/FaviconScriptHandler.swift @@ -11,13 +11,6 @@ import WebKit import os.log class FaviconScriptHandler: NSObject, TabContentScript { - private weak var tab: Tab? - - init(tab: Tab) { - self.tab = tab - super.init() - } - static let scriptName = "FaviconScript" static let scriptId = UUID().uuidString static let messageHandlerName = "\(scriptName)_\(messageUUID)" @@ -39,13 +32,12 @@ class FaviconScriptHandler: NSObject, TabContentScript { ) }() - func userContentController( - _ userContentController: WKUserContentController, - didReceiveScriptMessage message: WKScriptMessage, - replyHandler: (Any?, String?) -> Void + func tab( + _ tab: Tab, + receivedScriptMessage message: WKScriptMessage, + replyHandler: @escaping (Any?, String?) -> Void ) { defer { replyHandler(nil, nil) } - guard let tab = tab else { return } Task { @MainActor in // Assign default favicon diff --git a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/FocusScriptHandler.swift b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/FocusScriptHandler.swift index 16c862e62856..654cedeff8ad 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/FocusScriptHandler.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/FocusScriptHandler.swift @@ -8,22 +8,16 @@ import WebKit import os.log class FocusScriptHandler: TabContentScript { - fileprivate weak var tab: Tab? - - init(tab: Tab) { - self.tab = tab - } - static let scriptName = "FocusScript" static let scriptId = UUID().uuidString static let messageHandlerName = "focusHelper" static let scriptSandbox: WKContentWorld = .defaultClient static let userScript: WKUserScript? = nil - func userContentController( - _ userContentController: WKUserContentController, - didReceiveScriptMessage message: WKScriptMessage, - replyHandler: (Any?, String?) -> Void + func tab( + _ tab: Tab, + receivedScriptMessage message: WKScriptMessage, + replyHandler: @escaping (Any?, String?) -> Void ) { defer { replyHandler(nil, nil) } @@ -48,9 +42,9 @@ class FocusScriptHandler: TabContentScript { switch eventType { case "focus": - tab?.isEditing = true + tab.isEditing = true case "blur": - tab?.isEditing = false + tab.isEditing = false default: return Logger.module.error("FocusHelper.js sent unhandled eventType") } diff --git a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/LoginsScriptHandler.swift b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/LoginsScriptHandler.swift index f78039b018b8..b4e40283bb6b 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/LoginsScriptHandler.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/LoginsScriptHandler.swift @@ -12,14 +12,12 @@ import WebKit import os.log class LoginsScriptHandler: TabContentScript { - private weak var tab: Tab? private let profile: Profile private let passwordAPI: BravePasswordAPI private var snackBar: SnackBar? - required init(tab: Tab, profile: Profile, passwordAPI: BravePasswordAPI) { - self.tab = tab + required init(profile: Profile, passwordAPI: BravePasswordAPI) { self.profile = profile self.passwordAPI = passwordAPI } @@ -30,10 +28,10 @@ class LoginsScriptHandler: TabContentScript { static let messageHandlerName = "loginsScriptMessageHandler" static let userScript: WKUserScript? = nil - func userContentController( - _ userContentController: WKUserContentController, - didReceiveScriptMessage message: WKScriptMessage, - replyHandler: (Any?, String?) -> Void + func tab( + _ tab: Tab, + receivedScriptMessage message: WKScriptMessage, + replyHandler: @escaping (Any?, String?) -> Void ) { defer { replyHandler(nil, nil) } if !verifyMessage(message: message, securityToken: UserScriptManager.securityToken) { @@ -71,19 +69,20 @@ class LoginsScriptHandler: TabContentScript { formSubmitURL: res["formSubmitURL"] as? String ?? "", logins: logins, requestId: requestId, - frameInfo: message.frameInfo + frameInfo: message.frameInfo, + tab: tab ) } } } else if type == "submit" { if Preferences.General.saveLogins.value { - updateORSaveCredentials(for: url, script: res) + updateORSaveCredentials(for: url, script: res, tab: tab) } } } } - private func updateORSaveCredentials(for url: URL, script: [String: Any]) { + private func updateORSaveCredentials(for url: URL, script: [String: Any], tab: Tab) { guard let scriptCredentials = passwordAPI.fetchFromScript(url, script: script), let username = scriptCredentials.usernameValue, scriptCredentials.usernameElement != nil, @@ -112,20 +111,20 @@ class LoginsScriptHandler: TabContentScript { return } - self.showUpdatePrompt(from: login, to: scriptCredentials) + self.showUpdatePrompt(from: login, to: scriptCredentials, tab: tab) return } else { - self.showAddPrompt(for: scriptCredentials) + self.showAddPrompt(for: scriptCredentials, tab: tab) return } } - self.showAddPrompt(for: scriptCredentials) + self.showAddPrompt(for: scriptCredentials, tab: tab) } } - private func showAddPrompt(for login: PasswordForm) { - addSnackBarForPrompt(for: login, isUpdating: false) { [weak self] in + private func showAddPrompt(for login: PasswordForm, tab: Tab) { + addSnackBarForPrompt(for: login, tab: tab, isUpdating: false) { [weak self] in guard let self = self else { return } DispatchQueue.main.async { @@ -134,8 +133,8 @@ class LoginsScriptHandler: TabContentScript { } } - private func showUpdatePrompt(from old: PasswordForm, to new: PasswordForm) { - addSnackBarForPrompt(for: new, isUpdating: true) { [weak self] in + private func showUpdatePrompt(from old: PasswordForm, to new: PasswordForm, tab: Tab) { + addSnackBarForPrompt(for: new, tab: tab, isUpdating: true) { [weak self] in guard let self = self else { return } self.passwordAPI.updateLogin(new, oldPasswordForm: old) @@ -144,6 +143,7 @@ class LoginsScriptHandler: TabContentScript { private func addSnackBarForPrompt( for login: PasswordForm, + tab: Tab, isUpdating: Bool, _ completion: @escaping () -> Void ) { @@ -153,7 +153,7 @@ class LoginsScriptHandler: TabContentScript { // Remove the existing prompt if let existingPrompt = self.snackBar { - tab?.removeSnackbar(existingPrompt) + tab.removeSnackbar(existingPrompt) } let promptMessage = String( @@ -174,7 +174,7 @@ class LoginsScriptHandler: TabContentScript { ? Strings.loginsHelperDontUpdateButtonTitle : Strings.loginsHelperDontSaveButtonTitle, accessibilityIdentifier: "UpdateLoginPrompt.dontSaveUpdateButton" ) { [unowned self] bar in - self.tab?.removeSnackbar(bar) + tab.removeSnackbar(bar) self.snackBar = nil return } @@ -184,7 +184,7 @@ class LoginsScriptHandler: TabContentScript { ? Strings.loginsHelperUpdateButtonTitle : Strings.loginsHelperSaveLoginButtonTitle, accessibilityIdentifier: "UpdateLoginPrompt.saveUpdateButton" ) { [unowned self] bar in - self.tab?.removeSnackbar(bar) + tab.removeSnackbar(bar) self.snackBar = nil completion() @@ -194,7 +194,7 @@ class LoginsScriptHandler: TabContentScript { snackBar?.addButton(saveORUpdate) if let bar = snackBar { - tab?.addSnackbar(bar) + tab.addSnackbar(bar) } } @@ -202,7 +202,8 @@ class LoginsScriptHandler: TabContentScript { formSubmitURL: String, logins: [PasswordForm], requestId: String, - frameInfo: WKFrameInfo + frameInfo: WKFrameInfo, + tab: Tab ) { let securityOrigin = frameInfo.securityOrigin @@ -216,7 +217,7 @@ class LoginsScriptHandler: TabContentScript { // Check for current tab has a url to begin with // and the frame is not modified - guard let currentURL = tab?.webView?.url, + guard let currentURL = tab.webView?.url, LoginsScriptHandler.checkIsSameFrame( url: currentURL, frameScheme: securityOrigin.protocol, @@ -244,7 +245,7 @@ class LoginsScriptHandler: TabContentScript { return } - self.tab?.webView?.evaluateSafeJavaScript( + tab.webView?.evaluateSafeJavaScript( functionName: "window.__firefox__.logins.inject", args: [jsonString], contentWorld: LoginsScriptHandler.scriptSandbox, diff --git a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/ReaderModeScriptHandler.swift b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/ReaderModeScriptHandler.swift index 06abb5700df8..ecab4d4d7c16 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/ReaderModeScriptHandler.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/ReaderModeScriptHandler.swift @@ -267,48 +267,35 @@ let readerModeNamespace = "window.__firefox__.reader" class ReaderModeScriptHandler: TabContentScript { weak var delegate: ReaderModeScriptHandlerDelegate? - fileprivate weak var tab: Tab? var state: ReaderModeState = ReaderModeState.unavailable fileprivate var originalURL: URL? - required init(tab: Tab) { - self.tab = tab - } - static let scriptName = "ReaderModeScript" static let scriptId = UUID().uuidString static let messageHandlerName = "readerModeMessageHandler" static let scriptSandbox: WKContentWorld = .defaultClient static let userScript: WKUserScript? = nil - fileprivate func handleReaderPageEvent(_ readerPageEvent: ReaderPageEvent) { + fileprivate func handleReaderPageEvent(_ readerPageEvent: ReaderPageEvent, tab: Tab) { switch readerPageEvent { case .pageShow: - if let tab = tab { - delegate?.readerMode(self, didDisplayReaderizedContentForTab: tab) - } + delegate?.readerMode(self, didDisplayReaderizedContentForTab: tab) } } - fileprivate func handleReaderModeStateChange(_ state: ReaderModeState) { + fileprivate func handleReaderModeStateChange(_ state: ReaderModeState, tab: Tab) { self.state = state - guard let tab = tab else { - return - } delegate?.readerMode(self, didChangeReaderModeState: state, forTab: tab) } - fileprivate func handleReaderContentParsed(_ readabilityResult: ReadabilityResult) { - guard let tab = tab else { - return - } + fileprivate func handleReaderContentParsed(_ readabilityResult: ReadabilityResult, tab: Tab) { delegate?.readerMode(self, didParseReadabilityResult: readabilityResult, forTab: tab) } - func userContentController( - _ userContentController: WKUserContentController, - didReceiveScriptMessage message: WKScriptMessage, - replyHandler: (Any?, String?) -> Void + func tab( + _ tab: Tab, + receivedScriptMessage message: WKScriptMessage, + replyHandler: @escaping (Any?, String?) -> Void ) { defer { replyHandler(nil, nil) } @@ -326,34 +313,32 @@ class ReaderModeScriptHandler: TabContentScript { switch messageType { case .pageEvent: if let readerPageEvent = ReaderPageEvent(rawValue: msg["Value"] as? String ?? "Invalid") { - handleReaderPageEvent(readerPageEvent) + handleReaderPageEvent(readerPageEvent, tab: tab) } case .stateChange: if let readerModeState = ReaderModeState(rawValue: msg["Value"] as? String ?? "Invalid") { - handleReaderModeStateChange(readerModeState) + handleReaderModeStateChange(readerModeState, tab: tab) } case .contentParsed: if let readabilityResult = ReadabilityResult(object: msg["Value"] as AnyObject?) { - handleReaderContentParsed(readabilityResult) + handleReaderContentParsed(readabilityResult, tab: tab) } else { - handleReaderModeStateChange(.unavailable) + handleReaderModeStateChange(.unavailable, tab: tab) } } } } } - var style: ReaderModeStyle = defaultReaderModeStyle { - didSet { - if state == ReaderModeState.active { - tab?.webView?.evaluateSafeJavaScript( - functionName: "\(readerModeNamespace).setStyle", - args: [style.encode()], - contentWorld: Self.scriptSandbox, - escapeArgs: false - ) { (object, error) -> Void in - return - } + func setStyle(_ style: ReaderModeStyle, in tab: Tab) { + if state == ReaderModeState.active { + tab.webView?.evaluateSafeJavaScript( + functionName: "\(readerModeNamespace).setStyle", + args: [style.encode()], + contentWorld: Self.scriptSandbox, + escapeArgs: false + ) { (object, error) -> Void in + return } } } diff --git a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/ResourceDownloadScriptHandler.swift b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/ResourceDownloadScriptHandler.swift index 51d6f58c8ec5..910b5dce8c4a 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/ResourceDownloadScriptHandler.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/ResourceDownloadScriptHandler.swift @@ -34,12 +34,6 @@ struct DownloadedResourceResponse: Decodable { } class ResourceDownloadScriptHandler: TabContentScript { - fileprivate weak var tab: Tab? - - init(tab: Tab) { - self.tab = tab - } - static let scriptName = "ResourceDownloaderScript" static let scriptId = UUID().uuidString static let messageHandlerName = "\(scriptName)_\(messageUUID)" @@ -61,17 +55,17 @@ class ResourceDownloadScriptHandler: TabContentScript { ) }() - func userContentController( - _ userContentController: WKUserContentController, - didReceiveScriptMessage message: WKScriptMessage, + func tab( + _ tab: Tab, + receivedScriptMessage message: WKScriptMessage, replyHandler: @escaping (Any?, String?) -> Void ) { Task { @MainActor in do { let response = try DownloadedResourceResponse.from(message: message) - await tab?.temporaryDocument?.onDocumentDownloaded(document: response, error: nil) + await tab.temporaryDocument?.onDocumentDownloaded(document: response, error: nil) } catch { - await tab?.temporaryDocument?.onDocumentDownloaded(document: nil, error: error) + await tab.temporaryDocument?.onDocumentDownloaded(document: nil, error: error) } replyHandler(nil, nil) } diff --git a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/SiteStateListenerScriptHandler.swift b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/SiteStateListenerScriptHandler.swift index 3c069add2179..41355afc0f64 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/SiteStateListenerScriptHandler.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/SiteStateListenerScriptHandler.swift @@ -19,12 +19,6 @@ class SiteStateListenerScriptHandler: TabContentScript { let data: MessageDTOData } - private weak var tab: Tab? - - init(tab: Tab) { - self.tab = tab - } - static let scriptName = "SiteStateListenerScript" static let scriptId = UUID().uuidString static let messageHandlerName = "\(scriptName)_\(messageUUID)" @@ -46,9 +40,9 @@ class SiteStateListenerScriptHandler: TabContentScript { ) }() - func userContentController( - _ userContentController: WKUserContentController, - didReceiveScriptMessage message: WKScriptMessage, + func tab( + _ tab: Tab, + receivedScriptMessage message: WKScriptMessage, replyHandler: @escaping (Any?, String?) -> Void ) { defer { replyHandler(nil, nil) } @@ -58,7 +52,7 @@ class SiteStateListenerScriptHandler: TabContentScript { return } - guard let tab = tab, let webView = tab.webView else { + guard let webView = tab.webView else { assertionFailure("Should have a tab set") return } diff --git a/ios/brave-ios/Tests/ClientTests/Helpers/DownloadHelperTests.swift b/ios/brave-ios/Tests/ClientTests/Helpers/DownloadHelperTests.swift index b6f96f0daeb2..8fb0647d1c6c 100644 --- a/ios/brave-ios/Tests/ClientTests/Helpers/DownloadHelperTests.swift +++ b/ios/brave-ios/Tests/ClientTests/Helpers/DownloadHelperTests.swift @@ -18,8 +18,7 @@ class DownloadHelperTests: XCTestCase { request: anyRequest(), response: response, cookieStore: cookieStore(), - canShowInWebView: true, - forceDownload: false + canShowInWebView: true ) XCTAssertNotNil(sut) @@ -27,8 +26,7 @@ class DownloadHelperTests: XCTestCase { request: anyRequest(), response: response, cookieStore: cookieStore(), - canShowInWebView: false, - forceDownload: true + canShowInWebView: false ) XCTAssertNotNil(sut) @@ -36,8 +34,7 @@ class DownloadHelperTests: XCTestCase { request: anyRequest(), response: response, cookieStore: cookieStore(), - canShowInWebView: false, - forceDownload: false + canShowInWebView: false ) XCTAssertNotNil(sut) @@ -45,8 +42,7 @@ class DownloadHelperTests: XCTestCase { request: anyRequest(), response: response, cookieStore: cookieStore(), - canShowInWebView: true, - forceDownload: true + canShowInWebView: true ) XCTAssertNotNil(sut) } @@ -61,8 +57,7 @@ class DownloadHelperTests: XCTestCase { request: anyRequest(), response: response, cookieStore: cookieStore(), - canShowInWebView: true, - forceDownload: false + canShowInWebView: true ) XCTAssertNil(sut) @@ -70,8 +65,7 @@ class DownloadHelperTests: XCTestCase { request: anyRequest(), response: response, cookieStore: cookieStore(), - canShowInWebView: false, - forceDownload: true + canShowInWebView: false ) XCTAssertNotNil(sut) @@ -79,17 +73,7 @@ class DownloadHelperTests: XCTestCase { request: anyRequest(), response: response, cookieStore: cookieStore(), - canShowInWebView: false, - forceDownload: false - ) - XCTAssertNotNil(sut) - - sut = DownloadHelper( - request: anyRequest(), - response: response, - cookieStore: cookieStore(), - canShowInWebView: true, - forceDownload: true + canShowInWebView: false ) XCTAssertNotNil(sut) } @@ -102,8 +86,7 @@ class DownloadHelperTests: XCTestCase { request: anyRequest(), response: response, cookieStore: cookieStore(), - canShowInWebView: true, - forceDownload: false + canShowInWebView: true ) XCTAssertNotNil(sut) @@ -111,8 +94,7 @@ class DownloadHelperTests: XCTestCase { request: anyRequest(), response: response, cookieStore: cookieStore(), - canShowInWebView: false, - forceDownload: true + canShowInWebView: false ) XCTAssertNotNil(sut) @@ -120,8 +102,7 @@ class DownloadHelperTests: XCTestCase { request: anyRequest(), response: response, cookieStore: cookieStore(), - canShowInWebView: true, - forceDownload: true + canShowInWebView: true ) XCTAssertNotNil(sut) @@ -129,8 +110,7 @@ class DownloadHelperTests: XCTestCase { request: anyRequest(), response: response, cookieStore: cookieStore(), - canShowInWebView: false, - forceDownload: false + canShowInWebView: false ) XCTAssertNotNil(sut) } @@ -141,8 +121,7 @@ class DownloadHelperTests: XCTestCase { request: request, response: anyResponse(mimeType: nil), cookieStore: cookieStore(), - canShowInWebView: true, - forceDownload: false + canShowInWebView: true ) let downloadAlert = sut?.downloadAlert(from: UIView(), okAction: { _ in }) @@ -156,8 +135,7 @@ class DownloadHelperTests: XCTestCase { request: request, response: anyResponse(mimeType: nil), cookieStore: cookieStore(), - canShowInWebView: true, - forceDownload: false + canShowInWebView: true ) let downloadAlert = sut?.downloadAlert(from: UIView(), okAction: { _ in }) @@ -171,8 +149,7 @@ class DownloadHelperTests: XCTestCase { request: request, response: anyResponse(mimeType: nil), cookieStore: cookieStore(), - canShowInWebView: true, - forceDownload: false + canShowInWebView: true ) let okActionIndex = 0 @@ -191,8 +168,7 @@ class DownloadHelperTests: XCTestCase { request: anyRequest(), response: anyResponse(mimeType: nil), cookieStore: cookieStore(), - canShowInWebView: true, - forceDownload: false + canShowInWebView: true ) let downloadAlert = sut?.downloadAlert(from: UIView(), okAction: { _ in }) diff --git a/ios/brave-ios/Tests/ClientTests/SolanaProviderScriptHandlerTests.swift b/ios/brave-ios/Tests/ClientTests/SolanaProviderScriptHandlerTests.swift index de46220b83d3..93d34600bd5e 100644 --- a/ios/brave-ios/Tests/ClientTests/SolanaProviderScriptHandlerTests.swift +++ b/ios/brave-ios/Tests/ClientTests/SolanaProviderScriptHandlerTests.swift @@ -20,10 +20,10 @@ import XCTest completion(.internalError, Strings.Wallet.internalErrorMessage, "") } let tab = Tab(configuration: .init()) - let solProviderHelper = SolanaProviderScriptHandler(tab: tab) + let solProviderHelper = SolanaProviderScriptHandler() tab.walletSolProvider = provider - let (publicKey, error) = await solProviderHelper.connect(args: nil) + let (publicKey, error) = await solProviderHelper.connect(tab: tab, args: nil) XCTAssertNil(publicKey) guard let error = error, // dictionary's are unordered, need to check each value let errorDict = MojoBase.Value(jsonString: error)?.dictionaryValue, @@ -44,10 +44,10 @@ import XCTest completion(.success, "", kTestPublicKey) } let tab = Tab(configuration: .init()) - let solProviderHelper = SolanaProviderScriptHandler(tab: tab) + let solProviderHelper = SolanaProviderScriptHandler() tab.walletSolProvider = provider - let (publicKey, error) = await solProviderHelper.connect(args: nil) + let (publicKey, error) = await solProviderHelper.connect(tab: tab, args: nil) XCTAssertNil(error) guard let publicKey = publicKey as? String else { XCTFail("Unexpected result for publickey") @@ -70,10 +70,10 @@ import XCTest completion(.success, "", kTestPublicKey) } let tab = Tab(configuration: .init()) - let solProviderHelper = SolanaProviderScriptHandler(tab: tab) + let solProviderHelper = SolanaProviderScriptHandler() tab.walletSolProvider = provider - let (publicKey, error) = await solProviderHelper.connect(args: args) + let (publicKey, error) = await solProviderHelper.connect(tab: tab, args: args) XCTAssertNil(error) guard let publicKey = publicKey as? String else { XCTFail("Unexpected result for publickey") @@ -101,10 +101,10 @@ import XCTest completion(.internalError, Strings.Wallet.internalErrorMessage, [:]) } let tab = Tab(configuration: .init()) - let solProviderHelper = SolanaProviderScriptHandler(tab: tab) + let solProviderHelper = SolanaProviderScriptHandler() tab.walletSolProvider = provider - let (result, error) = await solProviderHelper.signAndSendTransaction(args: args) + let (result, error) = await solProviderHelper.signAndSendTransaction(tab: tab, args: args) XCTAssertNil(result) guard let error = error, // dictionary's are unordered, need to check each value let errorDict = MojoBase.Value(jsonString: error)?.dictionaryValue, @@ -144,10 +144,10 @@ import XCTest ) } let tab = Tab(configuration: .init()) - let solProviderHelper = SolanaProviderScriptHandler(tab: tab) + let solProviderHelper = SolanaProviderScriptHandler() tab.walletSolProvider = provider - let (result, error) = await solProviderHelper.signAndSendTransaction(args: args) + let (result, error) = await solProviderHelper.signAndSendTransaction(tab: tab, args: args) XCTAssertNil(error) guard let result = result as? [String: String] else { XCTFail("Unexpected result to signAndSendTransaction request") @@ -186,10 +186,10 @@ import XCTest ) } let tab = Tab(configuration: .init()) - let solProviderHelper = SolanaProviderScriptHandler(tab: tab) + let solProviderHelper = SolanaProviderScriptHandler() tab.walletSolProvider = provider - let (result, error) = await solProviderHelper.signAndSendTransaction(args: args) + let (result, error) = await solProviderHelper.signAndSendTransaction(tab: tab, args: args) XCTAssertNil(error) guard let result = result as? [String: String] else { XCTFail("Unexpected result to signAndSendTransaction request") @@ -215,10 +215,10 @@ import XCTest completion(.internalError, Strings.Wallet.internalErrorMessage, [:]) } let tab = Tab(configuration: .init()) - let solProviderHelper = SolanaProviderScriptHandler(tab: tab) + let solProviderHelper = SolanaProviderScriptHandler() tab.walletSolProvider = provider - let (result, error) = await solProviderHelper.signMessage(args: args) + let (result, error) = await solProviderHelper.signMessage(tab: tab, args: args) XCTAssertNil(result) guard let error = error, // dictionary's are unordered, need to check each value let errorDict = MojoBase.Value(jsonString: error)?.dictionaryValue, @@ -255,10 +255,10 @@ import XCTest ) } let tab = Tab(configuration: .init()) - let solProviderHelper = SolanaProviderScriptHandler(tab: tab) + let solProviderHelper = SolanaProviderScriptHandler() tab.walletSolProvider = provider - let (result, error) = await solProviderHelper.signMessage(args: args) + let (result, error) = await solProviderHelper.signMessage(tab: tab, args: args) XCTAssertNil(error) guard let result = MojoBase.Value(jsonString: (result as? String) ?? "")?.dictionaryValue else { XCTFail("Unexpected result to signMessage request") @@ -300,10 +300,10 @@ import XCTest ) } let tab = Tab(configuration: .init()) - let solProviderHelper = SolanaProviderScriptHandler(tab: tab) + let solProviderHelper = SolanaProviderScriptHandler() tab.walletSolProvider = provider - let (result, error) = await solProviderHelper.signMessage(args: args) + let (result, error) = await solProviderHelper.signMessage(tab: tab, args: args) XCTAssertNil(error) guard let result = MojoBase.Value(jsonString: (result as? String) ?? "")?.dictionaryValue else { XCTFail("Unexpected result to signMessage request") @@ -341,10 +341,10 @@ import XCTest completion(.internalError, Strings.Wallet.internalErrorMessage, [], .legacy) } let tab = Tab(configuration: .init()) - let solProviderHelper = SolanaProviderScriptHandler(tab: tab) + let solProviderHelper = SolanaProviderScriptHandler() tab.walletSolProvider = provider - let (result, error) = await solProviderHelper.signTransaction(args: args) + let (result, error) = await solProviderHelper.signTransaction(tab: tab, args: args) XCTAssertNil(result) guard let error = error, // dictionary's are unordered, need to check each value let errorDict = MojoBase.Value(jsonString: error)?.dictionaryValue, @@ -380,10 +380,10 @@ import XCTest completion(.success, "", kSerializedTx.map(NSNumber.init(value:)), .legacy) } let tab = Tab(configuration: .init()) - let solProviderHelper = SolanaProviderScriptHandler(tab: tab) + let solProviderHelper = SolanaProviderScriptHandler() tab.walletSolProvider = provider - let (result, error) = await solProviderHelper.signTransaction(args: args) + let (result, error) = await solProviderHelper.signTransaction(tab: tab, args: args) XCTAssertNil(error) guard let result = result as? [String: Any] else { XCTFail("Unexpected result to signTransaction request") @@ -419,10 +419,10 @@ import XCTest completion(.success, "", kSerializedTx.map(NSNumber.init(value:)), .V0) } let tab = Tab(configuration: .init()) - let solProviderHelper = SolanaProviderScriptHandler(tab: tab) + let solProviderHelper = SolanaProviderScriptHandler() tab.walletSolProvider = provider - let (result, error) = await solProviderHelper.signTransaction(args: args) + let (result, error) = await solProviderHelper.signTransaction(tab: tab, args: args) XCTAssertNil(error) guard let result = result as? [String: Any] else { XCTFail("Unexpected result to signTransaction request") @@ -458,10 +458,10 @@ import XCTest completion(.internalError, Strings.Wallet.internalErrorMessage, [], []) } let tab = Tab(configuration: .init()) - let solProviderHelper = SolanaProviderScriptHandler(tab: tab) + let solProviderHelper = SolanaProviderScriptHandler() tab.walletSolProvider = provider - let (result, error) = await solProviderHelper.signAllTransactions(args: args) + let (result, error) = await solProviderHelper.signAllTransactions(tab: tab, args: args) XCTAssertNil(result) guard let error = error, // dictionary's are unordered, need to check each value let errorDict = MojoBase.Value(jsonString: error)?.dictionaryValue, @@ -502,10 +502,10 @@ import XCTest ) } let tab = Tab(configuration: .init()) - let solProviderHelper = SolanaProviderScriptHandler(tab: tab) + let solProviderHelper = SolanaProviderScriptHandler() tab.walletSolProvider = provider - let (result, error) = await solProviderHelper.signAllTransactions(args: args) + let (result, error) = await solProviderHelper.signAllTransactions(tab: tab, args: args) XCTAssertNil(error) guard let result = result as? [[String: Any]] else { XCTFail("Unexpected result to signAllTransactions request") @@ -548,10 +548,10 @@ import XCTest ) } let tab = Tab(configuration: .init()) - let solProviderHelper = SolanaProviderScriptHandler(tab: tab) + let solProviderHelper = SolanaProviderScriptHandler() tab.walletSolProvider = provider - let (result, error) = await solProviderHelper.signAllTransactions(args: args) + let (result, error) = await solProviderHelper.signAllTransactions(tab: tab, args: args) XCTAssertNil(error) guard let result = result as? [[String: Any]] else { XCTFail("Unexpected result to signAllTransactions request")