From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Shelley Vohr <shelley.vohr@gmail.com>
Date: Mon, 17 Jan 2022 23:47:54 +0100
Subject: fix: crash when saving edited PDF files

This commit fixes a crash that persists any time a user attempts to
download an edited PDF. This was happening because the logic flow for
downloading of any edited PDF triggers a call to
chrome.fileSystem.chooseEntry, which we do not support and which
therefore causes unmapped page access crashes.

This patch can be removed should we choose to support chrome.fileSystem
or support it enough to fix the crash.

diff --git a/chrome/browser/resources/pdf/pdf_viewer.ts b/chrome/browser/resources/pdf/pdf_viewer.ts
index a861c34084b90561beab221ac13929bef295ac6d..94ea00dba8a6bb314421e6bd0ccef32e3ac02829 100644
--- a/chrome/browser/resources/pdf/pdf_viewer.ts
+++ b/chrome/browser/resources/pdf/pdf_viewer.ts
@@ -1086,28 +1086,27 @@ export class PdfViewerElement extends PdfViewerBaseElement {
       dataArray = [result.dataToSave];
     }
 
-    const blob = new Blob(dataArray);
     const fileName = this.attachments_[index].name;
-    chrome.fileSystem.chooseEntry(
-        {type: 'saveFile', suggestedName: fileName},
-        (entry?: FileSystemFileEntry) => {
-          if (chrome.runtime.lastError) {
-            if (chrome.runtime.lastError.message !== 'User cancelled') {
-              console.error(
-                  'chrome.fileSystem.chooseEntry failed: ' +
-                  chrome.runtime.lastError.message);
-            }
-            return;
-          }
-          entry!.createWriter((writer: FileWriter) => {
-            writer.write(blob);
-            // <if expr="enable_ink">
-            // Unblock closing the window now that the user has saved
-            // successfully.
-            this.setShowBeforeUnloadDialog_(false);
-            // </if>
-          });
-        });
+    const blob = new Blob(dataArray);
+
+    try {
+      const fileHandle = await window.showSaveFilePicker({
+        suggestedName: fileName,
+      });
+
+      const writable = await fileHandle.createWritable();
+      await writable.write(blob);
+      await writable.close();
+      // <if expr="enable_ink">
+      // Unblock closing the window now that the user has saved
+      // successfully.
+      this.setShowBeforeUnloadDialog_(false);
+      // </if>
+    } catch (error: any) {
+      if (error.name !== 'AbortError') {
+        console.error('window.showSaveFilePicker failed: ' + error);
+      }
+    }
   }
 
   /**
@@ -1298,36 +1297,33 @@ export class PdfViewerElement extends PdfViewerBaseElement {
       fileName = fileName + '.pdf';
     }
 
-    // Create blob before callback to avoid race condition.
     const blob = new Blob([result.dataToSave], {type: 'application/pdf'});
-    chrome.fileSystem.chooseEntry(
-        {
-          type: 'saveFile',
-          accepts: [{description: '*.pdf', extensions: ['pdf']}],
-          suggestedName: fileName,
-        },
-        (entry?: FileSystemFileEntry) => {
-          if (chrome.runtime.lastError) {
-            if (chrome.runtime.lastError.message !== 'User cancelled') {
-              console.error(
-                  'chrome.fileSystem.chooseEntry failed: ' +
-                  chrome.runtime.lastError.message);
-            }
-            return;
-          }
-          entry!.createWriter((writer: FileWriter) => {
-            writer.write(blob);
-            // <if expr="enable_ink or enable_pdf_ink2">
-            // Unblock closing the window now that the user has saved
-            // successfully.
-            this.setShowBeforeUnloadDialog_(false);
-            // </if>
-            // <if expr="enable_pdf_ink2">
-            this.hasSavedEdits_ =
-                this.hasSavedEdits_ || requestType === SaveRequestType.EDITED;
-            // </if>
-          });
-        });
+    try {
+      const fileHandle = await window.showSaveFilePicker({
+        suggestedName: fileName,
+        types: [{
+          description: 'PDF Files',
+          accept: { 'application/pdf': ['.pdf'] },
+        }],
+      });
+
+      const writable = await fileHandle.createWritable();
+      await writable.write(blob);
+      await writable.close();
+      // <if expr="enable_ink or enable_pdf_ink2">
+      // Unblock closing the window now that the user has saved
+      // successfully.
+      this.setShowBeforeUnloadDialog_(false);
+      // </if>
+      // <if expr="enable_pdf_ink2">
+      this.hasSavedEdits_ =
+        this.hasSavedEdits_ || requestType === SaveRequestType.EDITED;
+      // </if>
+    } catch (error: any) {
+      if (error.name !== 'AbortError') {
+        console.error('window.showSaveFilePicker failed: ' + error);
+      }
+    }
 
     // <if expr="enable_pdf_ink2">
     // Ink2 doesn't need to exit annotation mode after save.
@@ -1474,6 +1470,9 @@ declare global {
   interface HTMLElementTagNameMap {
     'pdf-viewer': PdfViewerElement;
   }
+  interface Window {
+    showSaveFilePicker(opts: unknown): Promise<FileSystemFileHandle>;
+  }
 }
 
 customElements.define(PdfViewerElement.is, PdfViewerElement);
