1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 import { WebChannelBroker } from "resource://gre/modules/WebChannel.sys.mjs";
8 const ERRNO_MISSING_PRINCIPAL = 1;
9 const ERRNO_NO_SUCH_CHANNEL = 2;
11 export class WebChannelParent extends JSWindowActorParent {
13 let data = msg.data.contentData;
14 let sendingContext = {
15 browsingContext: this.browsingContext,
16 browser: this.browsingContext.top.embedderElement,
17 eventTarget: msg.data.eventTarget,
18 principal: msg.data.principal,
20 // data must be a string except for a few legacy origins allowed by browser-content.js.
21 if (typeof data == "string") {
23 data = JSON.parse(data);
25 console.error("Failed to parse WebChannel data as a JSON object");
30 if (data && data.id) {
31 if (!msg.data.principal) {
32 this._sendErrorEventToContent(
35 ERRNO_MISSING_PRINCIPAL,
36 "Message principal missing"
39 let validChannelFound = WebChannelBroker.tryToDeliver(
44 // if no valid origins send an event that there is no such valid channel
45 if (!validChannelFound) {
46 this._sendErrorEventToContent(
49 ERRNO_NO_SUCH_CHANNEL,
55 console.error("WebChannel channel id missing");
62 * The WebChannel id to include in the message
63 * @param sendingContext {Object}
64 * Message sending context
65 * @param [errorMsg] {String}
69 _sendErrorEventToContent(id, sendingContext, errorNo, errorMsg) {
70 let { eventTarget, principal } = sendingContext;
72 errorMsg = errorMsg || "Web Channel Parent error";
74 let { currentWindowGlobal = null } = this.browsingContext;
75 if (currentWindowGlobal) {
77 .getActor("WebChannel")
78 .sendAsyncMessage("WebChannelMessageToContent", {
88 console.error("Failed to send a WebChannel error. Target invalid.");
90 console.error(id.toString() + " error message. ", errorMsg);