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

Unified Diff: lib/messaging.js

Issue 29730652: Issue 4580 - Stop using ext.onMessage in background page Base URL: https://hg.adblockplus.org/adblockpluschrome/
Patch Set: Created March 22, 2018, 7:53 p.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
« ext/background.js ('K') | « ext/background.js ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/messaging.js
===================================================================
--- a/lib/messaging.js
+++ b/lib/messaging.js
@@ -17,56 +17,48 @@
/** @module messaging */
"use strict";
const {EventEmitter} = require("events");
/**
- * Communication port wrapping ext.onMessage to receive messages.
+ * Communication port wrapping runtime.onMessage to receive messages.
*
* @constructor
*/
function Port()
{
this._eventEmitter = new EventEmitter();
this._onMessage = this._onMessage.bind(this);
- ext.onMessage.addListener(this._onMessage);
+ browser.runtime.onMessage.addListener(this._onMessage);
}
Port.prototype = {
- _onMessage(message, sender, sendResponse)
+ _onMessage(message, sender)
{
- let async = false;
- let callbacks = this._eventEmitter.listeners(message.type);
-
- for (let callback of callbacks)
+ // Add "page" and "frame" if the message was sent by a content script.
Manish Jethani 2018/03/22 19:56:55 This used to be in ext/background.js
+ // If sent by popup or the background page itself, there is no "tab".
+ if ("tab" in sender)
{
- 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);
- }
+ sender = {
+ page: new ext.Page(sender.tab),
+ frame: new ext.Frame(sender.tab.id, sender.frameId, sender.url)
+ };
}
- return async;
+ // The message sender receives only the first response.
Manish Jethani 2018/03/22 19:56:55 Note: using Promise.race here to resolve with firs
+ // https://developer.chrome.com/apps/messaging
+ // https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/runtime/onMessage
+ return Promise.race(
+ this._eventEmitter.listeners(message.type)
+ .map(callback => callback(message, sender))
+ .filter(response => typeof response != "undefined")
+ );
},
/**
* Function to be called when a particular message is received.
*
* @callback Port~messageCallback
* @param {object} message
* @param {object} sender
@@ -98,17 +90,17 @@
this._eventEmitter.off(name, callback);
},
/**
* Disables the port and makes it stop listening to incoming messages.
*/
disconnect()
{
- ext.onMessage.removeListener(this._onMessage);
+ brower.runtime.onMessage.removeListener(this._onMessage);
}
};
/**
* The default port to receive messages.
*
* @type {Port}
*/
« ext/background.js ('K') | « ext/background.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld