summaryrefslogtreecommitdiffstats
path: root/devtools/server/actors/inspector/custom-element-watcher.js
blob: 8eb57fea40b3cc3b56bdca4355193e556c896eb6 (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
/* 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";

const EventEmitter = require("resource://devtools/shared/event-emitter.js");

/**
 * The CustomElementWatcher can be used to be notified if a custom element definition
 * is created for a node.
 *
 * When a custom element is defined for a monitored name, an "element-defined" event is
 * fired with the following Object argument:
 * - {String} name: name of the custom element defined
 * - {Set} Set of impacted node actors
 */
class CustomElementWatcher extends EventEmitter {
  constructor(chromeEventHandler) {
    super();

    this.chromeEventHandler = chromeEventHandler;
    this._onCustomElementDefined = this._onCustomElementDefined.bind(this);
    this.chromeEventHandler.addEventListener(
      "customelementdefined",
      this._onCustomElementDefined
    );

    /**
     * Each window keeps its own custom element registry, all of them are watched
     * separately. The struture of the watchedRegistries is as follows
     *
     * WeakMap(
     *   registry -> Map (
     *     name -> Set(NodeActors)
     *   )
     * )
     */
    this.watchedRegistries = new WeakMap();
  }

  destroy() {
    this.watchedRegistries = null;
    this.chromeEventHandler.removeEventListener(
      "customelementdefined",
      this._onCustomElementDefined
    );
  }

  /**
   * Watch for custom element definitions matching the name of the provided NodeActor.
   */
  manageNode(nodeActor) {
    if (!this._isValidNode(nodeActor)) {
      return;
    }

    if (!this._shouldWatchDefinition(nodeActor)) {
      return;
    }

    const registry = nodeActor.rawNode.ownerGlobal.customElements;
    const registryMap = this._getMapForRegistry(registry);

    const name = nodeActor.rawNode.localName;
    const actorsSet = this._getActorsForName(name, registryMap);
    actorsSet.add(nodeActor);
  }

  /**
   * Stop watching the provided NodeActor.
   */
  unmanageNode(nodeActor) {
    if (!this._isValidNode(nodeActor)) {
      return;
    }

    const win = nodeActor.rawNode.ownerGlobal;
    const registry = win.customElements;
    const registryMap = this._getMapForRegistry(registry);
    const name = nodeActor.rawNode.localName;
    if (registryMap.has(name)) {
      registryMap.get(name).delete(nodeActor);
    }
  }

  /**
   * Retrieve the map of name->nodeActors for a given CustomElementsRegistry.
   * Will create the map if not created yet.
   */
  _getMapForRegistry(registry) {
    if (!this.watchedRegistries.has(registry)) {
      this.watchedRegistries.set(registry, new Map());
    }
    return this.watchedRegistries.get(registry);
  }

  /**
   * Retrieve the set of nodeActors for a given name and registry.
   * Will create the set if not created yet.
   */
  _getActorsForName(name, registryMap) {
    if (!registryMap.has(name)) {
      registryMap.set(name, new Set());
    }
    return registryMap.get(name);
  }

  _shouldWatchDefinition(nodeActor) {
    const doc = nodeActor.rawNode.ownerDocument;
    const namespaceURI = doc.documentElement.namespaceURI;
    const name = nodeActor.rawNode.localName;
    const isValidName = InspectorUtils.isCustomElementName(name, namespaceURI);

    const customElements = doc.defaultView.customElements;
    return isValidName && !customElements.get(name);
  }

  _onCustomElementDefined(event) {
    const doc = event.target;
    const registry = doc.defaultView.customElements;
    const registryMap = this._getMapForRegistry(registry);

    const name = event.detail;
    const actors = this._getActorsForName(name, registryMap);
    this.emit("element-defined", { name, actors });
    registryMap.delete(name);
  }

  /**
   * Some nodes (e.g. inside of <template> tags) don't have a documentElement or an
   * ownerGlobal and can't be watched by this helper.
   */
  _isValidNode(nodeActor) {
    const node = nodeActor.rawNode;
    return (
      !Cu.isDeadWrapper(node) &&
      node.ownerGlobal &&
      node.ownerDocument?.documentElement
    );
  }
}

exports.CustomElementWatcher = CustomElementWatcher;