objects-registry.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. 'use strict';
  2. const v8Util = process.electronBinding('v8_util');
  3. const getOwnerKey = (webContents, contextId) => {
  4. return `${webContents.id}-${contextId}`;
  5. };
  6. class ObjectsRegistry {
  7. constructor () {
  8. this.nextId = 0;
  9. // Stores all objects by ref-counting.
  10. // (id) => {object, count}
  11. this.storage = {};
  12. // Stores the IDs + refCounts of objects referenced by WebContents.
  13. // (ownerKey) => { id: refCount }
  14. this.owners = {};
  15. }
  16. // Register a new object and return its assigned ID. If the object is already
  17. // registered then the already assigned ID would be returned.
  18. add (webContents, contextId, obj) {
  19. // Get or assign an ID to the object.
  20. const id = this.saveToStorage(obj);
  21. // Add object to the set of referenced objects.
  22. const ownerKey = getOwnerKey(webContents, contextId);
  23. let owner = this.owners[ownerKey];
  24. if (!owner) {
  25. owner = this.owners[ownerKey] = new Map();
  26. this.registerDeleteListener(webContents, contextId);
  27. }
  28. if (!owner.has(id)) {
  29. owner.set(id, 0);
  30. // Increase reference count if not referenced before.
  31. this.storage[id].count++;
  32. }
  33. owner.set(id, owner.get(id) + 1);
  34. return id;
  35. }
  36. // Get an object according to its ID.
  37. get (id) {
  38. const pointer = this.storage[id];
  39. if (pointer != null) return pointer.object;
  40. }
  41. // Dereference an object according to its ID.
  42. // Note that an object may be double-freed (cleared when page is reloaded, and
  43. // then garbage collected in old page).
  44. // rendererSideRefCount is the ref count that the renderer process reported
  45. // at time of GC if this is different to the number of references we sent to
  46. // the given owner then a GC occurred between a ref being sent and the value
  47. // being pulled out of the weak map.
  48. // In this case we decrement out ref count and do not delete the stored
  49. // object
  50. // For more details on why we do renderer side ref counting see
  51. // https://github.com/electron/electron/pull/17464
  52. remove (webContents, contextId, id, rendererSideRefCount) {
  53. const ownerKey = getOwnerKey(webContents, contextId);
  54. const owner = this.owners[ownerKey];
  55. if (owner && owner.has(id)) {
  56. const newRefCount = owner.get(id) - rendererSideRefCount;
  57. // Only completely remove if the number of references GCed in the
  58. // renderer is the same as the number of references we sent them
  59. if (newRefCount <= 0) {
  60. // Remove the reference in owner.
  61. owner.delete(id);
  62. // Dereference from the storage.
  63. this.dereference(id);
  64. } else {
  65. owner.set(id, newRefCount);
  66. }
  67. }
  68. }
  69. // Clear all references to objects refrenced by the WebContents.
  70. clear (webContents, contextId) {
  71. const ownerKey = getOwnerKey(webContents, contextId);
  72. const owner = this.owners[ownerKey];
  73. if (!owner) return;
  74. for (const id of owner.keys()) this.dereference(id);
  75. delete this.owners[ownerKey];
  76. }
  77. // Private: Saves the object into storage and assigns an ID for it.
  78. saveToStorage (object) {
  79. let id = v8Util.getHiddenValue(object, 'atomId');
  80. if (!id) {
  81. id = ++this.nextId;
  82. this.storage[id] = {
  83. count: 0,
  84. object: object
  85. };
  86. v8Util.setHiddenValue(object, 'atomId', id);
  87. }
  88. return id;
  89. }
  90. // Private: Dereference the object from store.
  91. dereference (id) {
  92. const pointer = this.storage[id];
  93. if (pointer == null) {
  94. return;
  95. }
  96. pointer.count -= 1;
  97. if (pointer.count === 0) {
  98. v8Util.deleteHiddenValue(pointer.object, 'atomId');
  99. delete this.storage[id];
  100. }
  101. }
  102. // Private: Clear the storage when renderer process is destroyed.
  103. registerDeleteListener (webContents, contextId) {
  104. // contextId => ${processHostId}-${contextCount}
  105. const processHostId = contextId.split('-')[0];
  106. const listener = (event, deletedProcessHostId) => {
  107. if (deletedProcessHostId &&
  108. deletedProcessHostId.toString() === processHostId) {
  109. webContents.removeListener('render-view-deleted', listener);
  110. this.clear(webContents, contextId);
  111. }
  112. };
  113. // Note that the "render-view-deleted" event may not be emitted on time when
  114. // the renderer process get destroyed because of navigation, we rely on the
  115. // renderer process to send "ELECTRON_BROWSER_CONTEXT_RELEASE" message to
  116. // guard this situation.
  117. webContents.on('render-view-deleted', listener);
  118. }
  119. }
  120. module.exports = new ObjectsRegistry();