session.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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);
  23. if (this.extensions) {
  24. const rerouteExtensionEvent = (eventName: string) => {
  25. const warn = deprecate.warnOnce(`${eventName} event`, `session.extensions ${eventName} event`);
  26. this.extensions.on(eventName as any, (...args: any[]) => {
  27. if (this.listenerCount(eventName) !== 0) {
  28. warn();
  29. this.emit(eventName, ...args);
  30. }
  31. });
  32. };
  33. rerouteExtensionEvent('extension-loaded');
  34. rerouteExtensionEvent('extension-unloaded');
  35. rerouteExtensionEvent('extension-ready');
  36. }
  37. };
  38. Session.prototype.fetch = function (input: RequestInfo, init?: RequestInit) {
  39. return fetchWithSession(input, init, this, net.request);
  40. };
  41. Session.prototype.setDisplayMediaRequestHandler = function (handler, opts) {
  42. if (!handler) return this._setDisplayMediaRequestHandler(handler, opts);
  43. this._setDisplayMediaRequestHandler(async (req, callback) => {
  44. if (opts && opts.useSystemPicker && isDisplayMediaSystemPickerAvailable()) {
  45. return callback({ video: systemPickerVideoSource });
  46. }
  47. return handler(req, callback);
  48. }, opts);
  49. };
  50. const getPreloadsDeprecated = deprecate.warnOnce('session.getPreloads', 'session.getPreloadScripts');
  51. Session.prototype.getPreloads = function () {
  52. getPreloadsDeprecated();
  53. return this.getPreloadScripts()
  54. .filter((script) => script.type === 'frame')
  55. .map((script) => script.filePath);
  56. };
  57. const setPreloadsDeprecated = deprecate.warnOnce('session.setPreloads', 'session.registerPreloadScript');
  58. Session.prototype.setPreloads = function (preloads) {
  59. setPreloadsDeprecated();
  60. this.getPreloadScripts()
  61. .filter((script) => script.type === 'frame')
  62. .forEach((script) => {
  63. this.unregisterPreloadScript(script.id);
  64. });
  65. preloads.map(filePath => ({
  66. type: 'frame',
  67. filePath,
  68. _deprecated: true
  69. }) as Electron.PreloadScriptRegistration).forEach(script => {
  70. this.registerPreloadScript(script);
  71. });
  72. };
  73. Session.prototype.getAllExtensions = deprecate.moveAPI(
  74. function (this: Electron.Session) {
  75. return this.extensions.getAllExtensions();
  76. },
  77. 'session.getAllExtensions',
  78. 'session.extensions.getAllExtensions'
  79. );
  80. Session.prototype.getExtension = deprecate.moveAPI(
  81. function (this: Electron.Session, extensionId) {
  82. return this.extensions.getExtension(extensionId);
  83. },
  84. 'session.getExtension',
  85. 'session.extensions.getExtension'
  86. );
  87. Session.prototype.loadExtension = deprecate.moveAPI(
  88. function (this: Electron.Session, path, options) {
  89. return this.extensions.loadExtension(path, options);
  90. },
  91. 'session.loadExtension',
  92. 'session.extensions.loadExtension'
  93. );
  94. Session.prototype.removeExtension = deprecate.moveAPI(
  95. function (this: Electron.Session, extensionId) {
  96. return this.extensions.removeExtension(extensionId);
  97. },
  98. 'session.removeExtension',
  99. 'session.extensions.removeExtension'
  100. );
  101. export default {
  102. fromPartition,
  103. fromPath,
  104. get defaultSession () {
  105. return fromPartition('');
  106. }
  107. };