fix: crash when closing devtools after focus

This commit is contained in:
Shelley Vohr
2025-06-10 16:20:49 +02:00
parent d6c0691a63
commit c4caa9b99a
3 changed files with 35 additions and 9 deletions

View File

@@ -2164,9 +2164,12 @@ void WebContents::DevToolsOpened() {
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
v8::HandleScope handle_scope(isolate);
DCHECK(inspectable_web_contents_);
DCHECK(inspectable_web_contents_->GetDevToolsWebContents());
auto handle = FromOrCreate(
isolate, inspectable_web_contents_->GetDevToolsWebContents());
content::WebContents* const dtwc = GetDevToolsWebContents();
if (!dtwc)
return;
auto handle = FromOrCreate(isolate, dtwc);
devtools_web_contents_.Reset(isolate, handle.ToV8());
// Set inspected tabID.
@@ -2174,12 +2177,11 @@ void WebContents::DevToolsOpened() {
"DevToolsAPI", "setInspectedTabId", base::Value(ID()));
// Inherit owner window in devtools when it doesn't have one.
auto* devtools = inspectable_web_contents_->GetDevToolsWebContents();
bool has_window = devtools->GetUserData(NativeWindowRelay::UserDataKey());
bool has_window = dtwc->GetUserData(NativeWindowRelay::UserDataKey());
if (owner_window_ && !has_window) {
DCHECK(!owner_window_.WasInvalidated());
DCHECK_EQ(handle->owner_window(), nullptr);
handle->SetOwnerWindow(devtools, owner_window());
handle->SetOwnerWindow(dtwc, owner_window());
}
Emit("devtools-opened");
@@ -2862,7 +2864,7 @@ void WebContents::InspectElement(int x, int y) {
if (!enable_devtools_ || !inspectable_web_contents_)
return;
if (!inspectable_web_contents_->GetDevToolsWebContents())
if (!GetDevToolsWebContents())
OpenDevTools(nullptr);
inspectable_web_contents_->InspectElement(x, y);
}

View File

@@ -437,11 +437,15 @@ void InspectableWebContents::ShowDevTools(bool activate) {
void InspectableWebContents::CloseDevTools() {
if (GetDevToolsWebContents()) {
frontend_loaded_ = false;
embedder_message_dispatcher_.reset();
if (managed_devtools_web_contents_) {
view_->CloseDevTools();
managed_devtools_web_contents_.reset();
content::GetUIThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindOnce(
[](std::unique_ptr<content::WebContents> web_contents) {},
std::move(managed_devtools_web_contents_)));
}
embedder_message_dispatcher_.reset();
if (!is_guest())
web_contents_->Focus();
}
@@ -530,7 +534,11 @@ void InspectableWebContents::CloseWindow() {
void InspectableWebContents::LoadCompleted() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!GetDevToolsWebContents())
return;
frontend_loaded_ = true;
if (managed_devtools_web_contents_)
view_->ShowDevTools(activate_);

View File

@@ -1056,6 +1056,22 @@ describe('webContents module', () => {
await devtoolsOpened2;
expect(w.webContents.isDevToolsOpened()).to.be.true();
});
it('does not crash when closing DevTools immediately after opening', async () => {
const w = new BrowserWindow({ show: true });
await w.loadURL('about:blank');
const devToolsFocused = once(w.webContents, 'devtools-focused');
w.webContents.openDevTools({ mode: 'detach' });
w.webContents.inspectElement(100, 100);
await devToolsFocused;
expect(() => {
w.webContents.closeDevTools();
}).to.not.throw();
expect(w.webContents.isDevToolsOpened()).to.be.false();
});
});
describe('setDevToolsTitle() API', () => {