Skip to content

Commit

Permalink
tweaks for duckplayer
Browse files Browse the repository at this point in the history
  • Loading branch information
Shane Osbourne committed Apr 22, 2023
1 parent 50464fb commit 5f16e6a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,23 +109,17 @@ public struct ContentScopePlatform: Encodable {
#endif
}

public final class ContentScopeUserScript: NSObject, UserScript {
public let messaging: UserScriptMessaging
public static let context = "contentScopeScripts"
public final class ContentScopeUserScript: NSObject, UserScript, UserScriptMessaging {

public func registerSubFeatureFor(name: String, delegate: UserScriptMessagingSubFeature) {
self.messaging.registerSubFeatureFor(name: name, delegate: delegate)
}
public let broker = UserScriptMessageBroker(context: "contentScopeScripts")

public let messageNames: [String] = [
ContentScopeUserScript.context
"contentScopeScripts"
]

public init(_ privacyConfigManager: PrivacyConfigurationManaging,
properties: ContentScopeProperties,
messaging: UserScriptMessaging = UserScriptMessaging(context: ContentScopeUserScript.context)
properties: ContentScopeProperties
) {
self.messaging = messaging
source = ContentScopeUserScript.generateSource(privacyConfigManager, properties: properties)
}

Expand All @@ -152,18 +146,16 @@ public final class ContentScopeUserScript: NSObject, UserScript {
public let injectionTime: WKUserScriptInjectionTime = .atDocumentStart
public let forMainFrameOnly: Bool = false
public let requiresRunInPageContentWorld: Bool = true

}

@available(macOS 11, *)
extension ContentScopeUserScript: WKScriptMessageHandlerWithReply {

public func userContentController(_ userContentController: WKUserContentController,
didReceive message: WKScriptMessage,
replyHandler: @escaping (Any?, String?) -> Void) {
let action = messaging.messageHandlerFor(message)
let action = broker.messageHandlerFor(message)
do {
try messaging.execute(action: action) { json in
try broker.execute(action: action, original: message) { json in
replyHandler(json, nil)
}
} catch let error {
Expand Down
23 changes: 18 additions & 5 deletions Sources/UserScript/UserScriptMessaging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,30 @@ public protocol UserScriptMessagingSubFeature {
/// }
/// }
/// ```
typealias Handler = (Any, @escaping MessageReplyHandler) throws -> Void
typealias Handler = (_ params: Any, _ originl: UserScriptMessage, _ replyHandler: @escaping MessageReplyHandler) throws -> Void

/// This gives a feature the opportunity to select it's own handler on a
/// call-by-call basis. The 'method' key is present on `RequestMessage` & `NotificationMessage`
func handlerFor(_ method: String) -> Handler?

/// This allows the feature to be selective about which domains/origins it accepts messages from
var allowedOrigins: AllowedOrigins { get }

///
var featureName: String { get }
}

public protocol UserScriptMessaging {
var broker: UserScriptMessageBroker { get }
}

extension UserScriptMessaging {
public func registerSubFeature(delegate: UserScriptMessagingSubFeature) {
broker.registerSubFeatureFor(name: delegate.featureName, delegate: delegate)
}
}

public final class UserScriptMessaging: NSObject, UserScriptMessageEncryption {
public final class UserScriptMessageBroker: NSObject, UserScriptMessageEncryption {

public let encrypter: UserScriptEncrypter
public let hostProvider: UserScriptHostProvider
Expand Down Expand Up @@ -150,14 +163,14 @@ public final class UserScriptMessaging: NSObject, UserScriptMessageEncryption {
}

/// Perform the side-effect described in an action
public func execute(action: Action, completion: @escaping (String) -> Void) throws {
public func execute(action: Action, original: UserScriptMessage, completion: @escaping (String) -> Void) throws {
switch action {
/// for `notify` we just need to execute the handler and continue
/// we **do not** forward any errors to the client
/// As far as the client is concerned, a `notification` is fire-and-forget
case .notify(let handler, let notification):
do {
try handler(notification.params) { _ in
try handler(notification.params, original) { _ in
// handler ignored
}
} catch let error {
Expand All @@ -172,7 +185,7 @@ public final class UserScriptMessaging: NSObject, UserScriptMessageEncryption {
/// Since that's how the Javascript side determines if the request was successful or not.
case .respond(let handler, let request):
do {
try handler(request.params) { encodableResponse in
try handler(request.params, original) { encodableResponse in
// handle an error
guard let result = encodableResponse else {
let resp = MessageErrorResponse.forRequest(request: request, error: "could not access encodable result").toJSON()
Expand Down
4 changes: 2 additions & 2 deletions Tests/UserScriptTests/UserScriptMessagingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,9 @@ class UserScriptMessagingTests: XCTestCase {
///
/// - Parameter message: The incoming message
///
func setupWith(message: [String: Any]) -> (UserScriptMessaging, UserScriptMessaging.Action) {
func setupWith(message: [String: Any]) -> (UserScriptMessageBroker, UserScriptMessageBroker.Action) {
// create the instance of ContentScopeMessaging
let testee = UserScriptMessaging(context: message["context"] as? String ?? "default")
let testee = UserScriptMessageBroker(context: message["context"] as? String ?? "default")

// register a feature for a given name
testee.registerSubFeatureFor(name: TestDelegate.featureName, delegate: TestDelegate())
Expand Down

0 comments on commit 5f16e6a

Please sign in to comment.