Files
orca/mobile/modules/orca-mobile-web-shell/ios/MobileWebShellNavigationPolicy.swift
Kelvin Amoaba f375aba997 fix(mobile): make chat report buttons work in an isolated frame (#42954)
* fix(mobile): run chat report scripts in an isolated shell frame

* fix(mobile): theme chat visuals at handoff and pin the iOS frame to Android's

* test(mobile): probe the page origin by absolute URL from the visual
2026-11-16 06:12:04 +00:00

55 lines
2.3 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import Foundation
/// Keep allow/refuse/opener routing in one decision shared across platforms.
enum MobileWebShellNavigationVerdict: Equatable {
/// The served document, or a srcdoc or chat visual frame inside it, loading itself.
case allow
/// Refused, and the host is told nothing. Every navigation was this before the preview existed.
case cancel
/// Refused, and the URL is handed to the host's opener, which decides what may open.
case cancelAndOffer(String)
}
/// Framework-free navigation policy, checked off-device and kept in sync with Kotlin.
enum MobileWebShellNavigationPolicy {
/// Cap native-to-JS URL traffic; TypeScript’s readBridgeExternalLinkUrl owns scheme filtering and its smaller 2048-character limit.
static let maxCancelledNavigationUrlCharacters = 4096
/// The URL a frame's srcdoc document loads under; it carries the embedder's CSP and the frame's sandbox.
static let inlineDocumentUrl = "about:srcdoc"
/// Allow only srcdoc subframes (sealed HTML previews, chat visuals) and the chat visual frame; allow the document only with the one-shot shell-load flag, spent at decision.
/// Other document URLs (including downloads) are never offered to the opener; gestured foreign URLs may be.
/// Gestures cannot authorize loads: sandboxed top navigation can report .other without one.
/// Ignore sourceFrame: WebKit before iOS 18.5 omits it for our own load (c28b242).
/// TypeScript’s readBridgeExternalLinkUrl owns scheme filtering so OTA policy has one source.
static func verdict(
url: String?,
isMainFrame: Bool,
isDocumentUrl: Bool,
isChatVisualFrameUrl: Bool,
isShellLoad: Bool,
hasGesture: Bool,
isDownload: Bool
) -> MobileWebShellNavigationVerdict {
guard isMainFrame else {
return (url == inlineDocumentUrl || isChatVisualFrameUrl) && !isDownload ? .allow : .cancel
}
if isDocumentUrl {
return isShellLoad && !isDownload ? .allow : .cancel
}
guard hasGesture, let offered = offerableUrl(url) else {
return .cancel
}
return .cancelAndOffer(offered)
}
/// The URL a cancelled navigation may be offered under, or nil when nothing crosses.
static func offerableUrl(_ url: String?) -> String? {
guard let url, !url.isEmpty, url.count <= maxCancelledNavigationUrlCharacters else {
return nil
}
return url
}
}