Search code examples
google-chromegoogle-chrome-extensionnpapichrome-native-messaging

Can i embed program through native messaging in Chrome (like old npapi plugins)?


Is it possible to create Chrome extension which can embed a program into Chrome? I mean something like old NPAPI plugins such as VLC player, java, etc... I found some native messaging examples, but every example is only for sending simple messages to control an external program.

But for example IE Tab is using native messaging and their IE tab engine is running inside in Chrome... or does IE Tab use something else?


Solution

  • Updated answer:

    So as pointed out in comments, it does obviously use native messaging; what I can't figure out is what they are doing. The project claims to be open source (and GPL) but the source is not to be found in the linked repo; the most recent commit is Dec 13, which is nowhere near useful.

    From looking at the extension code I would guess that they are somehow using either some sort of interesting hackery using system APIs which may not be supported by Chrome long term or some sort of undocumented APIs; they set the document title to contain 'ietaba:' + the IE tab ID and then post a message to the native message host with that info.

    chrome.tabs.getCurrent(function(tab) {
        // We can't attach if we don't have a window id or aren't active
        if (!tab.active || !this.windowId) {
            this.restoreTitle();
            return;
        }
    
        // Remember the title change is asynchronous, so don't keep changing it or the helper
        // will never find it.  We may have to retry several times to find the window after
        // a single title change
        if (document.title.indexOf('ietaba:') == -1) {
            this.realTitle = document.title;
            document.title = 'ietaba:' + Background.getNextIETabId();
        }
    
        var msg = {
            type: 'ATTACH',
            tabTitle: document.title,
            innerWidth: this.getIEWidth(),
            innerHeight: this.getIEHeight()
        }
        NativeHost.postMessage(msg);
    }.bind(this));
    

    What I can't figure out is how they could use that to attach to it; they seem to be somehow getting an HWND for that section. It makes me wonder if Google gave them special APIs or if they found some clever hack. It seems a bit suspicious that the "GPL" project doesn't release its code...

    UPDATE

    After doing some more digging, I've found that you can enumerate open HWNDs in the system to get the HWND for the tab they are starting up; they set a unique tab ID in the title, so they just enumerate HWNDs until they find the one they want. After that I imagine they just instantiate the activex control into that spot.

    That said, this seems like something that could break pretty easily if Chrome changes the wrong things. I also don't see how you could easily target a specific part of a page; you'd have to be drawing to a specific region and it'd be tricky to get it all just right. I don't think I'd want to do it this way, but I can see how they must be doing it.

    Someone should pester them to release their code, since they are in violation of the GPL by not doing so.

    FURTHER UPDATE

    Just to verify things, I tried changing the "ATTACH" event to attach to "Developer Tools" instead -- as a result I now have a developer tools chrome window open with a web page in it.