From 3e07afe5420321e13c582e9d5c0556b0b6a1b817 Mon Sep 17 00:00:00 2001 From: samuelmaddock Date: Fri, 3 Sep 2021 12:27:24 -0400 Subject: [PATCH 1/2] feat: add frame to context-menu event params --- docs/api/web-contents.md | 1 + .../browser/api/electron_api_web_contents.cc | 2 +- .../gin_converters/content_converter.cc | 7 ++++-- .../common/gin_converters/content_converter.h | 12 +++++----- spec-main/api-web-contents-spec.ts | 22 +++++++++++++++++++ 5 files changed, 36 insertions(+), 8 deletions(-) diff --git a/docs/api/web-contents.md b/docs/api/web-contents.md index 040e5746e889d..a8bf1fb8877d2 100644 --- a/docs/api/web-contents.md +++ b/docs/api/web-contents.md @@ -650,6 +650,7 @@ Returns: * `params` Object * `x` Integer - x coordinate. * `y` Integer - y coordinate. + * `frame` WebFrameMain - Frame where the context menu was invoked in. * `linkURL` String - URL of the link that encloses the node the context menu was invoked on. * `linkText` String - Text associated with the link. May be an empty diff --git a/shell/browser/api/electron_api_web_contents.cc b/shell/browser/api/electron_api_web_contents.cc index 4c3686bf4f283..16dd29072ebc0 100644 --- a/shell/browser/api/electron_api_web_contents.cc +++ b/shell/browser/api/electron_api_web_contents.cc @@ -1288,7 +1288,7 @@ void WebContents::RendererResponsive( bool WebContents::HandleContextMenu(content::RenderFrameHost* render_frame_host, const content::ContextMenuParams& params) { - Emit("context-menu", std::make_pair(params, web_contents())); + Emit("context-menu", std::make_pair(params, render_frame_host)); return true; } diff --git a/shell/common/gin_converters/content_converter.cc b/shell/common/gin_converters/content_converter.cc index a9979d7f599c7..78062b3a49a67 100644 --- a/shell/common/gin_converters/content_converter.cc +++ b/shell/common/gin_converters/content_converter.cc @@ -13,6 +13,7 @@ #include "shell/browser/web_contents_permission_helper.h" #include "shell/common/gin_converters/blink_converter.h" #include "shell/common/gin_converters/callback_converter.h" +#include "shell/common/gin_converters/frame_converter.h" #include "shell/common/gin_converters/gfx_converter.h" #include "shell/common/gin_converters/gurl_converter.h" #include "shell/common/gin_helper/dictionary.h" @@ -73,11 +74,13 @@ v8::Local Converter::ToV8( } // static -v8::Local Converter::ToV8( +v8::Local Converter::ToV8( v8::Isolate* isolate, - const ContextMenuParamsWithWebContents& val) { + const ContextMenuParamsWithRenderFrameHost& val) { const auto& params = val.first; + content::RenderFrameHost* render_frame_host = val.second; gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate); + dict.SetGetter("frame", render_frame_host); dict.Set("x", params.x); dict.Set("y", params.y); dict.Set("linkURL", params.link_url); diff --git a/shell/common/gin_converters/content_converter.h b/shell/common/gin_converters/content_converter.h index 61f846c987f8e..26621c05a8e1b 100644 --- a/shell/common/gin_converters/content_converter.h +++ b/shell/common/gin_converters/content_converter.h @@ -17,11 +17,12 @@ namespace content { struct ContextMenuParams; struct NativeWebKeyboardEvent; +class RenderFrameHost; class WebContents; } // namespace content -using ContextMenuParamsWithWebContents = - std::pair; +using ContextMenuParamsWithRenderFrameHost = + std::pair; namespace gin { @@ -32,9 +33,10 @@ struct Converter { }; template <> -struct Converter { - static v8::Local ToV8(v8::Isolate* isolate, - const ContextMenuParamsWithWebContents& val); +struct Converter { + static v8::Local ToV8( + v8::Isolate* isolate, + const ContextMenuParamsWithRenderFrameHost& val); }; template <> diff --git a/spec-main/api-web-contents-spec.ts b/spec-main/api-web-contents-spec.ts index 771abfc7c7e60..2de7273c06496 100644 --- a/spec-main/api-web-contents-spec.ts +++ b/spec-main/api-web-contents-spec.ts @@ -2056,6 +2056,28 @@ describe('webContents module', () => { }); }); + describe('context-menu event', () => { + afterEach(closeAllWindows); + it('emits when right-clicked in page', async () => { + const w = new BrowserWindow({ show: false }); + await w.loadFile(path.join(fixturesPath, 'pages', 'base-page.html')); + + const promise = emittedOnce(w.webContents, 'context-menu'); + + // Simulate right-click to create context-menu event. + const opts = { x: 0, y: 0, button: 'right' as any }; + w.webContents.sendInputEvent({ ...opts, type: 'mouseDown' }); + w.webContents.sendInputEvent({ ...opts, type: 'mouseUp' }); + + const [, params] = await promise; + + expect(params.pageURL).to.equal(w.webContents.getURL()); + expect(params.frame).to.be.an('object'); + expect(params.x).to.be.a('number'); + expect(params.y).to.be.a('number'); + }); + }); + it('emits a cancelable event before creating a child webcontents', async () => { const w = new BrowserWindow({ show: false, From 28fd9703f2b3a6b15ea096f9590e8765afc9f662 Mon Sep 17 00:00:00 2001 From: samuelmaddock Date: Fri, 3 Sep 2021 15:04:35 -0400 Subject: [PATCH 2/2] doc: rephrase frame description --- docs/api/web-contents.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/api/web-contents.md b/docs/api/web-contents.md index a8bf1fb8877d2..2135a8d9439db 100644 --- a/docs/api/web-contents.md +++ b/docs/api/web-contents.md @@ -650,7 +650,7 @@ Returns: * `params` Object * `x` Integer - x coordinate. * `y` Integer - y coordinate. - * `frame` WebFrameMain - Frame where the context menu was invoked in. + * `frame` WebFrameMain - Frame from which the context menu was invoked. * `linkURL` String - URL of the link that encloses the node the context menu was invoked on. * `linkText` String - Text associated with the link. May be an empty