Script origins control inline policy, all scripts allowed for tab

This commit is contained in:
James Magahern
2020-07-29 17:46:53 -07:00
parent f330293606
commit 32cdcf71f7
14 changed files with 301 additions and 57 deletions

View File

@@ -9,10 +9,18 @@ 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)
@@ -23,14 +31,46 @@ class ScriptPolicyControlListCell: UICollectionViewListCell
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 policyControlWidth = CGFloat(100.0)
policyControl.frame = CGRect(x: bounds.maxX - policyControlWidth, y: 0, width: policyControlWidth, height: bounds.height)
bringSubviewToFront(policyControl)
contentView.frame = CGRect(origin: contentView.frame.origin, size: CGSize(width: bounds.width - policyControl.frame.width, height: contentView.frame.height))
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))
}
}
@@ -38,18 +78,58 @@ class ScriptPolicyViewController: UIViewController, UICollectionViewDelegate
{
var collectionView: UICollectionView?
var delegate: ScriptPolicyViewControllerDelegate? = nil
var dataSource: UICollectionViewDiffableDataSource<Int, String>?
var allowScriptsForTab = false
private var dataSource: UICollectionViewDiffableDataSource<Section, String>?
private var didChangeScriptPolicy = false
convenience init(policyManager: ResourcePolicyManager, blockedScripts: Set<String>) {
private enum Section: Int {
case tabOptions
case origins
}
private static let enableScriptsForTabItem: String = "enableScriptsForTab"
convenience init(policyManager: ResourcePolicyManager, hostOrigin: String, loadedScripts: Set<String>) {
self.init(nibName: nil, bundle: nil)
let listConfig = UICollectionLayoutListConfiguration(appearance: .grouped)
let listLayout = UICollectionViewCompositionalLayout.list(using: listConfig)
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: listLayout)
let registry = UICollectionView.CellRegistration<ScriptPolicyControlListCell, String> { (listCell, indexPath, item) in
// Make sure host origin goes first in the list.
let otherOriginScripts = loadedScripts.subtracting([ hostOrigin ])
let originItems = [ hostOrigin ] + otherOriginScripts
let switchCellRegistry = UICollectionView.CellRegistration<SwitchListCell, String> { (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<ScriptPolicyControlListCell, String> { (listCell, indexPath, item) in
var config = listCell.defaultContentConfiguration()
config.text = item
@@ -62,26 +142,44 @@ class ScriptPolicyViewController: UIViewController, UICollectionViewDelegate
}
listCell.policyControl.addAction(UIAction(handler: { _ in
if listCell.policyControl.policyStatus == .allowed {
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<Int, String>(collectionView: collectionView) { (collectionView, indexPath, item) -> UICollectionViewCell? in
collectionView.dequeueConfiguredReusableCell(using: registry, for: indexPath, item: item)
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([ 0 ])
snapshot.appendItems(Array(blockedScripts))
snapshot.appendSections([ .tabOptions, .origins ])
snapshot.appendItems([ Self.enableScriptsForTabItem ], toSection: .tabOptions)
snapshot.appendItems(originItems, toSection: .origins)
dataSource.apply(snapshot)
self.dataSource = dataSource
@@ -91,6 +189,7 @@ class ScriptPolicyViewController: UIViewController, UICollectionViewDelegate
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)