SPB Git

Repository Public License

ContentRuleLists.swift 3.0 KB 67 lines swift

1import Foundation2import WebKit34public enum ContentRuleListError: Error, Sendable, Hashable, CustomStringConvertible {5    /// WebKit rejected the JSON. `message` is WebKit's own explanation (it names the offending rule).6    case compileFailed(identifier: String, message: String)7    case notFound(identifier: String)8    case storeUnavailable(String)910    public var description: String {11        switch self {12        case .compileFailed(let identifier, let message): "Content rule list “\(identifier)” did not compile: \(message)"13        case .notFound(let identifier): "Content rule list “\(identifier)” is not in the store"14        case .storeUnavailable(let message): "Content rule list store unavailable: \(message)"15        }16    }17}1819/// Compiles rule lists (identifier + JSON in WebKit's content-blocker format) and keeps the compiled form on20/// disk. What the rules say belongs to PrivacyEngine; this type only talks to `WKContentRuleListStore`.21@MainActor22public final class ContentRuleListCompiler {23    private let store: WKContentRuleListStore2425    /// `directory`: where compiled lists are cached. `nil` uses WebKit's default store of the app.26    public init(directory: URL? = nil) throws(ContentRuleListError) {27        if let directory {28            guard let store = WKContentRuleListStore(url: directory) else {29                throw .storeUnavailable("cannot open \(directory.lastPathComponent)")30            }31            self.store = store32        } else {33            guard let store = WKContentRuleListStore.default() else { throw .storeUnavailable("no default store") }34            self.store = store35        }36    }3738    /// Compiles `json` under `identifier`, replacing a previous list with the same identifier.39    public func compile(identifier: String, json: String) async throws(ContentRuleListError) -> WKContentRuleList {40        do {41            guard let list = try await store.compileContentRuleList(forIdentifier: identifier, encodedContentRuleList: json) else {42                throw ContentRuleListError.compileFailed(identifier: identifier, message: "WebKit returned no list")43            }44            return list45        } catch let error as ContentRuleListError {46            throw error47        } catch {48            let nsError = error as NSError49            let detail = (nsError.userInfo[NSHelpAnchorErrorKey] as? String) ?? nsError.localizedDescription50            throw .compileFailed(identifier: identifier, message: detail)51        }52    }5354    /// A list compiled earlier, or `nil`.55    public func lookUp(identifier: String) async -> WKContentRuleList? {56        try? await store.contentRuleList(forIdentifier: identifier)57    }5859    public func remove(identifier: String) async throws(ContentRuleListError) {60        do { try await store.removeContentRuleList(forIdentifier: identifier) } catch {61            throw .notFound(identifier: identifier)62        }63    }6465    public func availableIdentifiers() async -> [String] { await store.availableIdentifiers() ?? [] }66}67
Files main
Menu

Theme