2019-06-08 04:09:29 +03: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/. */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This module holds weak references to DOM elements that exist within the
|
|
|
|
* current content process, and converts them to a unique identifier that can be
|
|
|
|
* passed between processes. The identifer, if received by the same content process
|
|
|
|
* that issued it, can then be converted back into the DOM element (presuming the
|
|
|
|
* element hasn't had all of its other references dropped).
|
|
|
|
*
|
|
|
|
* The hope is that this module can eliminate the need for passing CPOW references
|
|
|
|
* between processes during runtime.
|
|
|
|
*/
|
|
|
|
|
|
|
|
var EXPORTED_SYMBOLS = ["ContentDOMReference"];
|
|
|
|
|
|
|
|
const { XPCOMUtils } = ChromeUtils.import(
|
|
|
|
"resource://gre/modules/XPCOMUtils.jsm"
|
|
|
|
);
|
|
|
|
|
2019-06-19 22:06:38 +03:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(
|
|
|
|
this,
|
|
|
|
"finalizationService",
|
|
|
|
"@mozilla.org/toolkit/finalizationwitness;1",
|
|
|
|
"nsIFinalizationWitnessService"
|
|
|
|
);
|
|
|
|
|
2019-06-19 21:27:02 +03:00
|
|
|
/**
|
|
|
|
* @typedef {number} ElementID
|
|
|
|
* @typedef {Object} ElementIdentifier
|
|
|
|
*/
|
2019-06-08 04:09:29 +03:00
|
|
|
|
2019-06-19 22:06:38 +03:00
|
|
|
const FINALIZATION_TOPIC = "content-dom-reference-finalized";
|
|
|
|
|
|
|
|
// A WeakMap which ties finalization witness objects to the lifetime of the DOM
|
|
|
|
// nodes they're meant to witness. When the DOM node in the map key is
|
|
|
|
// finalized, the WeakMap stops holding the finalization witness in its value
|
|
|
|
// alive, which alerts our observer that the element has been destroyed.
|
|
|
|
const finalizerRoots = new WeakMap();
|
|
|
|
|
2019-06-08 04:09:29 +03:00
|
|
|
/**
|
|
|
|
* An identifier generated by ContentDOMReference is a unique pair of BrowsingContext
|
2019-06-19 21:27:02 +03:00
|
|
|
* ID and a numeric ID. gRegistry maps BrowsingContext's to an object with the following
|
2019-06-08 04:09:29 +03:00
|
|
|
* properties:
|
|
|
|
*
|
2019-06-19 21:27:02 +03:00
|
|
|
* IDToElement:
|
|
|
|
* A Map of IDs to WeakReference's to the elements they refer to.
|
2019-06-08 04:09:29 +03:00
|
|
|
*
|
2019-06-19 21:27:02 +03:00
|
|
|
* elementToID:
|
|
|
|
* A WeakMap from a DOM element to an ID that refers to it.
|
2019-06-08 04:09:29 +03:00
|
|
|
*/
|
|
|
|
var gRegistry = new WeakMap();
|
|
|
|
|
|
|
|
var ContentDOMReference = {
|
2019-06-19 22:06:38 +03:00
|
|
|
_init() {
|
|
|
|
const { Services } = ChromeUtils.import(
|
|
|
|
"resource://gre/modules/Services.jsm"
|
|
|
|
);
|
|
|
|
Services.obs.addObserver(this, FINALIZATION_TOPIC);
|
|
|
|
},
|
|
|
|
|
|
|
|
observe(subject, topic, data) {
|
|
|
|
if (topic !== FINALIZATION_TOPIC) {
|
|
|
|
throw new Error("Unexpected observer topic");
|
|
|
|
}
|
|
|
|
|
|
|
|
let identifier = JSON.parse(data);
|
2019-06-20 03:55:33 +03:00
|
|
|
this._revoke(identifier);
|
2019-06-19 22:06:38 +03:00
|
|
|
},
|
|
|
|
|
2019-06-08 04:09:29 +03:00
|
|
|
/**
|
|
|
|
* Generate and return an identifier for a given DOM element.
|
|
|
|
*
|
|
|
|
* @param {Element} element The DOM element to generate the identifier for.
|
2019-06-19 21:27:02 +03:00
|
|
|
* @return {ElementIdentifier} The identifier for the DOM element that can be passed between
|
2019-06-08 04:09:29 +03:00
|
|
|
* processes as a message.
|
|
|
|
*/
|
|
|
|
get(element) {
|
|
|
|
if (!element) {
|
|
|
|
throw new Error(
|
|
|
|
"Can't create a ContentDOMReference identifier for " +
|
|
|
|
"non-existant nodes."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-01-21 23:55:37 +03:00
|
|
|
let browsingContext = element.ownerGlobal.getWindowGlobalChild()
|
|
|
|
.browsingContext;
|
2019-06-08 04:09:29 +03:00
|
|
|
let mappings = gRegistry.get(browsingContext);
|
|
|
|
if (!mappings) {
|
|
|
|
mappings = {
|
2019-06-19 21:27:02 +03:00
|
|
|
IDToElement: new Map(),
|
|
|
|
elementToID: new WeakMap(),
|
2019-06-08 04:09:29 +03:00
|
|
|
};
|
|
|
|
gRegistry.set(browsingContext, mappings);
|
|
|
|
}
|
|
|
|
|
2019-06-19 21:27:02 +03:00
|
|
|
let id = mappings.elementToID.get(element);
|
|
|
|
if (id) {
|
|
|
|
// We already had this element registered, so return the pre-existing ID.
|
|
|
|
return { browsingContextId: browsingContext.id, id };
|
2019-06-08 04:09:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// We must be registering a new element at this point.
|
2019-06-19 21:27:02 +03:00
|
|
|
id = Math.random();
|
|
|
|
mappings.elementToID.set(element, id);
|
|
|
|
mappings.IDToElement.set(id, Cu.getWeakReference(element));
|
2019-06-08 04:09:29 +03:00
|
|
|
|
2019-06-19 22:06:38 +03:00
|
|
|
let identifier = { browsingContextId: browsingContext.id, id };
|
|
|
|
|
|
|
|
finalizerRoots.set(
|
|
|
|
element,
|
|
|
|
finalizationService.make(FINALIZATION_TOPIC, JSON.stringify(identifier))
|
|
|
|
);
|
|
|
|
|
|
|
|
return identifier;
|
2019-06-08 04:09:29 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolves an identifier back into the DOM Element that it was generated from.
|
|
|
|
*
|
2019-06-19 21:27:02 +03:00
|
|
|
* @param {ElementIdentifier} The identifier generated via ContentDOMReference.get for a
|
2019-06-08 04:09:29 +03:00
|
|
|
* DOM element.
|
|
|
|
* @return {Element} The DOM element that the identifier was generated for, or
|
|
|
|
* null if the element does not still exist.
|
|
|
|
*/
|
|
|
|
resolve(identifier) {
|
|
|
|
let browsingContext = BrowsingContext.get(identifier.browsingContextId);
|
2019-06-19 21:27:02 +03:00
|
|
|
let { id } = identifier;
|
|
|
|
return this._resolveIDToElement(browsingContext, id);
|
2019-06-08 04:09:29 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes an identifier from the registry so that subsequent attempts
|
2019-06-20 03:55:33 +03:00
|
|
|
* to resolve it will result in null. This is done automatically when the
|
|
|
|
* target node is GCed.
|
2019-06-08 04:09:29 +03:00
|
|
|
*
|
2019-06-19 21:27:02 +03:00
|
|
|
* @param {ElementIdentifier} The identifier to revoke, issued by ContentDOMReference.get for
|
2019-06-08 04:09:29 +03:00
|
|
|
* a DOM element.
|
|
|
|
*/
|
2019-06-20 03:55:33 +03:00
|
|
|
_revoke(identifier) {
|
2019-06-08 04:09:29 +03:00
|
|
|
let browsingContext = BrowsingContext.get(identifier.browsingContextId);
|
2019-06-19 21:27:02 +03:00
|
|
|
let { id } = identifier;
|
2019-06-08 04:09:29 +03:00
|
|
|
|
|
|
|
let mappings = gRegistry.get(browsingContext);
|
|
|
|
if (!mappings) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-19 21:27:02 +03:00
|
|
|
mappings.IDToElement.delete(id);
|
2019-06-08 04:09:29 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2019-06-19 21:27:02 +03:00
|
|
|
* Private helper function that resolves a BrowsingContext and ID (the
|
2019-06-08 04:09:29 +03:00
|
|
|
* pair that makes up an identifier) to a DOM element.
|
|
|
|
*
|
|
|
|
* @param {BrowsingContext} browsingContext The BrowsingContext that was hosting
|
|
|
|
* the DOM element at the time that the identifier was generated.
|
2019-06-19 21:27:02 +03:00
|
|
|
* @param {ElementID} id The ID generated for the DOM element.
|
2019-06-08 04:09:29 +03:00
|
|
|
*
|
|
|
|
* @return {Element} The DOM element that the identifier was generated for, or
|
|
|
|
* null if the element does not still exist.
|
|
|
|
*/
|
2019-06-19 21:27:02 +03:00
|
|
|
_resolveIDToElement(browsingContext, id) {
|
2019-06-08 04:09:29 +03:00
|
|
|
let mappings = gRegistry.get(browsingContext);
|
|
|
|
if (!mappings) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-06-19 21:27:02 +03:00
|
|
|
let weakReference = mappings.IDToElement.get(id);
|
2019-06-08 04:09:29 +03:00
|
|
|
if (!weakReference) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return weakReference.get();
|
|
|
|
},
|
|
|
|
};
|
2019-06-19 22:06:38 +03:00
|
|
|
|
|
|
|
ContentDOMReference._init();
|