-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Communication channel from page scripts to the automation client #157
Comments
It feels like it should still be a platform object that implements an interface (even if that's a callback interface) rather than it just being a magic function. But yes, I agree the general plan there would work and avoids the need to figure out sandbox-specific IDL for now. |
Er not callback interface but https://webidl.spec.whatwg.org/#dfn-callback-function perhaps. |
At risk of bikeshedding, arguably the term "binding" is misleading in this model because it's not bound to anything really. It makes more sense to me to have the event called |
The Browser Testing and Tools Working Group just discussed The full IRC log of that discussion<AutomatedTester> topic: Communication channel from page scripts to the automation client.<AutomatedTester> github: https://github.com//issues/157 <AutomatedTester> sadym: The first topic wanted to discuss was around what James brought up around the comms channel <AutomatedTester> ... there is an example in the discussion linked <jgraham> q+ <AutomatedTester> ... [discusses the example] and James had some ideas that I liked <AutomatedTester> ack jgraham <AutomatedTester> jgraham: To put this into context, we discussed to have a sandbox value and [discusses generation of custom events] <AutomatedTester> ... this would an alternative to the other way as we can have a callback that is called <AutomatedTester> ... this will be simpler from spec prose as we don't have to get webidl to do things it doesnt' normally do <AutomatedTester> ... one concern that could be there is "what happens to scripts that are loaded on page load". It's not going to be impossible just going to be "fun" <AutomatedTester> ... I will write on the issue for deeper discussion <AutomatedTester> q? |
In the current proposal it looks like the binding (callback? port?) object on the local end allows one-way communication from the browser to the client only. If the client wants to send a message to a page script or sandbox script running in the browser, I suppose they can always use script.execute/script.callFunction but should we consider making the binding object bidirectional? Having a common object to send/receive messages in either direction does seem more elegant from a user perspective although it would complicate the API somewhat. |
Let's say the client creates a binding object using a local value as proposed below:
For other local value types, the client that creates them doesn't assign any identity to them so it doesn't need to care about cleanup. "Binding" values (or whatever we call them) would be a little different from these other local value types though, because the client must have some way to ensure the corresponding platform object in the javascript runtime is kept alive. So I think we'll want to keep a strong ref on any binding objects that are created and have a command that clients can use to free that ref when they are done. |
At least for one-way communication, I don't see the resource management problem. On the local (client) side you do something like (pseudocode): // In the background this registers a listener for `script.callback` events, and handles any that
// match the randomly generated id
let target = new RemoteEventTarget();
// target is serialized as {type: "callback", value: {"id": <some string e.g. a uuid>}}
remoteSandbox.executeScript(callback => {
/* This bit executes in the browser */
// Forward all messages to the client
document.addEventListener("message", event => callback(event.data), false);
}, [target]);
// Have some way to handle the actual message data
target.onmessge = handleRemoteMessage; So the browser and the client both manage their own objects, and the lifetimes aren't tied. |
Ah, my mistake. I guess resource management is less of a problem for one-way communication. If there are no more references to the browser object then obviously the browser can't send any more messages to the local (client) end and the local end doesn't need to do anything. I do think there should be a way for the local end to stop receiving messages though. CDP has Runtime.removeBinding to go with |
One issue that came up was how to handle the case of scripts that are configured to start automatically when a new context is created. We would have to have some way to provide the callback functions to those scripts, whereas with a |
I agree that a I think it makes sense to align both of these approaches and fire the same kind of event when either
For events generated by callback objects passed from the client, the I also realized that a command similar to |
The Browser Testing and Tools Working Group just discussed The full IRC log of that discussion<foolip> topic: Communication channel from the browser to the client<foolip> github: https://github.com//issues/157 <foolip> sadym: there are a few approaches here <foolip> sadym: we could make it bi-directional, or just one direction with a binding <foolip> sadym: bindings would trigger an event on the BiDi client side. <foolip> q? <jgraham> q+ <foolip> foolip: this question was previously tied up with sandbox scripts and communicating with them. with the bindings approach, will we talk to sandbox scripts and content scripts in the same way? <foolip> sadym: yes, and if the client wants to talk to multiple sandboxes they have to implement that. <jgraham> ack jgraham <foolip> foolip: I agree that this is a good starting point, with just a way to emit an event. <foolip> foolip: there are questions about how exactly bindings should work still <foolip> jgraham: an earlier suggestion was that sandboxes get a postMessage. the new suggestion is bindings which is a method you can invoke. that works in both sandbox and content scripts. <foolip> jgraham: an issue we discussed last time is that once we have bootstrap scripts, we won't have the chance to pass in that binding. then maybe we need to pass it in when we configure the page load script. or an interface that exists only for page load scripts. <foolip> jgraham: but Brandon's proposal in the GitHub issue was to make the protocol level bits look the same regardless of the API <foolip> jgraham: at the protocol layer we seem to have broad agree, possible apart from bikeshedding the name, <foolip> jgraham: there's a question about what it should look like in the API <foolip> foolip: what is API and protocol in this discussion? <foolip> jgraham: the API is what an injected script sees <foolip> sadym: scripts initiated by a sandbox are seen by the client too? (scribe didn't get it all) <foolip> jgraham: for bootstrap scripts, we could pass in a function to support arguments (which is how bindings are passed) <foolip> foolip: ok, so you'd pass in a function to establish the binding, similar to how you now do it post-load for regular scripts? <foolip> jgraham: yes <foolip> q? <foolip> foolip: there was previously discussion about what a binding looks like. Is a binding a function which if invoked does the magic to emit the event? <foolip> jgraham: yes <foolip> Agenda is here: https://www.w3.org/wiki/WebDriver/2022-01-BiDi |
#65 (comment) contains some content relevant to this. |
I just landed #361 so I think we can close this, and use different issues for any future work in this area. |
Extract the discussion from #144 (comment).
The text was updated successfully, but these errors were encountered: