electron/lib/browser/api/session.ts
Keeley Hammond 309d5dade3
feat: add support for system picker in setDisplayMediaRequestHandler (#43581)
* tmp

* feat: add support for system picker in setDisplayMediaRequestHandler

* oops

* Apply suggestions from code review

Co-authored-by: Erick Zhao <erick@hotmail.ca>

* stuff

* well...

* seems legit

* chore: update patch to handle screenCapturer

* feat: modify API to use useSystemPicker

* fix: gate ScreenCaptureKitPicker to macos 15 or higher

* fix: don't use native picker with legacy media selection

* chore: code review, boolean set & docs update

* fix: add cancelCallback

* docs: clarify session & desktopCapturer docs

---------

Co-authored-by: Samuel Attard <marshallofsound@electronjs.org>
Co-authored-by: Samuel Attard <sam@electronjs.org>
Co-authored-by: Erick Zhao <erick@hotmail.ca>
2024-09-10 16:05:57 -07:00

41 lines
1.4 KiB
TypeScript

import { fetchWithSession } from '@electron/internal/browser/api/net-fetch';
import { net } from 'electron/main';
const { fromPartition, fromPath, Session } = process._linkedBinding('electron_browser_session');
const { isDisplayMediaSystemPickerAvailable } = process._linkedBinding('electron_browser_desktop_capturer');
// Fake video source that activates the native system picker
// This is used to get around the need for a screen/window
// id in Chrome's desktopCapturer.
let fakeVideoSourceId = -1;
const systemPickerVideoSource = Object.create(null);
Object.defineProperty(systemPickerVideoSource, 'id', {
get () {
return `window:${fakeVideoSourceId--}:0`;
}
});
systemPickerVideoSource.name = '';
Object.freeze(systemPickerVideoSource);
Session.prototype.fetch = function (input: RequestInfo, init?: RequestInit) {
return fetchWithSession(input, init, this, net.request);
};
Session.prototype.setDisplayMediaRequestHandler = function (handler, opts) {
if (!handler) return this._setDisplayMediaRequestHandler(handler, opts);
this._setDisplayMediaRequestHandler(async (req, callback) => {
if (opts && opts.useSystemPicker && isDisplayMediaSystemPickerAvailable()) {
return callback({ video: systemPickerVideoSource });
}
return handler(req, callback);
}, opts);
};
export default {
fromPartition,
fromPath,
get defaultSession () {
return fromPartition('');
}
};