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

fix: segfault on webContents.fromId(xxx) #26652

Merged
merged 1 commit into from Nov 23, 2020
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
3 changes: 2 additions & 1 deletion docs/api/web-contents.md
Expand Up @@ -42,7 +42,8 @@ returns `null`.

* `id` Integer

Returns `WebContents` - A WebContents instance with the given ID.
Returns `WebContents` | undefined - A WebContents instance with the given ID, or
`undefined` if there is no WebContents associated with the given ID.

## Class: WebContents

Expand Down
8 changes: 7 additions & 1 deletion shell/browser/api/electron_api_web_contents.cc
Expand Up @@ -3118,6 +3118,12 @@ namespace {
using electron::api::GetAllWebContents;
using electron::api::WebContents;

gin::Handle<WebContents> WebContentsFromID(v8::Isolate* isolate, int32_t id) {
WebContents* contents = WebContents::FromID(id);
return contents ? gin::CreateHandle(isolate, contents)
: gin::Handle<WebContents>();
}

std::vector<gin::Handle<WebContents>> GetAllWebContentsAsV8(
v8::Isolate* isolate) {
std::vector<gin::Handle<WebContents>> list;
Expand All @@ -3136,7 +3142,7 @@ void Initialize(v8::Local<v8::Object> exports,
gin_helper::Dictionary dict(isolate, exports);
dict.Set("WebContents", WebContents::GetConstructor(context));
dict.SetMethod("create", &WebContents::Create);
dict.SetMethod("fromId", &WebContents::FromID);
dict.SetMethod("fromId", &WebContentsFromID);
dict.SetMethod("getAllWebContents", &GetAllWebContentsAsV8);
}

Expand Down
6 changes: 6 additions & 0 deletions spec-main/api-web-contents-spec.ts
Expand Up @@ -42,6 +42,12 @@ describe('webContents module', () => {
});
});

describe('fromId()', () => {
it('returns undefined for an unknown id', () => {
expect(webContents.fromId(12345)).to.be.undefined();
});
});

describe('will-prevent-unload event', function () {
afterEach(closeAllWindows);
it('does not emit if beforeunload returns undefined', async () => {
Expand Down