You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Implement channel messaging (MessageChannel and MessagePort) #7457
A-content/domInteracting with the DOM from web contentB-interesting-projectRepresents work that is expected to be interesting in some fashionE-very-complexVery difficult. Do not attempt without significant relevant experience and motivation.
@ConnorGBrewster This is something I would want to have significant upfront planning for before having someone implement it. I started a branch earlier this week for experimenting and discovered that the specification is significantly more intricate than I anticipated, making the implementation much more complex than I originally expected.
The biggest issue is that the queued messages need to be able to be transferred between script event loops if the associated message port is transferred, as this means that postMessage can't just send a message to the current target script thread. Implementing the spec's concept of an additional task source per message port also conveniently map to our use of Select in ScriptThread, which can't deal with variable message sources.
@jdm ok that makes sense. I currently just have skeletons setup for the MessageChannel and MessagePort DOM structs with the WebIDL bindings. I haven't done any actual implementation yet, I will hold off on that until a plan is created.
I was reading the spec for this, and I feel like we may have chalked up the difficulty a bit too much on this one. I have in mind a preliminary version that may work and it goes like this:
We create and implement a new TaskSource called PortMessageQueue and make a new method in GlobalScope to return it. (Maybe have this TaskSource store the has-been-shipped flag and disabled flag as well?)
Code in the MessagePort DOM APIs and fields according to its WebIDL file, and add in a field called entangled_port which is of type DOMRefCell<Option<JS<MessagePort>>>
Create a new Runnable called PortMessageRunnable. This will be responsible for firing the event at the target MessagePort.
The entanglement operation would simply be accepting a new MessagePort object and creating a new Rc pointing to it, and then assigning this new Rc to the other MessagePort object's entangled_port field. postMessage would use the entangled_port field to get the GlobalScope for the entangled MessagePort, and transitively the PortMessageQueue. At this point, we now just need to create the MessageEvent and then queue it.
Am I missing something here? I'm going to try and code this in and see if there's anything that I'm missing.
Ah, of course I forgot -- the case where you need to transfer the MessagePort. In Gecko, they store an enum that represents the state of the MessagePort (i.e. whether it is being entangled or disentangled, or if it is closing, etc.). We could check for the state of the MessagePort in the methods postMessage and start, and when the port is in the disentangled or disentangling state, abort the algorithms completely, since they would be handled in a special way during transfer. Does that make sense?
In short, most of the complexities dealing with transferring tasks should occur in the transfer method.
That still doesn't address the issue where PortMessageRunnable are not supposed to be dropped when there is no corresponding PortMessageQueue. Perhaps we'll need to have the constellation store them temporarily, until the corresponding ScriptThread enables its PortMessageQueue. This would mean each individual Runnable will need to have a FrameId and registered domain name of its destination ScriptThread.
But if we're going down this route, we might as well let the constellation be the first point of contact for all PortMessageRunnables, so instead of sending them directly to the associated PortMessageQueue, we'd send them to the constellation and let it handle the arbitration. This would eliminate to have associated states with MessagePorts - all of that logic would be handled properly by the constellation.
A-content/domInteracting with the DOM from web contentB-interesting-projectRepresents work that is expected to be interesting in some fashionE-very-complexVery difficult. Do not attempt without significant relevant experience and motivation.
Activity
cbrewster commentedon Apr 7, 2016
I am working on this one
jdm commentedon Apr 7, 2016
@ConnorGBrewster This is something I would want to have significant upfront planning for before having someone implement it. I started a branch earlier this week for experimenting and discovered that the specification is significantly more intricate than I anticipated, making the implementation much more complex than I originally expected.
The biggest issue is that the queued messages need to be able to be transferred between script event loops if the associated message port is transferred, as this means that
postMessagecan't just send a message to the current target script thread. Implementing the spec's concept of an additional task source per message port also conveniently map to our use ofSelectinScriptThread, which can't deal with variable message sources.cbrewster commentedon Apr 7, 2016
@jdm ok that makes sense. I currently just have skeletons setup for the
MessageChannelandMessagePortDOM structs with the WebIDL bindings. I haven't done any actual implementation yet, I will hold off on that until a plan is created.KiChjang commentedon Dec 11, 2016
I was reading the spec for this, and I feel like we may have chalked up the difficulty a bit too much on this one. I have in mind a preliminary version that may work and it goes like this:
TaskSourcecalledPortMessageQueueand make a new method inGlobalScopeto return it. (Maybe have this TaskSource store the has-been-shipped flag and disabled flag as well?)MessagePortDOM APIs and fields according to its WebIDL file, and add in a field calledentangled_portwhich is of typeDOMRefCell<Option<JS<MessagePort>>>RunnablecalledPortMessageRunnable. This will be responsible for firing the event at the target MessagePort.The entanglement operation would simply be accepting a new MessagePort object and creating a new Rc pointing to it, and then assigning this new Rc to the other MessagePort object's entangled_port field.
postMessagewould use theentangled_portfield to get the GlobalScope for the entangledMessagePort, and transitively thePortMessageQueue. At this point, we now just need to create theMessageEventand then queue it.Am I missing something here? I'm going to try and code this in and see if there's anything that I'm missing.
KiChjang commentedon Dec 11, 2016
Ah, of course I forgot -- the case where you need to transfer the
MessagePort. In Gecko, they store an enum that represents the state of theMessagePort(i.e. whether it is being entangled or disentangled, or if it is closing, etc.). We could check for the state of theMessagePortin the methodspostMessageandstart, and when the port is in the disentangled or disentangling state, abort the algorithms completely, since they would be handled in a special way during transfer. Does that make sense?In short, most of the complexities dealing with transferring tasks should occur in the transfer method.
KiChjang commentedon Dec 12, 2016
That still doesn't address the issue where PortMessageRunnable are not supposed to be dropped when there is no corresponding PortMessageQueue. Perhaps we'll need to have the constellation store them temporarily, until the corresponding ScriptThread enables its PortMessageQueue. This would mean each individual Runnable will need to have a FrameId and registered domain name of its destination ScriptThread.
But if we're going down this route, we might as well let the constellation be the first point of contact for all PortMessageRunnables, so instead of sending them directly to the associated PortMessageQueue, we'd send them to the constellation and let it handle the arbitration. This would eliminate to have associated states with MessagePorts - all of that logic would be handled properly by the constellation.
Auto merge of #16622 - KiChjang:message-port, r=<try>
Auto merge of #16622 - KiChjang:message-port, r=<try>
Auto merge of #16622 - KiChjang:message-port, r=<try>
Auto merge of #16622 - KiChjang:message-port, r=<try>
Auto merge of #16622 - KiChjang:message-port, r=<try>
15 remaining items
Auto merge of #23637 - gterzian:continue-message-port, r=<try>
Auto merge of #23637 - gterzian:continue-message-port, r=<try>
Auto merge of #23637 - gterzian:continue-message-port, r=<try>
Auto merge of #23637 - gterzian:continue-message-port, r=<try>
Auto merge of #23637 - gterzian:continue-message-port, r=<try>
Auto merge of #23637 - gterzian:continue-message-port, r=jdm
Auto merge of #23637 - gterzian:continue-message-port, r=jdm
Auto merge of #23637 - gterzian:continue-message-port, r=jdm
Auto merge of #23637 - gterzian:continue-message-port, r=jdm
Auto merge of #23637 - gterzian:continue-message-port, r=jdm
Auto merge of #23637 - gterzian:continue-message-port, r=jdm
Auto merge of #23637 - gterzian:continue-message-port, r=jdm
Auto merge of #23637 - gterzian:continue-message-port, r=jdm
Auto merge of #23637 - gterzian:continue-message-port, r=jdm