* @param {!Element} iframe The iframe. * @param {string} type Type of the subscription message. * @param {boolean} is3p set to true if the iframe is 3p. * @param {function(!JsonObject, !Window, string)} requestCallback Callback * invoked whenever a new window subscribes.
(iframe, type, is3p, requestCallback)
| 441 | * invoked whenever a new window subscribes. |
| 442 | */ |
| 443 | constructor(iframe, type, is3p, requestCallback) { |
| 444 | /** @private @const {!Element} */ |
| 445 | this.iframe_ = iframe; |
| 446 | /** @private @const {boolean} */ |
| 447 | this.is3p_ = is3p; |
| 448 | /** @private @const {!Array<{win: !Window, origin: string}>} */ |
| 449 | this.clientWindows_ = []; |
| 450 | |
| 451 | /** @private @const {!UnlistenDef} */ |
| 452 | this.unlisten_ = listenFor( |
| 453 | this.iframe_, |
| 454 | type, |
| 455 | (data, source, origin) => { |
| 456 | // This message might be from any window within the iframe, we need |
| 457 | // to keep track of which windows want to be sent updates. |
| 458 | if (!this.clientWindows_.some((entry) => entry.win == source)) { |
| 459 | this.clientWindows_.push({win: source, origin}); |
| 460 | } |
| 461 | requestCallback(data, source, origin); |
| 462 | }, |
| 463 | this.is3p_, |
| 464 | // For 3P frames we also allow nested frames within them to subscribe.. |
| 465 | this.is3p_ /* opt_includingNestedWindows */ |
| 466 | ); |
| 467 | } |
| 468 | |
| 469 | /** |
| 470 | * Sends a message to all subscribed windows. |