Skip to content

Commit b60125f

Browse files
committed
fix: use webContentsId with contextId together
After after using `processId-contextCounter` as contextId, it may happen that contexts in different WebContents sharing the same renderer process get the same contextId. Using webContentsId as part of key in ObjectsRegistry can fix this.
1 parent 2af6d9c commit b60125f

File tree

7 files changed

+22
-53
lines changed

7 files changed

+22
-53
lines changed

atom/common/context_counter.cc

Lines changed: 0 additions & 19 deletions
This file was deleted.

atom/common/context_counter.h

Lines changed: 0 additions & 15 deletions
This file was deleted.

atom/renderer/renderer_client_base.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
#include "atom/common/atom_constants.h"
1111
#include "atom/common/color_util.h"
12-
#include "atom/common/context_counter.h"
1312
#include "atom/common/native_mate_converters/value_converter.h"
1413
#include "atom/common/options_switches.h"
1514
#include "atom/renderer/atom_autofill_agent.h"
@@ -92,9 +91,9 @@ RendererClientBase::~RendererClientBase() {
9291
void RendererClientBase::DidCreateScriptContext(
9392
v8::Handle<v8::Context> context,
9493
content::RenderFrame* render_frame) {
95-
// global.setHidden("contextId", `${processId}-${GetNextContextId()}`)
94+
// global.setHidden("contextId", `${processId}-${++nextContextId}`)
9695
std::string context_id = base::StringPrintf(
97-
"%" CrPRIdPid "-%d", base::GetCurrentProcId(), GetNextContextId());
96+
"%" CrPRIdPid "-%d", base::GetCurrentProcId(), ++next_context_id_);
9897
v8::Isolate* isolate = context->GetIsolate();
9998
v8::Local<v8::String> key = mate::StringToSymbol(isolate, "contextId");
10099
v8::Local<v8::Private> private_key = v8::Private::ForApi(isolate, key);

atom/renderer/renderer_client_base.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ class RendererClientBase : public content::ContentRendererClient {
5757
private:
5858
std::unique_ptr<PreferencesManager> preferences_manager_;
5959
bool isolated_world_;
60+
61+
// An increasing ID used for indentifying an V8 context in this process.
62+
int next_context_id_ = 0;
6063
};
6164

6265
} // namespace atom

filenames.gypi

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -437,8 +437,6 @@
437437
'atom/common/color_util.h',
438438
'atom/common/common_message_generator.cc',
439439
'atom/common/common_message_generator.h',
440-
'atom/common/context_counter.cc',
441-
'atom/common/context_counter.h',
442440
'atom/common/crash_reporter/crash_reporter.cc',
443441
'atom/common/crash_reporter/crash_reporter.h',
444442
'atom/common/crash_reporter/crash_reporter_linux.cc',

lib/browser/objects-registry.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class ObjectsRegistry {
1111
this.storage = {}
1212

1313
// Stores the IDs of objects referenced by WebContents.
14-
// (webContentsId) => [id]
14+
// (webContentsContextId) => [id]
1515
this.owners = {}
1616
}
1717

@@ -22,9 +22,10 @@ class ObjectsRegistry {
2222
const id = this.saveToStorage(obj)
2323

2424
// Add object to the set of referenced objects.
25-
let owner = this.owners[contextId]
25+
const webContentsContextId = `${webContents.id}-${contextId}`
26+
let owner = this.owners[webContentsContextId]
2627
if (!owner) {
27-
owner = this.owners[contextId] = new Set()
28+
owner = this.owners[webContentsContextId] = new Set()
2829
this.registerDeleteListener(webContents, contextId)
2930
}
3031
if (!owner.has(id)) {
@@ -44,8 +45,9 @@ class ObjectsRegistry {
4445
// Dereference an object according to its ID.
4546
// Note that an object may be double-freed (cleared when page is reloaded, and
4647
// then garbage collected in old page).
47-
remove (contextId, id) {
48-
let owner = this.owners[contextId]
48+
remove (webContents, contextId, id) {
49+
const webContentsContextId = `${webContents.id}-${contextId}`
50+
let owner = this.owners[webContentsContextId]
4951
if (owner) {
5052
// Remove the reference in owner.
5153
owner.delete(id)
@@ -55,13 +57,14 @@ class ObjectsRegistry {
5557
}
5658

5759
// Clear all references to objects refrenced by the WebContents.
58-
clear (contextId) {
59-
let owner = this.owners[contextId]
60+
clear (webContents, contextId) {
61+
const webContentsContextId = `${webContents.id}-${contextId}`
62+
let owner = this.owners[webContentsContextId]
6063
if (!owner) return
6164

6265
for (let id of owner) this.dereference(id)
6366

64-
delete this.owners[contextId]
67+
delete this.owners[webContentsContextId]
6568
}
6669

6770
// Private: Saves the object into storage and assigns an ID for it.
@@ -91,13 +94,13 @@ class ObjectsRegistry {
9194
}
9295
}
9396

94-
// Private: Clear the storage when webContents is reloaded/navigated.
97+
// Private: Clear the storage when renderer process is destoryed.
9598
registerDeleteListener (webContents, contextId) {
9699
const processId = webContents.getProcessId()
97100
const listener = (event, deletedProcessId) => {
98101
if (deletedProcessId === processId) {
99102
webContents.removeListener('render-view-deleted', listener)
100-
this.clear(contextId)
103+
this.clear(webContents, contextId)
101104
}
102105
}
103106
webContents.on('render-view-deleted', listener)

lib/browser/rpc-server.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -391,12 +391,12 @@ ipcMain.on('ELECTRON_BROWSER_MEMBER_GET', function (event, contextId, id, name)
391391
})
392392

393393
ipcMain.on('ELECTRON_BROWSER_DEREFERENCE', function (event, contextId, id) {
394-
objectsRegistry.remove(contextId, id)
394+
objectsRegistry.remove(event.sender, contextId, id)
395395
})
396396

397-
ipcMain.on('ELECTRON_BROWSER_CONTEXT_RELEASE', (e, contextId) => {
398-
objectsRegistry.clear(contextId)
399-
e.returnValue = null
397+
ipcMain.on('ELECTRON_BROWSER_CONTEXT_RELEASE', (event, contextId) => {
398+
objectsRegistry.clear(event.sender, contextId)
399+
event.returnValue = null
400400
})
401401

402402
ipcMain.on('ELECTRON_BROWSER_GUEST_WEB_CONTENTS', function (event, contextId, guestInstanceId) {

0 commit comments

Comments
 (0)