Commit eb2125e8 authored by Shimi Zhang's avatar Shimi Zhang Committed by Commit Bot

[Printing] Convert raw fd to ParcelFileDescriptor

There were ownership issue of the file descriptor, use
ParcelFileDescriptor instead of raw fd to prevent wrong ownership
exchange.

Current tests should cover this change.

Bug: 882608
Change-Id: I974739f4b3d59c9ac3ba21006f686e7a38244806
Reviewed-on: https://chromium-review.googlesource.com/1244803
Commit-Queue: Shimi Zhang <ctzsm@chromium.org>
Reviewed-by: default avatarTed Choc <tedchoc@chromium.org>
Cr-Commit-Position: refs/heads/master@{#594565}
parent d2c0cb0f
......@@ -55,7 +55,7 @@ public class PrintingControllerImpl implements PrintingController, PdfGenerator
private int mRenderFrameId;
/** The file descriptor into which the PDF will be written. Provided by the framework. */
private int mFileDescriptor;
private ParcelFileDescriptor mFileDescriptor;
/** Dots per inch, as provided by the framework. */
private int mDpi;
......@@ -143,7 +143,7 @@ public class PrintingControllerImpl implements PrintingController, PdfGenerator
@Override
public int getFileDescriptor() {
return mFileDescriptor;
return mFileDescriptor.getFd();
}
@Override
......@@ -215,8 +215,7 @@ public class PrintingControllerImpl implements PrintingController, PdfGenerator
public void pdfWritingDone(int pageCount) {
if (mPrintingState == PRINTING_STATE_FINISHED) return;
mPrintingState = PRINTING_STATE_READY;
closeFileDescriptor(mFileDescriptor);
mFileDescriptor = -1;
closeFileDescriptor();
if (pageCount > 0) {
PageRange[] pageRanges = convertIntegerArrayToPageRanges(mPages, pageCount);
mOnWriteCallback.onWriteFinished(pageRanges);
......@@ -279,8 +278,14 @@ public class PrintingControllerImpl implements PrintingController, PdfGenerator
mOnWriteCallback = callback;
assert mPrintingState == PRINTING_STATE_READY;
mFileDescriptor = destination.getFd();
assert mFileDescriptor == null;
try {
mFileDescriptor = destination.dup();
} catch (IOException e) {
mOnWriteCallback.onWriteFailed("ParcelFileDescriptor.dup() failed: " + e.toString());
resetCallbacks();
return;
}
mPages = convertPageRangesToIntegerArray(ranges);
// mRenderProcessId and mRenderFrameId could be invalid values, in this case we are going to
......@@ -306,8 +311,7 @@ public class PrintingControllerImpl implements PrintingController, PdfGenerator
mPrintingState = PRINTING_STATE_FINISHED;
closeFileDescriptor(mFileDescriptor);
mFileDescriptor = -1;
closeFileDescriptor();
resetCallbacks();
// The printmanager contract is that onFinish() is always called as the last
......@@ -320,12 +324,14 @@ public class PrintingControllerImpl implements PrintingController, PdfGenerator
mOnLayoutCallback = null;
}
private static void closeFileDescriptor(int fd) {
ParcelFileDescriptor fileDescriptor = ParcelFileDescriptor.adoptFd(fd);
private void closeFileDescriptor() {
if (mFileDescriptor == null) return;
try {
fileDescriptor.close();
mFileDescriptor.close();
} catch (IOException ioe) {
/* ignore */
} finally {
mFileDescriptor = null;
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment