Browse Source

fix: correctly notify WebViewGuestDelegate when webview is detached (#31374)

Co-authored-by: Cheng Zhao <[email protected]>
trop[bot] 3 years ago
parent
commit
7a3541d5af
2 changed files with 21 additions and 2 deletions
  1. 4 2
      shell/browser/api/electron_api_web_contents.cc
  2. 17 0
      spec-main/webview-spec.ts

+ 4 - 2
shell/browser/api/electron_api_web_contents.cc

@@ -935,8 +935,6 @@ WebContents::~WebContents() {
   }
 
   inspectable_web_contents_->GetView()->SetDelegate(nullptr);
-  if (guest_delegate_)
-    guest_delegate_->WillDestroy();
 
   // This event is only for internal use, which is emitted when WebContents is
   // being destroyed.
@@ -1947,6 +1945,10 @@ void WebContents::WebContentsDestroyed() {
     return;
   wrapper->SetAlignedPointerInInternalField(0, nullptr);
 
+  // Tell WebViewGuestDelegate that the WebContents has been destroyed.
+  if (guest_delegate_)
+    guest_delegate_->WillDestroy();
+
   Observe(nullptr);
   Emit("destroyed");
 }

+ 17 - 0
spec-main/webview-spec.ts

@@ -397,6 +397,23 @@ describe('<webview> tag', function () {
       expect(webview.getZoomFactor()).to.equal(1.2);
       await w.loadURL(`${zoomScheme}://host1`);
     });
+
+    it('does not crash when changing zoom level after webview is destroyed', async () => {
+      const w = new BrowserWindow({
+        show: false,
+        webPreferences: {
+          webviewTag: true,
+          nodeIntegration: true,
+          session: webviewSession,
+          contextIsolation: false
+        }
+      });
+      const attachPromise = emittedOnce(w.webContents, 'did-attach-webview');
+      await w.loadFile(path.join(fixtures, 'pages', 'webview-zoom-inherited.html'));
+      await attachPromise;
+      await w.webContents.executeJavaScript('view.remove()');
+      w.webContents.setZoomLevel(0.5);
+    });
   });
 
   describe('requestFullscreen from webview', () => {