if (tabId) {
if (browserPorts[tabId]) {
- browserPorts[tabId].postMessage(JSON.stringify(message));
+ browserPorts[tabId].postMessage(message);
} else {
console.warn(
console.debug('new port connected with id ' + tabId);
port.onMessage.addListener(function(msg) {
- console.log("Received message from browser on port " + tabId);
+ console.debug("Received message from browser on port " + tabId);
+
+ if (!msg) { // belt+suspenders
+ console.warn("Received NULL message");
+ return;
+ }
+
+ // tag the message with the browser tab ID for response routing.
+ msg.clientid = tabId;
+
+ if (msg.action == 'init') {
+ // "init" messages require origin info.
+ // Extract just the protocol + host
+ msg.origin = port.sender.url.match(/https?:\/\/[^\/]+/)[0];
+ console.debug("Init'ing message with origin: " + msg.origin);
+ }
+
hatchPort.postMessage(msg);
});
logger.info("Received message id=" +
response.get("msgid") + " action=" + action);
- // init must be called first to set the origin info
+ // init must be called first to set the origin info. Init may be
+ // called multiple times. Each call will update the origin info.
if (action.equals("init")) {
origin = request.getString("origin");
return false;
switch (action) {
case "printers":
- response.put("printers",
+ response.put("content",
new PrintManager().getPrintersAsMaps());
break;
case "keys": // Return stored keys
String pfxKey = request.optString("key");
- response.put("keys", fileIO.keys(pfxKey));
+ response.put("content", fileIO.keys(pfxKey));
break;
case "get":