-->

Native Messaging host tried sending a message that

2020-05-01 07:26发布

问题:

I'm using a java jar to send and receive messages using Chrome Native Messaging.

I enabled logging of Chrome so I could read C:\Users\%UserName%\AppData\Local\Google\Chrome\User Data\chrome_debug.log file

I'm actually unable to send or receive message with my Java app, but I know it is used.

Here is the manifest of my host :

{
   "allowed_origins" : 
    [ 
        "chrome-extension://EXTENSION-ID/" 
    ],
   "description" : "my.app",
   "name" : "my.app",
   "path" : "launch.bat",
   "type" : "stdio"
}

Here is content of the Batch file launch.bat :

java -jar "%~dp0ChromeSEOConnector.jar"

And here is my Java code :

private String readMessage(InputStream in) throws IOException {
        byte[] b = new byte[4];
        in.read(b);

        int size = getInt(b);

        b = new byte[size];
        in.read(b);

        return new String(b, "UTF-8");
    }

private void sendMessage(String message) throws IOException {
        Text text = new Text(message);
        String resposta = serializer.toJson(text);
        System.out.write(getBytes(resposta.length()));
        System.out.write(resposta.getBytes("UTF-8"));
        System.out.flush();
    }

public int getInt(byte[] bytes) {
        return (bytes[3] << 24) & 0xff000000 |
                (bytes[2] << 16) & 0x00ff0000 |
                (bytes[1] << 8) & 0x0000ff00 |
                (bytes[0] << 0) & 0x000000ff;
    }

 public byte[] getBytes(int length) {
        byte[] bytes = new byte[4];
        bytes[0] = (byte) (length & 0xFF);
        bytes[1] = (byte) ((length >> 8) & 0xFF);
        bytes[2] = (byte) ((length >> 16) & 0xFF);
        bytes[3] = (byte) ((length >> 24) & 0xFF);
        return bytes;
    }

It seems like the System.in does never get the input of my app, and the System.out never sends data also.

Chrome keeps on getting the same error :

Native Messaging host tried sending a message that is 977472013 bytes long.

What is weird is that the size of the message is always the same, even if I change manually the size of the message sent, as if the message was not analyzed at all. Did you encounter that kind of error ? Thanks in advance

回答1:

I think you have to swap the order of the bytes defining your message length. Change your getBytes() method to this:

public byte[] getBytes(int length) {
    byte[] bytes = new byte[4];
    bytes[3] = (byte) (length & 0xFF);
    bytes[2] = (byte) ((length >> 8) & 0xFF);
    bytes[1] = (byte) ((length >> 16) & 0xFF);
    bytes[0] = (byte) ((length >> 24) & 0xFF);
    return bytes;
}


回答2:

Many answers suggest checking the correctness of processing the first four bytes. But this is not always the real reason. It seems that in your case the reason is the absence of an @echo off in launch.bat.

When an error occurs

Native Messaging host tried sending a message that is 977472013 bytes long

First of all, try to start the application from the command line, it is possible that the output is "trash"