From: WonYoung Choi Date: Tue, 1 Dec 2015 11:16:35 +0000 (+0900) Subject: Add 'XW_MessagingInterface_2' for exchanging binary messages X-Git-Tag: submit/tizen/20151202.002733^0 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=cece09dc8b3c6f3353146ff88fef8f64f5be5347;p=platform%2Fframework%2Fweb%2Fxwalk-extensions-common.git Add 'XW_MessagingInterface_2' for exchanging binary messages Change-Id: I17f598d9fab856eba29dc2e21b21896b73472eb4 --- diff --git a/src/xwalk/extensions/public/XW_Extension_Message_2.h b/src/xwalk/extensions/public/XW_Extension_Message_2.h new file mode 100644 index 0000000..f417f88 --- /dev/null +++ b/src/xwalk/extensions/public/XW_Extension_Message_2.h @@ -0,0 +1,64 @@ +// Copyright (c) 2015 Intel Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef XWALK_EXTENSIONS_PUBLIC_XW_EXTENSION_MESSAGE_2_H_ +#define XWALK_EXTENSIONS_PUBLIC_XW_EXTENSION_MESSAGE_2_H_ + +#ifndef XWALK_EXTENSIONS_PUBLIC_XW_EXTENSION_H_ +#error "You should include XW_Extension.h before this file" +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +#define XW_MESSAGING_INTERFACE_2 "XW_MessagingInterface_2" + +typedef void (*XW_HandleBinaryMessageCallback)(XW_Instance instance, + const char* message, + const size_t size); + +struct XW_MessagingInterface_2 { + // Register a callback to be called when the JavaScript code associated + // with the extension posts a message. Note that the callback will be called + // with the XW_Instance that posted the message as well as the message + // contents. + void (*Register)(XW_Extension extension, + XW_HandleMessageCallback handle_message); + + // Post a message to the web content associated with the instance. To + // receive this message the extension's JavaScript code should set a + // listener using extension.setMessageListener() function. + // + // This function is thread-safe and can be called until the instance is + // destroyed. + void (*PostMessage)(XW_Instance instance, const char* message); + + // Register a callback to be called when the JavaScript code associated + // with the extension posts a binary message (ArrayBuffer object). + // Note that the callback will be called with the XW_Instance that posted + // the message as well as the message contents. + void (*RegisterBinaryMesssageCallback)( + XW_Extension extension, + XW_HandleBinaryMessageCallback handle_message); + + // Post a binary message to the web content associated with the instance. To + // receive this message the extension's JavaScript code should set a + // listener using extension.setMessageListener() function. + // The JavaScript message listener function would receive the binary message + // in an ArrayBuffer object. + // + // This function is thread-safe and can be called until the instance is + // destroyed. + void (*PostBinaryMessage)(XW_Instance instance, + const char* message, size_t size); +}; + +typedef struct XW_MessagingInterface_2 XW_MessagingInterface2; + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif // XWALK_EXTENSIONS_PUBLIC_XW_EXTENSION_MESSAGE_2_H_ diff --git a/src/xwalk/xwalk_extension.cc b/src/xwalk/xwalk_extension.cc index 33dd6cc..0fec3af 100644 --- a/src/xwalk/xwalk_extension.cc +++ b/src/xwalk/xwalk_extension.cc @@ -25,11 +25,11 @@ bool XWalkExtension::InitializeInterfaces(XW_GetInterface get_interface) { return false; } - messaging_interface_ = reinterpret_cast( - get_interface(XW_MESSAGING_INTERFACE)); + messaging_interface_ = reinterpret_cast( + get_interface(XW_MESSAGING_INTERFACE_2)); if (!messaging_interface_) { std::cerr << - "Can't initialize extension: error getting Messaging interface.\n"; + "Can't initialize extension: error getting Messaging interface 2.\n"; return false; } @@ -136,6 +136,18 @@ void XWalkExtension::HandleSyncMessage( instance->HandleSyncMessage(msg); } +// static +void XWalkExtension::HandleBinaryMessage(XWalkExtension* extension, + XW_Instance xw_instance, + const char* msg, const size_t size) { + XWalkExtensionInstance* instance = + reinterpret_cast( + extension->core_interface_->GetInstanceData(xw_instance)); + if (!instance) + return; + instance->HandleBinaryMessage(msg, size); +} + XWalkExtensionInstance::XWalkExtensionInstance() : xw_instance_(0) {} @@ -154,6 +166,19 @@ void XWalkExtensionInstance::PostMessage(const char* msg) { } } +void XWalkExtensionInstance::PostBinaryMessage( + const char* msg, const size_t size) { + if (!xw_instance_) { + std::cerr << "Ignoring PostMessage() in the constructor or after the " + << "instance was destroyed."; + return; + } + if (extension_) { + extension_->messaging_interface_->PostBinaryMessage( + xw_instance_, msg, size); + } +} + void XWalkExtensionInstance::SendSyncReply(const char* reply) { if (!xw_instance_) { std::cerr << "Ignoring SendSyncReply() in the constructor or after the " diff --git a/src/xwalk/xwalk_extension.h b/src/xwalk/xwalk_extension.h index 7e5055d..e899564 100644 --- a/src/xwalk/xwalk_extension.h +++ b/src/xwalk/xwalk_extension.h @@ -12,6 +12,7 @@ #include #include "xwalk/extensions/public/XW_Extension.h" +#include "xwalk/extensions/public/XW_Extension_Message_2.h" #include "xwalk/extensions/public/XW_Extension_EntryPoints.h" #include "xwalk/extensions/public/XW_Extension_Runtime.h" #include "xwalk/extensions/public/XW_Extension_SyncMessage.h" @@ -59,6 +60,12 @@ class XWalkExtension; xwalk::XWalkExtension::HandleMessage( \ g_extension_##NAME, xw_instance, msg); \ }); \ + g_extension_##NAME->messaging_interface_->RegisterBinaryMesssageCallback( \ + xw_extension, [](XW_Instance xw_instance, \ + const char* msg, const size_t size) { \ + xwalk::XWalkExtension::HandleBinaryMessage( \ + g_extension_##NAME, xw_instance, msg, size); \ + }); \ g_extension_##NAME->sync_messaging_interface_->Register( \ xw_extension, \ [](XW_Instance xw_instance, const char* msg) { \ @@ -114,13 +121,16 @@ class XWalkExtension { XW_Instance xw_instance, const char* msg); static void HandleSyncMessage(XWalkExtension* extension, XW_Instance xw_instance, const char* msg); + static void HandleBinaryMessage(XWalkExtension* extension, + XW_Instance xw_instance, + const char* msg, const size_t size); // Identifier of an extension XW_Extension xw_extension_; // XW_Extension interfaces. const XW_CoreInterface* core_interface_ = NULL; - const XW_MessagingInterface* messaging_interface_ = NULL; + const XW_MessagingInterface2* messaging_interface_ = NULL; const XW_Internal_SyncMessagingInterface* sync_messaging_interface_ = NULL; const XW_Internal_EntryPointsInterface* entry_points_interface_ = NULL; const XW_Internal_RuntimeInterface* runtime_interface_ = NULL; @@ -136,6 +146,9 @@ class XWalkExtensionInstance { // Sends a message to javascript scope asyncronously. void PostMessage(const char* msg); + // Sends a binary message to javascript scope asyncronously. + void PostBinaryMessage(const char* msg, const size_t size); + // Sends a reply of syncronous call to javascript scope immediately. void SendSyncReply(const char* reply); @@ -148,6 +161,10 @@ class XWalkExtensionInstance { // Override this function to handle syncronous messages sent from javascript. virtual void HandleSyncMessage(const char* /*msg*/) {} + // Override this function to handle binary message sent from javascript. + virtual void HandleBinaryMessage(const char* /*msg*/, + const size_t /*size*/) {} + private: friend class XWalkExtension;