electron: sendInputEvent doesn't work for iframe/webview

I’m trying to interact with an embedded video using manual mouse input via webContents.sendInputEvent api. It stopped working in 5-0-x.

Issue Details

  • Electron Version:
    • 5-0-x, 6-0-x
  • Operating System:
    • Win10 (1903), macOS 10.14.6
  • Last Known Working Electron version:
    • 4.2.10

Expected Behavior

sendInputEvent should produce the same effects as OS input

Actual Behavior

sendInputEvent doesn’t interact with <iframe> or <webview>

To Reproduce

const { app, BrowserWindow } = require('electron')
// Fails with 5.0.10
// Works with 4.2.10

app.once('ready', () => {
  const { webContents: wc } = new BrowserWindow({
    width: 800,
    height: 600
  })
  wc.once('did-finish-load', () => {
    setTimeout(() => {
      wc.sendInputEvent({ type: 'mouseMove', x: 600, y: 300 })
    }, 1000)
    setTimeout(() => {
      wc.sendInputEvent({ type: 'mouseDown', x: 600, y: 300, button: 'right' })
    }, 2000)
    setTimeout(() => {
      wc.sendInputEvent({ type: 'mouseUp', x: 600, y: 300, button: 'right' })
    }, 2100)
  })
  wc.on('context-menu', (event, params) => {
    console.log(params)
  })
  wc.loadURL('https://www.w3schools.com/html/tryit.asp?filename=tryhtml_youtubeiframe')
})

Additional Information

The test above manually triggers a right click on the embedded video, which should

  • activate iframe for the first mouse move
  • open the youtube context menu instead of main frame’s

About this issue

  • Original URL
  • State: open
  • Created 7 years ago
  • Reactions: 1
  • Comments: 25 (3 by maintainers)

Commits related to this issue

Most upvoted comments

Still an issue.

This is still very much relevant, please consider implementing this, it would make so much more sense to not have to worry about which frame is where and send the event to the right frame (which barely works, as it’s not a user event in that case 😢).

To make keyboard events propagate to iframes, Electron must be patched to choose a focused RenderWidgedHost instead of the default WebContents, like so:

diff --git a/shell/browser/api/electron_api_web_contents.cc b/shell/browser/api/electron_api_web_contents.cc
index fa521e25e..5f779ffbf 100644
--- a/shell/browser/api/electron_api_web_contents.cc
+++ b/shell/browser/api/electron_api_web_contents.cc
@@ -2386,7 +2386,10 @@ void WebContents::SendInputEvent(v8::Isolate* isolate,
   if (!view)
     return;
 
-  content::RenderWidgetHost* rwh = view->GetRenderWidgetHost();
+  content::RenderWidgetHost* rwh = reinterpret_cast<content::RenderWidgetHostViewBase*>(view)->GetFocusedWidget();
+  if (!rwh) {
+    rwh = view->GetRenderWidgetHost();
+  }
   blink::WebInputEvent::Type type =
       gin::GetWebInputEventType(isolate, input_event);
   if (blink::WebInputEvent::IsMouseEventType(type)) {

Or, in a cleaner fascion (first, patch Chromium to properly expose the GetFocusedWidget() function):

diff --git a/content/browser/renderer_host/render_widget_host_view_base.cc b/content/browser/renderer_host/render_widget_host_view_base.cc
index b41d39c8ced7..c841ff2fce98 100644
--- a/content/browser/renderer_host/render_widget_host_view_base.cc
+++ b/content/browser/renderer_host/render_widget_host_view_base.cc
@@ -63,6 +63,10 @@ RenderWidgetHostViewBase::~RenderWidgetHostViewBase() {
     host_->render_frame_metadata_provider()->RemoveObserver(this);
 }
 
+RenderWidgetHost* RenderWidgetHostViewBase::GetFocusedRenderWidgetHost() const {
+  return GetFocusedWidget();
+}
+
 RenderWidgetHostImpl* RenderWidgetHostViewBase::GetFocusedWidget() const {
   return host() && host()->delegate()
              ? host()->delegate()->GetFocusedRenderWidgetHost(host())
diff --git a/content/browser/renderer_host/render_widget_host_view_base.h b/content/browser/renderer_host/render_widget_host_view_base.h
index f7af7975abf7..3635e1e8aa1f 100644
--- a/content/browser/renderer_host/render_widget_host_view_base.h
+++ b/content/browser/renderer_host/render_widget_host_view_base.h
@@ -89,6 +89,9 @@ class CONTENT_EXPORT RenderWidgetHostViewBase
     return current_device_scale_factor_;
   }
 
+  // Returns the focused RenderWidgetHost inside this |view|'s RWH.
+  RenderWidgetHost* GetFocusedRenderWidgetHost() const override;
+
   // Returns the focused RenderWidgetHost inside this |view|'s RWH.
   RenderWidgetHostImpl* GetFocusedWidget() const;
 
diff --git a/content/public/browser/render_widget_host_view.h b/content/public/browser/render_widget_host_view.h
index 4810a45aa4b0..5d610bb1732e 100644
--- a/content/public/browser/render_widget_host_view.h
+++ b/content/public/browser/render_widget_host_view.h
@@ -62,6 +62,9 @@ class CONTENT_EXPORT RenderWidgetHostView {
   // initialize a child window.
   virtual void InitAsChild(gfx::NativeView parent_view) = 0;
 
+  // Returns the focused RenderWidgetHost inside this |view|'s RWH.
+  virtual RenderWidgetHost* GetFocusedRenderWidgetHost() const = 0;
+
   // Returns the associated RenderWidgetHost.
   virtual RenderWidgetHost* GetRenderWidgetHost() = 0;

Then, Electron can be patched without a reinterpret_cast:

diff --git a/shell/browser/api/electron_api_web_contents.cc b/shell/browser/api/electron_api_web_contents.cc
index fa521e25e..2c688a37a 100644
--- a/shell/browser/api/electron_api_web_contents.cc
+++ b/shell/browser/api/electron_api_web_contents.cc
@@ -2386,7 +2386,10 @@ void WebContents::SendInputEvent(v8::Isolate* isolate,
   if (!view)
     return;
 
-  content::RenderWidgetHost* rwh = view->GetRenderWidgetHost();
+  content::RenderWidgetHost* rwh = view->GetFocusedRenderWidgetHost();
+  if (!rwh) {
+    rwh = view->GetRenderWidgetHost();
+  }
   blink::WebInputEvent::Type type =
       gin::GetWebInputEventType(isolate, input_event);
   if (blink::WebInputEvent::IsMouseEventType(type)) {

Electron 10-x-y (Linux). Still need to patch it to make mouse events propagate to iframes/webviews:

diff --git a/content/browser/renderer_host/render_widget_host_impl.cc b/content/browser/renderer_host/render_widget_host_impl.cc
index 8d205a1b052c..559745bfe1f3 100644
--- a/content/browser/renderer_host/render_widget_host_impl.cc
+++ b/content/browser/renderer_host/render_widget_host_impl.cc
@@ -1292,8 +1292,15 @@ void RenderWidgetHostImpl::ForwardMouseEvent(const WebMouseEvent& mouse_event) {
     return;
   }
 
+  // NOTE: (Modified) Route mouse event just like in RenderWidgetHostViewEventHandler::OnMouseEvent
+  MouseEventWithLatencyInfo mouse_with_latency(mouse_event, ui::LatencyInfo(ui::SourceEventType::MOUSE));
+  delegate_->GetInputEventRouter()->RouteMouseEvent(
+                 GetView(), &mouse_with_latency.event, mouse_with_latency.latency);
+
+  /*
   ForwardMouseEventWithLatencyInfo(mouse_event,
                                    ui::LatencyInfo(ui::SourceEventType::MOUSE));
+  */
   if (owner_delegate_)
     owner_delegate_->RenderWidgetDidForwardMouseEvent(mouse_event);
 }
diff --git a/content/browser/renderer_host/render_widget_targeter.cc b/content/browser/renderer_host/render_widget_targeter.cc
index 0d1dd07d1920..f40951793a5f 100644
--- a/content/browser/renderer_host/render_widget_targeter.cc
+++ b/content/browser/renderer_host/render_widget_targeter.cc
@@ -246,6 +246,7 @@ void RenderWidgetTargeter::ResolveTargetingRequest(TargetingRequest request) {
     result = delegate_->FindTargetSynchronouslyAtPoint(request_target,
                                                        request_target_location);
   }
+  /*
   RenderWidgetHostViewBase* target = result.view;
   async_depth_ = 0;
   if (!is_autoscroll_in_progress_ && result.should_query_view) {
@@ -262,12 +263,15 @@ void RenderWidgetTargeter::ResolveTargetingRequest(TargetingRequest request) {
     // root_view and the original event location for the initial query.
     // Do not compare hit test results if we are forced to do async hit testing
     // by HitTestQuery.
+  */
     QueryClient(request_target, request_target_location, nullptr, gfx::PointF(),
                 std::move(request));
+  /*
   } else {
     FoundTarget(target, result.target_location, result.latched_target,
                 &request);
   }
+  */
 }
 
 void RenderWidgetTargeter::ViewWillBeDestroyed(RenderWidgetHostViewBase* view) {

It seems to be working for normal case if I patch the content::RenderWidgetHostImpl::ForwardMouseEvent method to this:

void RenderWidgetHostImpl::ForwardMouseEvent(const WebMouseEvent& mouse_event) {
  ...
  // NOTE: (Modified) Route mouse event just like in RenderWidgetHostViewEventHandler::OnMouseEvent
  MouseEventWithLatencyInfo mouse_with_latency(mouse_event, ui::LatencyInfo(ui::SourceEventType::MOUSE));
  delegate_->GetInputEventRouter()->RouteMouseEvent(
		  GetView(), &mouse_with_latency.event, mouse_with_latency.latency);

  /*ForwardMouseEventWithLatencyInfo(mouse_event,
                                   ui::LatencyInfo(ui::SourceEventType::MOUSE));*/
  ...
}

However, I don’t know how does this change fit the overall chromium structure or is okay at all.

Also, I want it to work with OSR primarily, and that still fails to work even with these changes


UPDATE: I managed to solve the OSR case as well with modifying contents::RenderWidgetTargeter::FindTargetAndDispatch.

If we call QueryClient unconditionally, then everything seems to be fine. Although in this case we actually don’t use the viz hittester code path, so I’m kinda lost here why it’s there or OSR should use it or not. 😕