Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move extra functions into notifyTools.js #84

Open
wants to merge 1 commit into
base: mailextension
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 43 additions & 37 deletions api/WindowListener/implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,43 @@
* This file is provided by the addon-developer-support repository at
* https://github.com/thundernest/addon-developer-support
*
* Version: 1.42
* - Add notifyLegacy() function to send data to privileged code. Together with the
* onNotifyBackground event a ping-pong-style communication is possible which can
* later be re-created with runtime.onMessage/sendMessage. Example:
* Version: 1.44
* - Add notifyExperiment() function to send data to privileged scripts inside
* an Experiment. The privileged script must include notifyTools.js from the
* addon-developer-support repository.
*
* //in background
* messenger.WindowListener.notifyLegacy({data: "voilá"});
* // In a WebExtension background script:
* // Note: Restrictions of the structured clone algorythm apply to the send data.
* messenger.WindowListener.notifyExperiment({data: "voilá"});
*
* // in privileged code
* let onNotifyLegacyObserver = {
* observe: function (aSubject, aTopic, aData) {
* if (aData != <add-on-id>)
* return;
* console.log(aSubject.wrappedJSObject);
* }
* }
* window.addEventListener("load", function (event) {
* Services.obs.addObserver(onNotifyLegacyObserver, "WindowListenerNotifyLegacyObserver", false);
* window.addEventListener("unload", function (event) {
* Services.obs.removeObserver(onNotifyLegacyObserver, "WindowListenerNotifyLegacyObserver");
* }, false);
* }, false);
*
* Version: 1.41
* - Add onNotifyBackground event, which can be registered in the background page, to receive
* commands from privileged code. Example:
* // In a privileged script inside an Experiment:
* let Listerner1 = notifyTools.registerListener((rv) => console.log("listener #1", rv));
* let Listerner2 = notifyTools.registerListener((rv) => console.log("listener #2", rv));
* let Listerner3 = notifyTools.registerListener((rv) => console.log("listener #3", rv));
* notifyTools.removeListener(Listerner2);
*
* // in background
* messenger.WindowListener.onNotifyBackground.addListener((info) => {
* - Add onNotifyBackground event, which can be registered in the background page,
* to receive data from privileged scripts inside an Experiment. The privileged
* script must include notifyTools.js from the addon-developer-support repository.
*
* // In a WebExtension background script:
* messenger.WindowListener.onNotifyBackground.addListener(async (info) => {
* switch (info.command) {
* case "doSomething":
* soSomething();
* let rv = await doSomething(info.data);
* return {
* result: rv,
* data: [1,2,3]
* };
* break;
* }
* });
*
* // in privileged code
* Services.obs.notifyObservers(
* {command: "doSomething"},
* "WindowListenerNotifyBackgroundObserver",
* <add-on-id>);
* // In a privileged script inside an Experiment:
* let rv = await notifyTools.notifyBackground({command: "doSomething", data: [1,2,3]});
* // rv will be whatever has been returned by the background script.
* // Note: Restrictions of the structured clone algorythm apply to
* // the send and recieved data.
*
* Version: 1.39
* - fix for 68
Expand Down Expand Up @@ -455,19 +451,29 @@ var WindowListener = class extends ExtensionCommon.ExtensionAPI {
};

this.onNotifyBackgroundObserver = {
observe: function (aSubject, aTopic, aData) {
observe: async function (aSubject, aTopic, aData) {
if (self.observerTracker && aData == self.extension.id) {
self.observerTracker(aSubject.wrappedJSObject);
let payload = aSubject.wrappedJSObject;
// This is called from the WL observer.js and therefore it should have a resolve
// payload, but better check.
if (payload.resolve) {
let rv = await self.observerTracker(payload.data);
payload.resolve(rv);
} else {
self.observerTracker(payload.data);
}
}
}
}
return {
WindowListener: {

notifyLegacy(info) {
notifyExperiment(data) {
Services.obs.notifyObservers(
info,
"WindowListenerNotifyLegacyObserver",
// Stuff data in an array so simple strings can be used as payload
// without the observerService complaining.
[data],
"WindowListenerNotifyExperimentObserver",
self.extension.id
);
},
Expand Down
14 changes: 7 additions & 7 deletions api/WindowListener/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@
{
"name": "onNotifyBackground",
"type": "function",
"description": "Fired when a new notification for 'WindowListenerNotifyBackgroundObserver' has been received with aData matching the ID of this extension. Only one listener is supported.",
"description": "Fired when a new notification from notifyTools.js in an Experiment has been received.",
"parameters": [
{
"name": "info",
"name": "data",
"type": "any",
"description": "Info object passed into notifyObserver as aSubject and forwarded to the listener. Restrictions of the structured clone algorythm apply."
"description": "Restrictions of the structured clone algorythm apply."
}
]
}
],
"functions": [
{
"name": "notifyLegacy",
"name": "notifyExperiment",
"type": "function",
"description": "Notifies the 'WindowListenerNotifyLegacyObserver' and passes and uses the provided info object as aSubject for notifyObserver and the add-on ID as aData.",
"description": "Notifies notifyTools.js in an Experiment and sends data.",
"parameters": [
{
"name": "info",
"name": "data",
"type": "any",
"description": "Info object forwarded to the observer. The value will be in aSubject.wrappedJSObject. Restrictions of the structured clone algorythm apply."
"description": "Restrictions of the structured clone algorythm apply."
}
]
},
Expand Down
7 changes: 0 additions & 7 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,23 @@ messenger.runtime.onMessage.addListener((info, sender) => {
}
});


/*
* Register a onNotify listener to catch messages send from privileged scope.
*/
messenger.WindowListener.onNotifyBackground.addListener((info) => {
switch (info.command) {
case "openFirstRunTab":
openFirstRunTab();
// I used LatexIt to implement and test these functions. Left it in as
// a working example for ping-pong communication.
messenger.WindowListener.notifyLegacy(info);
break;
}
});


function openFirstRunTab() {
messenger.tabs.create({
url: "content/firstrun.html"
});
}


(async () => {
messenger.WindowListener.registerDefaultPrefs(
"defaults/preferences/defaults.js");
Expand Down Expand Up @@ -69,4 +63,3 @@ function openFirstRunTab() {
}

})();

55 changes: 55 additions & 0 deletions content/notifyTools.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const ADDON_ID = "[email protected]";

var { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");

var notifyTools = {
registeredCallbacks: {},
registeredCallbacksNextId: 1,

onNotifyExperimentObserver: {
observe: async function (aSubject, aTopic, aData) {
if (ADDON_ID == "") {
throw new Error("notifyTools: ADDON_ID is empty!");
}
if (aData != ADDON_ID) {
return;
}
// The data has been stuffed in an array so simple strings can be used as
// payload without the observerService complaining.
let [data] = aSubject.wrappedJSObject;
for (let registeredCallback of Object.values(notifyTools.registeredCallbacks)) {
registeredCallback(data);
}
}
},

registerListener: function (listener) {
let id = this.registeredCallbacksNextId++;
this.registeredCallbacks[id] = listener;
return id;
},

removeListener: function (id) {
delete this.registeredCallbacks[id];
},

notifyBackground: function (data) {
if (ADDON_ID == "") {
throw new Error("notifyTools: ADDON_ID is empty!");
}
return new Promise(resolve => {
Services.obs.notifyObservers(
{data, resolve},
"WindowListenerNotifyBackgroundObserver",
ADDON_ID
);
});
}
}

window.addEventListener("load", function (event) {
Services.obs.addObserver(notifyTools.onNotifyExperimentObserver, "WindowListenerNotifyExperimentObserver", false);
window.addEventListener("unload", function (event) {
Services.obs.removeObserver(notifyTools.onNotifyExperimentObserver, "WindowListenerNotifyExperimentObserver");
}, false);
}, false);
27 changes: 2 additions & 25 deletions content/options.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,3 @@
var { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");

// I used LatexIt to implement and test these functions. Left it in as
// a working example for ping-pong communication.
let onNotifyLegacyObserver = {
observe: function (aSubject, aTopic, aData) {
if (aData != "[email protected]") {
return;
}
console.log(aSubject.wrappedJSObject);
}
}
window.addEventListener("load", function (event) {
Services.obs.addObserver(onNotifyLegacyObserver, "WindowListenerNotifyLegacyObserver", false);
window.addEventListener("unload", function (event) {
Services.obs.removeObserver(onNotifyLegacyObserver, "WindowListenerNotifyLegacyObserver");
}, false);
}, false);

function pick_file(pref, title) {
var nsIFilePicker = Components.interfaces.nsIFilePicker;
var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
Expand All @@ -30,12 +11,8 @@ function pick_file(pref, title) {
});
}

function open_autodetect() {
// Notify WebExtension Background to open the first run tab.
Services.obs.notifyObservers(
{command: "openFirstRunTab"},
"WindowListenerNotifyBackgroundObserver",
"[email protected]");
async function open_autodetect() {
notifyTools.notifyBackground({command: "openFirstRunTab"});
}

window.addEventListener("load", function (event) {
Expand Down
1 change: 1 addition & 0 deletions content/options.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
xmlns:html="http://www.w3.org/1999/xhtml"
>
<script type="application/javascript" src="chrome://tblatex/content/notifyTools.js" />
<script type="application/javascript" src="chrome://tblatex/content/options.js" />

<groupbox>
Expand Down