Bug 1240534 - Don't delete the temporary PDF file until printing service is really done with it. r=sebastian, a=sylvestre
authorMark Finkle <mfinkle@mozilla.com>
Mon, 18 Jan 2016 14:21:39 -0500
changeset 310868 9bb7a45e5b69d68ddd2752acc9cf3f500365b1d2
parent 310867 acfb00a0a215cdaa98510e6031ff5cddb97bf9c8
child 310869 12749267243fd0e94093f8249df0644faeea4546
push id5513
push userraliiev@mozilla.com
push dateMon, 25 Jan 2016 13:55:34 +0000
treeherdermozilla-beta@5ee97dd05b5c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssebastian, sylvestre
bugs1240534
milestone45.0a2
Bug 1240534 - Don't delete the temporary PDF file until printing service is really done with it. r=sebastian, a=sylvestre
mobile/android/base/java/org/mozilla/gecko/PrintHelper.java
--- a/mobile/android/base/java/org/mozilla/gecko/PrintHelper.java
+++ b/mobile/android/base/java/org/mozilla/gecko/PrintHelper.java
@@ -76,40 +76,49 @@ public class PrintHelper {
 
                                     byte[] buf = new byte[8192];
                                     int bytesRead;
                                     while ((bytesRead = input.read(buf)) > 0) {
                                         output.write(buf, 0, bytesRead);
                                     }
 
                                     callback.onWriteFinished(new PageRange[] { PageRange.ALL_PAGES });
-
-                                    // File is not really deleted until the input stream closes it
-                                    pdfFile.delete();
                                 } catch (FileNotFoundException ee) {
                                     Log.d(LOGTAG, "Unable to find the temporary PDF file.");
                                 } catch (IOException ioe) {
                                     Log.e(LOGTAG, "IOException while transferring temporary PDF file: ", ioe);
                                 } finally {
                                     IOUtils.safeStreamClose(input);
                                     IOUtils.safeStreamClose(output);
                                 }
                             }
                         });
                     }
 
                     @Override
-                    public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras){
+                    public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras) {
                         if (cancellationSignal.isCanceled()) {
                             callback.onLayoutCancelled();
                             return;
                         }
 
                         PrintDocumentInfo pdi = new PrintDocumentInfo.Builder(filePath).setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT).build();
                         callback.onLayoutFinished(pdi, true);
                     }
+
+                    @Override
+                    public void onFinish() {
+                        // Remove the temporary file when the printing system is finished.
+                        try {
+                            File pdfFile = new File(filePath);
+                            pdfFile.delete();
+                        } catch (NullPointerException npe) {
+                            // Silence the exception. We only want to delete a real file. We don't
+                            // care if the file doesn't exist.
+                        }
+                    }
                 };
 
                 printManager.print(jobName, pda, null);
             }
         });
     }
 }