Support message response and reply callbacks
ClosedPublic

Authored by broulik on May 10 2019, 7:39 AM.

Details

Summary

This allows to make use of the sendReply/sendResponse callbacks allowing for the callee to reply directly without having to manually track state and relate a message to a reply.
Also add a bit of documentation on the required callback signature.

Test Plan

Sent a message with a callback, replied, got reply in callback.
Returned true from a callback and stored the callback in a variable, did some processing, replied successfully at a later point.

Diff Detail

Repository
R856 Plasma Browser Integration
Lint
Automatic diff as part of commit; lint not applicable.
Unit
Automatic diff as part of commit; unit tests not applicable.
broulik created this revision.May 10 2019, 7:39 AM
Restricted Application added a project: Plasma. · View Herald TranscriptMay 10 2019, 7:39 AM
Restricted Application added a subscriber: plasma-devel. · View Herald Transcript
broulik requested review of this revision.May 10 2019, 7:39 AM
fvogt requested changes to this revision.May 27 2019, 12:00 PM

browser.runtime.sendMessage does not seem to have a callback parameter.

See https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/sendMessage:

if two arguments are given:
the arguments are interpreted as (message, options), and the message is sent internally, if the second argument is any of the following:
a valid options object (meaning, it is an object which contains only the properties of options that the browser supports)
null
undefined
otherwise, the arguments are interpreted as (extensionId, message). The message will be sent to the extension identified by extensionId.

This revision now requires changes to proceed.May 27 2019, 12:00 PM
broulik added a comment.EditedJun 11 2019, 1:29 PM

It works just fine in Firefox 67 here. Maybe the documentation is outdated?

browser.runtime.sendMessage({foo: "bar"}, callback)

works just fine. Also, I find posts about people using that in Firefox as early as 2017, so this is not a recent addition.

broulik planned changes to this revision.Jun 13 2019, 5:28 PM

Ok, I have an idea.
browser.runtime.sendMessage (Firefox) returns a promise that is fulfilled when a reply is sent. Chrome instead uses a callback function in chrome.runtime.sendMessage
I'm gonna rework the sendMessage() to always return a Promise (makes for nicer code) by either forwarding the promise from Firefox or creating one forwarding the callback in Chrome

broulik updated this revision to Diff 62129.Jul 20 2019, 4:22 PM
  • Use promises
fvogt added a comment.Jul 20 2019, 4:40 PM

Code looks good and seems to work fine in vivaldi, but I get an error in firefox:

Promise resolved after context unloaded
extension.js:78
    sendSettings moz-extension://d1f34b4e-4e97-40a5-b4ac-03dc2fea30a9/extension.js:78

appears to be harmless though?

fvogt accepted this revision.Jul 21 2019, 11:16 AM

Code looks good and seems to work fine in vivaldi, but I get an error in firefox:

Promise resolved after context unloaded
extension.js:78
    sendSettings moz-extension://d1f34b4e-4e97-40a5-b4ac-03dc2fea30a9/extension.js:78

appears to be harmless though?

Couldn't reproduce again, so likely fine.

This revision is now accepted and ready to land.Jul 21 2019, 11:16 AM
This revision was automatically updated to reflect the committed changes.