From 19dbbb0e1c5d91bd0ded67c65ef3c3d274d61bad Mon Sep 17 00:00:00 2001 From: Jin-Woo Jeong Date: Tue, 3 Mar 2015 08:06:30 +0900 Subject: [PATCH] Update message-port implementation Change-Id: Ia8a8d4d52bff3524a097cc12cebc1df92ef1ede6 --- src/messageport/messageport_api.js | 47 +++++---- src/messageport/messageport_instance.cc | 126 ++++++++++++------------ 2 files changed, 89 insertions(+), 84 deletions(-) diff --git a/src/messageport/messageport_api.js b/src/messageport/messageport_api.js index fb9d2ed2..ea206da0 100644 --- a/src/messageport/messageport_api.js +++ b/src/messageport/messageport_api.js @@ -6,16 +6,20 @@ var types_ = validator_.Types; var callbackId = 0; var callbacks = {}; +var ports = []; extension.setMessageListener(function(json) { var msg = JSON.parse(json); var listeners = callbacks[msg['local_port_id']]; + var rmp; console.log('Listeners length:' + listeners.length); - var rmp = new RemoteMessagePort(msg.remotePort, msg.remoteAppId, msg.trusted); + if (!msg.hasOwnProperty('remotePort')) + rmp = null; + else + rmp = new RemoteMessagePort(msg.remotePort, msg.remoteAppId, msg.trusted); for (var i = 0; i < listeners.length; i++) { - var func = listeners[i][0]; func(msg.message, rmp); } @@ -95,20 +99,21 @@ MessagePortManager.prototype.requestLocalMessagePort = function(localMessagePort var args = validator_.validateArgs(arguments, [ {'name' : 'localMessagePortName', 'type': types_.STRING} ]); - + var localPortId; var nativeParam = { 'localMessagePortName': args.localMessagePortName }; try { - var localPortId = callNative('MessagePortManager_requestLocalMessagePort', nativeParam); + localPortId = callNative('MessagePortManager_requestLocalMessagePort', nativeParam); } catch (e) { throw e; } - var returnObject = new LocalMessagePort(localPortId, args.localMessagePortName, false); + var returnObject = new LocalMessagePort(args.localMessagePortName, false); + ports[nativeParam.localMessagePortName] = localPortId; return returnObject; }; @@ -130,12 +135,14 @@ MessagePortManager.prototype.requestTrustedLocalMessagePort = function(localMess throw e; } - var returnObject = new LocalMessagePort(localPortId, args.localMessagePortName, true); + var returnObject = new LocalMessagePort(args.localMessagePortName, true); + ports[nativeParam.localMessagePortName] = localPortId; return returnObject; }; -MessagePortManager.prototype.requestRemoteMessagePort = function(appId, remoteMessagePortName) { +MessagePortManager.prototype.requestRemoteMessagePort = + function(appId, remoteMessagePortName) { var args = validator_.validateArgs(arguments, [ {'name' : 'appId', 'type': types_.STRING}, {'name' : 'remoteMessagePortName', 'type': types_.STRING} @@ -167,6 +174,7 @@ MessagePortManager.prototype.requestTrustedRemoteMessagePort = ]); var nativeParam = { + 'appId' : args.appId, 'remoteMessagePortName': args.remoteMessagePortName }; @@ -184,11 +192,10 @@ MessagePortManager.prototype.requestTrustedRemoteMessagePort = }; -function LocalMessagePort(id, messagePortName, isTrusted) { +function LocalMessagePort(messagePortName, isTrusted) { Object.defineProperties(this, { - 'id': { value: id, writable: false }, - 'messagePortName': { value: messagePortName, writable: false, enumerable: false }, - 'isTrusted': { value: !!isTrusted, writable: false } + 'messagePortName': { value: messagePortName, writable: false, enumerable: true }, + 'isTrusted': { value: !!isTrusted, writable: false, enumerable: true } }); } @@ -198,10 +205,12 @@ LocalMessagePort.prototype.addMessagePortListener = function(listener) { {'name' : 'listener', 'type': types_.FUNCTION, 'nullable': false} ]); - if (!callbacks.hasOwnProperty(this.id)) callbacks[this.id] = []; + var portId = ports[this.messagePortName]; + + if (!callbacks.hasOwnProperty(portId)) callbacks[portId] = []; callbackId++; - callbacks[this.id].push([listener, callbackId]); + callbacks[portId].push([listener, callbackId]); return callbackId; @@ -209,15 +218,11 @@ LocalMessagePort.prototype.addMessagePortListener = function(listener) { LocalMessagePort.prototype.removeMessagePortListener = function(watchId) { var args = validator_.validateArgs(arguments, [ - {'name' : 'watchId', 'type': types_.LONG } + {'name' : 'watchId', 'type': types_.LONG, 'nullable': false, 'optional': false } ]); - if (args.watchId <= 0) - throw new tizen.WebAPIException(tizen.WebAPIException.INVALID_VALUES_ERR, - 'The input parameter contains an invalid value.'); - var to_delete; - var listeners = callbacks[this.id]; + var listeners = callbacks[ports[this.messagePortName]]; for (var i = 0, j = listeners.length; i < j; i++) { var listener_id = listeners[i][1]; @@ -227,7 +232,7 @@ LocalMessagePort.prototype.removeMessagePortListener = function(watchId) { } } - if (typeof to_delete == 'undefined') + if (typeof to_delete === 'undefined') throw new tizen.WebAPIException(tizen.WebAPIException.NOT_FOUND_ERR, 'The port of the target application is not found.'); @@ -269,7 +274,7 @@ RemoteMessagePort.prototype.sendMessage = function(data) { 'messagePortName': this.messagePortName, 'data': filtered_data, 'trusted': this.isTrusted, - 'local_port_id': args.localMessagePort ? args.localMessagePort.id : -1 + 'local_port_id': args.localMessagePort ? ports[args.localMessagePort.messagePortName] : -1 }; try { diff --git a/src/messageport/messageport_instance.cc b/src/messageport/messageport_instance.cc index 5a65451f..67f3a227 100644 --- a/src/messageport/messageport_instance.cc +++ b/src/messageport/messageport_instance.cc @@ -74,11 +74,15 @@ static void OnReceiveLocalMessage(int local_port_id, picojson::value::array data; o["local_port_id"] = picojson::value(static_cast(local_port_id)); - o["remoteAppId"] = picojson::value(remote_app_id); - o["remotePort"] = picojson::value(remote_port); - o["trusted_remote_port"] = picojson::value(trusted_remote_port); - LoggerD("Msg received from: %s", remote_app_id); + if (remote_port) { + o["remoteAppId"] = picojson::value(remote_app_id); + o["remotePort"] = picojson::value(remote_port); + o["trusted"] = picojson::value(trusted_remote_port); + LoggerD("Msg received from: %s", remote_app_id); + } + + LoggerD("Msg received"); bundle_iterate(message, BundleJsonIterator, &data); @@ -103,26 +107,25 @@ void MessageportInstance::MessagePortManagerRequestlocalmessageport portId < 0 ? "false" : "true"); - if(portId < 0){ - + if (portId < 0) { switch (portId) { case MESSAGE_PORT_ERROR_INVALID_PARAMETER: ReportError(InvalidValuesException - ("The input parameter contains an invalid value."),out); + ("The input parameter contains an invalid value."), out); break; case MESSAGE_PORT_ERROR_OUT_OF_MEMORY: - ReportError(UnknownException("Out of memory."),out); + ReportError(UnknownException("Out of memory."), out); break; case MESSAGE_PORT_ERROR_IO_ERROR: - ReportError(UnknownException("Internal I/O error ocurred."),out); - break; + ReportError(UnknownException("Internal I/O error ocurred."), out); + break; default: - ReportError(UnknownException("Unknown Exception"),out); + ReportError(UnknownException("Unknown Exception"), out); break; } - } - else + } else { ReportSuccess(picojson::value(static_cast(portId)), out); + } } void MessageportInstance:: @@ -140,27 +143,25 @@ void MessageportInstance:: LoggerD("Registering trusted local port %s:%s", localMessagePortName.c_str(), portId < 0 ? "false" : "true"); - if(portId < 0){ - + if (portId < 0) { switch (portId) { case MESSAGE_PORT_ERROR_INVALID_PARAMETER: ReportError(InvalidValuesException - ("The input parameter contains an invalid value."),out); + ("The input parameter contains an invalid value."), out); break; case MESSAGE_PORT_ERROR_OUT_OF_MEMORY: - ReportError(UnknownException("Out of memory."),out); + ReportError(UnknownException("Out of memory."), out); break; case MESSAGE_PORT_ERROR_IO_ERROR: - ReportError(UnknownException("Internal I/O error ocurred."),out); - break; + ReportError(UnknownException("Internal I/O error ocurred."), out); + break; default: - ReportError(UnknownException("Unknown Exception"),out); + ReportError(UnknownException("Unknown Exception"), out); break; } - } - else + } else { ReportSuccess(picojson::value(static_cast(portId)), out); - + } } void MessageportInstance:: @@ -181,22 +182,21 @@ void MessageportInstance:: LoggerD("Checking remote port of %s: %s", remoteMessagePortName.c_str(), portCheck ? "true" : "false"); - if(ret == MESSAGE_PORT_ERROR_NONE){ + if (ret == MESSAGE_PORT_ERROR_NONE) { if (portCheck) ReportSuccess(out); else ReportError(NotFoundException ("The port of the target application is not found"), out); - } - else if(ret == MESSAGE_PORT_ERROR_INVALID_PARAMETER) + } else if (ret == MESSAGE_PORT_ERROR_INVALID_PARAMETER) { ReportError(InvalidValuesException - ("An input parameter contains an invalid value."),out); - else if(ret == MESSAGE_PORT_ERROR_OUT_OF_MEMORY) + ("An input parameter contains an invalid value."), out); + } else if (ret == MESSAGE_PORT_ERROR_OUT_OF_MEMORY) { ReportError(UnknownException("Out of memory."), out); - else if(ret == MESSAGE_PORT_ERROR_IO_ERROR) - ReportError(UnknownException("Internal I/O error ocurred."),out); - else - ReportError(UnknownException("Unknown Error"),out); - + } else if (ret == MESSAGE_PORT_ERROR_IO_ERROR) { + ReportError(UnknownException("Internal I/O error ocurred."), out); + } else { + ReportError(UnknownException("Unknown Error"), out); + } } void MessageportInstance:: @@ -217,26 +217,24 @@ void MessageportInstance:: LoggerD("Checking trusted remoteport of %s:%s", remoteMessagePortName.c_str(), portCheck ? "true":"false"); - - if(ret == MESSAGE_PORT_ERROR_NONE){ + if (ret == MESSAGE_PORT_ERROR_NONE) { if (portCheck) ReportSuccess(out); else ReportError(NotFoundException ("The port of the target application is not found"), out); - } - else if(ret == MESSAGE_PORT_ERROR_INVALID_PARAMETER) + } else if (ret == MESSAGE_PORT_ERROR_INVALID_PARAMETER) { ReportError(InvalidValuesException - ("An input parameter contains an invalid value."),out); - else if(ret == MESSAGE_PORT_ERROR_OUT_OF_MEMORY) + ("An input parameter contains an invalid value."), out); + } else if (ret == MESSAGE_PORT_ERROR_OUT_OF_MEMORY) { ReportError(UnknownException("Out of memory."), out); - else if(ret == MESSAGE_PORT_ERROR_IO_ERROR) - ReportError(UnknownException("Internal I/O error ocurred."),out); - else if(ret == MESSAGE_PORT_ERROR_CERTIFICATE_NOT_MATCH) + } else if (ret == MESSAGE_PORT_ERROR_IO_ERROR) { + ReportError(UnknownException("Internal I/O error ocurred."), out); + } else if (ret == MESSAGE_PORT_ERROR_CERTIFICATE_NOT_MATCH) { ReportError(UnknownException( - "The remote application is not signed with the same certificate"),out); - else - ReportError(UnknownException("Unknown Error"),out); - + "The remote application is not signed with the same certificate"), out); + } else { + ReportError(UnknownException("Unknown Error"), out); + } } void MessageportInstance::RemoteMessagePortSendmessage @@ -276,6 +274,8 @@ void MessageportInstance::RemoteMessagePortSendmessage if (local_port_id < 0) { result = message_port_send_message (appId.c_str(), message_port_name.c_str(), bundle); + LoggerD("-----------%s & %s---------------", + appId.c_str(), message_port_name.c_str()); } else { result = message_port_send_message_with_local_port (appId.c_str(), message_port_name.c_str(), bundle, local_port_id); @@ -284,28 +284,28 @@ void MessageportInstance::RemoteMessagePortSendmessage bundle_free(bundle); - if(result == MESSAGE_PORT_ERROR_NONE) + if (result == MESSAGE_PORT_ERROR_NONE) ReportSuccess(out); - else if(result == MESSAGE_PORT_ERROR_INVALID_PARAMETER) + else if (result == MESSAGE_PORT_ERROR_INVALID_PARAMETER) ReportError(InvalidValuesException - ("An input parameter contains an invalid value."),out); - else if(result == MESSAGE_PORT_ERROR_PORT_NOT_FOUND) + ("An input parameter contains an invalid value.") , out); + else if (result == MESSAGE_PORT_ERROR_PORT_NOT_FOUND) ReportError(NotFoundException - ("The port of the target application is not found"),out); - else if(result == MESSAGE_PORT_ERROR_MAX_EXCEEDED) + ("The port of the target application is not found"), out); + else if (result == MESSAGE_PORT_ERROR_MAX_EXCEEDED) ReportError(QuotaExceededException - ("The size of message has exceeded the maximum limit."),out); - else if(result == MESSAGE_PORT_ERROR_RESOURCE_UNAVAILABLE) - ReportError(UnknownException("A resource is temporarily unavailable."),out); - else if(result == MESSAGE_PORT_ERROR_OUT_OF_MEMORY) - ReportError(UnknownException("Out of memory."),out); - else if(result == MESSAGE_PORT_ERROR_IO_ERROR) - ReportError(UnknownException("Internal I/O error ocurred."),out); - else if(result == MESSAGE_PORT_ERROR_CERTIFICATE_NOT_MATCH) + ("The size of message has exceeded the maximum limit."), out); + else if (result == MESSAGE_PORT_ERROR_RESOURCE_UNAVAILABLE) + ReportError(UnknownException("A resource is temporarily unavailable"), out); + else if (result == MESSAGE_PORT_ERROR_OUT_OF_MEMORY) + ReportError(UnknownException("Out of memory."), out); + else if (result == MESSAGE_PORT_ERROR_IO_ERROR) + ReportError(UnknownException("Internal I/O error ocurred."), out); + else if (result == MESSAGE_PORT_ERROR_CERTIFICATE_NOT_MATCH) ReportError(UnknownException - ("The remote application is not signed with the same certificate"),out); - else ReportError(UnknownException("Unknown Exception"),out); - + ("The remote application is not signed with the same certificate") , out); + else + ReportError(UnknownException("Unknown Exception"), out); } -- 2.34.1