Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package app.handlive.android.feature.clipboard.system

import android.content.ClipDescription
import android.content.Context
import android.net.Uri
import app.handlive.android.feature.clipboard.engine.ClipLimits
import app.handlive.android.feature.clipboard.module.ClipFiles
import app.handlive.android.feature.clipboard.module.LocalRead
import app.handlive.android.feature.clipboard.module.ReadFailure
import app.handlive.android.feature.connection.bench.BenchEvent
import app.handlive.android.feature.connection.bench.BenchLog
import java.io.File
import java.io.InputStream
import java.util.UUID

/** Sensitive flag and read source (`auto` or `manual`) of one local clip. */
internal data class ClipOrigin(
val sensitive: Boolean,
val source: String,
)

/** CLIP-03 API 1: copy item 0's image into `cache/clip/` before the clipboard URI grant ends. */
internal object ClipImageCopier {
fun describedMime(description: ClipDescription): String? =
(0 until description.mimeTypeCount)
.map(description::getMimeType)
.firstOrNull { it.startsWith(IMAGE) }

/**
* The image, or null when [uri] is not one. `getType` and `openInputStream` run only after
* [ClipUriPolicy.isReadable].
*/
fun copyIfImage(
context: Context,
description: ClipDescription,
uri: Uri,
origin: ClipOrigin,
files: ClipFiles,
): LocalRead? {
val mime = imageMimeOf(context, description, uri) ?: return null
val target = files.source(UUID.randomUUID().toString())
val failure = copyImage(context, uri, target)
if (failure != null) logCopyFailure(uri, origin.source, failure)
return if (failure == null) {
LocalRead.Image(target, mime, origin.sensitive, origin.source)
} else {
LocalRead.Failed(failure, origin.source)
}
}

private fun imageMimeOf(
context: Context,
description: ClipDescription,
uri: Uri,
): String? {
if (!ClipUriPolicy.isReadable(context, uri)) return null
val provided = runCatching { context.contentResolver.getType(uri) }.getOrNull()
return provided?.takeIf { it.startsWith(IMAGE) } ?: describedMime(description)
}

/** Copies the image into [target]; a failure deletes it (E2, E3, E10). */
private fun copyImage(
context: Context,
uri: Uri,
target: File,
): ReadFailure? {
if (!ClipUriPolicy.isReadable(context, uri)) {
target.delete()
return ReadFailure.IMAGE_UNREADABLE
}
val copied = runCatching { context.contentResolver.openInputStream(uri)?.use { copyLimited(it, target) } }
val size = copied.getOrNull()
val failure =
when {
copied.exceptionOrNull() is SecurityException -> ReadFailure.PERMISSION_LOST
size == null -> ReadFailure.IMAGE_UNREADABLE
size > ClipLimits.MAX_IMAGE_BYTES -> ReadFailure.IMAGE_TOO_LARGE
else -> null
}
if (failure != null) target.delete()
return failure
}

/** Copies at most `CLIP_MAX_IMAGE` + 1 bytes (logic 3: one byte more tells "too large"). */
private fun copyLimited(
input: InputStream,
target: File,
): Long {
val limit = ClipLimits.MAX_IMAGE_BYTES + 1
var total = 0L
target.outputStream().use { out ->
val buffer = ByteArray(BUFFER)
while (total < limit) {
val read = input.read(buffer, 0, minOf(BUFFER.toLong(), limit - total).toInt())
if (read < 0) break
out.write(buffer, 0, read)
total += read
}
}
return total
}

private fun logCopyFailure(
uri: Uri,
source: String,
failure: ReadFailure,
) {
BenchLog.event(
BenchEvent.CLIP_READ_FAILED,
"reason" to failure.name.lowercase(),
"stage" to "copy",
"authority" to ClipUriPolicy.logAuthority(uri),
"source" to source,
)
}

private const val IMAGE = "image/"
private const val BUFFER = 64 * 1024
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,20 @@ import android.content.ClipDescription
import android.content.Context
import android.net.Uri
import app.handlive.android.core.protocol.clipboard.HtmlClipSanitizer
import app.handlive.android.feature.clipboard.engine.ClipLimits
import app.handlive.android.feature.clipboard.module.ClipFiles
import app.handlive.android.feature.clipboard.module.LocalRead
import app.handlive.android.feature.clipboard.module.ReadFailure
import app.handlive.android.feature.connection.bench.BenchEvent
import app.handlive.android.feature.connection.bench.BenchLog
import java.io.File
import java.io.InputStream
import java.util.UUID

/**
* CLIP-01 API 2 logic 2 and CLIP-03 API 1: item 0 of a clip is an image copied into `cache/clip/` right away when its
* URI is an image — the clipboard's URI grant ends when the clip changes — else plain text (`coerceToText`: HTML gives
* its text, a text URI is read by the system) with the sanitized `htmlText` beside it when the item has one. The
* image wins over a text beside it: browsers and OEM galleries put the image's URL, its alt text or an empty string
* next to the URI, and the user copied the picture, not that text.
* Anything else is E3. Runs off the main thread; the content is never logged.
* A URI is opened only after [ClipUriPolicy.isReadable] (CLIP-01 API 2 logic 7). Anything else is E3. Runs off the
* main thread; the content and the URI path are never logged.
*/
object ClipReader {
fun read(
Expand All @@ -30,41 +27,79 @@ object ClipReader {
source: String,
files: ClipFiles,
): LocalRead {
val item =
clip?.takeIf { it.itemCount > 0 }?.getItemAt(0)
?: return unsupported(clip, source, if (clip == null) "no_clip" else "no_item")
if (clip == null || clip.itemCount == 0) {
return unsupported(clip, source, if (clip == null) "no_clip" else "no_item")
}
return fromItem(context, clip, clip.getItemAt(0), source, files)
}

private fun fromItem(
context: Context,
clip: ClipData,
item: ClipData.Item,
source: String,
files: ClipFiles,
): LocalRead {
val sensitive = SystemClipboard.isSensitive(clip.description)
val refused = refusedUri(context, clip, item, source)
val uri = item.uri
val imageMime = uri?.let { imageMimeOf(context, clip.description, it) }
return when {
// CLIP-03 API 1 logic 1 first: an item whose URI is an image is a copied image, text beside it or not.
uri != null && imageMime != null -> {
val target = files.source(UUID.randomUUID().toString())
copyImage(context, uri, target)?.let { failure ->
// Debug builds only (HLBENCH): which provider refused the read; never the path or the content.
BenchLog.event(
BenchEvent.CLIP_READ_FAILED,
"reason" to failure.name.lowercase(),
"stage" to "copy",
"authority" to (uri.authority?.takeIf { uri.scheme == CONTENT } ?: "-"),
"source" to source,
)
LocalRead.Failed(failure, source)
} ?: LocalRead.Image(target, imageMime, sensitive, source)
val image =
if (refused == null && uri != null && ClipUriPolicy.isReadable(context, uri)) {
ClipImageCopier.copyIfImage(
context,
clip.description,
uri,
ClipOrigin(sensitive, source),
files,
)
} else {
null
}

item.text != null || item.htmlText != null -> {
text(item.coerceToText(context), sensitive, source, item.htmlText)
val textual =
if (refused == null && image == null) {
textIfAny(context, clip.description, item, sensitive, source)
} else {
null
}
return refused ?: image ?: textual ?: unsupported(clip, source, "neither_text_nor_image")
}

uri != null && clip.description.hasMimeType(TEXT_ANY) -> {
text(item.coerceToText(context), sensitive, source)
/** A refused URI that the next step would open, or null when the item can still be read. */
private fun refusedUri(
context: Context,
clip: ClipData,
item: ClipData.Item,
source: String,
): LocalRead? {
val uri = item.uri
if (uri == null || ClipUriPolicy.isReadable(context, uri) || !wouldOpenUri(clip.description, item)) {
return null
}
val failure =
if (ClipImageCopier.describedMime(clip.description) != null) {
ReadFailure.IMAGE_UNREADABLE
} else {
ReadFailure.EMPTY_OR_NOT_TEXT
}
return rejectUri(uri, source, failure)
}

else -> {
unsupported(clip, source, "neither_text_nor_image")
}
}
private fun textIfAny(
context: Context,
description: ClipDescription,
item: ClipData.Item,
sensitive: Boolean,
source: String,
): LocalRead? {
val uri = item.uri
val textual =
item.text != null ||
item.htmlText != null ||
(uri != null && description.hasMimeType(TEXT_ANY))
if (!textual) return null
// `htmlText` is the string already on the item. `coerceToHtmlText` is not used: with no text and no
// HTML it opens the URI through `openTypedAssetFileDescriptor`.
return text(plainText(context, item), sensitive, source, item.htmlText)
}

/**
Expand Down Expand Up @@ -94,7 +129,7 @@ object ClipReader {
"why" to why,
"mimes" to mimes.ifEmpty { "-" },
"parts" to parts.ifEmpty { "-" },
"authority" to (item?.uri?.takeIf { it.scheme == CONTENT }?.authority ?: "-"),
"authority" to (item?.uri?.let(ClipUriPolicy::logAuthority) ?: "-"),
"source" to source,
)
return LocalRead.Failed(ReadFailure.EMPTY_OR_NOT_TEXT, source)
Expand All @@ -111,61 +146,45 @@ object ClipReader {
}
?: LocalRead.Failed(ReadFailure.EMPTY_OR_NOT_TEXT, source)

/** Logic 1: an image when the description or the provider gives an `image` MIME type. */
private fun imageMimeOf(
context: Context,
/**
* True when the next step would open the item URI: an image MIME on the description, or `coerceToText` /
* `coerceToHtmlText` because `text` is absent (both open a content URI).
*/
private fun wouldOpenUri(
description: ClipDescription,
uri: Uri,
): String? {
val provided = runCatching { context.contentResolver.getType(uri) }.getOrNull()
val described =
(0 until description.mimeTypeCount)
.map(
description::getMimeType,
).firstOrNull { it.startsWith(IMAGE) }
return provided?.takeIf { it.startsWith(IMAGE) } ?: described
}
item: ClipData.Item,
): Boolean =
ClipImageCopier.describedMime(description) != null ||
(item.text == null && (item.htmlText != null || description.hasMimeType(TEXT_ANY)))

/** Copies the image into [target]; a failure deletes it (E2, E3, E10). */
private fun copyImage(
/**
* `coerceToText` opens a URI when `text` is absent. A refused URI is never passed to it: the text
* already on the item is returned as-is, otherwise nothing. HTML stays the item's own `htmlText`.
*/
private fun plainText(
context: Context,
uri: Uri,
target: File,
): ReadFailure? {
val copied = runCatching { context.contentResolver.openInputStream(uri)?.use { copyLimited(it, target) } }
val size = copied.getOrNull()
val failure =
when {
copied.exceptionOrNull() is SecurityException -> ReadFailure.PERMISSION_LOST
size == null -> ReadFailure.IMAGE_UNREADABLE
size > ClipLimits.MAX_IMAGE_BYTES -> ReadFailure.IMAGE_TOO_LARGE
else -> null
}
if (failure != null) target.delete()
return failure
item: ClipData.Item,
): CharSequence? {
val uri = item.uri
if (uri != null && !ClipUriPolicy.isReadable(context, uri)) return item.text
return item.coerceToText(context)
}

/** Copies at most `CLIP_MAX_IMAGE` + 1 bytes (logic 3: one byte more tells "too large"). */
private fun copyLimited(
input: InputStream,
target: File,
): Long {
val limit = ClipLimits.MAX_IMAGE_BYTES + 1
var total = 0L
target.outputStream().use { out ->
val buffer = ByteArray(BUFFER)
while (total < limit) {
val read = input.read(buffer, 0, minOf(BUFFER.toLong(), limit - total).toInt())
if (read < 0) break
out.write(buffer, 0, read)
total += read
}
}
return total
private fun rejectUri(
uri: Uri,
source: String,
failure: ReadFailure,
): LocalRead {
BenchLog.event(
BenchEvent.CLIP_READ_FAILED,
"reason" to failure.name.lowercase(),
"stage" to "read",
"why" to "uri_not_allowed",
"authority" to ClipUriPolicy.logAuthority(uri),
"source" to source,
)
return LocalRead.Failed(failure, source)
}

private const val IMAGE = "image/"
private const val CONTENT = "content"
private const val TEXT_ANY = "text/*"
private const val BUFFER = 64 * 1024
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package app.handlive.android.feature.clipboard.system

import android.content.Context
import android.net.Uri

/**
* CLIP-01 API 2 logic 7. A clip URI may be opened only when its scheme is `content` and
* `PackageManager.resolveContentProvider` returns a provider in another package. Anything else is refused.
* The path is never logged.
*/
internal object ClipUriPolicy {
fun isReadable(
context: Context,
uri: Uri,
): Boolean {
val authority = uri.authority
if (uri.scheme != CONTENT || authority.isNullOrEmpty()) return false
val owner = context.packageManager.resolveContentProvider(authority, 0)?.packageName
return owner != null && owner != context.packageName
}

/** Authority of a `content` URI, or `-`. Never the path. */
fun logAuthority(uri: Uri): String = uri.authority?.takeIf { uri.scheme == CONTENT } ?: "-"

private const val CONTENT = "content"
}
Loading
Loading