jstuardo
jstuardo

Reputation: 4373

Google chrome Extension Native Messaging host written in C# is not run at computer startup

I have developed a Google chrome Extension that uses Native Messaging communication.

Well... extension works perfectly, but the problem is that Native Messaging host is not launched at computer startup. To launch it, I have to open chrome, list extensions, find my extension and then disable and re-enable it.

One thing I have realized of. is that when I disable and re-enable the extension and see task manager, I could see that 3 processes started running: chrome.exe, cmd and my native messaging host exe.

Maybe all 3 processes are not launched at startup because of the "cmd" command?

Why the cmd command is launched together with the native messaging host exe? How can I avoid it?

EDIT:

This is the manifest.json file:

{
  "manifest_version": 2,

  "name": "Busca Cliente Lipigas",
  "short_name": "Busca Cliente",
  "description": "Permite recibir un número de teléfono desde SoftPhone y realizar la búsqueda del cliente en la página Web de Lipigas.",
  "version": "1.1",

  "permissions": [
    "tabs",
    "background",
    "nativeMessaging"
  ],

  "icons" : { "16": "img/icon16.png",
              "48": "img/icon48.png",
              "128": "img/icon128.png" },

  "background": {
    "scripts": ["main.js"],
    "persistent": false
    }

}

EDIT:

This is the background page:

var clientSearchPage;
var clientEditPage;
var executed;

//console.log('Extensión iniciada.');
var port = chrome.runtime.connectNative('com.desytec.lipigas.sll');

port.onMessage.addListener(function (msg) {
    //console.log("Se recibió el comando " + msg.command + ' con el parámetro ' + msg.parameter);

    port.postMessage({ status: processCommand(msg.command + ' ' + msg.parameter) });
});

chrome.runtime.onSuspend.addListener(function () {
    port.postMessage({ status: 'EXIT' });
})

function processCommand(cmd) {    
    var parts = cmd.split(/\s+/);
    try {
        switch (parts[0]) {
            case 'Phone':
                executed = false;
                setPhone(parts[1]);
                return '+OK-Phone';
            case 'SetClientSearchPage':
                clientSearchPage = parts[1];
                if (clientSearchPage[0] != '/')
                    clientSearchPage = '/' + clientSearchPage;
                //console.log('Se configuró la página de búsqueda del cliente ' + clientSearchPage);
                return '+OK-SetClientSearchPage';
            case 'SetClientEditPage':
                clientEditPage = parts[1];
                if (clientEditPage[0] != '/')
                    clientEditPage = '/' + clientEditPage;
                //console.log('Se configuró la página de edición del cliente ' + clientEditPage);
                return '+OK-SetClientEditPage';
            default:
                return '+ERR-Comando No Encontrado';
        }
        //tcpConnection.sendMessage(Commands.run(cmd[0], cmd.slice(1)));
    } catch (ex) {
        return '+ERR-Comando No Procesado'
    }
}

function setPhone(phone) {
    chrome.tabs.query({
    }, function (tabs) {
        var tab = null;

        for (var i = 0; i < tabs.length; i++) {
            var tabURL = tabs[i].url;
            if (tabURL.indexOf(clientSearchPage) != -1 || tabURL.indexOf(clientEditPage) != -1)
                tab = tabs[i];
            //console.log(tabURL);
        }

        if (!executed) {
            searchClient(tab, phone);
            executed = true;
        }
    });
}

function searchClient(tab, phone) {
    if (tab == null)
        console.log('No se encontró ninguna de las páginas del cliente: ' + clientSearchPage + ', ' + clientEditPage);
    else {
        var site = '';
        var url = tab.url;
        if (url.indexOf(clientEditPage) != -1)
            site = url.substr(0, url.indexOf(clientEditPage));
        else if (url.indexOf(clientSearchPage) != -1)
            site = url.substr(0, url.indexOf(clientSearchPage));

        //console.log(site + clientSearchPage + '?telefono=' + phone);

        chrome.tabs.update(tab.id, { active: true, url: site + clientSearchPage + '?telefono=' + phone });

        //console.log('Se actualizó la URL con: ' + site + clientSearchPage + '?telefono=' + phone);
    }
}

Upvotes: 0

Views: 1909

Answers (1)

woxxom
woxxom

Reputation: 73526

  1. Since the native messaging host is started only by chrome.runtime.connectNative as per the documentation, you need to start your extension at the computer startup by specifying the "background" permission in your extension's manifest.json:

    "permissions": ["background", "nativeMessaging"],
    

    When any installed hosted app, packaged app, or extension has "background" permission, Chrome runs (invisibly) as soon as the user logs into their computer—before the user launches Chrome. The "background" permission also makes Chrome continue running (even after its last window is closed) until the user explicitly quits Chrome.

    This behavior can be disabled by a user in Chrome's settings:
    [x] Continue running background apps when Google Chrome is closed option.

  2. cmd is launched to create the stdio pipeline. On Windows the command line is:

    C:\Windows\system32\cmd.exe /c YOURHOSTAPP.exe --parent-window=10158736 chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/ < \.\pipe\chrome.nativeMessaging.in.9c12d69ad9deb1ce > \.\pipe\chrome.nativeMessaging.out.9c12d69ad9deb1ce

    As of Oct 5, 2016 --parent-window= parameter will be moved after chrome-extension://.

Upvotes: 2

Related Questions