Skip to content

Commit

Permalink
[iOS] Move web view delegate logic into a shared components
Browse files Browse the repository at this point in the history
  • Loading branch information
kylehickinson committed Feb 20, 2025
1 parent 50c2e99 commit 3090a2c
Show file tree
Hide file tree
Showing 20 changed files with 2,108 additions and 2,202 deletions.
10 changes: 5 additions & 5 deletions ios/brave-ios/Sources/Brave/BraveSkus/BraveSkusAccountLink.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@ class BraveSkusAccountLink {
}

@MainActor
static func injectLocalStorage(webView: WKWebView) async {
static func injectLocalStorage(tab: Tab) async {
if let vpnSubscriptionProductId = Preferences.VPN.subscriptionProductId.value,
let product = BraveStoreProduct(rawValue: vpnSubscriptionProductId)
{
await BraveSkusAccountLink.injectLocalStorage(webView: webView, product: product)
await BraveSkusAccountLink.injectLocalStorage(tab: tab, product: product)
}

if let aiChatSubscriptionProductId = Preferences.AIChat.subscriptionProductId.value,
let product = BraveStoreProduct(rawValue: aiChatSubscriptionProductId)
{
await BraveSkusAccountLink.injectLocalStorage(webView: webView, product: product)
await BraveSkusAccountLink.injectLocalStorage(tab: tab, product: product)
}
}

Expand All @@ -63,11 +63,11 @@ class BraveSkusAccountLink {
/// - Parameter product: The product whose receipt information to inject
@MainActor
@discardableResult private static func injectLocalStorage(
webView: WKWebView,
tab: Tab,
product: BraveStoreProduct
) async -> Bool {
// The WebView has no URL so do nothing
guard let url = webView.url else {
guard let webView = tab.webView, let url = webView.url else {
return false
}

Expand Down
24 changes: 7 additions & 17 deletions ios/brave-ios/Sources/Brave/Frontend/Browser/BrowserPrompts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

import BraveCore
import Foundation
import Shared
import WebKit
Expand Down Expand Up @@ -92,13 +93,13 @@ protocol JSAlertInfo {

struct MessageAlert: JSAlertInfo {
let message: String
let frame: WKFrameInfo
let origin: URLOrigin
let completionHandler: () -> Void
var suppressHandler: SuppressHandler?

func alertController() -> JSPromptAlertController {
let alertController = JSPromptAlertController(
title: titleForJavaScriptPanelInitiatedByFrame(frame),
title: origin.serialized,
message: message,
info: self,
showCancel: false
Expand All @@ -119,14 +120,14 @@ struct MessageAlert: JSAlertInfo {

struct ConfirmPanelAlert: JSAlertInfo {
let message: String
let frame: WKFrameInfo
let origin: URLOrigin
let completionHandler: (Bool) -> Void
var suppressHandler: SuppressHandler?

func alertController() -> JSPromptAlertController {
// Show JavaScript confirm dialogs.
let alertController = JSPromptAlertController(
title: titleForJavaScriptPanelInitiatedByFrame(frame),
title: origin.serialized,
message: message,
info: self
)
Expand All @@ -146,14 +147,14 @@ struct ConfirmPanelAlert: JSAlertInfo {

struct TextInputAlert: JSAlertInfo {
let message: String
let frame: WKFrameInfo
let origin: URLOrigin
let completionHandler: (String?) -> Void
let defaultText: String?
var suppressHandler: SuppressHandler?

func alertController() -> JSPromptAlertController {
let alertController = JSPromptAlertController(
title: titleForJavaScriptPanelInitiatedByFrame(frame),
title: origin.serialized,
message: message,
info: self
)
Expand All @@ -176,17 +177,6 @@ struct TextInputAlert: JSAlertInfo {
}
}

/// Show a title for a JavaScript Panel (alert) based on the WKFrameInfo. On iOS9 we will use the new securityOrigin
/// and on iOS 8 we will fall back to the request URL. If the request URL is nil, which happens for JavaScript pages,
/// we fall back to "JavaScript" as a title.
private func titleForJavaScriptPanelInitiatedByFrame(_ frame: WKFrameInfo) -> String {
var title = "\(frame.securityOrigin.`protocol`)://\(frame.securityOrigin.host)"
if frame.securityOrigin.port != 0 {
title += ":\(frame.securityOrigin.port)"
}
return title
}

/// A generic browser alert that will execute a closure if its dismissed and no actions were picked
/// to allow us to call any neccessary completion handlers required by WebKit
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,7 @@ extension BrowserViewController {
activityType: .pageZoom,
callback: { [weak self] in
guard let self = self else { return }

self.displayPageZoom(visible: true)
self.displayPageZoomDialog()
}
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,15 @@ extension BrowserViewController: TabManagerDelegate {
}
}

displayPageZoom(visible: false)
clearPageZoomDialog()
updateTabsBarVisibility()
selected?.updatePullToRefreshVisibility()

topToolbar.locationView.loading = selected?.loading ?? false
updateBackForwardActionStatus(for: selected?.webView)
navigationToolbar.updateForwardStatus(selected?.canGoForward ?? false)
if let tab = selected {
topToolbar.locationView.loading = tab.loading
updateBackForwardActionStatus(for: tab)
navigationToolbar.updateForwardStatus(tab.canGoForward)
}

let shouldShowPlaylistURLBarButton = selected?.url?.isPlaylistSupportedSiteURL == true

Expand Down
Loading

0 comments on commit 3090a2c

Please sign in to comment.