Opened 4 years ago

Last modified 3 years ago

#3499 closed change

Create a clean messaging API for internal use — at Version 1

Reported by: trev Assignee: trev
Priority: P3 Milestone: Adblock-Plus-2.7.3-for-Firefox
Module: Adblock-Plus-for-Firefox Keywords:
Cc: sebastian, sergz Blocked By:
Blocking: Platform: Unknown / Cross platform
Ready: yes Confidential: no
Tester: Ross Verified working: yes
Review URL(s):

https://codereview.adblockplus.org/29338275/
https://codereview.adblockplus.org/29338279/
https://codereview.adblockplus.org/29338283/
https://codereview.adblockplus.org/29338417/
https://codereview.adblockplus.org/29338421/
https://codereview.adblockplus.org/29338425/

Description (last modified by trev)

Background

For our E10S support, we are currently using message manager directly. This results in overly complicated code however: message names have to avoid conflicts with other extensions, message handlers have to be removed explicitly, response handling has to be implemented explicitly, messaging code is duplicated for the first-run page.

What to change

Create a clean internal API, accessible in both parent and child via the messaging module:

  • Port constructor binds a communication port to a message manager.
  • port instance is the default port (communicating with the parent for process scripts, broadcasting to all children for the parent).
  • Port.disconnect() shuts down the port (done automatically for the default port when the extension is shut down).
  • Port.on() adds a handler for a particular message (message names are specific to Adblock Plus and won't conflict with other extensions).
  • Port.off() removes a handler.
  • Port.emit() sends/broadcasts an asynchronous message, if provided the response callback will be triggered with the response.
  • Port.emitSync() sends a synchronous message, response is returned from the function.

Message handlers have three parameters: payload (the data sent by the sender), sender (a Port instance) and sendResponse (callback to be called with the response). The handler has to return true if it wants to use the callback asynchronously, otherwise it will expire when the function returns.

Change History (1)

comment:1 Changed 4 years ago by trev

  • Description modified (diff)
Note: See TracTickets for help on using tickets.