Commit 93b817d1 authored by Gang Wu's avatar Gang Wu Committed by Commit Bot

Surround ClipboardManager#getPrimaryClip in try/catch

Like getCoercedText and getHTMLText in Clipboard.java, if another app
did something incorrectly when they put content into the clipboard,
getPrimaryClip may crash. For example, if the another app didn't set
permissions correctly this may crash. There are also some devices where
hasPrimaryClip returns true but calling getPrimaryClip causes an NPE.

Bug: 1059946
Change-Id: Idf5d64f86f7d85cd2723aeac3a1d4868fb579317
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2097019
Commit-Queue: Gang Wu <gangwu@chromium.org>
Reviewed-by: default avatarTheresa  <twellington@chromium.org>
Cr-Commit-Position: refs/heads/master@{#749246}
parent 46e7d479
......@@ -19,6 +19,8 @@ import android.text.style.CharacterStyle;
import android.text.style.ParagraphStyle;
import android.text.style.UpdateAppearance;
import androidx.annotation.Nullable;
import org.chromium.base.ApiCompatibilityUtils;
import org.chromium.base.BuildInfo;
import org.chromium.base.ContextUtils;
......@@ -150,16 +152,22 @@ public class Clipboard implements ClipboardManager.OnPrimaryClipChangedListener
* @return an Uri if mime type is image type, or null if there is no Uri or no entries on the
* primary clip.
*/
public Uri getImageUri() {
ClipData clipData = mClipboardManager.getPrimaryClip();
if (clipData == null || clipData.getItemCount() == 0) return null;
public @Nullable Uri getImageUri() {
// getPrimaryClip() has been observed to throw unexpected exceptions for some devices (see
// crbug.com/654802).
try {
ClipData clipData = mClipboardManager.getPrimaryClip();
if (clipData == null || clipData.getItemCount() == 0) return null;
ClipDescription description = clipData.getDescription();
if (description == null || !description.hasMimeType("image/*")) {
ClipDescription description = clipData.getDescription();
if (description == null || !description.hasMimeType("image/*")) {
return null;
}
return clipData.getItemAt(0).getUri();
} catch (Exception e) {
return null;
}
return clipData.getItemAt(0).getUri();
}
/**
......
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