0

I have a Google Chrome Extension which uses a native host. This is used only on a windows box and the extension's registry settings are added along with the installation of the native host exe. Currently the the port (chrome.runtime.connectNative) or native host is started when the extensions background script is loaded. This currently means that the native host runs whenever Google Chrome is running.

The extension is used for only 1 website "www.example.com" and so content scripts only run when a tab with this website is loaded. This means that the native host would only be needed when a tab with this website is loaded and not all the time.

I now want to also create an Edge Chromium extension and give users the option to use either Edge or Chrome. As I indicated above both extensions would be "installed" meaning the registry keys added on installation of the native host. This means the extensions would be there for both browsers.

My main question is thus when and how should the native host be started.

From this main question I have a whole host of thoughts or questions;

  1. When the user chooses to use Edge or Chromium should I at that point be making the changes to the registry? Problem here is users can of course independently of my app install the extension.
  2. Is there a problem just leaving things as they are. In other words if Edge and Chromium are running then there are 2 different native hosts running and my app simply chooses which native host to communicate with based on the users choice. Problem here is you can see the native host process running in task manager and it would therefore be running for a browser that the user has not chosen which might not be OK for some users.
  3. I thought about the extension having a flag to know if it should start the native host. Problem here is how could I turn this flag on. Without the native host I am unaware of a way to interact with the extension.

There are possibly more options so happy to hear them as well.

darbid
  • 2,545
  • 23
  • 55

1 Answers1

2

changes to the registry

Add chrome-extension://id/ with the id of your second extension to allowed_origins in the host's manifest as shown in the documentation.

there are 2 different native hosts running

Each host is started by its respective extension and it can communicate only to that parent extension, there's no confusion.

a flag to know if it should start the native host. Problem here is how could I turn this flag on. Without the native host I am unaware of a way to interact with the extension.

It depends on what the host and the extension do. Maybe you don't need to run the host all the time or maybe you can start it only when a tab with the matching URL is loaded. Maybe you can use the new experimental onConnectNative mechanism.

wOxxOm
  • 65,848
  • 11
  • 132
  • 136
  • Thank you for the answer. So you are suggesting 1 hosts file which both browser will use, right? This will mean the hosts file will have to know for which browser it is working for. – darbid May 05 '20 at 11:25
  • Quoting the documentation "The first argument to the native messaging host is the origin of the caller" so you can use that. Or your extension can send a message after connecting with the userAgent string, for example. – wOxxOm May 05 '20 at 11:30
  • Yep got that. I have a question here about those args. Which you were also commenting on. – darbid May 05 '20 at 12:38
  • I can start a new question if you like, but I just loaded the unpacked extension in Edge and it gave me the same ID as in Chrome meaning that the host (at least for testing) cannot be distinguished. – darbid May 05 '20 at 13:22
  • The temporary extension id is a hash of the full path. It'll be different in the web store. You can force a different id by making a symlink (or an NTFS junction) of the directory to a different name and loading it in the other browser. – wOxxOm May 05 '20 at 13:24
  • saved me a heap of searching. For anyone reading this for windows you need to make a junction with "mklink /J" if you make a symlink with "mklink /D" you get the same extension ID. – darbid May 05 '20 at 14:00