// // ScriptPolicyViewController.swift // SBrowser // // Created by James Magahern on 7/24/20. // import UIKit protocol ScriptPolicyViewControllerDelegate { 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) 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) 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 delegate: ScriptPolicyViewControllerDelegate? = nil var allowScriptsForTab = false private var dataSource: UICollectionViewDiffableDataSource? private var didChangeScriptPolicy = false private enum Section: Int { case tabOptions case origins } private static let enableScriptsForTabItem: String = "enableScriptsForTab" convenience init(policyManager: ResourcePolicyManager, hostOrigin: String, loadedScripts: Set) { self.init(nibName: nil, bundle: nil) 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 originItems = [ hostOrigin ] + otherOriginScripts let switchCellRegistry = UICollectionView.CellRegistration { (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 { snapshot.appendSections([ .origins ]) snapshot.appendItems(originItems, toSection: .origins) } self.dataSource?.apply(snapshot, animatingDifferences: true) } }), for: .valueChanged) } listCell.contentConfiguration = config } let scriptPolicyRegistry = UICollectionView.CellRegistration { (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(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, .origins ]) snapshot.appendItems([ Self.enableScriptsForTabItem ], toSection: .tabOptions) 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: { 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 } }