[XWalkExtension] Add window related interface for webapis 86/305686/23
authorDongHyun Song <dh81.song@samsung.com>
Fri, 23 Feb 2024 09:56:44 +0000 (18:56 +0900)
committerSangYong Park <sy302.park@samsung.com>
Wed, 6 Mar 2024 21:34:00 +0000 (21:34 +0000)
Add useful interface to handle window object for XWalkExtension

1) void* GetWindowObject()
 - webapis can get the  window object directly.

2) AddWindowEventListener(instance, callback)
 - register event listener for visibilitychange, rotation
 - event listner will be managed by instance ID

3) ClearWindowEventListener(instance)
 - unregister event listener
 - event listner will be managed by instance ID

4) SendKeyEvent(type, keycode)
5) SendMouseEvent(type, ...)
 - webapi can send its key/mouse events to the web application.
 - for example, TVWindow webapi can create Elm window for TV viewer,
   then they can grap key/mouse events, to propagate the events, these
   interface can be used.

6) GetMetadataValue(key)
 - return metadata value with given metadata key.

Change-Id: I9f63acf0c7e569ce4adccc472a6cdc7084ef76e1
Signed-off-by: DongHyun Song <dh81.song@samsung.com>
tizen_src/chromium_impl/components/xwalk_extensions/browser/xwalk_extension.cc
tizen_src/chromium_impl/components/xwalk_extensions/browser/xwalk_extension_manager.cc
tizen_src/chromium_impl/components/xwalk_extensions/browser/xwalk_extension_manager.h
tizen_src/chromium_impl/components/xwalk_extensions/common/xwalk_extension_browser_delegate.h
tizen_src/chromium_impl/components/xwalk_extensions/public/XW_Extension_Runtime.h
wrt/src/browser/tv/wrt_native_window_tv.cc
wrt/src/browser/tv/wrt_xwalk_extension_browser_tv.cc
wrt/src/browser/tv/wrt_xwalk_extension_browser_tv.h

index 7b1c423..361b43d 100644 (file)
@@ -139,6 +139,38 @@ class XWalkExtension::Interface {
     extension_manager->GetRuntimeVariable(key, value, value_len);
   }
 
+  static void* RuntimeGetWindowObject() {
+    auto* extension_manager = XWalkExtensionManager::GetInstance();
+    return extension_manager->GetWindowObject();
+  }
+
+  static void RuntimeAddWindowEventListener(XW_Instance instance,
+                                            WindowEvent event) {
+    auto* extension_manager = XWalkExtensionManager::GetInstance();
+    extension_manager->AddWindowEventListener(instance, event);
+  }
+
+  static void RuntimeRemoveWindowEventListener(XW_Instance instance) {
+    auto* extension_manager = XWalkExtensionManager::GetInstance();
+    extension_manager->RemoveWindowEventListener(instance);
+  }
+
+  static void RuntimeSendKeyEvent(const char* event, const char* key_code) {
+    auto* extension_manager = XWalkExtensionManager::GetInstance();
+    extension_manager->SendKeyEvent(event, key_code);
+  }
+
+  static void RuntimeSendMouseEvent(const char* event, int btn, int x, int y) {
+    auto* extension_manager = XWalkExtensionManager::GetInstance();
+    extension_manager->SendMouseEvent(event, btn, x, y);
+  }
+
+  static const char* RuntimeGetMetadataValue(const char* key) {
+    auto* extension_manager = XWalkExtensionManager::GetInstance();
+    auto value = extension_manager->GetMetadataValue(key);
+    return value.c_str();
+  }
+
   static int PermissionsCheckAPIAccessControl(
       XW_Extension xw_extension, const char* api_name) {
     XWalkExtension* extension = GetExtension(xw_extension);
@@ -380,6 +412,18 @@ const void* XWalkExtension::GetInterface(const char* name) {
     return &runtimeInterface1;
   }
 
+  if (interface_name == XW_INTERNAL_WINDOW_INTERFACE) {
+    static const XW_Internal_WindowInterface window_interface = {
+      Interface::RuntimeGetWindowObject,
+      Interface::RuntimeAddWindowEventListener,
+      Interface::RuntimeRemoveWindowEventListener,
+      Interface::RuntimeSendKeyEvent,
+      Interface::RuntimeSendMouseEvent,
+      Interface::RuntimeGetMetadataValue
+    };
+    return &window_interface;
+  }
+
   if (interface_name == XW_INTERNAL_PERMISSIONS_INTERFACE_1) {
     static const XW_Internal_PermissionsInterface_1 permissionsInterface1 = {
       Interface::PermissionsCheckAPIAccessControl,
index 954fdc3..46635f2 100644 (file)
@@ -502,6 +502,57 @@ void XWalkExtensionManager::GetRuntimeVariable(
     delegate_->GetRuntimeVariable(key, value, value_len);
 }
 
+void* XWalkExtensionManager::GetWindowObject() {
+  base::AutoLock lock(xmanager_instance_lock);
+  return delegate_? delegate_->GetWindowObject() : nullptr;
+}
+
+void XWalkExtensionManager::AddWindowEventListener(XW_Instance instance,
+                                                   WindowEvent event) {
+  base::AutoLock lock(xmanager_instance_lock);
+  if (delegate_)
+    delegate_->AddWindowEventListener(instance, event);
+}
+
+void XWalkExtensionManager::RemoveWindowEventListener(XW_Instance instance) {
+  base::AutoLock lock(xmanager_instance_lock);
+  if (delegate_)
+    delegate_->RemoveWindowEventListener(instance);
+}
+
+void XWalkExtensionManager::NotifyWindowEvent(const std::string& event_name,
+                                              const std::string& event_value) {
+  LOG(INFO) << "event : " << event_name << ", value : " << event_value;
+  base::AutoLock lock(xmanager_instance_lock);
+  if (delegate_)
+    delegate_->NotifyWindowEvent(event_name, event_value);
+}
+
+void XWalkExtensionManager::SendKeyEvent(const std::string& event,
+                                         const std::string& key_code) {
+  LOG(INFO) << "event : " << event << ", key_code : " << key_code;
+  if (delegate_)
+    delegate_->SendKeyEvent(event, key_code);
+}
+
+void XWalkExtensionManager::SendMouseEvent(const std::string& event, int button,
+                                           int x, int y) {
+  LOG(INFO) << "event : " << event << ", x : " << x << ", y : " << y;
+  if (delegate_)
+    delegate_->SendMouseEvent(event, button, x, y);
+}
+
+std::string XWalkExtensionManager::GetMetadataValue(const std::string& key) {
+  base::AutoLock lock(xmanager_instance_lock);
+  return delegate_ ? delegate_->GetMetadataValue(key) : std::string();
+}
+
+bool XWalkExtensionManager::IsAliveInstance(XW_Instance instance) {
+  base::AutoLock lock(xmanager_instance_lock);
+  auto it = instances_.find(instance);
+  return it != instances_.end();
+}
+
 std::string XWalkExtensionManager::GetUpgradableLibPath(
     const std::string& name) {
   return delegate_ ? delegate_->GetUpgradableLibPath(name) : std::string();
index 67626d5..9868e40 100644 (file)
@@ -57,7 +57,16 @@ class XWalkExtensionManager : public mojom::XWalkExtensionBrowser {
   void UnregisterInstance(XWalkExtension::Instance* instance);
 
   // product specific features
+  void* GetWindowObject();
+  void AddWindowEventListener(XW_Instance instance, WindowEvent event);
+  bool IsAliveInstance(XW_Instance instance);
+  void NotifyWindowEvent(const std::string& event_name,
+                         const std::string& event_value);
+  void RemoveWindowEventListener(XW_Instance instance);
   void RegisterUpgradableExtensions();
+  void SendKeyEvent(const std::string& event, const std::string& key_code);
+  void SendMouseEvent(const std::string& event, int button, int x, int y);
+  std::string GetMetadataValue(const std::string& name);
   std::string GetUpgradableLibPath(const std::string& name);
 
  protected:
index 575ffb9..de3f895 100644 (file)
@@ -7,8 +7,12 @@
 
 #include <string>
 
+#include "tizen_src/chromium_impl/components/xwalk_extensions/public/XW_Extension.h"
+
 namespace wrt {
 
+typedef void (*WindowEvent)(const char* event_name, const char* event_value);
+
 class XWalkExtensionBrowserDelegate {
  public:
   XWalkExtensionBrowserDelegate() {}
@@ -25,10 +29,23 @@ class XWalkExtensionBrowserDelegate {
 
   // product specific functions
   virtual void RegisterUpgradableExtensions() {}
+  virtual std::string GetMetadataValue(const std::string& key) {
+    return std::string();
+  }
   virtual std::string GetUpgradableLibPath(const std::string& name) {
     return std::string();
   }
   virtual bool NeedClearLibHandle() { return true; }
+  virtual void* GetWindowObject() { return nullptr; }
+  virtual void AddWindowEventListener(XW_Instance instance,
+                                      WindowEvent event) {}
+  virtual void RemoveWindowEventListener(XW_Instance instance) {}
+  virtual void NotifyWindowEvent(const std::string& event_name,
+                                 const std::string& event_value) {}
+  virtual void SendKeyEvent(const std::string& event,
+                            const std::string& key_code) {}
+  virtual void SendMouseEvent(const std::string& event, int button,
+                              int x, int y) {}
 };
 
 }  // namespace wrt
index 11ad307..d7b3cea 100644 (file)
@@ -36,6 +36,18 @@ struct XW_Internal_RuntimeInterface_1 {
 typedef struct XW_Internal_RuntimeInterface_1
     XW_Internal_RuntimeInterface;
 
+#define XW_INTERNAL_WINDOW_INTERFACE "XW_Internal_WindowInterface"
+
+typedef void (*WindowEvent)(const char* event_name, const char* event_value);
+typedef struct XW_Internal_WindowInterface_ {
+  void* (*GetWindowObject)(void);
+  void (*AddWindowEventListener)(XW_Instance instance, WindowEvent event);
+  void (*RemoveWindowEventListener)(XW_Instance instance);
+  void (*SendKeyEvent)(const char* event, const char* key_code);
+  void (*SendMouseEvent)(const char* event, int button, int x, int y);
+  const char* (*GetMetadataValue)(const char* key);
+} XW_Internal_WindowInterface;
+
 #ifdef __cplusplus
 }  // extern "C"
 #endif
index 86e2398..298779f 100644 (file)
@@ -35,6 +35,7 @@
 #if !defined(WRT_JS_BRINGUP)
 #include "tizen_src/chromium_impl/chrome/browser/net/proxy_config_monitor.h"
 #endif
+#include "tizen_src/chromium_impl/components/xwalk_extensions/browser/xwalk_extension_manager.h"
 #include "tizen_src/chromium_impl/tizen/vconf_handle.h"
 #include "ui/ozone/platform/efl/efl_screen.h"
 #include "ui/platform_window/platform_window.h"
@@ -1161,6 +1162,9 @@ void WRTNativeWindowTV::SetPageVisibility(bool visible) {
   else
     VisibilityChangedAsBackground();
 
+  auto* extension_manager = XWalkExtensionManager::GetInstance();
+  extension_manager->NotifyWindowEvent("visibility", visibility_state_);
+
 #if defined(THREAD_BOOSTER_SERVICE)
   if (visible)
     suspend_resume::NotifyStateChange(suspend_resume::State::RESUMED);
@@ -1806,6 +1810,9 @@ void WRTNativeWindowTV::RotateWindow(int degree) {
   }
   LOG(INFO) << "degree : " << degree << " (" << screen_width << "x"
             << screen_height << ")";
+
+  auto* extension_manager = XWalkExtensionManager::GetInstance();
+  extension_manager->NotifyWindowEvent("orientation", std::to_string(degree));
 }
 
 void WRTNativeWindowTV::ScreenSaverResetTimeout(void) {
index 4a6bd97..1850f5d 100644 (file)
@@ -17,6 +17,7 @@
 #include "electron/shell/browser/browser.h"
 #include "electron/shell/browser/window_list.h"
 #include "tizen_src/chromium_impl/components/xwalk_extensions/browser/xwalk_extension_manager.h"
+#include "tizen_src/chromium_impl/ui/ozone/platform/efl/efl_event_handler.h"
 #include "wrt/src/browser/native_web_runtime.h"
 #include "wrt/src/browser/tv/input_device_manager.h"
 #include "wrt/src/browser/tv/native_web_runtime_delegate_tv.h"
@@ -457,4 +458,65 @@ void WRTXWalkExtensionBrowserTV::PreloadExtensions() {
   }
 }
 
+void* WRTXWalkExtensionBrowserTV::GetWindowObject() {
+  return WRTNativeWindow::GetTopWindow();
+}
+
+void WRTXWalkExtensionBrowserTV::AddWindowEventListener(
+  XW_Instance instance, WindowEvent event_listener) {
+  event_listeners_[instance] = event_listener;
+}
+
+void WRTXWalkExtensionBrowserTV::RemoveWindowEventListener(
+    XW_Instance instance) {
+  event_listeners_.erase(instance);
+}
+
+void WRTXWalkExtensionBrowserTV::NotifyWindowEvent(
+    const std::string& event_name, const std::string& event_value) {
+  auto* extension_manager = XWalkExtensionManager::GetInstance();
+  std::vector<XW_Instance> dead_instances;
+  for (const auto& event : event_listeners_) {
+    auto instance_id = event.first;
+    if (extension_manager->IsAliveInstance(instance_id)) {
+      auto callback = event.second;
+      callback(event_name.c_str(), event_value.c_str());
+    } else {
+      dead_instances.push_back(instance_id);
+    }
+  }
+  for (const auto instance_id : dead_instances) {
+    RemoveWindowEventListener(instance_id);
+  }
+}
+
+void WRTXWalkExtensionBrowserTV::SendKeyEvent(const std::string& event,
+                                              const std::string& key_code) {
+  LOG(INFO) << "event : " << event << ", key_code : " << key_code;
+}
+
+void WRTXWalkExtensionBrowserTV::SendMouseEvent(const std::string& event,
+                                                int button, int x, int y) {
+  LOG(INFO) << "event : " << event;
+  auto event_handler = InputDeviceManager::GetInstance()->GetEventHandler();
+  if (event == "mount-in") {
+    event_handler->SendMouseIn();
+  } else if (event == "mouse-out") {
+    event_handler->SendMouseOut();
+  } else if (event == "mouse-down") {
+    event_handler->SendMouseDown(button, x, y);
+  } else if (event == "mouse-up") {
+    event_handler->SendMouseUp(button, x, y);
+  } else if (event == "mouse-move") {
+    event_handler->SendMouseMove(x, y);
+  } else {
+    LOG(INFO) << "Unsupported mouse event";
+  }
+}
+
+std::string WRTXWalkExtensionBrowserTV::GetMetadataValue(
+    const std::string& key) {
+  return ApplicationData::GetInstance().GetMetadata(key);
+}
+
 }  // namespace wrt
index 252c88f..3ebbb67 100644 (file)
@@ -20,9 +20,20 @@ class WRTXWalkExtensionBrowserTV : public WRTXWalkExtensionBrowser {
 
  private:
   // XWalkExtensionBrowserDelegate
+  void* GetWindowObject() override;
   void LoadUIAppExtensions() override;
   void PreloadExtensions() override;
   void RegisterUpgradableExtensions() override;
+  void AddWindowEventListener(XW_Instance instance,
+                              WindowEvent event_listener) override;
+  void RemoveWindowEventListener(XW_Instance instance) override;
+  void NotifyWindowEvent(const std::string& event_name,
+                         const std::string& event_value) override;
+  void SendKeyEvent(const std::string& event,
+                    const std::string& key_code) override;
+  void SendMouseEvent(const std::string& event, int button,
+                      int x, int y) override;
+  std::string GetMetadataValue(const std::string& key) override;
   std::string GetUpgradableLibPath(const std::string& name) override;
   std::string HandleRuntimeMessageInternal(
       const std::string& type, const std::string& value) override;
@@ -45,6 +56,7 @@ class WRTXWalkExtensionBrowserTV : public WRTXWalkExtensionBrowser {
   void* tizen_common_dlhandle_ = nullptr;
   bool is_ui_app_ = false;
   std::map<std::string, std::string> lazyload_extensions_;
+  std::map<XW_Extension, WindowEvent> event_listeners_;
 };
 
 }  // namespace wrt