--- /dev/null
+// 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_
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;
}
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) {}
}
}
+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 "
#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"
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) { \
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;
// 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);
// 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;