From da04fd9eb887e52ca26c1be72e06b90e54e7ce8b Mon Sep 17 00:00:00 2001 From: Yohei Yukawa Date: Fri, 30 Jul 2021 09:06:19 -0700 Subject: [PATCH] Revert "Fix error swallowing in InputConnectionCommand unparcelling" This reverts commit 11694b0633b4c5f43045b28f42fe0c89e8d7d44a [1]. Reason for revert: We will try another approach to implement InputConnection task cancellation without InputConnectionCommand. [1]: Icd2b40e63771085cb8d88721fbdca7a089256b28 Bug: 194151409 Bug: 194567417 Change-Id: Ia404317848d74a0d8ca0606bfb07cbad36ab97c1 Test: atest CtsInputMethodTestCases:InputConnectionEndToEndTest --- .../inputmethod/InputConnectionCommand.java | 42 ++++++++--------- .../InputConnectionCommandTest.java | 47 ------------------- 2 files changed, 21 insertions(+), 68 deletions(-) delete mode 100644 core/tests/coretests/src/com/android/internal/inputmethod/InputConnectionCommandTest.java diff --git a/core/java/com/android/internal/inputmethod/InputConnectionCommand.java b/core/java/com/android/internal/inputmethod/InputConnectionCommand.java index acc1e40fc6bab..4116b028fbbf9 100644 --- a/core/java/com/android/internal/inputmethod/InputConnectionCommand.java +++ b/core/java/com/android/internal/inputmethod/InputConnectionCommand.java @@ -23,12 +23,12 @@ import android.annotation.IntDef; import android.annotation.IntRange; import android.annotation.NonNull; import android.annotation.Nullable; -import android.os.BadParcelableException; import android.os.Bundle; import android.os.IBinder; import android.os.Parcel; import android.os.Parcelable; import android.text.TextUtils; +import android.util.Log; import android.view.KeyEvent; import android.view.inputmethod.CompletionInfo; import android.view.inputmethod.CorrectionInfo; @@ -356,24 +356,14 @@ public final class InputConnectionCommand implements Parcelable { | (mResultCallbackType != ResultCallbackType.NULL ? FieldMask.CALLBACK : 0); } - - /** - * A utility method to unparcel {@link InputConnectionCommand} from the given {@link Parcel}. - * - *

When this method throws any {@link RuntimeException} or its derived class, notably - * {@link BadParcelableException}, {@code source} is considered to be in an unexpected state and - * unsafe to continue reading any subsequent data.

- * - * @param source {@link Parcel} to read the data from. - * @return {@link InputConnectionCommand} that is parcelled from {@code source}. - */ @AnyThread - @NonNull + @Nullable private static InputConnectionCommand createFromParcel(@NonNull Parcel source) { final int type = source.readInt(); if (type < InputConnectionCommandType.FIRST_COMMAND || InputConnectionCommandType.LAST_COMMAND < type) { - throw new BadParcelableException("Invalid InputConnectionCommandType=" + type); + Log.e(TAG, "Invalid InputConnectionCommand type=" + type); + return null; } @FieldMask final int fieldMask = source.readInt(); @@ -390,6 +380,9 @@ public final class InputConnectionCommand implements Parcelable { if ((fieldMask & FieldMask.PARCELABLE) != 0) { parcelableType = source.readInt(); switch (parcelableType) { + case ParcelableType.NULL: + Log.e(TAG, "Unexpected ParcelableType=NULL"); + return null; case ParcelableType.EXTRACTED_TEXT_REQUEST: parcelable = source.readTypedObject(ExtractedTextRequest.CREATOR); break; @@ -406,8 +399,8 @@ public final class InputConnectionCommand implements Parcelable { parcelable = source.readTypedObject(InputContentInfo.CREATOR); break; default: - throw new BadParcelableException( - "Invalid InputConnectionCommand.ParcelableType=" + parcelableType); + Log.e(TAG, "Unknown ParcelableType=" + parcelableType); + return null; } } else { parcelableType = ParcelableType.NULL; @@ -418,6 +411,9 @@ public final class InputConnectionCommand implements Parcelable { if ((fieldMask & FieldMask.CALLBACK) != 0) { resultCallbackType = source.readInt(); switch (resultCallbackType) { + case ResultCallbackType.NULL: + Log.e(TAG, "Unexpected ResultCallbackType=NULL"); + return null; case ResultCallbackType.BOOLEAN: case ResultCallbackType.INT: case ResultCallbackType.CHAR_SEQUENCE: @@ -426,9 +422,8 @@ public final class InputConnectionCommand implements Parcelable { resultCallback = source.readStrongBinder(); break; default: - throw new BadParcelableException( - "Invalid InputConnectionCommand.ResultCallbackType=" - + resultCallbackType); + Log.e(TAG, "Unknown ResultCallbackType=" + resultCallbackType); + return null; } } else { resultCallbackType = ResultCallbackType.NULL; @@ -444,10 +439,15 @@ public final class InputConnectionCommand implements Parcelable { public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { @AnyThread - @NonNull + @Nullable @Override public InputConnectionCommand createFromParcel(Parcel source) { - return InputConnectionCommand.createFromParcel(source); + try { + return InputConnectionCommand.createFromParcel(source); + } catch (Exception e) { + Log.e(TAG, "Returning null due to exception.", e); + return null; + } } @AnyThread diff --git a/core/tests/coretests/src/com/android/internal/inputmethod/InputConnectionCommandTest.java b/core/tests/coretests/src/com/android/internal/inputmethod/InputConnectionCommandTest.java deleted file mode 100644 index efe6cec24458e..0000000000000 --- a/core/tests/coretests/src/com/android/internal/inputmethod/InputConnectionCommandTest.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright (C) 2021 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.internal.inputmethod; - -import static org.junit.Assert.assertThrows; - -import android.os.Parcel; -import android.platform.test.annotations.Presubmit; - -import androidx.test.filters.SmallTest; -import androidx.test.runner.AndroidJUnit4; - -import org.junit.Test; -import org.junit.runner.RunWith; - -@SmallTest -@Presubmit -@RunWith(AndroidJUnit4.class) -public class InputConnectionCommandTest { - @Test - public void testCreateFromParcelDoesNotSwallowExceptions() { - final Parcel parcel = Parcel.obtain(); - try { - parcel.writeInt(InputConnectionCommandType.FIRST_COMMAND); - parcel.writeInt(InputConnectionCommand.FieldMask.PARCELABLE); - parcel.writeInt(InputConnectionCommand.ParcelableType.NULL); // invalid - assertThrows(RuntimeException.class, - () -> InputConnectionCommand.CREATOR.createFromParcel(parcel)); - } finally { - parcel.recycle(); - } - } -}