Firefox web extension native messageing to Scala application exceeding limits of bytes for messages

0

On starting the java application from the firefox browser plugin, I get an error before even sending a message (so I am assuming this is some 'handshake').

I am on Ubuntu I am using JDK: java-11-openjdk-amd64

I start the native application through a sh script:

#!/bin/sh
set echo off
cd "/home/empeor/work/COLLEGE/final year project/application/NativeApplication/out/artifacts/NativeApplication_jar"
java -Dfile.encoding=UTF-8 -jar NativeApplication.jar

My test localApplication Scala (main) object is:

object LocalApplication extends App {
  val logger = Logger("Local application")

  var shook_hands=false

  logger.info("native application started")

  System.out.println("started application")

  var requestJson = "";
  while(!requestJson.equals("done")){
    requestJson = readMessage(System.in)
    logger.info("the message is : " + requestJson)
    sendMessage("{'message':'hello''}")
  }

  logger.info("recieved message says: " + requestJson)

  private def readMessage(in: InputStream):String = {
    logger.info("trying to read in input")
    var b = new Array[Byte](4)
    in.read(b,0,4) // Read the size of message

    val size = getInt(b)
    logger.info("read input length: " + size)

    if (size == 0) throw new InterruptedIOException("Blocked communication")
    b = new Array[Byte](size)
    in.read(b,0,size)
    val theMessage = new String(b, "UTF-8")
    logger.info("read input message: " + theMessage)
    return  theMessage
  }

  @throws[IOException]
  private def sendMessage(message: String): Unit = {
    System.out.write(getBytes(message.length))
    System.out.write(message.getBytes("UTF-8"))
    System.out.flush()
  }

  def getInt(bytes: Array[Byte]): Int =
    return ((bytes(3) << 24) & 0xff000000 |
      (bytes(2) << 16) & 0x00ff0000 |
      (bytes(1) << 8) & 0x0000ff00 |
      (bytes(0) << 0) & 0x000000ff)

  def getBytes(length: Int):Array[Byte] = {
    val bytes = new Array[Byte](4)
    bytes(0) = (length & 0xFF).toByte
    bytes(1) = ((length >> 8) & 0xFF).toByte
    bytes(2) = ((length >> 16) & 0xFF).toByte
    bytes(3) = ((length >> 24) & 0xFF).toByte
    return bytes
  }

  System.out.println("ended application")

}

I think the reason for this 'exceeding byte limit' error is because of incorrect encoding of the Scala system.in/out encoding (firefox native messaging uses UTF-8). Am I setting the encoding in Scala correctly?

The exact error messaging is:

1609255145622   addons.xpi  WARN    Addon with ID NativeApplication@example.org already installed, older version will be disabled
InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable 2 PictureInPictureChild.jsm:298
ExtensionError: Native application tried to send a message of 1918989427 bytes, which exceeds the limit of 1048576 bytes. ExtensionUtils.jsm:58:5
stderr output from native app NativeApplication: Exception in thread "main" java.io.InterruptedIOException: Blocked communication
stderr output from native app NativeApplication:    at LocalApplication$.readMessage(LocalApplication.scala:118)
stderr output from native app NativeApplication:    at LocalApplication$.delayedEndpoint$LocalApplication$1(LocalApplication.scala:62)
stderr output from native app NativeApplication:    at LocalApplication$delayedInit$body.apply(LocalApplication.scala:51)
stderr output from native app NativeApplication:    at scala.Function0.apply$mcV$sp(Function0.scala:39)
stderr output from native app NativeApplication:    at scala.Function0.apply$mcV$sp$(Function0.scala:39)
stderr output from native app NativeApplication:    at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:17)
stderr output from native app NativeApplication:    at scala.App.$anonfun$main$1(App.scala:76)
stderr output from native app NativeApplication:    at scala.App.$anonfun$main$1$adapted(App.scala:76)
stderr output from native app NativeApplication:    at scala.collection.IterableOnceOps.foreach(IterableOnce.scala:563)
stderr output from native app NativeApplication:    at scala.collection.IterableOnceOps.foreach$(IterableOnce.scala:561)
stderr output from native app NativeApplication:    at scala.collection.AbstractIterable.foreach(Iterable.scala:919)
stderr output from native app NativeApplication:    at scala.App.main(App.scala:76)
stderr output from native app NativeApplication:    at scala.App.main$(App.scala:74)
stderr output from native app NativeApplication:    at LocalApplication$.main(LocalApplication.scala:51)
stderr output from native app NativeApplication:    at LocalApplication.main(LocalApplication.scala)

Just to add: I can't send message through:

browser.browserAction.onClicked.addListener(() => {
console.log("Sending:  ping");
port.postMessage("ping");
});

because it gives error: Attempt to postMessage on disconnected port

java
scala
ubuntu
firefox-addon
firefox-addon-webextensions
asked on Stack Overflow Dec 29, 2020 by user3038404 • edited Dec 29, 2020 by user3038404

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0