Skip to content

Commit

Permalink
refactor: use WeakRef on renderer side of remote (#24037)
Browse files Browse the repository at this point in the history
  • Loading branch information
nornagon committed Jun 12, 2020
1 parent 178e46c commit 379bb17
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 117 deletions.
2 changes: 0 additions & 2 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -595,8 +595,6 @@ source_set("electron_lib") {
sources += [
"shell/common/api/remote/remote_callback_freer.cc",
"shell/common/api/remote/remote_callback_freer.h",
"shell/common/api/remote/remote_object_freer.cc",
"shell/common/api/remote/remote_object_freer.h",
]
}

Expand Down
31 changes: 26 additions & 5 deletions lib/renderer/api/remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,28 @@ const { hasSwitch } = process.electronBinding('command_line');
const { NativeImage } = process.electronBinding('native_image');

const callbacksRegistry = new CallbacksRegistry();
const remoteObjectCache = v8Util.createIDWeakMap();
const remoteObjectCache = new Map();
const finalizationRegistry = new (window as any).FinalizationRegistry((id: number) => {
const ref = remoteObjectCache.get(id);
if (ref !== undefined && ref.deref() === undefined) {
remoteObjectCache.delete(id);
ipcRendererInternal.send('ELECTRON_BROWSER_DEREFERENCE', contextId, id, 0);
}
});

function getCachedRemoteObject (id: number) {
const ref = remoteObjectCache.get(id);
if (ref !== undefined) {
const deref = ref.deref();
if (deref !== undefined) return deref;
}
}
function setCachedRemoteObject (id: number, value: any) {
const wr = new (window as any).WeakRef(value);
remoteObjectCache.set(id, wr);
finalizationRegistry.register(value, id);
return value;
}

// An unique ID that can represent current context.
const contextId = v8Util.getHiddenValue<string>(global, 'contextId');
Expand Down Expand Up @@ -234,8 +255,9 @@ function metaToValue (meta: MetaType): any {
if (meta.value.type === 'error') { throw metaToError(meta.value); } else { throw new Error(`Unexpected value type in exception: ${meta.value.type}`); }
} else {
let ret;
if ('id' in meta && remoteObjectCache.has(meta.id)) {
return remoteObjectCache.get(meta.id);
if ('id' in meta) {
const cached = getCachedRemoteObject(meta.id);
if (cached !== undefined) { return cached; }
}

// A shadow class to represent the remote function object.
Expand All @@ -262,9 +284,8 @@ function metaToValue (meta: MetaType): any {
}

// Track delegate obj's lifetime & tell browser to clean up when object is GCed.
v8Util.setRemoteObjectFreer(ret, contextId, meta.id);
v8Util.setHiddenValue(ret, 'electronId', meta.id);
remoteObjectCache.set(meta.id, ret);
setCachedRemoteObject(meta.id, ret);
return ret;
}
}
Expand Down
2 changes: 0 additions & 2 deletions shell/common/api/electron_api_v8_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

#if BUILDFLAG(ENABLE_REMOTE_MODULE)
#include "shell/common/api/remote/remote_callback_freer.h"
#include "shell/common/api/remote/remote_object_freer.h"
#endif

namespace std {
Expand Down Expand Up @@ -148,7 +147,6 @@ void Initialize(v8::Local<v8::Object> exports,
#if BUILDFLAG(ENABLE_REMOTE_MODULE)
dict.SetMethod("setRemoteCallbackFreer",
&electron::RemoteCallbackFreer::BindTo);
dict.SetMethod("setRemoteObjectFreer", &electron::RemoteObjectFreer::BindTo);
dict.SetMethod(
"createDoubleIDWeakMap",
&electron::api::KeyWeakMap<std::pair<std::string, int32_t>>::Create);
Expand Down
66 changes: 0 additions & 66 deletions shell/common/api/remote/remote_object_freer.cc

This file was deleted.

41 changes: 0 additions & 41 deletions shell/common/api/remote/remote_object_freer.h

This file was deleted.

1 change: 0 additions & 1 deletion typings/internal-ambient.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ declare namespace NodeJS {
getWeaklyTrackedValues(): any[];
addRemoteObjectRef(contextId: string, id: number): void;
setRemoteCallbackFreer(fn: Function, contextId: string, id: number, sender: any): void
setRemoteObjectFreer(object: any, contextId: string, id: number): void
}

type DataPipe = {
Expand Down

0 comments on commit 379bb17

Please sign in to comment.