Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: lib/messaging.js

Issue 29338491: Issue 3823 - Split up message responder code (Closed)
Patch Set: Moved get-domain-enabled-state to whitelisting module Created March 22, 2016, 8:22 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/filterComposer.js ('k') | lib/whitelisting.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/messaging.js
===================================================================
new file mode 100644
--- /dev/null
+++ b/lib/messaging.js
@@ -0,0 +1,141 @@
+/*
+ * This file is part of Adblock Plus <https://adblockplus.org/>,
+ * Copyright (C) 2006-2016 Eyeo GmbH
+ *
+ * Adblock Plus is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3 as
+ * published by the Free Software Foundation.
+ *
+ * Adblock Plus is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/** @module messaging */
+
+"use strict";
+
+/**
+ * Communication port wrapping ext.onMessage to receive messages.
+ *
+ * @constructor
+ */
+function Port()
+{
+ this._callbacks = Object.create(null);
+ this._onMessage = this._onMessage.bind(this);
+ ext.onMessage.addListener(this._onMessage);
+};
+
+Port.prototype = {
+ _onMessage: function(message, sender, sendResponse)
+ {
+ let async = false;
+ let callbacks = this._callbacks[message.type];
+
+ if (callbacks)
+ {
+ for (let callback of callbacks)
+ {
+ let response = callback(message, sender);
+
+ if (response && typeof response.then == "function")
+ {
+ response.then(
+ sendResponse,
+ reason => {
+ console.error(reason);
+ sendResponse(undefined);
+ }
+ );
+ async = true;
+ }
+ else if (typeof response != "undefined")
+ {
+ sendResponse(response);
+ }
+ }
+ }
+
+ return async;
+ },
+
+ /**
+ * Function to be called when a particular message is received.
+ *
+ * @callback Port~messageCallback
+ * @param {object} message
+ * @param {object} sender
+ * @return The callback can return undefined (no response),
+ * a value (response to be sent to sender immediately)
+ * or a promise (asynchronous response).
+ */
+
+ /**
+ * Adds a callback for the specified message.
+ *
+ * The return value of the callback (if not undefined) is sent as response.
+ * @param {string} name
+ * @param {Port~messageCallback} callback
+ */
+ on: function(name, callback)
+ {
+ if (name in this._callbacks)
+ this._callbacks[name].push(callback);
+ else
+ this._callbacks[name] = [callback];
+ },
+
+ /**
+ * Removes a callback for the specified message.
+ *
+ * @param {string} name
+ * @param {Port~messageCallback} callback
+ */
+ off: function(name, callback)
+ {
+ let callbacks = this._callbacks[name];
+ if (callbacks)
+ {
+ let idx = callbacks.indexOf(callback);
+ if (idx != -1)
+ callbacks.splice(idx, 1);
+ }
+ },
+
+ /**
+ * Disables the port and makes it stop listening to incoming messages.
+ */
+ disconnect: function()
+ {
+ ext.onMessage.removeListener(this._onMessage);
+ }
+};
+
+/**
+ * The default port to receive messages.
+ *
+ * @type {Port}
+ */
+exports.port = new Port();
+
+/**
+ * Creates a new port that is disconnected when the given window is unloaded.
+ *
+ * @param {Window} window
+ * @return {Port}
+ */
+exports.getPort = function(window)
+{
+ let port = new Port();
+ window.addEventListener("unload", () =>
+ {
+ port.disconnect();
+ });
+ return port;
+};
+
« no previous file with comments | « lib/filterComposer.js ('k') | lib/whitelisting.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld