fix_crash_when_saving_edited_pdf_files.patch 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 2ce9eaccfe7d9f39a14f088fdfdf8fd4b0ef85ab..f0136a8da926471798588670d3dd062f081bcb7a 100644
  14. --- a/chrome/browser/resources/pdf/pdf_viewer.ts
  15. +++ b/chrome/browser/resources/pdf/pdf_viewer.ts
  16. @@ -900,26 +900,12 @@ export class PdfViewerElement extends PdfViewerBaseElement {
  17. dataArray = [result.dataToSave];
  18. }
  19. + const a = document.createElement('a');
  20. + a.download = this.attachments_[index].name;
  21. const blob = new Blob(dataArray);
  22. - const fileName = this.attachments_[index].name;
  23. - chrome.fileSystem.chooseEntry(
  24. - {type: 'saveFile', suggestedName: fileName},
  25. - (entry?: FileSystemFileEntry) => {
  26. - if (chrome.runtime.lastError) {
  27. - if (chrome.runtime.lastError.message !== 'User cancelled') {
  28. - console.error(
  29. - 'chrome.fileSystem.chooseEntry failed: ' +
  30. - chrome.runtime.lastError.message);
  31. - }
  32. - return;
  33. - }
  34. - entry!.createWriter((writer: FileWriter) => {
  35. - writer.write(blob);
  36. - // Unblock closing the window now that the user has saved
  37. - // successfully.
  38. - chrome.mimeHandlerPrivate.setShowBeforeUnloadDialog(false);
  39. - });
  40. - });
  41. + a.href = URL.createObjectURL(blob);
  42. + a.click();
  43. + URL.revokeObjectURL(a.href);
  44. }
  45. /**
  46. @@ -1027,30 +1013,12 @@ export class PdfViewerElement extends PdfViewerBaseElement {
  47. fileName = fileName + '.pdf';
  48. }
  49. - // Create blob before callback to avoid race condition.
  50. + const a = document.createElement('a');
  51. + a.download = fileName;
  52. const blob = new Blob([result.dataToSave], {type: 'application/pdf'});
  53. - chrome.fileSystem.chooseEntry(
  54. - {
  55. - type: 'saveFile',
  56. - accepts: [{description: '*.pdf', extensions: ['pdf']}],
  57. - suggestedName: fileName,
  58. - },
  59. - (entry?: FileSystemFileEntry) => {
  60. - if (chrome.runtime.lastError) {
  61. - if (chrome.runtime.lastError.message !== 'User cancelled') {
  62. - console.error(
  63. - 'chrome.fileSystem.chooseEntry failed: ' +
  64. - chrome.runtime.lastError.message);
  65. - }
  66. - return;
  67. - }
  68. - entry!.createWriter((writer: FileWriter) => {
  69. - writer.write(blob);
  70. - // Unblock closing the window now that the user has saved
  71. - // successfully.
  72. - chrome.mimeHandlerPrivate.setShowBeforeUnloadDialog(false);
  73. - });
  74. - });
  75. + a.href = URL.createObjectURL(blob);
  76. + a.click();
  77. + URL.revokeObjectURL(a.href);
  78. // <if expr="enable_ink">
  79. // Saving in Annotation mode is destructive: crbug.com/919364