Browse Source

fix: properly generate requestID in webContents.printToPDF() (#20769) (#20810)

Milan Burda 5 years ago
parent
commit
b1fb7c7bfb
2 changed files with 23 additions and 2 deletions
  1. 4 2
      lib/browser/api/web-contents.js
  2. 19 0
      spec/api-web-contents-spec.js

+ 4 - 2
lib/browser/api/web-contents.js

@@ -72,7 +72,6 @@ const defaultPrintingSetting = {
   headerFooterEnabled: false,
   marginsType: 0,
   isFirstRequest: false,
-  requestID: getNextId(),
   previewUIID: 0,
   previewModifiable: true,
   printToPDF: true,
@@ -205,7 +204,10 @@ WebContents.prototype.executeJavaScript = function (code, hasUserGesture) {
 
 // Translate the options of printToPDF.
 WebContents.prototype.printToPDF = function (options) {
-  const printingSetting = Object.assign({}, defaultPrintingSetting)
+  const printingSetting = {
+    ...defaultPrintingSetting,
+    requestID: getNextId()
+  }
   if (options.landscape) {
     printingSetting.landscape = options.landscape
   }

+ 19 - 0
spec/api-web-contents-spec.js

@@ -1275,6 +1275,25 @@ describe('webContents module', () => {
       const data = await w.webContents.printToPDF({})
       expect(data).to.be.an.instanceof(Buffer).that.is.not.empty()
     })
+
+    it('does not crash when called multiple times', async () => {
+      w.destroy()
+      w = new BrowserWindow({
+        show: false,
+        webPreferences: {
+          sandbox: true
+        }
+      })
+      await w.loadURL('data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E')
+      const promises = []
+      for (let i = 0; i < 2; i++) {
+        promises.push(w.webContents.printToPDF({}))
+      }
+      const results = await Promise.all(promises)
+      for (const data of results) {
+        expect(data).to.be.an.instanceof(Buffer).that.is.not.empty()
+      }
+    })
   })
 
   describe('PictureInPicture video', () => {