implement module for load user defined crosswalk extension
authorInhwan Lee <ideal.lee@samsung.com>
Thu, 9 Jun 2016 02:39:04 +0000 (11:39 +0900)
committerInhwan Lee <ideal.lee@samsung.com>
Thu, 16 Jun 2016 05:17:28 +0000 (14:17 +0900)
14 files changed:
extensions/common/xwalk_extension.cc [changed mode: 0644->0755]
extensions/common/xwalk_extension.h [changed mode: 0644->0755]
extensions/common/xwalk_extension_adapter.cc [changed mode: 0644->0755]
extensions/common/xwalk_extension_adapter.h [changed mode: 0644->0755]
extensions/common/xwalk_extension_manager.cc [changed mode: 0644->0755]
extensions/common/xwalk_extension_manager.h [changed mode: 0644->0755]
extensions/common/xwalk_extension_server.cc [changed mode: 0644->0755]
extensions/common/xwalk_extension_server.h [changed mode: 0644->0755]
extensions/public/XW_Extension_Message_2.h [new file with mode: 0755]
extensions/renderer/xwalk_extension_client.cc [changed mode: 0644->0755]
extensions/renderer/xwalk_extension_client.h [changed mode: 0644->0755]
extensions/renderer/xwalk_extension_renderer_controller.cc [changed mode: 0644->0755]
extensions/renderer/xwalk_extension_renderer_controller.h [changed mode: 0644->0755]
runtime/renderer/injected_bundle.cc

old mode 100644 (file)
new mode 100755 (executable)
index 5cb8bc7..a5f9292
@@ -25,7 +25,8 @@ XWalkExtension::XWalkExtension(const std::string& path,
     destroyed_instance_callback_(NULL),
     shutdown_callback_(NULL),
     handle_msg_callback_(NULL),
-    handle_sync_msg_callback_(NULL) {
+    handle_sync_msg_callback_(NULL),
+    handle_binary_msg_callback_(NULL) {
 }
 
 XWalkExtension::XWalkExtension(const std::string& path,
@@ -43,7 +44,8 @@ XWalkExtension::XWalkExtension(const std::string& path,
     destroyed_instance_callback_(NULL),
     shutdown_callback_(NULL),
     handle_msg_callback_(NULL),
-    handle_sync_msg_callback_(NULL) {
+    handle_sync_msg_callback_(NULL),
+    handle_binary_msg_callback_(NULL) {
 }
 
 XWalkExtension::~XWalkExtension() {
old mode 100644 (file)
new mode 100755 (executable)
index 85af14c..c7aaf83
@@ -12,6 +12,7 @@
 #include "extensions/common/xwalk_extension_instance.h"
 #include "extensions/public/XW_Extension.h"
 #include "extensions/public/XW_Extension_SyncMessage.h"
+#include "extensions/public/XW_Extension_Message_2.h"
 
 namespace extensions {
 
@@ -73,6 +74,7 @@ class XWalkExtension {
   XW_ShutdownCallback shutdown_callback_;
   XW_HandleMessageCallback handle_msg_callback_;
   XW_HandleSyncMessageCallback handle_sync_msg_callback_;
+  XW_HandleBinaryMessageCallback handle_binary_msg_callback_;
 };
 
 }  // namespace extensions
old mode 100644 (file)
new mode 100755 (executable)
index 86df5b5..ef693ff
@@ -99,6 +99,16 @@ const void* XWalkExtensionAdapter::GetInterface(const char* name) {
     return &messagingInterface1;
   }
 
+  if (!strcmp(name, XW_MESSAGING_INTERFACE_2)) {
+    static const XW_MessagingInterface_2 messagingInterface2 = {
+      MessagingRegister,
+      MessagingPostMessage,
+      MessagingRegisterBinaryMessageCallback,
+      MessagingPostBinaryMessage
+    };
+    return &messagingInterface2;
+  }
+
   if (!strcmp(name, XW_INTERNAL_SYNC_MESSAGING_INTERFACE_1)) {
     static const XW_Internal_SyncMessagingInterface_1
         syncMessagingInterface1 = {
@@ -292,6 +302,21 @@ int XWalkExtensionAdapter::PermissionsRegisterPermissions(
     return XW_ERROR;
 }
 
+void XWalkExtensionAdapter::MessagingRegisterBinaryMessageCallback(
+  XW_Extension xw_extension, XW_HandleBinaryMessageCallback handle_message) {
+  XWalkExtension* extension = GetExtension(xw_extension);
+  CHECK(extension, xw_extension);
+  RETURN_IF_INITIALIZED(extension);
+  extension->handle_binary_msg_callback_ = handle_message;
+}
+
+void XWalkExtensionAdapter::MessagingPostBinaryMessage(
+  XW_Instance xw_instance, const char* message, size_t size) {
+  XWalkExtensionInstance* instance = GetExtensionInstance(xw_instance);
+  CHECK(instance, xw_instance);
+  instance->PostMessageToJS(message);
+}
+
 #undef CHECK
 #undef RETURN_IF_INITIALIZED
 
old mode 100644 (file)
new mode 100755 (executable)
index 3c350d1..9a29c35
@@ -15,6 +15,7 @@
 #include "extensions/public/XW_Extension_Permissions.h"
 #include "extensions/public/XW_Extension_Runtime.h"
 #include "extensions/public/XW_Extension_SyncMessage.h"
+#include "extensions/public/XW_Extension_Message_2.h"
 
 namespace extensions {
 
@@ -78,6 +79,10 @@ class XWalkExtensionAdapter {
       XW_Extension xw_extension, const char* api_name);
   static int PermissionsRegisterPermissions(
       XW_Extension xw_extension, const char* perm_table);
+  static void MessagingRegisterBinaryMessageCallback(
+      XW_Extension xw_extension, XW_HandleBinaryMessageCallback handle_message);
+  static void MessagingPostBinaryMessage(
+      XW_Instance xw_instance, const char* message, size_t size);
 
   ExtensionMap extension_map_;
   InstanceMap instance_map_;
old mode 100644 (file)
new mode 100755 (executable)
index e654550..0b6fc40
@@ -6,6 +6,7 @@
 
 #include <glob.h>
 #include <dlfcn.h>
+#include <sys/utsname.h>
 
 #include <fstream>
 #include <set>
@@ -43,6 +44,11 @@ static const char* kPreloadLibs[] = {
   NULL
 };
 
+const char kUserPluginsDirectory[] = "plugin/";
+const char kArchArmv7l[] = "armv7l";
+const char kArchI586[] = "i586";
+const char kArchDefault[] = "default";
+
 }  // namespace
 
 XWalkExtensionManager::XWalkExtensionManager() {
@@ -108,6 +114,50 @@ void XWalkExtensionManager::LoadExtensions(bool meta_only) {
   }
 }
 
+void XWalkExtensionManager::LoadUserExtensions(const std::string app_path) {
+  if (app_path.empty()) {
+    LOGGER(ERROR) << "Failed to get package root path";
+    return;
+  }
+  LOGGER(DEBUG) << "app path is : " <<app_path;
+  std::string app_ext_pattern(app_path);
+  app_ext_pattern.append(kUserPluginsDirectory);
+  struct utsname u;
+  if (0 == uname(&u)) {
+    std::string machine = u.machine;
+    if (!machine.empty()) {
+      if (machine == kArchArmv7l) {
+        app_ext_pattern.append(kArchArmv7l);
+      } else if (machine == kArchI586) {
+        app_ext_pattern.append(kArchI586);
+      } else {
+        app_ext_pattern.append(kArchDefault);
+      }
+    } else {
+      LOGGER(ERROR) << "cannot get machine info";
+      app_ext_pattern.append(kArchDefault);
+    }
+    app_ext_pattern.append("/");
+  }
+  app_ext_pattern.append("*");
+  app_ext_pattern.append(kExtensionSuffix);
+
+  LOGGER(DEBUG) << "plugins directory : " << app_ext_pattern;
+  StringSet files;
+  {
+    glob_t glob_result;
+    glob(app_ext_pattern.c_str(), GLOB_TILDE, NULL, &glob_result);
+    for (unsigned int i = 0; i < glob_result.gl_pathc; ++i) {
+      files.insert(glob_result.gl_pathv[i]);
+    }
+  }
+  for (auto it = files.begin(); it != files.end(); ++it) {
+    XWalkExtension* ext = new XWalkExtension(*it, this);
+    RegisterExtension(ext);
+  }
+  LOGGER(DEBUG) << "finish load user extension plugins";
+}
+
 void XWalkExtensionManager::UnloadExtensions() {
   for (auto it = extensions_.begin(); it != extensions_.end(); ++it) {
     delete it->second;
old mode 100644 (file)
new mode 100755 (executable)
index 7bfd611..cfbdede
@@ -24,6 +24,7 @@ class XWalkExtensionManager : public XWalkExtension::XWalkExtensionDelegate {
   ExtensionMap extensions() const { return extensions_; }
 
   void LoadExtensions(bool meta_only = true);
+  void LoadUserExtensions(const std::string app_path);
   void PreloadExtensions();
 
   void UnloadExtensions();
old mode 100644 (file)
new mode 100755 (executable)
index d0faec7..ae6fb86
@@ -221,5 +221,8 @@ void XWalkExtensionServer::HandleGetAPIScript(
 
   eina_stringshare_del(extension_name);
 }
+void XWalkExtensionServer::LoadUserExtensions(const std::string app_path) {
+  manager_.LoadUserExtensions(app_path);
+}
 
 }  // namespace extensions
old mode 100644 (file)
new mode 100755 (executable)
index 186fb98..cfcb112
@@ -29,6 +29,7 @@ class XWalkExtensionServer {
   void HandleIPCMessage(Ewk_IPC_Wrt_Message_Data* data);
 
   void Shutdown();
+  void LoadUserExtensions(const std::string app_path);
 
  private:
   XWalkExtensionServer();
diff --git a/extensions/public/XW_Extension_Message_2.h b/extensions/public/XW_Extension_Message_2.h
new file mode 100755 (executable)
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_
old mode 100644 (file)
new mode 100755 (executable)
index 4cb5738..fcebf33
@@ -122,5 +122,9 @@ void XWalkExtensionClient::OnReceivedIPCMessage(
 
   it->second->HandleMessageFromNative(msg);
 }
+void XWalkExtensionClient::LoadUserExtensions(const std::string app_path) {
+  XWalkExtensionServer* server = XWalkExtensionServer::GetInstance();
+  server->LoadUserExtensions(app_path);
+}
 
 }  // namespace extensions
old mode 100644 (file)
new mode 100755 (executable)
index 275be52..5b98ee9
@@ -48,6 +48,7 @@ class XWalkExtensionClient {
 
   void OnReceivedIPCMessage(const std::string& instance_id,
                             const std::string& msg);
+  void LoadUserExtensions(const std::string app_path);
 
   struct ExtensionCodePoints {
     std::string api;
old mode 100644 (file)
new mode 100755 (executable)
index 2c916ea..7a83325
@@ -106,5 +106,9 @@ void XWalkExtensionRendererController::OnReceivedIPCMessage(
 void XWalkExtensionRendererController::InitializeExtensionClient() {
   extensions_client_->Initialize();
 }
+void XWalkExtensionRendererController::LoadUserExtensions(
+  const std::string app_path) {
+  extensions_client_->LoadUserExtensions(app_path);
+}
 
 }  // namespace extensions
old mode 100644 (file)
new mode 100755 (executable)
index b550721..03370a7
@@ -26,6 +26,7 @@ class XWalkExtensionRendererController {
   void OnReceivedIPCMessage(const Ewk_IPC_Wrt_Message_Data* data);
 
   void InitializeExtensionClient();
+  void LoadUserExtensions(const std::string app_path);
 
  private:
   XWalkExtensionRendererController();
index 7998732..eb7538d 100755 (executable)
@@ -97,6 +97,12 @@ extern "C" void DynamicSetWidgetInfo(const char* tizen_id) {
   ecore_init();
 
   runtime::BundleGlobalData::GetInstance()->Initialize(tizen_id);
+  extensions::XWalkExtensionRendererController& controller =
+    extensions::XWalkExtensionRendererController::GetInstance();
+  auto appdata_manager = common::ApplicationDataManager::GetInstance();
+  common::ApplicationData* app_data =
+    appdata_manager->GetApplicationData(tizen_id);
+  controller.LoadUserExtensions(app_data->application_path());
 }
 
 extern "C" void DynamicPluginStartSession(const char* tizen_id,