0

I took this chrome app example for NativeMessaging, it allows a python program to run and talk back to app. I converted it to extension and am trying to replace the python script with a C program but my C program is not able to communicate back to extension.my question is, Is it possible to use C instead python (looks possible to me, at least). if it is possible then what am I doing wrong?

Code for replacement of python:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>

#define NUM_THREADS 2

 int t1=0;
void *thr_fwrite(void *arg) {
        FILE *p;
        if(arg==NULL)
                p=(FILE*)arg;
        char buffer[200]={"0023 hello how are you    q"};;
        int len=27;
while(1)
{
        if(t1==0){
        fwrite(buffer,len,sizeof(char),stdout);
        fwrite(buffer,len,sizeof(char),p);
        t1=1;
        }

}
  pthread_exit(NULL);
}
/* thread function */
void *thr_fread(void *arg) {
        FILE *p;
        if(arg==NULL)
                p=(FILE*)arg;
        char buffer[200]={""};
        int len=0;
while(1)
{
        if(t1==1){
        fread(buffer,1,sizeof(int),stdin);
        if(strcmp(buffer,"")==0)
                exit(0);
        len=atoi(buffer);
        if(len==0 || len>200)
                exit(0);
        fread(buffer,len,sizeof(char),stdin);
        fwrite(buffer,len,sizeof(char),p);
        t1=0;
        }

}
  pthread_exit(NULL);
}

int main(int argc, char **argv) {
  pthread_t thr[NUM_THREADS];
  int i, rc;

FILE *p;
FILE *q;


p=fopen("~/temp.txt","w+");
q=fopen("~/temp1.txt","w+");
  /* create threads */
    if ((rc = pthread_create(&thr[0], NULL, thr_fwrite, &p))) {
      fprintf(stderr, "error: pthread_create, rc: %d\n", rc);
      return EXIT_FAILURE;
    }
    if ((rc = pthread_create(&thr[1], NULL, thr_fread, &q))) {
      fprintf(stderr, "error: pthread_create, rc: %d\n", rc);
      return EXIT_FAILURE;
    }
  /* block until all threads complete */
  for (i = 0; i < NUM_THREADS; ++i) {
    pthread_join(thr[i], NULL);
  }

  return EXIT_SUCCESS;
}

partial code for chrome extension code is:

function ConnectHost() {
  var hostName = "com.example.nativehost";
  port = chrome.runtime.connectNative(hostName);
  if(port == null)
  {
     alert("Could not connect to Host Client");
  }
  else
  { 
    port.onMessage.addListener(onNativeMessage);
    port.onDisconnect.addListener(onDisconnected);
  }     

function onDisconnected() {
  alert(chrome.runtime.lastError.message);
  port = null;
}

function onNativeMessage(message) {
  alert(message);
  SendToNative("exit");
}

Do we have any example already which uses C instead of python?

Cœur
  • 37,241
  • 25
  • 195
  • 267
VikasPushkar
  • 392
  • 3
  • 18

1 Answers1

2

Chrome doesn't care (and cannot know) what's on the other side as long as the program answers in expected format (see Native Messaging Protocol). Which does not seem to be the case here from a brief glance at your code.

Chrome will only accept messages consisting of 4-byte length header + UTF-8 encoded string that is valid JSON.

There are some examples of C Native Hosts on SO.

An important caveat to consider is that the communication should be in binary mode, not text mode:

Windows-only: Make sure that the program's I/O mode is set to O_BINARY. By default, the I/O mode is O_TEXT, which corrupts the message format as line breaks (\n = 0A) are replaced with Windows-style line endings (\r\n = 0D 0A). The I/O mode can be set using __setmode.

Community
  • 1
  • 1
Xan
  • 74,770
  • 16
  • 179
  • 206