Add 'XW_MessagingInterface_2' for exchanging binary messages 61/53061/1 accepted/tizen/mobile/20151202.040849 accepted/tizen/tv/20151202.040914 accepted/tizen/wearable/20151202.040944 submit/tizen/20151202.002733
authorWonYoung Choi <wy80.choi@samsung.com>
Tue, 1 Dec 2015 11:16:35 +0000 (20:16 +0900)
committerWonYoung Choi <wy80.choi@samsung.com>
Tue, 1 Dec 2015 11:16:35 +0000 (20:16 +0900)
Change-Id: I17f598d9fab856eba29dc2e21b21896b73472eb4

src/xwalk/extensions/public/XW_Extension_Message_2.h [new file with mode: 0644]
src/xwalk/xwalk_extension.cc
src/xwalk/xwalk_extension.h

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 (file)
index 0000000..f417f88
--- /dev/null
@@ -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_
index 33dd6cc071e2d11fc821fc1fdc0b0202900936ec..0fec3afe000bb82153046bb7a55a233f20ae4b29 100644 (file)
@@ -25,11 +25,11 @@ bool XWalkExtension::InitializeInterfaces(XW_GetInterface get_interface) {
     return false;
   }
 
-  messaging_interface_ = reinterpret_cast<const XW_MessagingInterface*>(
-      get_interface(XW_MESSAGING_INTERFACE));
+  messaging_interface_ = reinterpret_cast<const XW_MessagingInterface2*>(
+      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<XWalkExtensionInstance*>(
+          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 "
index 7e5055d89088b75a5ce1c6dd968081abd18c3cc7..e8995646657d980124a78491864013ff05c95dea 100644 (file)
@@ -12,6 +12,7 @@
 #include <functional>
 
 #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;