From 76c4d4de7d752cb1ea96f948f5b2f232762c100f Mon Sep 17 00:00:00 2001 From: Robin Westberg Date: Mon, 31 Jan 2022 09:01:20 +0100 Subject: [PATCH] SDA-3561 - Allow C2 / extensions to retrieve the native window handle (#1332) --- spec/mainApiHandler.spec.ts | 14 +++ src/app/main-api-handler.ts | 8 ++ src/common/api-interface.ts | 1 + src/demo/index.html | 26 +++++ src/demo/win.html | 183 ++++++++++++++++++++++------------- src/renderer/preload-main.ts | 1 + src/renderer/ssf-api.ts | 10 ++ 7 files changed, 175 insertions(+), 68 deletions(-) diff --git a/spec/mainApiHandler.spec.ts b/spec/mainApiHandler.spec.ts index 29344dcc..367fd232 100644 --- a/spec/mainApiHandler.spec.ts +++ b/spec/mainApiHandler.spec.ts @@ -469,5 +469,19 @@ describe('main api handler', () => { ipcMain.send(apiName.symphonyApi, value); expect(spy).toBeCalled(); }); + + it('should call `getNativeWindowHandle` correctly', () => { + const fromWebContentsMocked = { + getNativeWindowHandle: jest.fn(), + }; + jest.spyOn(BrowserWindow, 'fromWebContents').mockImplementation(() => { + return fromWebContentsMocked; + }); + const value = { + cmd: apiCmds.getNativeWindowHandle, + }; + ipcMain.send(apiName.symphonyApi, value); + expect(fromWebContentsMocked.getNativeWindowHandle).toBeCalledTimes(1); + }); }); }); diff --git a/src/app/main-api-handler.ts b/src/app/main-api-handler.ts index 2bb67045..e193638b 100644 --- a/src/app/main-api-handler.ts +++ b/src/app/main-api-handler.ts @@ -449,6 +449,14 @@ ipcMain.handle( : false; } break; + case apiCmds.getNativeWindowHandle: + const browserWin = BrowserWindow.fromWebContents( + event.sender, + ) as ICustomBrowserWindow; + if (browserWin && windowExists(browserWin)) { + return browserWin.getNativeWindowHandle(); + } + break; } return; }, diff --git a/src/common/api-interface.ts b/src/common/api-interface.ts index ab4ff2b4..42d955a5 100644 --- a/src/common/api-interface.ts +++ b/src/common/api-interface.ts @@ -61,6 +61,7 @@ export enum apiCmds { setBroadcastMessage = 'set-broadcast-message', handleSwiftSearchMessageEvents = 'handle-shift-search-message-events', onSwiftSearchMessage = 'on-shift-search-message', + getNativeWindowHandle = 'get-native-window-handle', } export enum apiName { diff --git a/src/demo/index.html b/src/demo/index.html index 0d3227f5..4cea94c1 100644 --- a/src/demo/index.html +++ b/src/demo/index.html @@ -276,6 +276,11 @@ Full path to MSI
+ +
+

Native Window Handle:

+ +

@@ -323,6 +328,7 @@ checkMediaPermission: 'check-media-permission', restartApp: 'restart-app', autoUpdate: 'auto-update', + getNativeWindowHandle: 'get-native-window-handle', }; let requestId = 0; @@ -1176,5 +1182,25 @@ postMessage(apiCmds.restartApp); } }); + + document + .getElementById('get-window-handle') + .addEventListener('click', () => { + const resultCallback = (handle) => { + const handleStr = [...handle] + .map((b) => b.toString(16).padStart(2, '0')) + .join(''); + document.getElementById('text-window-handle').value = handleStr; + }; + if (window.ssf) { + window.ssf.getNativeWindowHandle().then(resultCallback); + } else if (window.manaSSF) { + window.manaSSF.getNativeWindowHandle().then(resultCallback); + } else { + postRequest(apiCmds.getNativeWindowHandle, null, { + successCallback: resultCallback, + }); + } + }); diff --git a/src/demo/win.html b/src/demo/win.html index fdea1610..ebd80d7c 100644 --- a/src/demo/win.html +++ b/src/demo/win.html @@ -1,87 +1,134 @@ - - + + Test pop-out window - - -Test Window has been opened -

- - -

+ + + Test Window has been opened +

+ + +

- -
-
+ +
+
- -
-
+ +
+
-Open Symphony -
- -
- - - +

Badge Count:

+

+ +
+

+ +
+

Native Window Handle:

+ + + +
+
diff --git a/src/renderer/preload-main.ts b/src/renderer/preload-main.ts index 7701b3a8..c628f4fc 100644 --- a/src/renderer/preload-main.ts +++ b/src/renderer/preload-main.ts @@ -88,6 +88,7 @@ if (ssfWindow.ssf) { setZoomLevel: ssfWindow.ssf.setZoomLevel, getZoomLevel: ssfWindow.ssf.getZoomLevel, supportedSettings: ssfWindow.ssf.supportedSettings, + getNativeWindowHandle: ssfWindow.ssf.getNativeWindowHandle, }); } diff --git a/src/renderer/ssf-api.ts b/src/renderer/ssf-api.ts index c2dbddd4..ea265532 100644 --- a/src/renderer/ssf-api.ts +++ b/src/renderer/ssf-api.ts @@ -730,6 +730,16 @@ export class SSFApi { public supportedSettings(): string[] { return SUPPORTED_SETTINGS || []; } + + /** + * Get native window handle of the window where the renderer is displayed + * @returns the platform-specific handle of the window. + */ + public getNativeWindowHandle(): Promise { + return ipcRenderer.invoke(apiName.symphonyApi, { + cmd: apiCmds.getNativeWindowHandle, + }); + } } /**