Browse Source

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

Milan Burda 5 years ago
parent
commit
7801e3e560
2 changed files with 29 additions and 2 deletions
  1. 4 2
      lib/browser/api/web-contents.js
  2. 25 0
      spec/api-web-contents-spec.js

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

@@ -71,7 +71,6 @@ const defaultPrintingSetting = {
   headerFooterEnabled: false,
   marginsType: 0,
   isFirstRequest: false,
-  requestID: getNextId(),
   previewUIID: 0,
   previewModifiable: true,
   printToPDF: true,
@@ -265,7 +264,10 @@ WebContents.prototype.takeHeapSnapshot = function (filePath) {
 
 // Translate the options of printToPDF.
 WebContents.prototype.printToPDF = function (options, callback) {
-  const printingSetting = Object.assign({}, defaultPrintingSetting)
+  const printingSetting = {
+    ...defaultPrintingSetting,
+    requestID: getNextId()
+  }
   if (options.landscape) {
     printingSetting.landscape = options.landscape
   }

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

@@ -1284,5 +1284,30 @@ describe('webContents module', () => {
       })
       w.loadURL('data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E')
     })
+
+    it('does not crash when called multiple times', (done) => {
+      w.destroy()
+      w = new BrowserWindow({
+        show: false,
+        webPreferences: {
+          sandbox: true
+        }
+      })
+      w.webContents.once('did-finish-load', () => {
+        const count = 2
+        let current = 0
+        for (let i = 0; i < count; i++) {
+          w.webContents.printToPDF({}, function (error, data) {
+            assert.strictEqual(error, null)
+            assert.strictEqual(data instanceof Buffer, true)
+            assert.notStrictEqual(data.length, 0)
+            if (++current === count) {
+              done()
+            }
+          })
+        }
+      })
+      w.loadURL('data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E')
+    })
   })
 })