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

This commit is contained in:
Jeremy Rose 2020-11-23 09:21:00 -08:00 committed by GitHub
parent abb1504ecc
commit 329494cfeb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 2 deletions

View file

@ -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

View file

@ -3781,6 +3781,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;
@ -3798,7 +3804,7 @@ void Initialize(v8::Local<v8::Object> exports,
v8::Isolate* isolate = context->GetIsolate();
gin_helper::Dictionary dict(isolate, exports);
dict.Set("WebContents", WebContents::GetConstructor(context));
dict.SetMethod("fromId", &WebContents::FromID);
dict.SetMethod("fromId", &WebContentsFromID);
dict.SetMethod("getAllWebContents", &GetAllWebContentsAsV8);
}

View file

@ -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 () => {