Can an extension open multiple instances of a native application?

In an extension that uses the native-messaging API to communicate with a C application compiled with SQLite, a browser action button permits the user to open the extension UI, which consists of extension pages only, in multiple tabs.

It is never necessary for the user to open the same extension page in more than one tab but, if it is the user’s preferred method of organization, it will provide something like multiple views of the same SQLite database across multiple tabs instead of within the same single tab.

So, I assume, each browser tab has its own connection to the database. My question is does each browser tab have its own instance of the native C application also?

For example, suppose there is a global variable in RAM in the C application that is incremented each time a message is received from the extension, will the same variable be incremented regardless of which browser tab posted the message or will each tab have its own global variable and count?

I’m not trying to count messages but am using this as an example only to try to understand how the native application is used when there are multiple browser tabs loaded with the same extension page.

Also, is it accurate that regardless of how many browser tabs may be opened with the extension pages loaded and multipe connections to the same SQLite database, it would never be considered a multiple-thread scenario, such that it would always be safe to compile SQLite without the thread-safe option?

Thank you.

How does your extension connect to the native app?
Connection-based messaging or connectionless messaging?

Connection-based messaging.

I’ve written a test extension that calls runtime.connectNative() every time you click on the browser action.
The call starts a native python app that consists of an infinite loop.

Every click on the browser action creates a new instance of the native python app.
(checked with the task manager in Manjaro Linux)

I assume it’s the same with native C apps.
No idea about thread safety, though…

1 Like

Thank you for taking the time to test that out.

I was going to try and have two instances of the same extension page write to the same local file through the C application and see if the global C values were different or shared; but got caught up trying to learn some of the basic SQLite API code, that I haven’t got to it yet.

I likely wouldn’t have known how to confirm if there were really two instances anyway even if the values written to the file were different.

I appreciate it very much. Thanks again.

Do you mind if I ask a non-extension follow-up question? You mention Manjaro Linux. Is that a good distribution? I’m still using Windows 7 and get a bit frustrated trying to follow all of the linux distros or even tell what they all start off with. If I knew more, I’d probably like to start from arch linux and build one to suit me; but I’d be an old man by the time I got it working. Matter of fact, I might be an old man before I get this extension working because I’m pretty close to it now. Thanks.

Definitely!
Unlike some other distros, the software is up-to-date.
It’s fast if you use XFCE.
(even on old computers like mine)
And it’s always been stable.

Though on my system, I’ve had to revert to the previous kernel, because the new one gives an error message related to the display manager (?).
It’s probably fixable but I’m too lazy…

Thank you. I think I’ll give it a try soon.

I was too stupid to think of this before and it is really what I wanted to know.

If an extension acts on a certain set of URLs or a single URL and more than one tab has one of those URLs opened, will each tab have its own instance of the native application?

The reason I asked before is that I wanted to retain prepared SQLite statements for common database tasks and wondered if each tab would require a new instance and copy of those stored statements.

I think the answer is no because the background script can connect to the native application before a target URL is ever loaded. I tried it and the second URL can access the data without making a new connection to the native application, although one can be made, and there is only one process open in the task manager.

I assume that isn’t the right way to go about it though, in that the native application shouldn’t be opened until the first tab of the target URL is opened and it should be closed after there are no more tabs with the target URL opened. I hadn’t thought of that before.