• Robert Knight's avatar
    Convert host <-> guest communication to use MessageChannel · 036fb8c6
    Robert Knight authored
    This commit implements the initial transition of communication between
    guest/host frames and the sidebar to use `MessageChannel` as the
    underlying transport instead of `window.postMessage`, building on
    previous changes to the `Bridge` and `RPC` classes.
    
    This change aligns with an ongoing to plan to use `MessageChannel`
    instead of `window.postMessage`, and also resolves an issue where guest
    frames may fail to connect to the sidebar if the annotator code loads in
    the guest before the sidebar application has finished loading.
    
    This initial version has several limitations compared to what is planned
    for the final version of the new inter-frame communication system:
    
     - It only supports guest frames which are the same frame as the host or
       a direct, same-origin child.
    
     - Sidebar <-> host communication relies on the host frame also being a
       guest frame. In order for the host frame to receive messages from the
       sidebar, it must run the logic to establish sidebar <-> guest communication.
       In other words, it is not possible to have a host frame which cannot
       also be annotated.
    
     - The only supported roles for frames are sidebar and host/guest. There
       is no separate role for notebook frames for use in notebook <->
       sidebar communication.
    
    Making this change involved replacing the protocol used by guest frames
    and the sidebar to discover each other. The new one is simpler than the
    previous one and works as follows:
    
    1. When the sidebar application starts up it notifies the parent frame that
       it is ready to connect to guests via a `hypothesisSidebarReady`
       message.
    
    2. Guest frames wait for this notification to be received before
       connecting to the sidebar. When they connect, they create a
       `MessageChannel` and send one port to the sidebar via a
       `hypothesisGuestReady` message and use the other port locally.
    
    3. When the sidebar receives a `hypothesisGuestReady` message it creates
       a channel to communicate with the guest.
    036fb8c6
annotator.js 5.9 KB