Hi Aaron, Thanks for your answers, In my testing I see that a background page and a regular page do run separate javascript threads. Here is the answer as I understand it, although I would like confirmation and a guarantee that it will always work this way....
The reason (from what I can see) why there is no race condition here is because each message sent to another page (both connect and posted messages) is sent down a single pipe which is read and dispatched in the order received by the receiving thread. (Messages are also sent immediately down this pipe (as soon as connect or postMessage is called).) This means there is no race condition in either of the examples I have given. If for example the messages would be queued to their receive listeners by the sending thread, then it would be possible for a posted message to not have a listener hooked up yet and thus be discarded by the sending thread before the receiving thread processed the connect and hooked up the onMessage. This could be a problem for both of the examples given, but this is not how it is done from what I can see. Thanx, Nachum On Dec 5, 2:01 am, Aaron Boodman <[email protected]> wrote: > Good question.. > > On Fri, Dec 4, 2009 at 8:19 PM, nachumk <[email protected]> wrote: > > function createExtensionInMainPage() { > > this.port = chrome.extension.connect(); > > this.port.onMessage = extOnMessage; > > > this.port.postMessage('ready'); // *** I don't know if the background > > page has hooked up it's onMessage! > > } > > The system promises to run background page initialization code before > attempting to deliver any messages from content scripts, so this is > fine. > > > function extOnMessage(msg) { > > } > > > function createBackgroundPage() { > > chrome.extension.onConnect.addListener(backOnConnect); > > } > > > function backOnConnect(port) { > > port.onMessage.addListener(backOnMessage); > > > port.postMessage('alternateReady'); // *** I also don't know if the > > main page has hooked up it's onMessage! > > } > > JavaScript is not multithreaded, it is event driven. > > So: > > port.sendMessage(...); > port.onMessage.addListener(function() { > > }); > > You never need to worry about the other end of the connection sending > the message before the onMessage event is hooked, that is impossible. > All the code after sendMessage will run to completion before any > asynchronous events are delivered. > > - a -- You received this message because you are subscribed to the Google Groups "Chromium-extensions" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/chromium-extensions?hl=en.
