# common library
SET(TARGET_COMMON_LIB ${PROJECT_NAME})
+PKG_CHECK_MODULES(TARGET_COMMON_LIB_DEPS REQUIRED jsoncpp)
INCLUDE_DIRECTORIES (
${PROJECT_SOURCE_DIR}/src
+ ${TARGET_COMMON_LIB_DEPS_INCLUDE_DIRS}
)
ADD_LIBRARY(${TARGET_COMMON_LIB} SHARED
${PROJECT_SOURCE_DIR}/src/xwalk/xwalk_extension.cc
)
+TARGET_LINK_LIBRARIES(${TARGET_COMMON_LIB} ${TARGET_COMMON_LIB_DEPS_LIBRARIES})
SET_TARGET_PROPERTIES(${TARGET_COMMON_LIB}
PROPERTIES SOVERSION ${VERSION_MAJOR})
SET_TARGET_PROPERTIES(${TARGET_COMMON_LIB}
Source1: %{name}.manifest
BuildRequires: cmake
+BuildRequires: pkgconfig(jsoncpp)
%description
Common modules and tools for Crosswalk Extensions
#include "xwalk/xwalk_extension.h"
#include <assert.h>
+#include <string.h>
+
#include <iostream>
#include <vector>
instance->HandleBinaryMessage(msg, size);
}
-XWalkExtensionInstance::XWalkExtensionInstance()
- : xw_instance_(0) {}
+XWalkExtensionInstance::XWalkExtensionInstance(const std::string& key_cmd)
+ : xw_instance_(0), key_cmd_(key_cmd) {
+}
XWalkExtensionInstance::~XWalkExtensionInstance() {
assert(xw_instance_ == 0);
void XWalkExtensionInstance::PostMessage(const char* msg) {
if (!xw_instance_) {
std::cerr << "Ignoring PostMessage() in the constructor or after the "
- << "instance was destroyed.";
+ << "instance was destroyed.\n";
return;
}
if (extension_) {
const char* msg, const size_t size) {
if (!xw_instance_) {
std::cerr << "Ignoring PostMessage() in the constructor or after the "
- << "instance was destroyed.";
+ << "instance was destroyed.\n";
return;
}
if (extension_) {
void XWalkExtensionInstance::SendSyncReply(const char* reply) {
if (!xw_instance_) {
std::cerr << "Ignoring SendSyncReply() in the constructor or after the "
- << "instance was destroyed.";
+ << "instance was destroyed.\n";
return;
}
if (extension_) {
}
}
+void XWalkExtensionInstance::RegisterMethod(
+ const std::string& name, MappedMethod func) {
+ method_map_[name] = func;
+}
+
+void XWalkExtensionInstance::DispatchMethod(
+ const Json::Value& args, Json::Value& reply) {
+ std::string method = args.get(key_cmd_, "").asString();
+ if (method.empty()) {
+ std::cerr << "Invalid arguments. The key for '"
+ << key_cmd_ << "' was not found.\n";
+ return;
+ }
+ auto it = method_map_.find(method);
+ if (method_map_.end() == it) {
+ std::cerr << "Can not find dispatchable method '" << method << "'\n";
+ return;
+ }
+
+ it->second(args, reply);
+}
+
+void XWalkExtensionInstance::HandleMessage(const char* msg) {
+ Json::Value args;
+ Json::Reader reader;
+ if (!reader.parse(msg, msg + strlen(msg), args)) {
+ std::cerr << "Ignoring message. Can't parse msessage. "
+ << reader.getFormattedErrorMessages() << "\n";
+ return;
+ }
+
+ Json::Value reply;
+ DispatchMethod(args, reply);
+
+ if (!reply.isNull()) {
+ Json::FastWriter writer;
+ PostMessage(writer.write(reply).c_str());
+ }
+}
+
+void XWalkExtensionInstance::HandleSyncMessage(const char* msg) {
+ Json::Value args;
+ Json::Reader reader;
+ if (!reader.parse(msg, msg + strlen(msg), args)) {
+ std::cerr << "Ignoring message. Can't parse msessage. "
+ << reader.getFormattedErrorMessages() << "\n";
+ return;
+ }
+
+ Json::Value reply;
+ DispatchMethod(args, reply);
+
+ Json::FastWriter writer;
+ SendSyncReply(writer.write(reply).c_str());
+}
+
} // namespace xwalk
// Copyright (c) 2013 Intel Corporation. All rights reserved.
-// Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved.
+// Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <sys/types.h>
+#include <map>
#include <string>
#include <functional>
+#include <json/json.h>
+
#include "xwalk/extensions/public/XW_Extension.h"
#include "xwalk/extensions/public/XW_Extension_Message_2.h"
#include "xwalk/extensions/public/XW_Extension_EntryPoints.h"
} \
}
+#define REGISTER_XWALK_METHOD(m, o, f) \
+ RegisterMethod(m, \
+ std::bind(f, o, std::placeholders::_1, std::placeholders::_2));
+
namespace xwalk {
// XWalkExtension is a super-class of all crosswalk extenions. It implements
// XWalkExtensionInstance is a super-class of instances created from extensions.
class XWalkExtensionInstance {
public:
- XWalkExtensionInstance();
+ typedef std::function<void(const Json::Value&, Json::Value&)> MappedMethod;
+
+ XWalkExtensionInstance(const std::string& key_cmd = std::string("cmd"));
virtual ~XWalkExtensionInstance();
// Sends a message to javascript scope asyncronously.
// Sends a reply of syncronous call to javascript scope immediately.
void SendSyncReply(const char* reply);
+ // Register synchronous method for mapping
+ void RegisterMethod(const std::string& name, MappedMethod func);
+
// Override this function to initialize the sub-class of this class.
virtual void Initialize() {}
// Override this function to handle asyncronous messages sent from javascript.
- virtual void HandleMessage(const char* /*msg*/) {}
+ virtual void HandleMessage(const char* msg);
// Override this function to handle syncronous messages sent from javascript.
- virtual void HandleSyncMessage(const char* /*msg*/) {}
+ virtual void HandleSyncMessage(const char* msg);
// Override this function to handle binary message sent from javascript.
virtual void HandleBinaryMessage(const char* /*msg*/,
// Pointer of parent extension
const XWalkExtension* extension_;
+
+ // Key name for cmd of json message
+ std::string key_cmd_;
+
+ // Map for mapped methods
+ std::map<std::string, MappedMethod> method_map_;
+
+ // Call a mapped method internally
+ void DispatchMethod(const Json::Value& args, Json::Value& reply);
};
} // namespace xwalk