summaryrefslogtreecommitdiffstats
path: root/toolkit/jetpack/sdk/input
diff options
context:
space:
mode:
authorMatt A. Tobin <email@mattatobin.com>2018-02-09 06:46:43 -0500
committerMatt A. Tobin <email@mattatobin.com>2018-02-09 06:46:43 -0500
commitac46df8daea09899ce30dc8fd70986e258c746bf (patch)
tree2750d3125fc253fd5b0671e4bd268eff1fd97296 /toolkit/jetpack/sdk/input
parent8cecf8d5208f3945b35f879bba3015bb1a11bec6 (diff)
downloadUXP-ac46df8daea09899ce30dc8fd70986e258c746bf.tar
UXP-ac46df8daea09899ce30dc8fd70986e258c746bf.tar.gz
UXP-ac46df8daea09899ce30dc8fd70986e258c746bf.tar.lz
UXP-ac46df8daea09899ce30dc8fd70986e258c746bf.tar.xz
UXP-ac46df8daea09899ce30dc8fd70986e258c746bf.zip
Move Add-on SDK source to toolkit/jetpack
Diffstat (limited to 'toolkit/jetpack/sdk/input')
-rw-r--r--toolkit/jetpack/sdk/input/browser.js73
-rw-r--r--toolkit/jetpack/sdk/input/customizable-ui.js28
-rw-r--r--toolkit/jetpack/sdk/input/frame.js85
-rw-r--r--toolkit/jetpack/sdk/input/system.js113
4 files changed, 299 insertions, 0 deletions
diff --git a/toolkit/jetpack/sdk/input/browser.js b/toolkit/jetpack/sdk/input/browser.js
new file mode 100644
index 000000000..daea875bf
--- /dev/null
+++ b/toolkit/jetpack/sdk/input/browser.js
@@ -0,0 +1,73 @@
+/* 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 { windows, isBrowser, isInteractive, isDocumentLoaded,
+ getOuterId } = require("../window/utils");
+const { InputPort } = require("./system");
+const { lift, merges, foldp, keepIf, start, Input } = require("../event/utils");
+const { patch } = require("diffpatcher/index");
+const { Sequence, seq, filter, object, pairs } = require("../util/sequence");
+
+
+// Create lazy iterators from the regular arrays, although
+// once https://github.com/mozilla/addon-sdk/pull/1314 lands
+// `windows` will be transforme to lazy iterators.
+// When iterated over belowe sequences items will represent
+// state of windows at the time of iteration.
+const opened = seq(function*() {
+ const items = windows("navigator:browser", {includePrivate: true});
+ for (let item of items) {
+ yield [getOuterId(item), item];
+ }
+});
+const interactive = filter(([_, window]) => isInteractive(window), opened);
+const loaded = filter(([_, window]) => isDocumentLoaded(window), opened);
+
+// Helper function that converts given argument to a delta.
+const Update = window => window && object([getOuterId(window), window]);
+const Delete = window => window && object([getOuterId(window), null]);
+
+
+// Signal represents delta for last top level window close.
+const LastClosed = lift(Delete,
+ keepIf(isBrowser, null,
+ new InputPort({topic: "domwindowclosed"})));
+exports.LastClosed = LastClosed;
+
+const windowFor = document => document && document.defaultView;
+
+// Signal represent delta for last top level window document becoming interactive.
+const InteractiveDoc = new InputPort({topic: "chrome-document-interactive"});
+const InteractiveWin = lift(windowFor, InteractiveDoc);
+const LastInteractive = lift(Update, keepIf(isBrowser, null, InteractiveWin));
+exports.LastInteractive = LastInteractive;
+
+// Signal represent delta for last top level window loaded.
+const LoadedDoc = new InputPort({topic: "chrome-document-loaded"});
+const LoadedWin = lift(windowFor, LoadedDoc);
+const LastLoaded = lift(Update, keepIf(isBrowser, null, LoadedWin));
+exports.LastLoaded = LastLoaded;
+
+
+const initialize = input => {
+ if (!input.initialized) {
+ input.value = object(...input.value);
+ Input.start(input);
+ input.initialized = true;
+ }
+};
+
+// Signal represents set of top level interactive windows, updated any
+// time new window becomes interactive or one get's closed.
+const Interactive = foldp(patch, interactive, merges([LastInteractive,
+ LastClosed]));
+Interactive[start] = initialize;
+exports.Interactive = Interactive;
+
+// Signal represents set of top level loaded window, updated any time
+// new window becomes interactive or one get's closed.
+const Loaded = foldp(patch, loaded, merges([LastLoaded, LastClosed]));
+Loaded[start] = initialize;
+exports.Loaded = Loaded;
diff --git a/toolkit/jetpack/sdk/input/customizable-ui.js b/toolkit/jetpack/sdk/input/customizable-ui.js
new file mode 100644
index 000000000..a41d0971a
--- /dev/null
+++ b/toolkit/jetpack/sdk/input/customizable-ui.js
@@ -0,0 +1,28 @@
+/* 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 { Cu } = require("chrome");
+const { CustomizableUI } = Cu.import('resource:///modules/CustomizableUI.jsm', {});
+const { receive } = require("../event/utils");
+const { InputPort } = require("./system");
+const { object} = require("../util/sequence");
+const { getOuterId } = require("../window/utils");
+
+const Input = function() {};
+Input.prototype = Object.create(InputPort.prototype);
+
+Input.prototype.onCustomizeStart = function (window) {
+ receive(this, object([getOuterId(window), true]));
+}
+
+Input.prototype.onCustomizeEnd = function (window) {
+ receive(this, object([getOuterId(window), null]));
+}
+
+Input.prototype.addListener = input => CustomizableUI.addListener(input);
+
+Input.prototype.removeListener = input => CustomizableUI.removeListener(input);
+
+exports.CustomizationInput = Input;
diff --git a/toolkit/jetpack/sdk/input/frame.js b/toolkit/jetpack/sdk/input/frame.js
new file mode 100644
index 000000000..50efaa745
--- /dev/null
+++ b/toolkit/jetpack/sdk/input/frame.js
@@ -0,0 +1,85 @@
+/* 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 { Ci } = require("chrome");
+const { InputPort } = require("./system");
+const { getFrameElement, getOuterId,
+ getOwnerBrowserWindow } = require("../window/utils");
+const { isnt } = require("../lang/functional");
+const { foldp, lift, merges, keepIf } = require("../event/utils");
+const { object } = require("../util/sequence");
+const { compose } = require("../lang/functional");
+const { LastClosed } = require("./browser");
+const { patch } = require("diffpatcher/index");
+
+const Document = Ci.nsIDOMDocument;
+
+const isntNull = isnt(null);
+
+const frameID = frame => frame.id;
+const browserID = compose(getOuterId, getOwnerBrowserWindow);
+
+const isInnerFrame = frame =>
+ frame && frame.hasAttribute("data-is-sdk-inner-frame");
+
+// Utility function that given content window loaded in our frame views returns
+// an actual frame. This basically takes care of fact that actual frame document
+// is loaded in the nested iframe. If content window is not loaded in the nested
+// frame of the frame view it returs null.
+const getFrame = document =>
+ document && document.defaultView && getFrameElement(document.defaultView);
+
+const FrameInput = function(options) {
+ const input = keepIf(isInnerFrame, null,
+ lift(getFrame, new InputPort(options)));
+ return lift(frame => {
+ if (!frame) return frame;
+ const [id, owner] = [frameID(frame), browserID(frame)];
+ return object([id, {owners: object([owner, options.update])}]);
+ }, input);
+};
+
+const LastLoading = new FrameInput({topic: "document-element-inserted",
+ update: {readyState: "loading"}});
+exports.LastLoading = LastLoading;
+
+const LastInteractive = new FrameInput({topic: "content-document-interactive",
+ update: {readyState: "interactive"}});
+exports.LastInteractive = LastInteractive;
+
+const LastLoaded = new FrameInput({topic: "content-document-loaded",
+ update: {readyState: "complete"}});
+exports.LastLoaded = LastLoaded;
+
+const LastUnloaded = new FrameInput({topic: "content-page-hidden",
+ update: null});
+exports.LastUnloaded = LastUnloaded;
+
+// Represents state of SDK frames in form of data structure:
+// {"frame#1": {"id": "frame#1",
+// "inbox": {"data": "ping",
+// "target": {"id": "frame#1", "owner": "outerWindowID#2"},
+// "source": {"id": "frame#1"}}
+// "url": "resource://addon-1/data/index.html",
+// "owners": {"outerWindowID#1": {"readyState": "loading"},
+// "outerWindowID#2": {"readyState": "complete"}}
+//
+//
+// frame#2: {"id": "frame#2",
+// "url": "resource://addon-1/data/main.html",
+// "outbox": {"data": "pong",
+// "source": {"id": "frame#2", "owner": "outerWindowID#1"}
+// "target": {"id": "frame#2"}}
+// "owners": {outerWindowID#1: {readyState: "interacitve"}}}}
+const Frames = foldp(patch, {}, merges([
+ LastLoading,
+ LastInteractive,
+ LastLoaded,
+ LastUnloaded,
+ new InputPort({ id: "frame-mailbox" }),
+ new InputPort({ id: "frame-change" }),
+ new InputPort({ id: "frame-changed" })
+]));
+exports.Frames = Frames;
diff --git a/toolkit/jetpack/sdk/input/system.js b/toolkit/jetpack/sdk/input/system.js
new file mode 100644
index 000000000..66bc6daec
--- /dev/null
+++ b/toolkit/jetpack/sdk/input/system.js
@@ -0,0 +1,113 @@
+/* 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 { Cc, Ci, Cr, Cu } = require("chrome");
+const { Input, start, stop, end, receive, outputs } = require("../event/utils");
+const { once, off } = require("../event/core");
+const { id: addonID } = require("../self");
+
+const unloadMessage = require("@loader/unload");
+const observerService = Cc['@mozilla.org/observer-service;1'].
+ getService(Ci.nsIObserverService);
+const { ShimWaiver } = Cu.import("resource://gre/modules/ShimWaiver.jsm");
+const addObserver = ShimWaiver.getProperty(observerService, "addObserver");
+const removeObserver = ShimWaiver.getProperty(observerService, "removeObserver");
+
+
+const addonUnloadTopic = "sdk:loader:destroy";
+
+const isXrayWrapper = Cu.isXrayWrapper;
+// In the past SDK used to double-wrap notifications dispatched, which
+// made them awkward to use outside of SDK. At present they no longer
+// do that, although we still supported for legacy reasons.
+const isLegacyWrapper = x =>
+ x && x.wrappedJSObject &&
+ "observersModuleSubjectWrapper" in x.wrappedJSObject;
+
+const unwrapLegacy = x => x.wrappedJSObject.object;
+
+// `InputPort` provides a way to create a signal out of the observer
+// notification subject's for the given `topic`. If `options.initial`
+// is provided it is used as initial value otherwise `null` is used.
+// Constructor can be given `options.id` that will be used to create
+// a `topic` which is namespaced to an add-on (this avoids conflicts
+// when multiple add-on are used, although in a future host probably
+// should just be shared across add-ons). It is also possible to
+// specify a specific `topic` via `options.topic` which is used as
+// without namespacing. Created signal ends whenever add-on is
+// unloaded.
+const InputPort = function InputPort({id, topic, initial}) {
+ this.id = id || topic;
+ this.topic = topic || "sdk:" + addonID + ":" + id;
+ this.value = initial === void(0) ? null : initial;
+ this.observing = false;
+ this[outputs] = [];
+};
+
+// InputPort type implements `Input` signal interface.
+InputPort.prototype = new Input();
+InputPort.prototype.constructor = InputPort;
+
+// When port is started (which is when it's subgraph get's
+// first subscriber) actual observer is registered.
+InputPort.start = input => {
+ input.addListener(input);
+ // Also register add-on unload observer to end this signal
+ // when that happens.
+ addObserver(input, addonUnloadTopic, false);
+};
+InputPort.prototype[start] = InputPort.start;
+
+InputPort.addListener = input => addObserver(input, input.topic, false);
+InputPort.prototype.addListener = InputPort.addListener;
+
+// When port is stopped (which is when it's subgraph has no
+// no subcribers left) an actual observer unregistered.
+// Note that port stopped once it ends as well (which is when
+// add-on is unloaded).
+InputPort.stop = input => {
+ input.removeListener(input);
+ removeObserver(input, addonUnloadTopic);
+};
+InputPort.prototype[stop] = InputPort.stop;
+
+InputPort.removeListener = input => removeObserver(input, input.topic);
+InputPort.prototype.removeListener = InputPort.removeListener;
+
+// `InputPort` also implements `nsIObserver` interface and
+// `nsISupportsWeakReference` interfaces as it's going to be used as such.
+InputPort.prototype.QueryInterface = function(iid) {
+ if (!iid.equals(Ci.nsIObserver) && !iid.equals(Ci.nsISupportsWeakReference))
+ throw Cr.NS_ERROR_NO_INTERFACE;
+
+ return this;
+};
+
+// `InputPort` instances implement `observe` method, which is invoked when
+// observer notifications are dispatched. The `subject` of that notification
+// are received on this signal.
+InputPort.prototype.observe = function(subject, topic, data) {
+ // Unwrap message from the subject. SDK used to have it's own version of
+ // wrappedJSObjects which take precedence, if subject has `wrappedJSObject`
+ // and it's not an XrayWrapper use it as message. Otherwise use subject as
+ // is.
+ const message = subject === null ? null :
+ isLegacyWrapper(subject) ? unwrapLegacy(subject) :
+ isXrayWrapper(subject) ? subject :
+ subject.wrappedJSObject ? subject.wrappedJSObject :
+ subject;
+
+ // If observer topic matches topic of the input port receive a message.
+ if (topic === this.topic) {
+ receive(this, message);
+ }
+
+ // If observe topic is add-on unload topic we create an end message.
+ if (topic === addonUnloadTopic && message === unloadMessage) {
+ end(this);
+ }
+};
+
+exports.InputPort = InputPort;