From: Maciek Blim Date: Mon, 15 Dec 2014 10:43:16 +0000 (+0100) Subject: [Messaging] Changed data patter using in comunication JS - CPP X-Git-Tag: submit/tizen_tv/20150603.064601~1^2~829^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=b922c9dd8927cc7b4de1f3fe6c110919631cb41a;p=platform%2Fcore%2Fapi%2Fwebapi-plugins.git [Messaging] Changed data patter using in comunication JS - CPP Change-Id: I40b82da6e2a344f85d90270f0c22bf314e40a340 Signed-off-by: Maciek Blim --- diff --git a/src/messaging/messaging_api.js b/src/messaging/messaging_api.js index d97b1385..b89c70b6 100644 --- a/src/messaging/messaging_api.js +++ b/src/messaging/messaging_api.js @@ -6,6 +6,7 @@ var validator_ = xwalk.utils.validator; var types_ = validator_.Types; +var bridge = xwalk.utils.NativeBridge(extension, true); function throwException_(err) { throw new tizen.WebAPIException(err.code, err.name, err.message); @@ -55,214 +56,6 @@ function propertyFactory_(that, name, value, flags, options) { ); } -/* - *bridge is a two way communication interface - *Example usage: - * To send sync method: - * var result = bridge.sync({ - * cmd: 'my_cpp_function_symbol', - * data: { - * name: 'My name', - * age: 28 - * } - * }); - * console.log(result); - * - * To send async method and handle response: - * bridge.async({ - * cmd: 'my_cpp_function_symbol', - * data: { - * name: 'My name' - * } - * }).then({ - * success: function (data) { - * var age = data.age; - * args.successCallback(age); - * }, - * error: function (e) {...}, - * someCallback: function (data) {...} - * }); - *bridge.async will add special param to passed data called cid - *that param need to be kept and returned with respons - *To determine which callback should be invoked, response should - *contain "action" param. Value of "action" param indicates name of - *triggered callback. - *Callbask are removed from listenr by defoult to prevent that behaviour - *param "keep" should be assigned to value true - *Example of c++ async response: - * Simple succes with data: - * { - * cid: 23, - * action: 'success', - * data: { - * age: 23 - * } - * } - * More complicated example: - * { - * cid: 23, - * action: 'progress', - * keep: true, - * data: { - * age: 23 - * } - * } - */ -var bridge = (function (extension) { - var Callbacks = (function () { - var _collection = {}; - var _cid = 0; - var _next = function () { - return (_cid += 1); - }; - - var CallbackManager = function () {}; - - CallbackManager.prototype = { - add: function (/*callbacks, cid?*/) { - console.log('bridge', 'CallbackManager', 'add'); - var args = Array.prototype.slice.call(arguments); - console.dir('bridge', 'CallbackManager', 'add', args); - var c = args.shift(); - var cid = args.pop(); - if (cid) { - if (c !== null && typeof c === 'object') { - for (var key in c) { - if (c.hasOwnProperty(key)) _collection[cid][key] = c[key]; - } - } - } else { - cid = _next(); - _collection[cid] = c; - } - return cid; - }, - remove: function (cid) { - console.log('bridge', 'CallbackManager', 'remove'); - if (_collection[cid]) delete _collection[cid]; - }, - call: function (cid, key, args, keep) { - console.log('bridge', 'CallbackManager', 'call'); - var callbacks = _collection[cid]; - keep = !!keep; - if (callbacks) { - var fn = callbacks[key]; - if (fn) { - fn.apply(null, args); - if (!keep) this.remove(cid) - } - } - } - }; - - return { - getInstance: function () { - return this.instance || (this.instance = new CallbackManager); - } - }; - })(); - - - var Listeners = (function () { - var _listeners = {}; - var _id = 0; - var _next = function () { - return (_id += 1); - }; - - var ListenerManager = function () {}; - - ListenerManager.prototype = { - add: function (l) { - console.log('bridge', 'ListenerManager', 'add'); - var id = _next(); - _listeners[id] = l; - return id; - }, - resolve: function (id, action, data, keep) { - console.log('bridge', 'ListenerManager', 'resolve'); - keep = !!keep; - var l = _listeners[id]; - if (l) { - var cm = Callbacks.getInstance(); - cm.call(l.cid, action, [data], keep); - } - return l; - }, - remove: function (id) { - console.log('bridge', 'ListenerManager', 'remove'); - var l = _listeners[id]; - if (l) { - var cm = Callbacks.getInstance(); - if (l.cid) cm.remove(l.cid); - delete _listeners[id]; - } - } - } - - return { - getInstance: function () { - return this.instance || (this.instance = new ListenerManager); - } - }; - })(); - - var Listener = function () { - console.log('bridge', 'Listener constructor'); - this.cid = null; - }; - Listener.prototype = { - then: function (c) { - console.log('bridge', 'Listener', 'then'); - var cm = Callbacks.getInstance(); - this.cid = cm.add(c, this.cid); - return this; - } - }; - - var Bridge = function () {}; - Bridge.prototype = { - sync: function (data) { - console.log('bridge', 'sync'); - var result = extension.internal.sendSyncMessage(JSON.stringify(data)); - var obj = JSON.parse(result); - if (obj.error) - throw new tizen.WebAPIException(obj.code, obj.name, obj.message); - return obj.result; - }, - async: function (data) { - console.log('bridge', 'async'); - var l = new Listener(); - data.cid = Listeners.getInstance().add(l); - console.log('SENDING: ', JSON.stringify(data)); - setTimeout(function () { - extension.postMessage(JSON.stringify(data)); - }); - return l; - } - }; - - extension.setMessageListener(function (json) { - /* - *Expected response: - *{ - * cid: 23, // callback id - * action: 'success', // expected callback action - * keep: false // optional param - * data: {...} // data pased to callback - *} - */ - - console.log('bridge', 'setMessageListener', json); - var data = JSON.parse(json); - if (data.cid && data.action) { - Listeners.getInstance().resolve(data.cid, data.action, data.data, data.keep); - } - }); - - return new Bridge; -})(extension); - /** * Specifies the Messaging service tags. */ @@ -471,7 +264,7 @@ Messaging.prototype.getMessageServices = function () { bridge.async({ cmd: 'Messaging_getMessageServices', - data: { + args: { messageServiceType: args.messageServiceType } }).then({ @@ -510,7 +303,7 @@ MessageService.prototype.sendMessage = function () { bridge.async({ cmd: 'MessageService_sendMessage', - data: { + args: { message: args.message, simIndex: args.simIndex } @@ -540,7 +333,7 @@ MessageService.prototype.loadMessageBody = function () { bridge.async({ cmd: 'MessageService_loadMessageBody', - data: { + args: { message: args.message } }).then({ @@ -569,7 +362,7 @@ MessageService.prototype.loadMessageAttachment = function () { bridge.async({ cmd: 'MessageService_loadMessageAttachment', - data: { + args: { attachment: args.attachment } }).then({ @@ -603,7 +396,7 @@ MessageService.prototype.sync = function () { bridge.async({ cmd: 'MessageService_sync', - data: { + args: { id: self.id, limit: args.limit || null } @@ -633,7 +426,7 @@ MessageService.prototype.syncFolder = function () { bridge.async({ cmd: 'MessageService_syncFolder', - data: { + args: { folder: args.folder, limit: args.limit } @@ -660,7 +453,7 @@ MessageService.prototype.stopSync = function () { bridge.sync({ cmd: 'MessageService_stopSync', - data: { + args: { opId: args.opId } }); @@ -677,7 +470,7 @@ MessageStorage.prototype.addDraftMessage = function () { bridge.async({ cmd: 'MessageStorage_addDraftMessage', - data: { + args: { message: args.message } }).then({ @@ -710,7 +503,7 @@ MessageStorage.prototype.findMessages = function () { bridge.async({ cmd: 'MessageStorage_findMessages', - data: { + args: { filter: args.filter, sort: args.sort, limit: args.limit, @@ -744,7 +537,7 @@ MessageStorage.prototype.removeMessages = function () { bridge.async({ cmd: 'MessageStorage_removeMessages', - data: { + args: { messages: args.messages } }).then({ @@ -773,7 +566,7 @@ MessageStorage.prototype.updateMessages = function () { bridge.async({ cmd: 'MessageStorage_updateMessages', - data: { + args: { messages: args.messages } }).then({ @@ -806,7 +599,7 @@ MessageStorage.prototype.findConversations = function () { bridge.async({ cmd: 'MessageStorage_findConversations', - data: { + args: { filter: args.filter, sort: args.sort, limit: args.limit, @@ -840,7 +633,7 @@ MessageStorage.prototype.removeConversations = function () { bridge.async({ cmd: 'MessageStorage_removeConversations', - data: { + args: { conversations: args.conversations } }).then({ @@ -869,7 +662,7 @@ MessageStorage.prototype.findFolders = function () { bridge.async({ cmd: 'MessageStorage_findFolders', - data: { + args: { filter: args.filter, sort: args.sort, limit: args.limit, @@ -909,7 +702,7 @@ MessageStorage.prototype.addMessagesChangeListener = function () { bridge({ cmd: 'MessageStorage_addMessagesChangeListener', - data: { + args: { filter: args.filter, listeners: listeners } @@ -962,7 +755,7 @@ MessageStorage.prototype. addConversationsChangeListener = function () { bridge({ cmd: 'MessageStorage_addConversationsChangeListener', - data: { + args: { filter: args.filter, listeners: listeners } @@ -1011,7 +804,7 @@ MessageStorage.prototype. addFoldersChangeListener = function () { bridge({ cmd: 'MessageStorage_addFoldersChangeListener', - data: { + args: { filter: args.filter, listeners: listeners } @@ -1053,7 +846,7 @@ MessageStorage.prototype.removeChangeListener = function () { bridge.sync({ cmd: 'MessageStorage_removeChangeListener', - data: { + args: { watchId: args.watchId } }); diff --git a/src/messaging/messaging_util.cc b/src/messaging/messaging_util.cc index 76fe7eb3..329bfd96 100644 --- a/src/messaging/messaging_util.cc +++ b/src/messaging/messaging_util.cc @@ -19,7 +19,7 @@ const char* JSON_CALLBACK_SUCCCESS = "success"; const char* JSON_CALLBACK_ERROR = "error"; const char* JSON_CALLBACK_PROGRESS = "progress"; const char* JSON_CALLBACK_KEEP = "keep"; -const char* JSON_DATA = "data"; +const char* JSON_DATA = "args"; namespace { const std::string TYPE_SMS = "messaging.sms";