Skip to content
Permalink
Browse files

refactor: convert some more remote stuff to typescript (#20324)

  • Loading branch information...
nornagon committed Sep 30, 2019
1 parent d3922f9 commit b7b0992768910ffdf72a33c3d656fd082f343b71
@@ -258,8 +258,8 @@ auto_filenames = {
"lib/browser/ipc-main-internal-utils.ts",
"lib/browser/ipc-main-internal.ts",
"lib/browser/navigation-controller.js",
"lib/browser/remote/objects-registry.js",
"lib/browser/remote/server.js",
"lib/browser/remote/objects-registry.ts",
"lib/browser/remote/server.ts",
"lib/browser/rpc-server.js",
"lib/browser/utils.ts",
"lib/common/api/clipboard.js",
@@ -1,27 +1,27 @@
'use strict'

import { WebContents } from 'electron'

const v8Util = process.electronBinding('v8_util')

const getOwnerKey = (webContents, contextId) => {
const getOwnerKey = (webContents: WebContents, contextId: string) => {
return `${webContents.id}-${contextId}`
}

class ObjectsRegistry {
constructor () {
this.nextId = 0
private nextId: number = 0

// Stores all objects by ref-counting.
// (id) => {object, count}
this.storage = {}
// Stores all objects by ref-counting.
// (id) => {object, count}
private storage: Record<number, { count: number, object: any }> = {}

// Stores the IDs + refCounts of objects referenced by WebContents.
// (ownerKey) => { id: refCount }
this.owners = {}
}
// Stores the IDs + refCounts of objects referenced by WebContents.
// (ownerKey) => { id: refCount }
private owners: Record<string, Map<number, number>> = {}

// Register a new object and return its assigned ID. If the object is already
// registered then the already assigned ID would be returned.
add (webContents, contextId, obj) {
add (webContents: WebContents, contextId: string, obj: any) {
// Get or assign an ID to the object.
const id = this.saveToStorage(obj)

@@ -38,12 +38,12 @@ class ObjectsRegistry {
this.storage[id].count++
}

owner.set(id, owner.get(id) + 1)
owner.set(id, owner.get(id)! + 1)
return id
}

// Get an object according to its ID.
get (id) {
get (id: number) {
const pointer = this.storage[id]
if (pointer != null) return pointer.object
}
@@ -59,11 +59,11 @@ class ObjectsRegistry {
// object
// For more details on why we do renderer side ref counting see
// https://github.com/electron/electron/pull/17464
remove (webContents, contextId, id, rendererSideRefCount) {
remove (webContents: WebContents, contextId: string, id: number, rendererSideRefCount: number) {
const ownerKey = getOwnerKey(webContents, contextId)
const owner = this.owners[ownerKey]
if (owner && owner.has(id)) {
const newRefCount = owner.get(id) - rendererSideRefCount
const newRefCount = owner.get(id)! - rendererSideRefCount

// Only completely remove if the number of references GCed in the
// renderer is the same as the number of references we sent them
@@ -79,7 +79,7 @@ class ObjectsRegistry {
}

// Clear all references to objects refrenced by the WebContents.
clear (webContents, contextId) {
clear (webContents: WebContents, contextId: string) {
const ownerKey = getOwnerKey(webContents, contextId)
const owner = this.owners[ownerKey]
if (!owner) return
@@ -90,8 +90,8 @@ class ObjectsRegistry {
}

// Private: Saves the object into storage and assigns an ID for it.
saveToStorage (object) {
let id = v8Util.getHiddenValue(object, 'atomId')
saveToStorage (object: any) {
let id: number = v8Util.getHiddenValue(object, 'atomId')
if (!id) {
id = ++this.nextId
this.storage[id] = {
@@ -104,7 +104,7 @@ class ObjectsRegistry {
}

// Private: Dereference the object from store.
dereference (id) {
dereference (id: number) {
const pointer = this.storage[id]
if (pointer == null) {
return
@@ -117,18 +117,18 @@ class ObjectsRegistry {
}

// Private: Clear the storage when renderer process is destroyed.
registerDeleteListener (webContents, contextId) {
registerDeleteListener (webContents: WebContents, contextId: string) {
// contextId => ${processHostId}-${contextCount}
const processHostId = contextId.split('-')[0]
const listener = (event, deletedProcessHostId) => {
const listener = (_: any, deletedProcessHostId: string) => {
if (deletedProcessHostId &&
deletedProcessHostId.toString() === processHostId) {
webContents.removeListener('render-view-deleted', listener)
webContents.removeListener('render-view-deleted' as any, listener)
this.clear(webContents, contextId)
}
}
webContents.on('render-view-deleted', listener)
webContents.on('render-view-deleted' as any, listener)
}
}

module.exports = new ObjectsRegistry()
export default new ObjectsRegistry()

0 comments on commit b7b0992

Please sign in to comment.
You can’t perform that action at this time.