Bug 1888590 - Mark some subtests on trusted-types-event-handlers.html as failing...
[gecko.git] / toolkit / actors / WebChannelParent.sys.mjs
blob98b3ad093a31e3ba55623142220b65029ffa2feb
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 {
12   receiveMessage(msg) {
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,
19     };
20     // data must be a string except for a few legacy origins allowed by browser-content.js.
21     if (typeof data == "string") {
22       try {
23         data = JSON.parse(data);
24       } catch (e) {
25         console.error("Failed to parse WebChannel data as a JSON object");
26         return;
27       }
28     }
30     if (data && data.id) {
31       if (!msg.data.principal) {
32         this._sendErrorEventToContent(
33           data.id,
34           sendingContext,
35           ERRNO_MISSING_PRINCIPAL,
36           "Message principal missing"
37         );
38       } else {
39         let validChannelFound = WebChannelBroker.tryToDeliver(
40           data,
41           sendingContext
42         );
44         // if no valid origins send an event that there is no such valid channel
45         if (!validChannelFound) {
46           this._sendErrorEventToContent(
47             data.id,
48             sendingContext,
49             ERRNO_NO_SUCH_CHANNEL,
50             "No Such Channel"
51           );
52         }
53       }
54     } else {
55       console.error("WebChannel channel id missing");
56     }
57   }
59   /**
60    *
61    * @param id {String}
62    *        The WebChannel id to include in the message
63    * @param sendingContext {Object}
64    *        Message sending context
65    * @param [errorMsg] {String}
66    *        Error message
67    * @private
68    */
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) {
76       currentWindowGlobal
77         .getActor("WebChannel")
78         .sendAsyncMessage("WebChannelMessageToContent", {
79           id,
80           message: {
81             errno: errorNo,
82             error: errorMsg,
83           },
84           eventTarget,
85           principal,
86         });
87     } else {
88       console.error("Failed to send a WebChannel error. Target invalid.");
89     }
90     console.error(id.toString() + " error message. ", errorMsg);
91   }