2018-06-27 18:34:07 +00:00
|
|
|
/* vim: set ts=2 sw=2 sts=2 et tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var EXPORTED_SYMBOLS = ["UAWidgetsChild"];
|
|
|
|
|
2019-01-17 18:18:31 +00:00
|
|
|
const {ActorChild} = ChromeUtils.import("resource://gre/modules/ActorChild.jsm");
|
|
|
|
const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
|
2018-06-27 18:34:07 +00:00
|
|
|
|
|
|
|
class UAWidgetsChild extends ActorChild {
|
2018-09-26 21:46:18 +00:00
|
|
|
constructor(dispatcher) {
|
|
|
|
super(dispatcher);
|
2018-06-27 18:34:07 +00:00
|
|
|
|
|
|
|
this.widgets = new WeakMap();
|
|
|
|
}
|
|
|
|
|
|
|
|
handleEvent(aEvent) {
|
|
|
|
switch (aEvent.type) {
|
2018-12-15 02:48:46 +00:00
|
|
|
case "UAWidgetSetupOrChange":
|
2018-06-27 18:34:07 +00:00
|
|
|
this.setupOrNotifyWidget(aEvent.target);
|
|
|
|
break;
|
2018-12-15 02:48:46 +00:00
|
|
|
case "UAWidgetTeardown":
|
2018-06-27 18:34:07 +00:00
|
|
|
this.teardownWidget(aEvent.target);
|
|
|
|
break;
|
|
|
|
}
|
2018-10-03 15:14:46 +00:00
|
|
|
|
|
|
|
// In case we are a nested frame, prevent the message manager of the
|
|
|
|
// parent frame from receving the event.
|
|
|
|
aEvent.stopPropagation();
|
2018-06-27 18:34:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setupOrNotifyWidget(aElement) {
|
|
|
|
let widget = this.widgets.get(aElement);
|
|
|
|
if (!widget) {
|
|
|
|
this.setupWidget(aElement);
|
|
|
|
return;
|
|
|
|
}
|
2019-02-04 13:56:22 +00:00
|
|
|
if (typeof widget.onchange == "function") {
|
2018-12-13 20:59:00 +00:00
|
|
|
try {
|
2019-02-04 13:56:22 +00:00
|
|
|
widget.onchange();
|
2018-12-13 20:59:00 +00:00
|
|
|
} catch (ex) {
|
|
|
|
Cu.reportError(ex);
|
|
|
|
}
|
2018-06-27 18:34:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setupWidget(aElement) {
|
|
|
|
let uri;
|
|
|
|
let widgetName;
|
|
|
|
switch (aElement.localName) {
|
|
|
|
case "video":
|
|
|
|
case "audio":
|
|
|
|
uri = "chrome://global/content/elements/videocontrols.js";
|
2018-12-13 20:59:00 +00:00
|
|
|
widgetName = "VideoControlsWidget";
|
2018-06-27 18:34:07 +00:00
|
|
|
break;
|
|
|
|
case "input":
|
2018-11-03 05:31:05 +00:00
|
|
|
uri = "chrome://global/content/elements/datetimebox.js";
|
|
|
|
widgetName = "DateTimeBoxWidget";
|
2018-06-27 18:34:07 +00:00
|
|
|
break;
|
|
|
|
case "embed":
|
|
|
|
case "object":
|
2018-11-22 05:49:54 +00:00
|
|
|
uri = "chrome://global/content/elements/pluginProblem.js";
|
|
|
|
widgetName = "PluginProblemWidget";
|
2018-06-27 18:34:07 +00:00
|
|
|
break;
|
2018-11-18 01:23:52 +00:00
|
|
|
case "marquee":
|
|
|
|
uri = "chrome://global/content/elements/marquee.js";
|
|
|
|
widgetName = "MarqueeWidget";
|
|
|
|
break;
|
2018-06-27 18:34:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!uri || !widgetName) {
|
2018-12-15 02:48:46 +00:00
|
|
|
Cu.reportError("Getting a UAWidgetSetupOrChange event on undefined element.");
|
2018-06-27 18:34:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let shadowRoot = aElement.openOrClosedShadowRoot;
|
2018-12-13 20:59:00 +00:00
|
|
|
if (!shadowRoot) {
|
2018-12-15 02:48:46 +00:00
|
|
|
Cu.reportError("Getting a UAWidgetSetupOrChange event without the Shadow Root.");
|
2018-12-13 20:59:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let isSystemPrincipal = aElement.nodePrincipal.isSystemPrincipal;
|
|
|
|
let sandbox = isSystemPrincipal ?
|
2018-06-27 18:34:07 +00:00
|
|
|
Object.create(null) : Cu.getUAWidgetScope(aElement.nodePrincipal);
|
|
|
|
|
|
|
|
if (!sandbox[widgetName]) {
|
2018-12-19 21:46:20 +00:00
|
|
|
Services.scriptloader.loadSubScript(uri, sandbox);
|
2018-06-27 18:34:07 +00:00
|
|
|
}
|
|
|
|
|
2018-12-01 22:28:46 +00:00
|
|
|
let widget = new sandbox[widgetName](shadowRoot);
|
2019-02-04 13:56:22 +00:00
|
|
|
if (!isSystemPrincipal) {
|
|
|
|
widget = widget.wrappedJSObject;
|
|
|
|
}
|
2018-06-27 18:34:07 +00:00
|
|
|
this.widgets.set(aElement, widget);
|
2018-12-13 20:59:00 +00:00
|
|
|
try {
|
2019-02-04 13:56:22 +00:00
|
|
|
widget.onsetup();
|
2018-12-13 20:59:00 +00:00
|
|
|
} catch (ex) {
|
|
|
|
Cu.reportError(ex);
|
|
|
|
}
|
2018-06-27 18:34:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
teardownWidget(aElement) {
|
|
|
|
let widget = this.widgets.get(aElement);
|
|
|
|
if (!widget) {
|
|
|
|
return;
|
|
|
|
}
|
2019-02-04 13:56:22 +00:00
|
|
|
if (typeof widget.destructor == "function") {
|
2018-12-13 20:59:00 +00:00
|
|
|
try {
|
2019-02-04 13:56:22 +00:00
|
|
|
widget.destructor();
|
2018-12-13 20:59:00 +00:00
|
|
|
} catch (ex) {
|
|
|
|
Cu.reportError(ex);
|
|
|
|
}
|
2018-06-27 18:34:07 +00:00
|
|
|
}
|
|
|
|
this.widgets.delete(aElement);
|
|
|
|
}
|
|
|
|
}
|