fix_crash_when_saving_edited_pdf_files.patch 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
  2. From: Shelley Vohr <[email protected]>
  3. Date: Mon, 17 Jan 2022 23:47:54 +0100
  4. Subject: fix: crash when saving edited PDF files
  5. This commit fixes a crash that persists any time a user attempts to
  6. download an edited PDF. This was happening because the logic flow for
  7. downloading of any edited PDF triggers a call to
  8. chrome.fileSystem.chooseEntry, which we do not support and which
  9. therefore causes unmapped page access crashes.
  10. This patch can be removed should we choose to support chrome.fileSystem
  11. or support it enough to fix the crash.
  12. diff --git a/chrome/browser/resources/pdf/pdf_viewer.ts b/chrome/browser/resources/pdf/pdf_viewer.ts
  13. index 26391c1965a83aa646db3ef9dbb6cb72fd564933..a3480ba54553406dc3576caefc814d854a9db21d 100644
  14. --- a/chrome/browser/resources/pdf/pdf_viewer.ts
  15. +++ b/chrome/browser/resources/pdf/pdf_viewer.ts
  16. @@ -1108,29 +1108,27 @@ export class PdfViewerElement extends PdfViewerBaseElement {
  17. dataArray = [result.dataToSave];
  18. }
  19. - const blob = new Blob(dataArray);
  20. const fileName = this.attachments_[index].name;
  21. - // TODO(crbug.com/373852607): Update to `showSaveFilePicker`.
  22. - chrome.fileSystem.chooseEntry(
  23. - {type: 'saveFile', suggestedName: fileName},
  24. - (entry?: FileSystemFileEntry) => {
  25. - if (chrome.runtime.lastError) {
  26. - if (chrome.runtime.lastError.message !== 'User cancelled') {
  27. - console.error(
  28. - 'chrome.fileSystem.chooseEntry failed: ' +
  29. - chrome.runtime.lastError.message);
  30. - }
  31. - return;
  32. - }
  33. - entry!.createWriter((writer: FileWriter) => {
  34. - writer.write(blob);
  35. - // <if expr="enable_ink">
  36. - // Unblock closing the window now that the user has saved
  37. - // successfully.
  38. - this.setShowBeforeUnloadDialog_(false);
  39. - // </if>
  40. - });
  41. - });
  42. + const blob = new Blob(dataArray);
  43. +
  44. + try {
  45. + const fileHandle = await window.showSaveFilePicker({
  46. + suggestedName: fileName,
  47. + });
  48. +
  49. + const writable = await fileHandle.createWritable();
  50. + await writable.write(blob);
  51. + await writable.close();
  52. + // <if expr="enable_ink">
  53. + // Unblock closing the window now that the user has saved
  54. + // successfully.
  55. + this.setShowBeforeUnloadDialog_(false);
  56. + // </if>
  57. + } catch (error: any) {
  58. + if (error.name !== 'AbortError') {
  59. + console.error('window.showSaveFilePicker failed: ' + error);
  60. + }
  61. + }
  62. }
  63. /**
  64. @@ -1340,7 +1338,6 @@ export class PdfViewerElement extends PdfViewerBaseElement {
  65. fileName = fileName + '.pdf';
  66. }
  67. - // Create blob before callback to avoid race condition.
  68. const blob = new Blob([result.dataToSave], {type: 'application/pdf'});
  69. // TODO(crbug.com/373852607): When OOPIF PDF is enabled, cross origin
  70. // checks block the request for `showSaveFilePicker`. Fix the issue by