From e5e9186d613277c855d913fa21d0f03496c2ff87 Mon Sep 17 00:00:00 2001 From: Milan Burda Date: Thu, 9 Feb 2023 11:38:39 +0100 Subject: [PATCH] docs: add missing clipboard-sanitized-write permission value to setPermissionRequestHandler() (#37173) Co-authored-by: Milan Burda --- docs/api/session.md | 1 + spec/chromium-spec.ts | 56 ++++++++++++++++++++++++++++++++++++++----- 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/docs/api/session.md b/docs/api/session.md index 199202dd8a..ad0cb87ac3 100644 --- a/docs/api/session.md +++ b/docs/api/session.md @@ -784,6 +784,7 @@ win.webContents.session.setCertificateVerifyProc((request, callback) => { * `webContents` [WebContents](web-contents.md) - WebContents requesting the permission. Please note that if the request comes from a subframe you should use `requestingUrl` to check the request origin. * `permission` string - The type of requested permission. * `clipboard-read` - Request access to read from the clipboard. + * `clipboard-sanitized-write` - Request access to write to the clipboard. * `media` - Request access to media devices such as camera, microphone and speakers. * `display-capture` - Request access to capture the screen. * `mediaKeySystem` - Request access to DRM protected content. diff --git a/spec/chromium-spec.ts b/spec/chromium-spec.ts index 45c28350cd..14a9873be0 100644 --- a/spec/chromium-spec.ts +++ b/spec/chromium-spec.ts @@ -2547,14 +2547,10 @@ describe('window.getScreenDetails', () => { }); }); -describe('navigator.clipboard', () => { +describe('navigator.clipboard.read', () => { let w: BrowserWindow; before(async () => { - w = new BrowserWindow({ - webPreferences: { - enableBlinkFeatures: 'Serial' - } - }); + w = new BrowserWindow(); await w.loadFile(path.join(fixturesPath, 'pages', 'blank.html')); }); @@ -2599,6 +2595,54 @@ describe('navigator.clipboard', () => { }); }); +describe('navigator.clipboard.write', () => { + let w: BrowserWindow; + before(async () => { + w = new BrowserWindow(); + await w.loadFile(path.join(fixturesPath, 'pages', 'blank.html')); + }); + + const writeClipboard: any = () => { + return w.webContents.executeJavaScript(` + navigator.clipboard.writeText('Hello World!').catch(err => err.message); + `, true); + }; + + after(closeAllWindows); + afterEach(() => { + session.defaultSession.setPermissionRequestHandler(null); + }); + + it('returns clipboard contents when a PermissionRequestHandler is not defined', async () => { + const clipboard = await writeClipboard(); + expect(clipboard).to.not.equal('Write permission denied.'); + }); + + it('returns an error when permission denied', async () => { + session.defaultSession.setPermissionRequestHandler((wc, permission, callback) => { + if (permission === 'clipboard-sanitized-write') { + callback(false); + } else { + callback(true); + } + }); + const clipboard = await writeClipboard(); + expect(clipboard).to.equal('Write permission denied.'); + }); + + it('returns clipboard contents when permission is granted', async () => { + session.defaultSession.setPermissionRequestHandler((wc, permission, callback) => { + if (permission === 'clipboard-sanitized-write') { + callback(true); + } else { + callback(false); + } + }); + const clipboard = await writeClipboard(); + expect(clipboard).to.not.equal('Write permission denied.'); + }); +}); + ifdescribe((process.platform !== 'linux' || app.isUnityRunning()))('navigator.setAppBadge/clearAppBadge', () => { let w: BrowserWindow;