session.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { fetchWithSession } from '@electron/internal/browser/api/net-fetch';
  2. import { net } from 'electron/main';
  3. const { fromPartition, fromPath, Session } = process._linkedBinding('electron_browser_session');
  4. const { isDisplayMediaSystemPickerAvailable } = process._linkedBinding('electron_browser_desktop_capturer');
  5. // Fake video window that activates the native system picker
  6. // This is used to get around the need for a screen/window
  7. // id in Chrome's desktopCapturer.
  8. let fakeVideoWindowId = -1;
  9. // See content/public/browser/desktop_media_id.h
  10. const kMacOsNativePickerId = -4;
  11. const systemPickerVideoSource = Object.create(null);
  12. Object.defineProperty(systemPickerVideoSource, 'id', {
  13. get () {
  14. return `window:${kMacOsNativePickerId}:${fakeVideoWindowId--}`;
  15. }
  16. });
  17. systemPickerVideoSource.name = '';
  18. Object.freeze(systemPickerVideoSource);
  19. Session.prototype.fetch = function (input: RequestInfo, init?: RequestInit) {
  20. return fetchWithSession(input, init, this, net.request);
  21. };
  22. Session.prototype.setDisplayMediaRequestHandler = function (handler, opts) {
  23. if (!handler) return this._setDisplayMediaRequestHandler(handler, opts);
  24. this._setDisplayMediaRequestHandler(async (req, callback) => {
  25. if (opts && opts.useSystemPicker && isDisplayMediaSystemPickerAvailable()) {
  26. return callback({ video: systemPickerVideoSource });
  27. }
  28. return handler(req, callback);
  29. }, opts);
  30. };
  31. export default {
  32. fromPartition,
  33. fromPath,
  34. get defaultSession () {
  35. return fromPartition('');
  36. }
  37. };