Manually closing a port between content and extension

719 views
Skip to first unread message

Avishay

unread,
Jul 5, 2011, 5:21:06 AM7/5/11
to Chromium-extensions
I create a port between content and extension using
chrome.extension.connect. Is there a way to manually close it (trigger
the onDisconnet event)?

Thanks, Avishay.

Avishay Ben Shabtai

unread,
Jul 5, 2011, 10:07:55 AM7/5/11
to Michael Gundlach, Chromium-extensions
i didnt see that port supports this function? are you sure it supports it?

On Tue, Jul 5, 2011 at 5:05 PM, Michael Gundlach <adblockf...@gmail.com> wrote:
I seem to recall you can just call port.disconnect() ?

Michael


--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.
For more options, visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/?hl=en.



Michael Gundlach

unread,
Jul 5, 2011, 10:05:55 AM7/5/11
to Avishay, Chromium-extensions
I seem to recall you can just call port.disconnect() ?

Michael

On Tue, Jul 5, 2011 at 5:21 AM, Avishay <avishay.b...@gmail.com> wrote:

Michael Gundlach

unread,
Jul 5, 2011, 10:18:33 AM7/5/11
to Avishay Ben Shabtai, Chromium-extensions
Here's the implementation of chrome.extension.sendRequest (found by putting a breakpoint in an extension then typing 'chrome.extension.sendRequest' into the Console).  Note near the bottom that the sender waits for a response on a port then .disconnect()s the port. - Michael

function (targetId_opt, request, responseCallback_opt) { var targetId = extensionId; var responseCallback = null; var lastArg = arguments.length - 1; if (typeof(arguments[lastArg]) == "function") responseCallback = arguments[lastArg--]; request = arguments[lastArg--]; if (lastArg >= 0 && typeof(arguments[lastArg]) == "string") targetId = arguments[lastArg--]; if (lastArg != -1) throw new Error("Invalid arguments to sendRequest."); var port = chrome.extension.connect(targetId, {name: chromeHidden.kRequestChannel}); port.postMessage(request); port.onDisconnect.addListener(function() { // For onDisconnects, we only notify the callback if there was an error try { if (chrome.extension.lastError && responseCallback) responseCallback(); } finally { port = null; } }); port.onMessage.addListener(function(response) { try { if (responseCallback) responseCallback(response); } finally { port.disconnect(); port = null; } }); }
Reply all
Reply to author
Forward
0 new messages