Files
Attractor/App/Script Policy UI/ScriptPolicyViewController.swift

243 lines
9.2 KiB
Swift
Raw Normal View History

2020-07-24 19:26:35 -07:00
//
// ScriptPolicyViewController.swift
// SBrowser
//
// Created by James Magahern on 7/24/20.
//
import UIKit
protocol ScriptPolicyViewControllerDelegate: class {
2020-07-24 19:26:35 -07:00
func didChangeScriptPolicy()
func setScriptsEnabledForTab(_ enabled: Bool)
2020-07-24 19:26:35 -07:00
}
class ScriptPolicyControlListCell: UICollectionViewListCell
{
var enabled: Bool = true {
didSet {
if enabled != oldValue {
setNeedsLayout()
}
}
}
2020-07-24 19:26:35 -07:00
let policyControl = ScriptPolicyControl()
override init(frame: CGRect) {
super.init(frame: frame)
addSubview(policyControl)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
2020-07-24 19:26:35 -07:00
override func layoutSubviews() {
2020-08-14 18:47:11 -07:00
let policyControlWidth = policyControl.sizeThatFits(bounds.size).width
2020-08-14 17:40:01 -07:00
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)
2020-07-24 19:26:35 -07:00
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()
2020-07-24 19:26:35 -07:00
let switchWidth: CGFloat = switchView.sizeThatFits(bounds.size).width
2020-08-14 17:40:01 -07:00
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))
2020-07-24 19:26:35 -07:00
}
}
class ScriptPolicyViewController: UIViewController, UICollectionViewDelegate
{
var collectionView: UICollectionView?
var allowScriptsForTab = false
weak var delegate: ScriptPolicyViewControllerDelegate? = nil
2020-07-24 19:26:35 -07:00
private var dataSource: UICollectionViewDiffableDataSource<Section, String>?
2020-07-24 19:26:35 -07:00
private var didChangeScriptPolicy = false
private enum Section: Int {
case tabOptions
case origins
}
2020-08-14 17:40:01 -07:00
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) {
2020-07-24 19:26:35 -07:00
self.init(nibName: nil, bundle: nil)
allowScriptsForTab = scriptsAllowedForTab
2020-07-24 19:26:35 -07:00
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.
2020-08-14 17:40:01 -07:00
let otherOriginScripts = loadedScripts.subtracting([ hostOrigin ])
let allowedScripts = otherOriginScripts.filter { policyManager.allowedOriginsForScriptResources().contains($0) }
2020-08-14 16:44:39 -07:00
2020-08-14 17:40:01 -07:00
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
2020-07-24 19:26:35 -07:00
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 {
2020-07-24 19:26:35 -07:00
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)
}
}
2020-07-24 19:26:35 -07:00
self.didChangeScriptPolicy = true
}), for: .valueChanged)
if item != hostOrigin {
listCell.enabled = policyManager.allowedOriginsForScriptResources().contains(hostOrigin)
}
2020-07-24 19:26:35 -07:00
}
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)
2020-07-24 19:26:35 -07:00
}
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)
}
2020-07-24 19:26:35 -07:00
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
2020-07-24 19:26:35 -07:00
if self.didChangeScriptPolicy {
self.delegate?.didChangeScriptPolicy()
self.delegate?.setScriptsEnabledForTab(self.allowScriptsForTab)
2020-07-24 19:26:35 -07:00
}
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
}
}