2017-11-29 01:13:59 +03:00
|
|
|
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
|
|
|
|
/* vim: set sts=2 sw=2 et tw=80: */
|
2015-06-04 01:34:44 +03:00
|
|
|
/* 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";
|
|
|
|
|
2018-02-23 22:50:01 +03:00
|
|
|
var EXPORTED_SYMBOLS = ["ExtensionUtils"];
|
2015-06-04 01:34:44 +03:00
|
|
|
|
2019-01-17 21:18:31 +03:00
|
|
|
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
|
|
|
|
const { XPCOMUtils } = ChromeUtils.import(
|
|
|
|
"resource://gre/modules/XPCOMUtils.jsm"
|
|
|
|
);
|
2015-06-04 01:34:44 +03:00
|
|
|
|
2018-06-06 22:43:26 +03:00
|
|
|
ChromeUtils.defineModuleGetter(
|
|
|
|
this,
|
|
|
|
"setTimeout",
|
|
|
|
"resource://gre/modules/Timer.jsm"
|
|
|
|
);
|
2016-10-21 01:27:51 +03:00
|
|
|
|
2018-02-23 00:55:40 +03:00
|
|
|
// xpcshell doesn't handle idle callbacks well.
|
|
|
|
XPCOMUtils.defineLazyGetter(this, "idleTimeout", () =>
|
|
|
|
Services.appinfo.name === "XPCShell" ? 500 : undefined
|
|
|
|
);
|
|
|
|
|
2017-10-30 19:29:58 +03:00
|
|
|
// It would be nicer to go through `Services.appinfo`, but some tests need to be
|
|
|
|
// able to replace that field with a custom implementation before it is first
|
|
|
|
// called.
|
|
|
|
// eslint-disable-next-line mozilla/use-services
|
2017-07-11 08:51:28 +03:00
|
|
|
const appinfo = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULRuntime);
|
|
|
|
|
2016-11-11 08:44:05 +03:00
|
|
|
let nextId = 0;
|
2017-09-26 01:00:17 +03:00
|
|
|
const uniqueProcessID = appinfo.uniqueProcessID;
|
|
|
|
// Store the process ID in a 16 bit field left shifted to end of a
|
|
|
|
// double's mantissa.
|
|
|
|
// Note: We can't use bitwise ops here, since they truncate to a 32 bit
|
|
|
|
// integer and we need all 53 mantissa bits.
|
|
|
|
const processIDMask = (uniqueProcessID & 0xffff) * 2 ** 37;
|
2016-11-11 08:44:05 +03:00
|
|
|
|
|
|
|
function getUniqueId() {
|
2017-09-26 01:00:17 +03:00
|
|
|
// Note: We can't use bitwise ops here, since they truncate to a 32 bit
|
|
|
|
// integer and we need all 53 mantissa bits.
|
|
|
|
return processIDMask + nextId++;
|
2016-11-11 08:44:05 +03:00
|
|
|
}
|
|
|
|
|
2018-06-06 22:43:26 +03:00
|
|
|
function promiseTimeout(delay) {
|
|
|
|
return new Promise(resolve => setTimeout(resolve, delay));
|
|
|
|
}
|
2017-03-08 20:16:01 +03:00
|
|
|
|
2016-11-07 05:08:46 +03:00
|
|
|
/**
|
|
|
|
* An Error subclass for which complete error messages are always passed
|
|
|
|
* to extensions, rather than being interpreted as an unknown error.
|
|
|
|
*/
|
|
|
|
class ExtensionError extends Error {}
|
|
|
|
|
2016-01-20 08:57:34 +03:00
|
|
|
function filterStack(error) {
|
2016-01-20 23:08:56 +03:00
|
|
|
return String(error.stack).replace(
|
|
|
|
/(^.*(Task\.jsm|Promise-backend\.js).*\n)+/gm,
|
|
|
|
"<Promise Chain>\n"
|
|
|
|
);
|
2016-01-20 08:57:34 +03:00
|
|
|
}
|
|
|
|
|
2016-09-07 13:05:11 +03:00
|
|
|
/**
|
|
|
|
* Similar to a WeakMap, but creates a new key with the given
|
|
|
|
* constructor if one is not present.
|
|
|
|
*/
|
|
|
|
class DefaultWeakMap extends WeakMap {
|
2017-09-21 03:00:44 +03:00
|
|
|
constructor(defaultConstructor = undefined, init = undefined) {
|
2016-09-07 13:05:11 +03:00
|
|
|
super(init);
|
2017-09-21 03:00:44 +03:00
|
|
|
if (defaultConstructor) {
|
|
|
|
this.defaultConstructor = defaultConstructor;
|
|
|
|
}
|
2016-09-07 13:05:11 +03:00
|
|
|
}
|
2015-06-04 01:34:44 +03:00
|
|
|
|
|
|
|
get(key) {
|
2017-09-11 01:39:49 +03:00
|
|
|
let value = super.get(key);
|
|
|
|
if (value === undefined && !this.has(key)) {
|
|
|
|
value = this.defaultConstructor(key);
|
|
|
|
this.set(key, value);
|
2015-06-04 01:34:44 +03:00
|
|
|
}
|
2017-09-11 01:39:49 +03:00
|
|
|
return value;
|
2016-09-07 13:05:11 +03:00
|
|
|
}
|
|
|
|
}
|
2015-06-04 01:34:44 +03:00
|
|
|
|
2016-10-21 01:27:51 +03:00
|
|
|
class DefaultMap extends Map {
|
2017-09-21 03:00:44 +03:00
|
|
|
constructor(defaultConstructor = undefined, init = undefined) {
|
2016-10-21 01:27:51 +03:00
|
|
|
super(init);
|
2017-09-21 03:00:44 +03:00
|
|
|
if (defaultConstructor) {
|
|
|
|
this.defaultConstructor = defaultConstructor;
|
|
|
|
}
|
2016-10-21 01:27:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
get(key) {
|
2017-09-11 01:39:49 +03:00
|
|
|
let value = super.get(key);
|
|
|
|
if (value === undefined && !this.has(key)) {
|
|
|
|
value = this.defaultConstructor(key);
|
|
|
|
this.set(key, value);
|
2016-10-21 01:27:51 +03:00
|
|
|
}
|
2017-09-11 01:39:49 +03:00
|
|
|
return value;
|
2016-10-21 01:27:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 01:16:16 +03:00
|
|
|
const _winUtils = new DefaultWeakMap(win => {
|
2018-07-25 02:47:42 +03:00
|
|
|
return win.windowUtils;
|
2017-03-19 01:16:16 +03:00
|
|
|
});
|
|
|
|
const getWinUtils = win => _winUtils.get(win);
|
|
|
|
|
|
|
|
function getInnerWindowID(window) {
|
|
|
|
return getWinUtils(window).currentInnerWindowID;
|
|
|
|
}
|
|
|
|
|
2017-01-29 11:58:25 +03:00
|
|
|
/**
|
|
|
|
* A set with a limited number of slots, which flushes older entries as
|
|
|
|
* newer ones are added.
|
2017-08-12 21:08:35 +03:00
|
|
|
*
|
|
|
|
* @param {integer} limit
|
|
|
|
* The maximum size to trim the set to after it grows too large.
|
|
|
|
* @param {integer} [slop = limit * .25]
|
|
|
|
* The number of extra entries to allow in the set after it
|
|
|
|
* reaches the size limit, before it is truncated to the limit.
|
|
|
|
* @param {iterable} [iterable]
|
|
|
|
* An iterable of initial entries to add to the set.
|
2017-01-29 11:58:25 +03:00
|
|
|
*/
|
|
|
|
class LimitedSet extends Set {
|
2017-08-12 21:08:35 +03:00
|
|
|
constructor(limit, slop = Math.round(limit * 0.25), iterable = undefined) {
|
2017-01-29 11:58:25 +03:00
|
|
|
super(iterable);
|
|
|
|
this.limit = limit;
|
2017-08-12 21:08:35 +03:00
|
|
|
this.slop = slop;
|
2017-01-29 11:58:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
truncate(limit) {
|
|
|
|
for (let item of this) {
|
2017-08-12 00:46:44 +03:00
|
|
|
// Live set iterators can ge relatively expensive, since they need
|
|
|
|
// to be updated after every modification to the set. Since
|
|
|
|
// breaking out of the loop early will keep the iterator alive
|
|
|
|
// until the next full GC, we're currently better off finishing
|
|
|
|
// the entire loop even after we're done truncating.
|
|
|
|
if (this.size > limit) {
|
|
|
|
this.delete(item);
|
2017-01-29 11:58:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
add(item) {
|
2017-09-11 01:39:49 +03:00
|
|
|
if (this.size >= this.limit + this.slop && !this.has(item)) {
|
2017-01-29 11:58:25 +03:00
|
|
|
this.truncate(this.limit - 1);
|
|
|
|
}
|
|
|
|
super.add(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-27 00:20:28 +03:00
|
|
|
/**
|
|
|
|
* Returns a Promise which resolves when the given document's DOM has
|
|
|
|
* fully loaded.
|
|
|
|
*
|
|
|
|
* @param {Document} doc The document to await the load of.
|
|
|
|
* @returns {Promise<Document>}
|
|
|
|
*/
|
|
|
|
function promiseDocumentReady(doc) {
|
|
|
|
if (doc.readyState == "interactive" || doc.readyState == "complete") {
|
|
|
|
return Promise.resolve(doc);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Promise(resolve => {
|
|
|
|
doc.addEventListener(
|
|
|
|
"DOMContentLoaded",
|
|
|
|
function onReady(event) {
|
|
|
|
if (event.target === event.currentTarget) {
|
|
|
|
doc.removeEventListener("DOMContentLoaded", onReady, true);
|
|
|
|
resolve(doc);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
true
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-02-23 00:55:40 +03:00
|
|
|
/**
|
|
|
|
* Returns a Promise which resolves when the given window's document's DOM has
|
|
|
|
* fully loaded, the <head> stylesheets have fully loaded, and we have hit an
|
|
|
|
* idle time.
|
|
|
|
*
|
|
|
|
* @param {Window} window The window whose document we will await
|
|
|
|
the readiness of.
|
|
|
|
* @returns {Promise<IdleDeadline>}
|
|
|
|
*/
|
|
|
|
function promiseDocumentIdle(window) {
|
|
|
|
return window.document.documentReadyForIdle.then(() => {
|
|
|
|
return new Promise(resolve =>
|
|
|
|
window.requestIdleCallback(resolve, { timeout: idleTimeout })
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-07-25 01:09:07 +03:00
|
|
|
/**
|
|
|
|
* Returns a Promise which resolves when the given document is fully
|
|
|
|
* loaded.
|
|
|
|
*
|
|
|
|
* @param {Document} doc The document to await the load of.
|
|
|
|
* @returns {Promise<Document>}
|
|
|
|
*/
|
|
|
|
function promiseDocumentLoaded(doc) {
|
|
|
|
if (doc.readyState == "complete") {
|
|
|
|
return Promise.resolve(doc);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Promise(resolve => {
|
2017-09-11 01:37:08 +03:00
|
|
|
doc.defaultView.addEventListener("load", () => resolve(doc), {
|
|
|
|
once: true,
|
|
|
|
});
|
2016-07-25 01:09:07 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-10-21 01:27:51 +03:00
|
|
|
/**
|
|
|
|
* Returns a Promise which resolves when the given event is dispatched to the
|
|
|
|
* given element.
|
|
|
|
*
|
|
|
|
* @param {Element} element
|
|
|
|
* The element on which to listen.
|
|
|
|
* @param {string} eventName
|
|
|
|
* The event to listen for.
|
|
|
|
* @param {boolean} [useCapture = true]
|
|
|
|
* If true, listen for the even in the capturing rather than
|
|
|
|
* bubbling phase.
|
|
|
|
* @param {Event} [test]
|
|
|
|
* An optional test function which, when called with the
|
|
|
|
* observer's subject and data, should return true if this is the
|
|
|
|
* expected event, false otherwise.
|
|
|
|
* @returns {Promise<Event>}
|
|
|
|
*/
|
|
|
|
function promiseEvent(
|
|
|
|
element,
|
|
|
|
eventName,
|
|
|
|
useCapture = true,
|
|
|
|
test = event => true
|
|
|
|
) {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
function listener(event) {
|
|
|
|
if (test(event)) {
|
|
|
|
element.removeEventListener(eventName, listener, useCapture);
|
|
|
|
resolve(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
element.addEventListener(eventName, listener, useCapture);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-07-25 01:09:07 +03:00
|
|
|
/**
|
|
|
|
* Returns a Promise which resolves the given observer topic has been
|
|
|
|
* observed.
|
|
|
|
*
|
|
|
|
* @param {string} topic
|
|
|
|
* The topic to observe.
|
|
|
|
* @param {function(nsISupports, string)} [test]
|
|
|
|
* An optional test function which, when called with the
|
|
|
|
* observer's subject and data, should return true if this is the
|
|
|
|
* expected notification, false otherwise.
|
|
|
|
* @returns {Promise<object>}
|
|
|
|
*/
|
|
|
|
function promiseObserved(topic, test = () => true) {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
let observer = (subject, topic, data) => {
|
|
|
|
if (test(subject, data)) {
|
|
|
|
Services.obs.removeObserver(observer, topic);
|
|
|
|
resolve({ subject, data });
|
|
|
|
}
|
|
|
|
};
|
2017-04-14 22:51:38 +03:00
|
|
|
Services.obs.addObserver(observer, topic);
|
2016-07-25 01:09:07 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-12-03 03:58:53 +03:00
|
|
|
function getMessageManager(target) {
|
2017-08-19 23:32:58 +03:00
|
|
|
if (target.frameLoader) {
|
|
|
|
return target.frameLoader.messageManager;
|
2016-01-06 21:10:35 +03:00
|
|
|
}
|
2018-04-12 07:06:25 +03:00
|
|
|
return target;
|
2015-06-04 01:34:44 +03:00
|
|
|
}
|
|
|
|
|
2016-11-18 07:00:30 +03:00
|
|
|
function flushJarCache(jarPath) {
|
|
|
|
Services.obs.notifyObservers(null, "flush-cache-entry", jarPath);
|
2015-08-28 02:29:24 +03:00
|
|
|
}
|
|
|
|
|
2018-02-23 22:50:01 +03:00
|
|
|
var ExtensionUtils = {
|
2016-03-01 06:04:03 +03:00
|
|
|
flushJarCache,
|
2016-08-05 02:18:25 +03:00
|
|
|
getInnerWindowID,
|
2016-11-10 23:35:22 +03:00
|
|
|
getMessageManager,
|
2016-11-11 08:44:05 +03:00
|
|
|
getUniqueId,
|
2017-03-12 06:05:09 +03:00
|
|
|
filterStack,
|
2017-03-19 01:16:16 +03:00
|
|
|
getWinUtils,
|
2018-02-23 00:55:40 +03:00
|
|
|
promiseDocumentIdle,
|
2016-07-25 01:09:07 +03:00
|
|
|
promiseDocumentLoaded,
|
2016-02-27 00:20:28 +03:00
|
|
|
promiseDocumentReady,
|
2016-10-21 01:27:51 +03:00
|
|
|
promiseEvent,
|
2016-07-25 01:09:07 +03:00
|
|
|
promiseObserved,
|
2018-06-06 22:43:26 +03:00
|
|
|
promiseTimeout,
|
2016-11-03 05:09:04 +03:00
|
|
|
DefaultMap,
|
2015-06-04 01:34:44 +03:00
|
|
|
DefaultWeakMap,
|
2016-11-07 05:08:46 +03:00
|
|
|
ExtensionError,
|
2017-01-29 11:58:25 +03:00
|
|
|
LimitedSet,
|
2015-06-04 01:34:44 +03:00
|
|
|
};
|