243 lines
9.1 KiB
Swift
243 lines
9.1 KiB
Swift
//
|
|
// ScriptPolicyViewController.swift
|
|
// SBrowser
|
|
//
|
|
// Created by James Magahern on 7/24/20.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
protocol ScriptPolicyViewControllerDelegate: class {
|
|
func didChangeScriptPolicy()
|
|
func setScriptsEnabledForTab(_ enabled: Bool)
|
|
}
|
|
|
|
class ScriptPolicyControlListCell: UICollectionViewListCell
|
|
{
|
|
var enabled: Bool = true {
|
|
didSet {
|
|
if enabled != oldValue {
|
|
setNeedsLayout()
|
|
}
|
|
}
|
|
}
|
|
let policyControl = ScriptPolicyControl()
|
|
override init(frame: CGRect) {
|
|
super.init(frame: frame)
|
|
addSubview(policyControl)
|
|
}
|
|
|
|
required init?(coder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
|
|
override func layoutSubviews() {
|
|
let policyControlWidth = CGFloat(80.0)
|
|
policyControl.frame = CGRect(
|
|
x: bounds.maxX - policyControlWidth - layoutMargins.right,
|
|
y: 0,
|
|
width: policyControlWidth,
|
|
height: bounds.height * 0.75
|
|
)
|
|
policyControl.frame = policyControl.frame.centeredY(inRect: bounds)
|
|
bringSubviewToFront(policyControl)
|
|
|
|
super.layoutSubviews()
|
|
|
|
if enabled {
|
|
contentView.alpha = 1.0
|
|
policyControl.alpha = 1.0
|
|
policyControl.isUserInteractionEnabled = true
|
|
} else {
|
|
contentView.alpha = 0.5
|
|
policyControl.alpha = 0.5
|
|
policyControl.isUserInteractionEnabled = false
|
|
}
|
|
}
|
|
}
|
|
|
|
class SwitchListCell: UICollectionViewListCell
|
|
{
|
|
let switchView = UISwitch(frame: .zero)
|
|
|
|
override init(frame: CGRect) {
|
|
super.init(frame: frame)
|
|
addSubview(switchView)
|
|
}
|
|
|
|
required init?(coder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
override func layoutSubviews() {
|
|
super.layoutSubviews()
|
|
|
|
let switchWidth: CGFloat = switchView.sizeThatFits(bounds.size).width
|
|
switchView.frame = CGRect(x: bounds.maxX - switchWidth - layoutMargins.right, y: 0,
|
|
width: switchWidth, height: bounds.height * 0.85)
|
|
switchView.frame = switchView.frame.centeredY(inRect: bounds)
|
|
contentView.frame = CGRect(origin: contentView.frame.origin, size: CGSize(width: switchView.frame.minX, height: contentView.frame.height))
|
|
}
|
|
}
|
|
|
|
class ScriptPolicyViewController: UIViewController, UICollectionViewDelegate
|
|
{
|
|
var collectionView: UICollectionView?
|
|
var allowScriptsForTab = false
|
|
weak var delegate: ScriptPolicyViewControllerDelegate? = nil
|
|
|
|
private var dataSource: UICollectionViewDiffableDataSource<Section, String>?
|
|
private var didChangeScriptPolicy = false
|
|
|
|
private enum Section: Int {
|
|
case tabOptions
|
|
case origins
|
|
}
|
|
|
|
override var traitCollection: UITraitCollection {
|
|
get {
|
|
let actualTraits = super.traitCollection
|
|
if actualTraits.userInterfaceIdiom == .mac {
|
|
// Override traits to be iPad like on mac. We dont want small list cells here.
|
|
let desiredTraits = UITraitCollection(userInterfaceIdiom: .pad)
|
|
return UITraitCollection(traitsFrom: [ actualTraits, desiredTraits ])
|
|
}
|
|
|
|
return actualTraits
|
|
}
|
|
}
|
|
|
|
private static let enableScriptsForTabItem: String = "enableScriptsForTab"
|
|
|
|
convenience init(policyManager: ResourcePolicyManager, hostOrigin: String, loadedScripts: Set<String>, scriptsAllowedForTab: Bool) {
|
|
self.init(nibName: nil, bundle: nil)
|
|
allowScriptsForTab = scriptsAllowedForTab
|
|
|
|
let listConfig = UICollectionLayoutListConfiguration(appearance: .grouped)
|
|
let listLayout = UICollectionViewCompositionalLayout.list(using: listConfig)
|
|
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: listLayout)
|
|
|
|
// Make sure host origin goes first in the list.
|
|
let otherOriginScripts = loadedScripts.subtracting([ hostOrigin ])
|
|
let allowedScripts = otherOriginScripts.filter { policyManager.allowedOriginsForScriptResources().contains($0) }
|
|
|
|
let originItems = [ hostOrigin ] + allowedScripts + otherOriginScripts.subtracting(allowedScripts)
|
|
|
|
let switchCellRegistry = UICollectionView.CellRegistration<SwitchListCell, String> { [unowned self] (listCell, indexPath, item) in
|
|
var config = listCell.defaultContentConfiguration()
|
|
if item == Self.enableScriptsForTabItem {
|
|
config.text = "Allow for Tab"
|
|
listCell.switchView.isOn = self.allowScriptsForTab
|
|
listCell.switchView.addAction(.init(handler: { _ in
|
|
let enabled = listCell.switchView.isOn
|
|
|
|
self.allowScriptsForTab = enabled
|
|
self.didChangeScriptPolicy = true
|
|
|
|
if var snapshot = self.dataSource?.snapshot() {
|
|
if enabled {
|
|
// Hide script origins
|
|
snapshot.deleteSections([ .origins ])
|
|
} else {
|
|
if !snapshot.sectionIdentifiers.contains(.origins) {
|
|
snapshot.appendSections([ .origins ])
|
|
}
|
|
snapshot.appendItems(originItems, toSection: .origins)
|
|
}
|
|
|
|
self.dataSource?.apply(snapshot, animatingDifferences: true)
|
|
}
|
|
}), for: .valueChanged)
|
|
}
|
|
|
|
listCell.contentConfiguration = config
|
|
}
|
|
|
|
let scriptPolicyRegistry = UICollectionView.CellRegistration<ScriptPolicyControlListCell, String> { [unowned self] (listCell, indexPath, item) in
|
|
var config = listCell.defaultContentConfiguration()
|
|
config.text = item
|
|
|
|
listCell.contentConfiguration = config
|
|
|
|
if policyManager.allowedOriginsForScriptResources().contains(item) {
|
|
listCell.policyControl.policyStatus = .allowed
|
|
} else {
|
|
listCell.policyControl.policyStatus = .blocked
|
|
}
|
|
|
|
listCell.policyControl.addAction(UIAction(handler: { _ in
|
|
let allowed: Bool = listCell.policyControl.policyStatus == .allowed
|
|
|
|
if allowed {
|
|
policyManager.allowOriginToLoadScriptResources(item)
|
|
} else {
|
|
policyManager.disallowOriginToLoadScriptResources(item)
|
|
}
|
|
|
|
if item == hostOrigin {
|
|
if var snapshot = self.dataSource?.snapshot() {
|
|
snapshot.reloadItems(Array(otherOriginScripts))
|
|
self.dataSource?.apply(snapshot, animatingDifferences: true)
|
|
}
|
|
}
|
|
|
|
self.didChangeScriptPolicy = true
|
|
}), for: .valueChanged)
|
|
|
|
if item != hostOrigin {
|
|
listCell.enabled = policyManager.allowedOriginsForScriptResources().contains(hostOrigin)
|
|
}
|
|
}
|
|
|
|
let dataSource = UICollectionViewDiffableDataSource<Section, String>(collectionView: collectionView) { (collectionView, indexPath, item) -> UICollectionViewCell? in
|
|
if item == Self.enableScriptsForTabItem {
|
|
return collectionView.dequeueConfiguredReusableCell(using: switchCellRegistry, for: indexPath, item: item)
|
|
}
|
|
|
|
return collectionView.dequeueConfiguredReusableCell(using: scriptPolicyRegistry, for: indexPath, item: item)
|
|
}
|
|
|
|
collectionView.dataSource = dataSource
|
|
collectionView.delegate = self
|
|
|
|
var snapshot = dataSource.snapshot()
|
|
snapshot.appendSections([ .tabOptions ])
|
|
snapshot.appendItems([ Self.enableScriptsForTabItem ], toSection: .tabOptions)
|
|
|
|
if !allowScriptsForTab {
|
|
snapshot.appendSections([ .origins ])
|
|
snapshot.appendItems(originItems, toSection: .origins)
|
|
}
|
|
|
|
dataSource.apply(snapshot)
|
|
|
|
self.dataSource = dataSource
|
|
self.collectionView = collectionView
|
|
|
|
title = "Script Origin Policy"
|
|
navigationItem.rightBarButtonItem = UIBarButtonItem(systemItem: .done, primaryAction: UIAction(handler: { [unowned self] action in
|
|
if self.didChangeScriptPolicy {
|
|
self.delegate?.didChangeScriptPolicy()
|
|
self.delegate?.setScriptsEnabledForTab(self.allowScriptsForTab)
|
|
}
|
|
|
|
self.dismiss(animated: true, completion: nil)
|
|
}), menu: nil)
|
|
}
|
|
|
|
override func loadView() {
|
|
self.view = collectionView
|
|
}
|
|
|
|
// MARK: UICollectionViewDelegate
|
|
|
|
func collectionView(_ collectionView: UICollectionView, shouldHighlightItemAt indexPath: IndexPath) -> Bool {
|
|
false
|
|
}
|
|
|
|
func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
|
|
false
|
|
}
|
|
}
|