Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use native WeakRef instead of v8util.weaklyTrackValue() #31164

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 0 additions & 26 deletions shell/common/api/electron_api_v8_util.cc
Expand Up @@ -107,28 +107,6 @@ bool IsSameOrigin(const GURL& l, const GURL& 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.
void TriggerFatalErrorForTesting(v8::Isolate* isolate) {
static const char* aDeps[] = {"B"};
Expand All @@ -142,7 +120,6 @@ void TriggerFatalErrorForTesting(v8::Isolate* isolate) {
void RunUntilIdle() {
base::RunLoop().RunUntilIdle();
}
#endif

void Initialize(v8::Local<v8::Object> exports,
v8::Local<v8::Value> unused,
Expand All @@ -159,9 +136,6 @@ void Initialize(v8::Local<v8::Object> exports,
dict.SetMethod("isSameOrigin", &IsSameOrigin);
#if DCHECK_IS_ON()
dict.SetMethod("triggerFatalErrorForTesting", &TriggerFatalErrorForTesting);
dict.SetMethod("getWeaklyTrackedValues", &GetWeaklyTrackedValues);
dict.SetMethod("clearWeaklyTrackedValues", &ClearWeaklyTrackedValues);
dict.SetMethod("weaklyTrackValue", &WeaklyTrackValue);
dict.SetMethod("runUntilIdle", &RunUntilIdle);
#endif
}
Expand Down
12 changes: 6 additions & 6 deletions spec-main/api-context-bridge-spec.ts
Expand Up @@ -588,11 +588,11 @@ describe('contextBridge', () => {
if (!useSandbox) {
it('should release the global hold on methods sent across contexts', async () => {
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 { weaklyTrackValue } = process._linkedBinding('electron_common_v8_util');
const trackedValues: WeakRef<object>[] = [];
require('electron').ipcRenderer.on('get-gc-info', e => e.sender.send('gc-info', { trackedValues: trackedValues.filter(value => value.deref()).length }));
contextBridge.exposeInMainWorld('example', {
getFunction: () => () => 123,
track: weaklyTrackValue
track: (value: object) => { trackedValues.push(new WeakRef(value)); }
});
});
await callWithBindings(async (root: any) => {
Expand All @@ -616,11 +616,11 @@ describe('contextBridge', () => {
if (useSandbox) {
it('should not leak the global hold on methods sent across contexts when reloading a sandboxed renderer', async () => {
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 { weaklyTrackValue } = process._linkedBinding('electron_common_v8_util');
const trackedValues: WeakRef<object>[] = [];
require('electron').ipcRenderer.on('get-gc-info', e => e.sender.send('gc-info', { trackedValues: trackedValues.filter(value => value.deref()).length }));
contextBridge.exposeInMainWorld('example', {
getFunction: () => () => 123,
track: weaklyTrackValue
track: (value: object) => { trackedValues.push(new WeakRef(value)); }
});
require('electron').ipcRenderer.send('window-ready-for-tasking');
});
Expand Down
3 changes: 0 additions & 3 deletions typings/internal-ambient.d.ts
Expand Up @@ -45,9 +45,6 @@ declare namespace NodeJS {
setHiddenValue<T>(obj: any, key: string, value: T): void;
deleteHiddenValue(obj: any, key: string): void;
requestGarbageCollectionForTesting(): void;
weaklyTrackValue(value: any): void;
clearWeaklyTrackedValues(): void;
getWeaklyTrackedValues(): any[];
runUntilIdle(): void;
isSameOrigin(a: string, b: string): boolean;
triggerFatalErrorForTesting(): void;
Expand Down