refactor: use native WeakRef instead of v8util.weaklyTrackValue() (#31153)
This commit is contained in:
parent
a5f1fbdc54
commit
80577a4f08
3 changed files with 6 additions and 35 deletions
|
@ -106,28 +106,6 @@ bool IsSameOrigin(const GURL& l, const GURL& r) {
|
||||||
return url::Origin::Create(l).IsSameOriginWith(url::Origin::Create(r));
|
return url::Origin::Create(l).IsSameOriginWith(url::Origin::Create(r));
|
||||||
}
|
}
|
||||||
|
|
||||||
#if DCHECK_IS_ON()
|
|
||||||
std::vector<v8::Global<v8::Value>> weakly_tracked_values;
|
|
||||||
|
|
||||||
void WeaklyTrackValue(v8::Isolate* isolate, v8::Local<v8::Value> value) {
|
|
||||||
v8::Global<v8::Value> global_value(isolate, value);
|
|
||||||
global_value.SetWeak();
|
|
||||||
weakly_tracked_values.push_back(std::move(global_value));
|
|
||||||
}
|
|
||||||
|
|
||||||
void ClearWeaklyTrackedValues() {
|
|
||||||
weakly_tracked_values.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<v8::Local<v8::Value>> GetWeaklyTrackedValues(v8::Isolate* isolate) {
|
|
||||||
std::vector<v8::Local<v8::Value>> locals;
|
|
||||||
for (const auto& value : weakly_tracked_values) {
|
|
||||||
if (!value.IsEmpty())
|
|
||||||
locals.push_back(value.Get(isolate));
|
|
||||||
}
|
|
||||||
return locals;
|
|
||||||
}
|
|
||||||
|
|
||||||
// This causes a fatal error by creating a circular extension dependency.
|
// This causes a fatal error by creating a circular extension dependency.
|
||||||
void TriggerFatalErrorForTesting(v8::Isolate* isolate) {
|
void TriggerFatalErrorForTesting(v8::Isolate* isolate) {
|
||||||
static const char* aDeps[] = {"B"};
|
static const char* aDeps[] = {"B"};
|
||||||
|
@ -141,7 +119,6 @@ void TriggerFatalErrorForTesting(v8::Isolate* isolate) {
|
||||||
void RunUntilIdle() {
|
void RunUntilIdle() {
|
||||||
base::RunLoop().RunUntilIdle();
|
base::RunLoop().RunUntilIdle();
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
void Initialize(v8::Local<v8::Object> exports,
|
void Initialize(v8::Local<v8::Object> exports,
|
||||||
v8::Local<v8::Value> unused,
|
v8::Local<v8::Value> unused,
|
||||||
|
@ -157,9 +134,6 @@ void Initialize(v8::Local<v8::Object> exports,
|
||||||
&RequestGarbageCollectionForTesting);
|
&RequestGarbageCollectionForTesting);
|
||||||
dict.SetMethod("isSameOrigin", &IsSameOrigin);
|
dict.SetMethod("isSameOrigin", &IsSameOrigin);
|
||||||
dict.SetMethod("triggerFatalErrorForTesting", &TriggerFatalErrorForTesting);
|
dict.SetMethod("triggerFatalErrorForTesting", &TriggerFatalErrorForTesting);
|
||||||
dict.SetMethod("getWeaklyTrackedValues", &GetWeaklyTrackedValues);
|
|
||||||
dict.SetMethod("clearWeaklyTrackedValues", &ClearWeaklyTrackedValues);
|
|
||||||
dict.SetMethod("weaklyTrackValue", &WeaklyTrackValue);
|
|
||||||
dict.SetMethod("runUntilIdle", &RunUntilIdle);
|
dict.SetMethod("runUntilIdle", &RunUntilIdle);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -615,11 +615,11 @@ describe('contextBridge', () => {
|
||||||
if (!useSandbox) {
|
if (!useSandbox) {
|
||||||
it('should release the global hold on methods sent across contexts', async () => {
|
it('should release the global hold on methods sent across contexts', async () => {
|
||||||
await makeBindingWindow(() => {
|
await makeBindingWindow(() => {
|
||||||
require('electron').ipcRenderer.on('get-gc-info', (e: Electron.IpcRendererEvent) => e.sender.send('gc-info', { trackedValues: process._linkedBinding('electron_common_v8_util').getWeaklyTrackedValues().length }));
|
const trackedValues: WeakRef<object>[] = [];
|
||||||
const { weaklyTrackValue } = process._linkedBinding('electron_common_v8_util');
|
require('electron').ipcRenderer.on('get-gc-info', e => e.sender.send('gc-info', { trackedValues: trackedValues.filter(value => value.deref()).length }));
|
||||||
contextBridge.exposeInMainWorld('example', {
|
contextBridge.exposeInMainWorld('example', {
|
||||||
getFunction: () => () => 123,
|
getFunction: () => () => 123,
|
||||||
track: weaklyTrackValue
|
track: (value: object) => { trackedValues.push(new WeakRef(value)); }
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
await callWithBindings(async (root: any) => {
|
await callWithBindings(async (root: any) => {
|
||||||
|
@ -643,11 +643,11 @@ describe('contextBridge', () => {
|
||||||
if (useSandbox) {
|
if (useSandbox) {
|
||||||
it('should not leak the global hold on methods sent across contexts when reloading a sandboxed renderer', async () => {
|
it('should not leak the global hold on methods sent across contexts when reloading a sandboxed renderer', async () => {
|
||||||
await makeBindingWindow(() => {
|
await makeBindingWindow(() => {
|
||||||
require('electron').ipcRenderer.on('get-gc-info', e => e.sender.send('gc-info', { trackedValues: process._linkedBinding('electron_common_v8_util').getWeaklyTrackedValues().length }));
|
const trackedValues: WeakRef<object>[] = [];
|
||||||
const { weaklyTrackValue } = process._linkedBinding('electron_common_v8_util');
|
require('electron').ipcRenderer.on('get-gc-info', e => e.sender.send('gc-info', { trackedValues: trackedValues.filter(value => value.deref()).length }));
|
||||||
contextBridge.exposeInMainWorld('example', {
|
contextBridge.exposeInMainWorld('example', {
|
||||||
getFunction: () => () => 123,
|
getFunction: () => () => 123,
|
||||||
track: weaklyTrackValue
|
track: (value: object) => { trackedValues.push(new WeakRef(value)); }
|
||||||
});
|
});
|
||||||
require('electron').ipcRenderer.send('window-ready-for-tasking');
|
require('electron').ipcRenderer.send('window-ready-for-tasking');
|
||||||
});
|
});
|
||||||
|
|
3
typings/internal-ambient.d.ts
vendored
3
typings/internal-ambient.d.ts
vendored
|
@ -44,9 +44,6 @@ declare namespace NodeJS {
|
||||||
setHiddenValue<T>(obj: any, key: string, value: T): void;
|
setHiddenValue<T>(obj: any, key: string, value: T): void;
|
||||||
deleteHiddenValue(obj: any, key: string): void;
|
deleteHiddenValue(obj: any, key: string): void;
|
||||||
requestGarbageCollectionForTesting(): void;
|
requestGarbageCollectionForTesting(): void;
|
||||||
weaklyTrackValue(value: any): void;
|
|
||||||
clearWeaklyTrackedValues(): void;
|
|
||||||
getWeaklyTrackedValues(): any[];
|
|
||||||
runUntilIdle(): void;
|
runUntilIdle(): void;
|
||||||
isSameOrigin(a: string, b: string): boolean;
|
isSameOrigin(a: string, b: string): boolean;
|
||||||
triggerFatalErrorForTesting(): void;
|
triggerFatalErrorForTesting(): void;
|
||||||
|
|
Loading…
Reference in a new issue