fix_crash_when_saving_edited_pdf_files.patch 3.3 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.js b/chrome/browser/resources/pdf/pdf_viewer.js
  13. index 4d45dfa892fec8857dabdd70e3c09c46b784ef44..d9b72b9298494df57e2ad53738a213226a7c45e4 100644
  14. --- a/chrome/browser/resources/pdf/pdf_viewer.js
  15. +++ b/chrome/browser/resources/pdf/pdf_viewer.js
  16. @@ -959,25 +959,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}, entry => {
  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 => {
  34. - writer.write(blob);
  35. - // Unblock closing the window now that the user has saved
  36. - // successfully.
  37. - chrome.mimeHandlerPrivate.setShowBeforeUnloadDialog(false);
  38. - });
  39. - });
  40. + a.href = URL.createObjectURL(blob);
  41. + a.click();
  42. + URL.revokeObjectURL(a.href);
  43. }
  44. /**
  45. @@ -1104,30 +1091,13 @@ export class PDFViewerElement extends PDFViewerBaseElement {
  46. if (!fileName.toLowerCase().endsWith('.pdf')) {
  47. fileName = fileName + '.pdf';
  48. }
  49. - // Create blob before callback to avoid race condition.
  50. +
  51. + const a = document.createElement('a');
  52. + a.download = fileName;
  53. const blob = new Blob([result.dataToSave], {type: 'application/pdf'});
  54. - chrome.fileSystem.chooseEntry(
  55. - {
  56. - type: 'saveFile',
  57. - accepts: [{description: '*.pdf', extensions: ['pdf']}],
  58. - suggestedName: fileName
  59. - },
  60. - entry => {
  61. - if (chrome.runtime.lastError) {
  62. - if (chrome.runtime.lastError.message !== 'User cancelled') {
  63. - console.error(
  64. - 'chrome.fileSystem.chooseEntry failed: ' +
  65. - chrome.runtime.lastError.message);
  66. - }
  67. - return;
  68. - }
  69. - entry.createWriter(writer => {
  70. - writer.write(blob);
  71. - // Unblock closing the window now that the user has saved
  72. - // successfully.
  73. - chrome.mimeHandlerPrivate.setShowBeforeUnloadDialog(false);
  74. - });
  75. - });
  76. + a.href = URL.createObjectURL(blob);
  77. + a.click();
  78. + URL.revokeObjectURL(a.href);
  79. // <if expr="enable_ink">
  80. // Saving in Annotation mode is destructive: crbug.com/919364