2020-07-13 16:58:49 +00:00
|
|
|
import { WebContents } from 'electron/main';
|
2019-09-30 22:00:22 +00:00
|
|
|
|
|
|
|
const getOwnerKey = (webContents: WebContents, contextId: string) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
return `${webContents.id}-${contextId}`;
|
|
|
|
};
|
2018-07-24 07:21:38 +00:00
|
|
|
|
2020-11-24 21:11:39 +00:00
|
|
|
const electronIds = new WeakMap<Object, number>();
|
|
|
|
|
2016-02-22 06:10:25 +00:00
|
|
|
class ObjectsRegistry {
|
2019-09-30 22:00:22 +00:00
|
|
|
private nextId: number = 0
|
2016-01-15 17:57:36 +00:00
|
|
|
|
2019-09-30 22:00:22 +00:00
|
|
|
// Stores all objects by ref-counting.
|
|
|
|
// (id) => {object, count}
|
|
|
|
private storage: Record<number, { count: number, object: any }> = {}
|
2016-01-15 17:57:36 +00:00
|
|
|
|
2019-09-30 22:00:22 +00:00
|
|
|
// Stores the IDs + refCounts of objects referenced by WebContents.
|
|
|
|
// (ownerKey) => { id: refCount }
|
|
|
|
private owners: Record<string, Map<number, number>> = {}
|
2016-01-15 17:57:36 +00:00
|
|
|
|
2016-02-22 06:03:31 +00:00
|
|
|
// Register a new object and return its assigned ID. If the object is already
|
|
|
|
// registered then the already assigned ID would be returned.
|
2019-09-30 22:00:22 +00:00
|
|
|
add (webContents: WebContents, contextId: string, obj: any) {
|
2016-02-22 06:03:31 +00:00
|
|
|
// Get or assign an ID to the object.
|
2020-03-20 20:28:31 +00:00
|
|
|
const id = this.saveToStorage(obj);
|
2016-01-15 22:15:42 +00:00
|
|
|
|
2016-02-22 06:03:31 +00:00
|
|
|
// Add object to the set of referenced objects.
|
2020-03-20 20:28:31 +00:00
|
|
|
const ownerKey = getOwnerKey(webContents, contextId);
|
|
|
|
let owner = this.owners[ownerKey];
|
2016-02-22 06:54:55 +00:00
|
|
|
if (!owner) {
|
2020-03-20 20:28:31 +00:00
|
|
|
owner = this.owners[ownerKey] = new Map();
|
|
|
|
this.registerDeleteListener(webContents, contextId);
|
2016-02-22 06:54:55 +00:00
|
|
|
}
|
2016-02-22 06:03:31 +00:00
|
|
|
if (!owner.has(id)) {
|
2020-03-20 20:28:31 +00:00
|
|
|
owner.set(id, 0);
|
2016-02-22 06:03:31 +00:00
|
|
|
// Increase reference count if not referenced before.
|
2020-03-20 20:28:31 +00:00
|
|
|
this.storage[id].count++;
|
2016-01-15 22:15:42 +00:00
|
|
|
}
|
2019-04-16 20:08:11 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
owner.set(id, owner.get(id)! + 1);
|
|
|
|
return id;
|
2016-01-15 17:57:36 +00:00
|
|
|
}
|
|
|
|
|
2016-01-15 22:15:42 +00:00
|
|
|
// Get an object according to its ID.
|
2019-09-30 22:00:22 +00:00
|
|
|
get (id: number) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const pointer = this.storage[id];
|
|
|
|
if (pointer != null) return pointer.object;
|
2016-01-15 17:57:36 +00:00
|
|
|
}
|
2016-01-15 22:15:42 +00:00
|
|
|
|
|
|
|
// Dereference an object according to its ID.
|
2018-07-10 08:15:40 +00:00
|
|
|
// Note that an object may be double-freed (cleared when page is reloaded, and
|
|
|
|
// then garbage collected in old page).
|
2020-06-11 17:22:28 +00:00
|
|
|
remove (webContents: WebContents, contextId: string, id: number) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const ownerKey = getOwnerKey(webContents, contextId);
|
|
|
|
const owner = this.owners[ownerKey];
|
2019-04-16 20:08:11 +00:00
|
|
|
if (owner && owner.has(id)) {
|
2020-06-11 17:22:28 +00:00
|
|
|
const newRefCount = owner.get(id)! - 1;
|
2019-04-16 20:08:11 +00:00
|
|
|
|
|
|
|
// Only completely remove if the number of references GCed in the
|
|
|
|
// renderer is the same as the number of references we sent them
|
|
|
|
if (newRefCount <= 0) {
|
|
|
|
// Remove the reference in owner.
|
2020-03-20 20:28:31 +00:00
|
|
|
owner.delete(id);
|
2019-04-16 20:08:11 +00:00
|
|
|
// Dereference from the storage.
|
2020-03-20 20:28:31 +00:00
|
|
|
this.dereference(id);
|
2019-04-16 20:08:11 +00:00
|
|
|
} else {
|
2020-03-20 20:28:31 +00:00
|
|
|
owner.set(id, newRefCount);
|
2019-04-16 20:08:11 +00:00
|
|
|
}
|
2016-04-21 02:17:12 +00:00
|
|
|
}
|
2016-01-15 17:57:36 +00:00
|
|
|
}
|
|
|
|
|
2016-01-15 22:15:42 +00:00
|
|
|
// Clear all references to objects refrenced by the WebContents.
|
2019-09-30 22:00:22 +00:00
|
|
|
clear (webContents: WebContents, contextId: string) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const ownerKey = getOwnerKey(webContents, contextId);
|
|
|
|
const owner = this.owners[ownerKey];
|
|
|
|
if (!owner) return;
|
2016-03-24 20:15:04 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
for (const id of owner.keys()) this.dereference(id);
|
2016-03-24 20:15:04 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
delete this.owners[ownerKey];
|
2016-01-15 17:57:36 +00:00
|
|
|
}
|
|
|
|
|
2016-01-15 22:15:42 +00:00
|
|
|
// Private: Saves the object into storage and assigns an ID for it.
|
2019-09-30 22:00:22 +00:00
|
|
|
saveToStorage (object: any) {
|
2020-11-24 21:11:39 +00:00
|
|
|
let id = electronIds.get(object);
|
2016-01-15 22:15:42 +00:00
|
|
|
if (!id) {
|
2020-03-20 20:28:31 +00:00
|
|
|
id = ++this.nextId;
|
2016-01-15 22:15:42 +00:00
|
|
|
this.storage[id] = {
|
|
|
|
count: 0,
|
|
|
|
object: object
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
2020-11-24 21:11:39 +00:00
|
|
|
electronIds.set(object, id);
|
2016-01-15 22:15:42 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
return id;
|
2016-01-15 17:57:36 +00:00
|
|
|
}
|
2016-01-15 22:15:42 +00:00
|
|
|
|
|
|
|
// Private: Dereference the object from store.
|
2019-09-30 22:00:22 +00:00
|
|
|
dereference (id: number) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const pointer = this.storage[id];
|
2016-01-15 22:15:42 +00:00
|
|
|
if (pointer == null) {
|
2020-03-20 20:28:31 +00:00
|
|
|
return;
|
2016-01-15 22:15:42 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
pointer.count -= 1;
|
2016-01-15 22:15:42 +00:00
|
|
|
if (pointer.count === 0) {
|
2020-11-24 21:11:39 +00:00
|
|
|
electronIds.delete(pointer.object);
|
2020-03-20 20:28:31 +00:00
|
|
|
delete this.storage[id];
|
2016-01-15 22:15:42 +00:00
|
|
|
}
|
2016-01-15 17:57:36 +00:00
|
|
|
}
|
2016-12-01 00:59:59 +00:00
|
|
|
|
2018-07-24 07:21:38 +00:00
|
|
|
// Private: Clear the storage when renderer process is destroyed.
|
2019-09-30 22:00:22 +00:00
|
|
|
registerDeleteListener (webContents: WebContents, contextId: string) {
|
2018-09-11 18:18:10 +00:00
|
|
|
// contextId => ${processHostId}-${contextCount}
|
2020-03-20 20:28:31 +00:00
|
|
|
const processHostId = contextId.split('-')[0];
|
2019-09-30 22:00:22 +00:00
|
|
|
const listener = (_: any, deletedProcessHostId: string) => {
|
2018-09-11 18:18:10 +00:00
|
|
|
if (deletedProcessHostId &&
|
|
|
|
deletedProcessHostId.toString() === processHostId) {
|
2020-03-20 20:28:31 +00:00
|
|
|
webContents.removeListener('render-view-deleted' as any, listener);
|
|
|
|
this.clear(webContents, contextId);
|
2016-12-01 00:59:59 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
2019-10-23 04:44:21 +00:00
|
|
|
// Note that the "render-view-deleted" event may not be emitted on time when
|
|
|
|
// the renderer process get destroyed because of navigation, we rely on the
|
|
|
|
// renderer process to send "ELECTRON_BROWSER_CONTEXT_RELEASE" message to
|
|
|
|
// guard this situation.
|
2020-03-20 20:28:31 +00:00
|
|
|
webContents.on('render-view-deleted' as any, listener);
|
2016-12-01 00:59:59 +00:00
|
|
|
}
|
2016-01-15 22:15:42 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
export default new ObjectsRegistry();
|