Add method dispatcher to XWalkExtensionInstance 03/57303/2
authorWonYoung Choi <wy80.choi@samsung.com>
Tue, 19 Jan 2016 01:11:45 +0000 (10:11 +0900)
committerWonYoung Choi <wy80.choi@samsung.com>
Wed, 20 Jan 2016 07:56:42 +0000 (16:56 +0900)
Default behavior of HandleMessage and HandleSyncMessage parse json
object and dispatch registered method automatically.

Change-Id: Ib721dbebdc9cd2833dc11ab4dce106310df52969

CMakeLists.txt
packaging/xwalk-extensions-common.spec
src/xwalk/xwalk_extension.cc
src/xwalk/xwalk_extension.h

index 1ceae33c14a32b2c81d8d571170900e1588ac980..458fb46ba286d293aea36e00934e4dd74ab0a8a0 100644 (file)
@@ -28,12 +28,15 @@ ADD_DEFINITIONS("-std=c++0x")           # accept C++11x standard
 
 # 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}
index 0e019d57d880868598a3b0a07f048297f64ac65c..c2ec2dd68f8d2990742d9a17fa50d1684c840b8f 100644 (file)
@@ -9,6 +9,7 @@ Source0:    %{name}-%{version}.tar.gz
 Source1:    %{name}.manifest
 
 BuildRequires: cmake
+BuildRequires: pkgconfig(jsoncpp)
 
 %description
 Common modules and tools for Crosswalk Extensions
index 0fec3afe000bb82153046bb7a55a233f20ae4b29..4458c7713caad59c061a0e7eee231dccbf84eca1 100644 (file)
@@ -6,6 +6,8 @@
 #include "xwalk/xwalk_extension.h"
 
 #include <assert.h>
+#include <string.h>
+
 #include <iostream>
 #include <vector>
 
@@ -148,8 +150,9 @@ void XWalkExtension::HandleBinaryMessage(XWalkExtension* extension,
   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);
@@ -158,7 +161,7 @@ XWalkExtensionInstance::~XWalkExtensionInstance() {
 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_) {
@@ -170,7 +173,7 @@ 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.";
+              << "instance was destroyed.\n";
     return;
   }
   if (extension_) {
@@ -182,7 +185,7 @@ void XWalkExtensionInstance::PostBinaryMessage(
 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_) {
@@ -190,4 +193,60 @@ void XWalkExtensionInstance::SendSyncReply(const char* reply) {
   }
 }
 
+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
index e8995646657d980124a78491864013ff05c95dea..2d1a2cd07e5e67ba55e8e1b0aa2b1cd40438b120 100644 (file)
@@ -1,5 +1,5 @@
 // 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.
 
@@ -8,9 +8,12 @@
 
 #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"
@@ -78,6 +81,10 @@ class XWalkExtension;
   }                                                                            \
   }
 
+#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
@@ -140,7 +147,9 @@ class XWalkExtension {
 // 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.
@@ -152,14 +161,17 @@ class XWalkExtensionInstance {
   // 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*/,
@@ -173,6 +185,15 @@ class XWalkExtensionInstance {
 
   // 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