From fce84f035c35606c5707e735f503f7bdcfd5b2a1 Mon Sep 17 00:00:00 2001 From: Svet Ganov Date: Fri, 31 Oct 2014 16:56:52 -0700 Subject: Crash apps that print malformed or password protected PDFs. If apps are writing malformed content (typically not a PDF file) or if the PDF content they provide to the print system is password protected, are now crashed as both of these are app bugs. bug:17636435 Change-Id: Ifce6a3199e587448dd38f6a84290a965c24b698b --- core/java/android/print/IPrintDocumentAdapter.aidl | 1 + core/java/android/print/PrintManager.java | 21 +++++++++++++++++++++ core/jni/android/graphics/pdf/PdfEditor.cpp | 13 +++++++++++-- core/jni/android/graphics/pdf/PdfRenderer.cpp | 13 +++++++++++-- 4 files changed, 44 insertions(+), 4 deletions(-) (limited to 'core') diff --git a/core/java/android/print/IPrintDocumentAdapter.aidl b/core/java/android/print/IPrintDocumentAdapter.aidl index 9d384fb..8f33e0b 100644 --- a/core/java/android/print/IPrintDocumentAdapter.aidl +++ b/core/java/android/print/IPrintDocumentAdapter.aidl @@ -37,4 +37,5 @@ oneway interface IPrintDocumentAdapter { void write(in PageRange[] pages, in ParcelFileDescriptor fd, IWriteResultCallback callback, int sequence); void finish(); + void kill(String reason); } diff --git a/core/java/android/print/PrintManager.java b/core/java/android/print/PrintManager.java index bf8ac65..3fb812e 100644 --- a/core/java/android/print/PrintManager.java +++ b/core/java/android/print/PrintManager.java @@ -634,6 +634,17 @@ public final class PrintManager { } @Override + public void kill(String reason) { + synchronized (mLock) { + // If destroyed the handler is null. + if (!isDestroyedLocked()) { + mHandler.obtainMessage(MyHandler.MSG_ON_KILL, + reason).sendToTarget(); + } + } + } + + @Override public void onActivityPaused(Activity activity) { /* do nothing */ } @@ -719,6 +730,7 @@ public final class PrintManager { public static final int MSG_ON_LAYOUT = 2; public static final int MSG_ON_WRITE = 3; public static final int MSG_ON_FINISH = 4; + public static final int MSG_ON_KILL = 5; public MyHandler(Looper looper) { super(looper, null, true); @@ -794,6 +806,15 @@ public final class PrintManager { } } break; + case MSG_ON_KILL: { + if (DEBUG) { + Log.i(LOG_TAG, "onKill()"); + } + + String reason = (String) message.obj; + throw new RuntimeException(reason); + } + default: { throw new IllegalArgumentException("Unknown message: " + message.what); diff --git a/core/jni/android/graphics/pdf/PdfEditor.cpp b/core/jni/android/graphics/pdf/PdfEditor.cpp index 2b756e2..ed6f1d6 100644 --- a/core/jni/android/graphics/pdf/PdfEditor.cpp +++ b/core/jni/android/graphics/pdf/PdfEditor.cpp @@ -89,8 +89,17 @@ static jlong nativeOpen(JNIEnv* env, jclass thiz, jint fd, jlong size) { if (!document) { const long error = FPDF_GetLastError(); - jniThrowException(env, "java/io/IOException", - "cannot create document. Error:" + error); + switch (error) { + case FPDF_ERR_PASSWORD: + case FPDF_ERR_SECURITY: { + jniThrowException(env, "java/lang/SecurityException", + "cannot create document. Error:" + error); + } break; + default: { + jniThrowException(env, "java/io/IOException", + "cannot create document. Error:" + error); + } break; + } destroyLibraryIfNeeded(); return -1; } diff --git a/core/jni/android/graphics/pdf/PdfRenderer.cpp b/core/jni/android/graphics/pdf/PdfRenderer.cpp index 303ddea..357d3c0 100644 --- a/core/jni/android/graphics/pdf/PdfRenderer.cpp +++ b/core/jni/android/graphics/pdf/PdfRenderer.cpp @@ -82,8 +82,17 @@ static jlong nativeCreate(JNIEnv* env, jclass thiz, jint fd, jlong size) { if (!document) { const long error = FPDF_GetLastError(); - jniThrowException(env, "java/io/IOException", - "cannot create document. Error:" + error); + switch (error) { + case FPDF_ERR_PASSWORD: + case FPDF_ERR_SECURITY: { + jniThrowException(env, "java/lang/SecurityException", + "cannot create document. Error:" + error); + } break; + default: { + jniThrowException(env, "java/io/IOException", + "cannot create document. Error:" + error); + } break; + } destroyLibraryIfNeeded(); return -1; } -- cgit v1.1