aboutsummaryrefslogtreecommitdiffstats
path: root/src/webview/screenshare.js
diff options
context:
space:
mode:
authorLibravatar Kristóf Marussy <kristof@marussy.com>2021-07-24 02:23:48 +0200
committerLibravatar GitHub <noreply@github.com>2021-07-24 02:23:48 +0200
commit9c3c441941ad5060ec2db89b805a958a914547f3 (patch)
treea4224d7bd2576797b14a2ffa1d25ba6e167351ae /src/webview/screenshare.js
parentUpdate submodules, browserslist data updates and linter fixes [skip ci] (diff)
downloadferdium-app-9c3c441941ad5060ec2db89b805a958a914547f3.tar.gz
ferdium-app-9c3c441941ad5060ec2db89b805a958a914547f3.tar.zst
ferdium-app-9c3c441941ad5060ec2db89b805a958a914547f3.zip
Recipe context isolation (#1456)
* Enable service contextIsolation * Enable contextIsolation on the service webviews * Expose a new API window.ferdi in the service main world to allow calling back into the service isolated world * Expose a new IPC message inject-js-unsafe from the service isolated world to execute Javascript in the service main world (i.e., run code without context isolation). While the name contains the "unsafe" suffix to show the lack of context isolation, this should mostly be safe, as no nodejs APIs are available in the injected code. * Refactor the Notifications shim into a part in the isolated world that handles displaying and modifying notifications, and a shim in the main world for the Notifications class. The two communicate via the window.ferdi endpoint and a Promise object can be used to detect notification clicks. * Refactor the screen sharing shim into a part in the isolated world that enumerated shareable screens and windows and a shim in the main world that displays the media selector and completes the media selection promise. * Expose the injectJSUnsafe API to recipes to inject javascript code into the main world without context isolation. * Expose setBadge to the main world The window.ferdi.setBadge API can be used to update the service badge from injected unsafe Javascript * Safer script injection into the service main world Make sure that we don't try to serialize stray objects back from the main world to the isolated world by always surrounding the script to be executed by an anonymous function. * Always read recipe assets as utf8 * Remove window.log from recipes We didn't use it anywhere and its behavior was confusing in production mode. * Inject multiple unsafe scripts at the same time * Find in page without remote module Remove the @electron/remote dependency from the find in page (Ctrl+F) functionality. The remote webContents is replaced with Electron IPC. Synchronous IPC messages are handled in the main Electron process, because the renderer process cannot reply to IPC messages synchronously. * Update to latest contextIsolation recipes * Fixing issue with missing 'fs' functions. Co-authored-by: Vijay A <avijayr@protonmail.com>
Diffstat (limited to 'src/webview/screenshare.js')
-rw-r--r--src/webview/screenshare.js81
1 files changed, 35 insertions, 46 deletions
diff --git a/src/webview/screenshare.js b/src/webview/screenshare.js
index 84d2e1e95..ab548a625 100644
--- a/src/webview/screenshare.js
+++ b/src/webview/screenshare.js
@@ -2,6 +2,27 @@ import { desktopCapturer } from 'electron';
2 2
3const CANCEL_ID = 'desktop-capturer-selection__cancel'; 3const CANCEL_ID = 'desktop-capturer-selection__cancel';
4 4
5export async function getDisplayMediaSelector() {
6 const sources = await desktopCapturer.getSources({ types: ['screen', 'window'] });
7 return `<div class="desktop-capturer-selection__scroller">
8 <ul class="desktop-capturer-selection__list">
9 ${sources.map(({ id, name, thumbnail }) => `
10 <li class="desktop-capturer-selection__item">
11 <button class="desktop-capturer-selection__btn" data-id="${id}" title="${name}">
12 <img class="desktop-capturer-selection__thumbnail" src="${thumbnail.toDataURL()}" />
13 <span class="desktop-capturer-selection__name">${name}</span>
14 </button>
15 </li>
16 `).join('')}
17 <li class="desktop-capturer-selection__item">
18 <button class="desktop-capturer-selection__btn" data-id="${CANCEL_ID}" title="Cancel">
19 <span class="desktop-capturer-selection__name desktop-capturer-selection__name--cancel">Cancel</span>
20 </button>
21 </li>
22 </ul>
23</div>`;
24}
25
5export const screenShareCss = ` 26export const screenShareCss = `
6.desktop-capturer-selection { 27.desktop-capturer-selection {
7 position: fixed; 28 position: fixed;
@@ -72,38 +93,12 @@ export const screenShareCss = `
72} 93}
73`; 94`;
74 95
75// Patch getDisplayMedia for screen sharing 96export const screenShareJs = `
76window.navigator.mediaDevices.getDisplayMedia = () => async (resolve, reject) => { 97window.navigator.mediaDevices.getDisplayMedia = () => new Promise(async (resolve, reject) => {
77 try { 98 try {
78 const sources = await desktopCapturer.getSources({
79 types: ['screen', 'window'],
80 });
81
82 const selectionElem = document.createElement('div'); 99 const selectionElem = document.createElement('div');
83 selectionElem.classList = 'desktop-capturer-selection'; 100 selectionElem.classList = ['desktop-capturer-selection'];
84 selectionElem.innerHTML = ` 101 selectionElem.innerHTML = await window.ferdi.getDisplayMediaSelector();
85 <div class="desktop-capturer-selection__scroller">
86 <ul class="desktop-capturer-selection__list">
87 ${sources
88 .map(
89 ({ id, name, thumbnail }) => `
90 <li class="desktop-capturer-selection__item">
91 <button class="desktop-capturer-selection__btn" data-id="${id}" title="${name}">
92 <img class="desktop-capturer-selection__thumbnail" src="${thumbnail.toDataURL()}" />
93 <span class="desktop-capturer-selection__name">${name}</span>
94 </button>
95 </li>
96 `,
97 )
98 .join('')}
99 <li class="desktop-capturer-selection__item">
100 <button class="desktop-capturer-selection__btn" data-id="${CANCEL_ID}" title="Cancel">
101 <span class="desktop-capturer-selection__name desktop-capturer-selection__name--cancel">Cancel</span>
102 </button>
103 </li>
104 </ul>
105 </div>
106 `;
107 document.body.appendChild(selectionElem); 102 document.body.appendChild(selectionElem);
108 103
109 document 104 document
@@ -112,25 +107,18 @@ window.navigator.mediaDevices.getDisplayMedia = () => async (resolve, reject) =>
112 button.addEventListener('click', async () => { 107 button.addEventListener('click', async () => {
113 try { 108 try {
114 const id = button.getAttribute('data-id'); 109 const id = button.getAttribute('data-id');
115 if (id === CANCEL_ID) { 110 if (id === '${CANCEL_ID}') {
116 reject(new Error('Cancelled by user')); 111 reject(new Error('Cancelled by user'));
117 } else { 112 } else {
118 const mediaSource = sources.find((source) => source.id === id); 113 const stream = await window.navigator.mediaDevices.getUserMedia({
119 if (!mediaSource) { 114 audio: false,
120 throw new Error(`Source with id ${id} does not exist`); 115 video: {
121 } 116 mandatory: {
122 117 chromeMediaSource: 'desktop',
123 const stream = await window.navigator.mediaDevices.getUserMedia( 118 chromeMediaSourceId: id,
124 {
125 audio: false,
126 video: {
127 mandatory: {
128 chromeMediaSource: 'desktop',
129 chromeMediaSourceId: mediaSource.id,
130 },
131 }, 119 },
132 }, 120 },
133 ); 121 });
134 resolve(stream); 122 resolve(stream);
135 } 123 }
136 } catch (err) { 124 } catch (err) {
@@ -143,4 +131,5 @@ window.navigator.mediaDevices.getDisplayMedia = () => async (resolve, reject) =>
143 } catch (err) { 131 } catch (err) {
144 reject(err); 132 reject(err);
145 } 133 }
146}; 134});
135`;