session.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { fetchWithSession } from '@electron/internal/browser/api/net-fetch';
  2. import { addIpcDispatchListeners } from '@electron/internal/browser/ipc-dispatch';
  3. import * as deprecate from '@electron/internal/common/deprecate';
  4. import { net } from 'electron/main';
  5. const { fromPartition, fromPath, Session } = process._linkedBinding('electron_browser_session');
  6. const { isDisplayMediaSystemPickerAvailable } = process._linkedBinding('electron_browser_desktop_capturer');
  7. // Fake video window that activates the native system picker
  8. // This is used to get around the need for a screen/window
  9. // id in Chrome's desktopCapturer.
  10. let fakeVideoWindowId = -1;
  11. // See content/public/browser/desktop_media_id.h
  12. const kMacOsNativePickerId = -4;
  13. const systemPickerVideoSource = Object.create(null);
  14. Object.defineProperty(systemPickerVideoSource, 'id', {
  15. get () {
  16. return `window:${kMacOsNativePickerId}:${fakeVideoWindowId--}`;
  17. }
  18. });
  19. systemPickerVideoSource.name = '';
  20. Object.freeze(systemPickerVideoSource);
  21. Session.prototype._init = function () {
  22. addIpcDispatchListeners(this, this.serviceWorkers);
  23. };
  24. Session.prototype.fetch = function (input: RequestInfo, init?: RequestInit) {
  25. return fetchWithSession(input, init, this, net.request);
  26. };
  27. Session.prototype.setDisplayMediaRequestHandler = function (handler, opts) {
  28. if (!handler) return this._setDisplayMediaRequestHandler(handler, opts);
  29. this._setDisplayMediaRequestHandler(async (req, callback) => {
  30. if (opts && opts.useSystemPicker && isDisplayMediaSystemPickerAvailable()) {
  31. return callback({ video: systemPickerVideoSource });
  32. }
  33. return handler(req, callback);
  34. }, opts);
  35. };
  36. const getPreloadsDeprecated = deprecate.warnOnce('session.getPreloads', 'session.getPreloadScripts');
  37. Session.prototype.getPreloads = function () {
  38. getPreloadsDeprecated();
  39. return this.getPreloadScripts()
  40. .filter((script) => script.type === 'frame')
  41. .map((script) => script.filePath);
  42. };
  43. const setPreloadsDeprecated = deprecate.warnOnce('session.setPreloads', 'session.registerPreloadScript');
  44. Session.prototype.setPreloads = function (preloads) {
  45. setPreloadsDeprecated();
  46. this.getPreloadScripts()
  47. .filter((script) => script.type === 'frame')
  48. .forEach((script) => {
  49. this.unregisterPreloadScript(script.id);
  50. });
  51. preloads.map(filePath => ({
  52. type: 'frame',
  53. filePath,
  54. _deprecated: true
  55. }) as Electron.PreloadScriptRegistration).forEach(script => {
  56. this.registerPreloadScript(script);
  57. });
  58. };
  59. export default {
  60. fromPartition,
  61. fromPath,
  62. get defaultSession () {
  63. return fromPartition('');
  64. }
  65. };