summaryrefslogtreecommitdiffstats
path: root/toolkit/actors/UAWidgetsChild.sys.mjs
blob: 6f4244ffe9dda28e01efaa775f313f0e0b5d0d51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/* 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/. */

export class UAWidgetsChild extends JSWindowActorChild {
  constructor() {
    super();

    this.widgets = new WeakMap();
    this.prefsCache = new Map();
    this.observedPrefs = [];

    // Bug 1570744 - JSWindowActorChild's cannot be used as nsIObserver's
    // directly, so we create a new function here instead to act as our
    // nsIObserver, which forwards the notification to the observe method.
    this.observerFunction = (subject, topic, data) => {
      this.observe(subject, topic, data);
    };
  }

  didDestroy() {
    for (let pref in this.observedPrefs) {
      Services.prefs.removeObserver(pref, this.observerFunction);
    }
  }

  unwrap(obj) {
    return Cu.isXrayWrapper(obj) ? obj.wrappedJSObject : obj;
  }

  handleEvent(aEvent) {
    switch (aEvent.type) {
      case "UAWidgetSetupOrChange":
        this.setupOrNotifyWidget(aEvent.target);
        break;
      case "UAWidgetTeardown":
        this.teardownWidget(aEvent.target);
        break;
    }
  }

  setupOrNotifyWidget(aElement) {
    if (!this.widgets.has(aElement)) {
      this.setupWidget(aElement);
      return;
    }

    let { widget } = this.widgets.get(aElement);

    if (typeof widget.onchange == "function") {
      if (
        this.unwrap(aElement.openOrClosedShadowRoot) !=
        this.unwrap(widget.shadowRoot)
      ) {
        console.error(
          "Getting a UAWidgetSetupOrChange event without the ShadowRoot. " +
            "Torn down already?"
        );
        return;
      }
      try {
        widget.onchange();
      } catch (ex) {
        console.error(ex);
      }
    }
  }

  setupWidget(aElement) {
    let uri;
    let widgetName;
    // Use prefKeys to optionally send a list of preferences to forward to
    // the UAWidget. The UAWidget will receive those preferences as key-value
    // pairs as the second argument to its constructor. Updates to those prefs
    // can be observed by implementing an optional onPrefChange method for the
    // UAWidget that receives the changed pref name as the first argument, and
    // the updated value as the second.
    let prefKeys = [];
    switch (aElement.localName) {
      case "video":
      case "audio":
        uri = "chrome://global/content/elements/videocontrols.js";
        widgetName = "VideoControlsWidget";
        prefKeys = [
          "media.videocontrols.picture-in-picture.enabled",
          "media.videocontrols.picture-in-picture.video-toggle.enabled",
          "media.videocontrols.picture-in-picture.video-toggle.always-show",
          "media.videocontrols.picture-in-picture.video-toggle.min-video-secs",
          "media.videocontrols.picture-in-picture.video-toggle.position",
          "media.videocontrols.picture-in-picture.video-toggle.has-used",
          "media.videocontrols.keyboard-tab-to-all-controls",
          "media.videocontrols.picture-in-picture.respect-disablePictureInPicture",
        ];
        break;
      case "input":
        uri = "chrome://global/content/elements/datetimebox.js";
        widgetName = "DateTimeBoxWidget";
        break;
      case "marquee":
        uri = "chrome://global/content/elements/marquee.js";
        widgetName = "MarqueeWidget";
        break;
      case "img":
        uri = "chrome://global/content/elements/textrecognition.js";
        widgetName = "TextRecognitionWidget";
    }

    if (!uri || !widgetName) {
      console.error(
        "Getting a UAWidgetSetupOrChange event on undefined element."
      );
      return;
    }

    let shadowRoot = aElement.openOrClosedShadowRoot;
    if (!shadowRoot) {
      console.error(
        "Getting a UAWidgetSetupOrChange event without the Shadow Root. " +
          "Torn down already?"
      );
      return;
    }

    let isSystemPrincipal = aElement.nodePrincipal.isSystemPrincipal;
    let sandbox = isSystemPrincipal
      ? Object.create(null)
      : Cu.getUAWidgetScope(aElement.nodePrincipal);

    if (!sandbox[widgetName]) {
      Services.scriptloader.loadSubScript(uri, sandbox);
    }

    let prefs = Cu.cloneInto(
      this.getPrefsForUAWidget(widgetName, prefKeys),
      sandbox
    );

    let widget = new sandbox[widgetName](shadowRoot, prefs);
    if (!isSystemPrincipal) {
      widget = widget.wrappedJSObject;
    }
    if (this.unwrap(widget.shadowRoot) != this.unwrap(shadowRoot)) {
      console.error("Widgets should expose their shadow root.");
    }
    this.widgets.set(aElement, { widget, widgetName });
    try {
      widget.onsetup();
    } catch (ex) {
      console.error(ex);
    }
  }

  teardownWidget(aElement) {
    if (!this.widgets.has(aElement)) {
      return;
    }
    let { widget } = this.widgets.get(aElement);
    if (typeof widget.teardown == "function") {
      try {
        widget.teardown();
      } catch (ex) {
        console.error(ex);
      }
    }
    this.widgets.delete(aElement);
  }

  getPrefsForUAWidget(aWidgetName, aPrefKeys) {
    let result = this.prefsCache.get(aWidgetName);
    if (result) {
      return result;
    }

    result = {};
    for (let key of aPrefKeys) {
      result[key] = this.getPref(key);
      this.observePref(key);
    }

    this.prefsCache.set(aWidgetName, result);
    return result;
  }

  observePref(prefKey) {
    Services.prefs.addObserver(prefKey, this.observerFunction);
    this.observedPrefs.push(prefKey);
  }

  getPref(prefKey) {
    switch (Services.prefs.getPrefType(prefKey)) {
      case Ci.nsIPrefBranch.PREF_BOOL: {
        return Services.prefs.getBoolPref(prefKey);
      }
      case Ci.nsIPrefBranch.PREF_INT: {
        return Services.prefs.getIntPref(prefKey);
      }
      case Ci.nsIPrefBranch.PREF_STRING: {
        return Services.prefs.getStringPref(prefKey);
      }
    }

    return undefined;
  }

  observe(subject, topic, data) {
    if (topic == "nsPref:changed") {
      for (let [widgetName, prefCache] of this.prefsCache) {
        if (prefCache.hasOwnProperty(data)) {
          let newValue = this.getPref(data);
          prefCache[data] = newValue;

          this.notifyWidgetsOnPrefChange(widgetName, data, newValue);
        }
      }
    }
  }

  notifyWidgetsOnPrefChange(nameOfWidgetToNotify, prefKey, newValue) {
    let elements = ChromeUtils.nondeterministicGetWeakMapKeys(this.widgets);
    for (let element of elements) {
      if (!Cu.isDeadWrapper(element) && element.isConnected) {
        let { widgetName, widget } = this.widgets.get(element);
        if (widgetName == nameOfWidgetToNotify) {
          if (typeof widget.onPrefChange == "function") {
            try {
              widget.onPrefChange(prefKey, newValue);
            } catch (ex) {
              console.error(ex);
            }
          }
        }
      }
    }
  }
}