Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Limit and fix communication between frames
The client relies on an inter-frame communication between the host frame, where the client was initially loaded, and a number of iframe children. For the communication to work, every frame needs to be able to discover the iframe that acts as a server by sending a `frame.postMessage`. Currently, the `sidebar` iframe is the server. The following frames must establish communication with the server `sidebar` iframe: - `host` frame (where the client was initially loaded) - `notebook` iframe - additional annotatable iframe(s) (each have an `enable-annotation` attribute) where the `guest` instance is injected. This layout represents the current arrangement of frames: ``` host frame (client) |-> sidebar iframe (server) |-> notebook iframe (client) |-> annotatable iframe/s (client) ``` There are two problems with the current discoverability algorithm: 1. It relies on `window.frames` to list all the other frames. Because `sidebar` and `notebook` iframes are now wrapped on a shadow DOM they are not listed on `window.frames`. 2. It is very generic: the algorithm starts from the topmost frame in the hierarchy (`window.top`) and send messages to all the frame children *recursively*. If there are several clients initialised on individual frames, this algorithm causes *all* the `host` frames to be connected to *all* the `sidebar` iframes. To solve both problems, I have swapped the server from the `sidebar` to the `host` frame. All iframe children are able to find the `host` frame (`window.parent`). In addition, this approach has the the advantage that the server is initialised first and ready to listen for message before any of the iframe children are created. This layout describes the changes introduced: ``` host frame (server) |-> sidebar iframe (client) |-> notebook iframe (client) |-> annotatable iframe/s (client) ``` This PR resolves both both problems mentioned earlier: 1. No dependent on `window.frames`: all children iframe have access to `window.parent` no matter if the iframe is from another origin. 2. Targeted discoverability: - if the sender is the `sidebar` or `notebook` iframes, the postMessage is *only* sent to the `host` frame (server) using `window.parent`. - if the sender is an `annotatable` iframe(s), we find the closest parent that is not an `annotatable` iframe and send a postMessage. We assume that parent to be the `host` frame (server).
- Loading branch information