2020-07-30 23:54:20 -07:00
|
|
|
//
|
|
|
|
|
// Tab.swift
|
|
|
|
|
// SBrowser
|
|
|
|
|
//
|
|
|
|
|
// Created by James Magahern on 7/29/20.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
|
import Combine
|
|
|
|
|
|
2021-02-15 22:47:02 -08:00
|
|
|
protocol TabDelegate: AnyObject
|
2020-07-30 23:54:20 -07:00
|
|
|
{
|
|
|
|
|
func didBlockScriptOrigin(_ origin: String, forTab: Tab)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Tab: NSObject, SBRProcessBundleBridgeDelegate
|
|
|
|
|
{
|
|
|
|
|
public weak var delegate: TabDelegate?
|
|
|
|
|
|
2022-08-05 15:13:37 -07:00
|
|
|
public var tabInfo: TabInfo {
|
|
|
|
|
get {
|
|
|
|
|
TabInfo(
|
|
|
|
|
title: loadedWebView?.title,
|
2022-08-05 18:55:19 -07:00
|
|
|
urlString: loadedWebView?.url?.absoluteString ?? self.homeURL?.absoluteString,
|
|
|
|
|
faviconData: self.favicon?.pngData(),
|
2022-08-05 15:13:37 -07:00
|
|
|
identifier: self.identifier
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-31 14:39:18 -07:00
|
|
|
public let homeURL: URL?
|
2021-02-15 22:34:05 -08:00
|
|
|
public let bridge: ProcessBundleBridge
|
2020-07-30 23:54:20 -07:00
|
|
|
public var webView: WKWebView {
|
|
|
|
|
if self.loadedWebView == nil {
|
|
|
|
|
self.loadedWebView = bridge.webView
|
2020-07-31 14:39:18 -07:00
|
|
|
|
|
|
|
|
if let homeURL = homeURL {
|
|
|
|
|
beginLoadingURL(homeURL)
|
|
|
|
|
}
|
2020-07-30 23:54:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return bridge.webView
|
|
|
|
|
}
|
|
|
|
|
public var policyManager: ResourcePolicyManager
|
|
|
|
|
|
|
|
|
|
private var loadedWebView: WKWebView? = nil
|
2022-08-05 15:13:37 -07:00
|
|
|
public var title: String? { get { tabInfo.title } }
|
2022-08-05 18:55:19 -07:00
|
|
|
public var url: URL? {
|
|
|
|
|
get {
|
|
|
|
|
if let urlString = tabInfo.urlString {
|
|
|
|
|
return URL(string: urlString)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-30 23:54:20 -07:00
|
|
|
|
|
|
|
|
public var javaScriptEnabled: Bool = false {
|
|
|
|
|
didSet { bridge.allowAllScripts = javaScriptEnabled }
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-11 17:54:22 -08:00
|
|
|
public lazy var colorTheme: [UIColor] = {
|
|
|
|
|
// Looks really cool as-is, but maybe compute a color based on the
|
|
|
|
|
// title or URL to make it show up the same for each website?
|
|
|
|
|
return [
|
|
|
|
|
UIColor(
|
|
|
|
|
hue: CGFloat.random(in: 0...1.0),
|
|
|
|
|
saturation: 0.89,
|
|
|
|
|
brightness: 0.3,
|
|
|
|
|
alpha: 1.0
|
|
|
|
|
),
|
|
|
|
|
UIColor(
|
|
|
|
|
hue: CGFloat.random(in: 0...1.0),
|
|
|
|
|
saturation: 1.0,
|
|
|
|
|
brightness: 0.7,
|
|
|
|
|
alpha: 1.0
|
|
|
|
|
),
|
|
|
|
|
]
|
|
|
|
|
}()
|
|
|
|
|
|
2020-07-30 23:54:20 -07:00
|
|
|
public var identifier = UUID()
|
2021-02-15 19:21:48 -08:00
|
|
|
@Published public var favicon: UIImage?
|
2020-07-30 23:54:20 -07:00
|
|
|
|
2021-05-06 16:42:04 -07:00
|
|
|
public var loadError: Error?
|
|
|
|
|
|
2020-07-30 23:54:20 -07:00
|
|
|
private var faviconHost: String?
|
|
|
|
|
private var faviconRequest: AnyCancellable?
|
|
|
|
|
|
|
|
|
|
public var allowedScriptOrigins = Set<String>()
|
|
|
|
|
public var blockedScriptOrigins = Set<String>()
|
|
|
|
|
|
2022-02-21 16:01:01 -08:00
|
|
|
public var titleObservation: NSKeyValueObservation?
|
|
|
|
|
public var urlObservation: NSKeyValueObservation?
|
|
|
|
|
public var faviconObservation: AnyCancellable?
|
2020-07-30 23:54:20 -07:00
|
|
|
|
2020-07-31 14:39:18 -07:00
|
|
|
convenience init(policyManager: ResourcePolicyManager) {
|
2020-09-24 16:36:31 -07:00
|
|
|
self.init(url: nil, policyManager: policyManager, webViewConfiguration: nil)
|
2020-07-31 14:39:18 -07:00
|
|
|
}
|
|
|
|
|
|
2020-07-30 23:54:20 -07:00
|
|
|
convenience init(urlString: String, policyManager: ResourcePolicyManager) {
|
2020-09-24 16:36:31 -07:00
|
|
|
self.init(url: URL(string: urlString), policyManager: policyManager, webViewConfiguration: nil)
|
2020-07-30 23:54:20 -07:00
|
|
|
}
|
|
|
|
|
|
2020-09-24 16:36:31 -07:00
|
|
|
init(url: URL?, policyManager: ResourcePolicyManager, webViewConfiguration: WKWebViewConfiguration?) {
|
2020-07-30 23:54:20 -07:00
|
|
|
self.homeURL = url
|
|
|
|
|
self.policyManager = policyManager
|
2020-09-24 16:36:31 -07:00
|
|
|
|
2021-02-15 22:34:05 -08:00
|
|
|
self.bridge = ProcessBundleBridge(webViewConfiguration: webViewConfiguration)
|
2020-09-24 16:36:31 -07:00
|
|
|
self.bridge.policyDataSource = policyManager
|
2020-07-30 23:54:20 -07:00
|
|
|
|
|
|
|
|
super.init()
|
|
|
|
|
|
|
|
|
|
bridge.delegate = self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
deinit {
|
|
|
|
|
bridge.tearDown()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func beginLoadingURL(_ url: URL) {
|
|
|
|
|
let request = URLRequest(url: url)
|
|
|
|
|
webView.load(request)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: SBRProcessBundleBridgeDelegate
|
|
|
|
|
|
2021-02-15 22:34:05 -08:00
|
|
|
func webProcess(_ bridge: ProcessBundleBridge, didAllowScriptResourceFromOrigin origin: String) {
|
2020-07-30 23:54:20 -07:00
|
|
|
print("Allowed script resource from origin: \(origin)")
|
|
|
|
|
allowedScriptOrigins.formUnion([ origin ])
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-15 22:34:05 -08:00
|
|
|
func webProcess(_ bridge: ProcessBundleBridge, didBlockScriptResourceFromOrigin origin: String) {
|
2020-07-30 23:54:20 -07:00
|
|
|
print("Blocked script resource from origin: \(origin)")
|
|
|
|
|
blockedScriptOrigins.formUnion([ origin ])
|
|
|
|
|
delegate?.didBlockScriptOrigin(origin, forTab: self)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func updateFaviconForURL(_ url: URL) {
|
|
|
|
|
if let faviconHost = faviconHost, url.host == faviconHost {} else {
|
|
|
|
|
guard var faviconURLComponents = URLComponents(url: url, resolvingAgainstBaseURL: false) else { return }
|
|
|
|
|
faviconURLComponents.path = "/favicon.ico"
|
|
|
|
|
|
|
|
|
|
let defaultImage = UIImage(systemName: "globe")
|
|
|
|
|
guard let faviconURL = faviconURLComponents.url else { return }
|
|
|
|
|
faviconRequest = URLSession.shared.dataTaskPublisher(for: faviconURL)
|
|
|
|
|
.map { (data: Data, response: URLResponse) -> UIImage? in
|
|
|
|
|
UIImage(data: data)
|
|
|
|
|
}
|
|
|
|
|
.replaceError(with: defaultImage)
|
|
|
|
|
.replaceNil(with: defaultImage)
|
|
|
|
|
.assign(to: \.favicon, on: self)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|