Browse Source

fix: clean up devtools frontend_host on webcontents destroy (#40679)

* fix: clean up devtools frontend_host on destroy

Co-authored-by: Albert Xing <[email protected]>

* chore: use IsInPrimaryMainFrame instead of IsInMainFrame

Co-authored-by: Albert Xing <[email protected]>

* test: add a test for re-opening devtools

Co-authored-by: Albert Xing <[email protected]>

---------

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Albert Xing <[email protected]>
trop[bot] 1 year ago
parent
commit
fe49f1c79a
2 changed files with 21 additions and 2 deletions
  1. 3 2
      shell/browser/ui/inspectable_web_contents.cc
  2. 18 0
      spec/api-web-contents-spec.ts

+ 3 - 2
shell/browser/ui/inspectable_web_contents.cc

@@ -1007,6 +1007,7 @@ void InspectableWebContents::WebContentsDestroyed() {
   Observe(nullptr);
   Detach();
   embedder_message_dispatcher_.reset();
+  frontend_host_.reset();
 
   if (view_ && view_->GetDelegate())
     view_->GetDelegate()->DevToolsClosed();
@@ -1052,7 +1053,7 @@ void InspectableWebContents::OnWebContentsFocused(
 
 void InspectableWebContents::ReadyToCommitNavigation(
     content::NavigationHandle* navigation_handle) {
-  if (navigation_handle->IsInMainFrame()) {
+  if (navigation_handle->IsInPrimaryMainFrame()) {
     if (navigation_handle->GetRenderFrameHost() ==
             GetDevToolsWebContents()->GetPrimaryMainFrame() &&
         frontend_host_) {
@@ -1069,7 +1070,7 @@ void InspectableWebContents::ReadyToCommitNavigation(
 
 void InspectableWebContents::DidFinishNavigation(
     content::NavigationHandle* navigation_handle) {
-  if (navigation_handle->IsInMainFrame() ||
+  if (navigation_handle->IsInPrimaryMainFrame() ||
       !navigation_handle->GetURL().SchemeIs("chrome-extension") ||
       !navigation_handle->HasCommitted())
     return;

+ 18 - 0
spec/api-web-contents-spec.ts

@@ -640,6 +640,24 @@ describe('webContents module', () => {
       await devtoolsOpened;
       expect(w.webContents.getDevToolsTitle()).to.equal('myTitle');
     });
+
+    it('can re-open devtools', async () => {
+      const w = new BrowserWindow({ show: false });
+      const devtoolsOpened = once(w.webContents, 'devtools-opened');
+      w.webContents.openDevTools({ mode: 'detach', activate: true });
+      await devtoolsOpened;
+      expect(w.webContents.isDevToolsOpened()).to.be.true();
+
+      const devtoolsClosed = once(w.webContents, 'devtools-closed');
+      w.webContents.closeDevTools();
+      await devtoolsClosed;
+      expect(w.webContents.isDevToolsOpened()).to.be.false();
+
+      const devtoolsOpened2 = once(w.webContents, 'devtools-opened');
+      w.webContents.openDevTools({ mode: 'detach', activate: true });
+      await devtoolsOpened2;
+      expect(w.webContents.isDevToolsOpened()).to.be.true();
+    });
   });
 
   describe('setDevToolsTitle() API', () => {