diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md index df2489e..5219e67 100644 --- a/ARCHITECTURE.md +++ b/ARCHITECTURE.md @@ -85,7 +85,7 @@ package/ LruByteCache.kt pure: byte-budgeted LRU BitmapPool.kt bitmap reuse + eager recycling MemoryBudgets.kt pure: bitmap pool budget - PdfSourceResolver.kt source → seekable ParcelFileDescriptor (base64: write, open, delete) + PdfSourceResolver.kt source → seekable ParcelFileDescriptor (base64, streamed content://: write, open, delete) NativeBase64Document.kt JNI entry: base64 text in a native buffer → decoded into a file DocumentCache.kt fresh copy or shared download/revalidation, stale fallback DocumentCacheStore.kt copies on disk: session/persistent dirs, metadata @@ -129,7 +129,7 @@ the render thread, once per render (see "Cache strategy"). | `getFormFields` | main: the changed fields' values → `com.betterpdf.read` queue: separate `PDFDocument` filled in with them, every widget page read | main → the document's `BetterPdf-render` thread (form work in call order) | | `setFormFieldValues`, the reader's form edits | main: PDFKit's widgets of the fields' pages only (field → pages index from the page dictionaries) | main → `BetterPdf-render` thread (form work in call order); results on main | | Remote download (`DocumentCache`) | lookup on `com.betterpdf.cache`; transfer and store on the `URLSession` delegate queue | lookup and transfer on `BetterPdf-download` threads (`HttpURLConnection`); the document worker waits | -| Download progress (throttle ≤ 10/s) | throttled on the delegate queue → main | throttled on the download thread → main | +| Download progress (throttle ≤ 10/s) | throttled on the delegate queue → main | throttled on the download thread → main (`content://` copies: the resolving thread) | | `PdfCache` (`getEntry`, `remove`, `clear`, `getByteSize`) | `com.betterpdf.cache.api` queue, disk only | `Promise.parallel` thread, disk only | | `PdfDocument.open` | as the view's open (`PdfDocumentLoader`), plain `PDFDocument` | `BetterPdf-document` thread: source, then a renderer to count pages (closed) | | `PdfDocument` reads (in call order) | the document's `com.betterpdf.document` serial queue, its own `PDFDocument` | the document's `BetterPdf-document` thread (exits when idle), `DocumentReads` on duplicated FDs | diff --git a/docs/api-design.md b/docs/api-design.md index fecf905..5ef5479 100644 --- a/docs/api-design.md +++ b/docs/api-design.md @@ -180,6 +180,28 @@ type PdfSource = an API that returns a PDF as base64 in JSON is better served by returning the PDF and passing `uri` + `headers`: native download, progress, caching, offline. +- **`content://` (Android, issue #69).** `PdfRenderer` needs a seekable + descriptor, and a provider may hand out a pipe + (`ContentProvider.openPipeHelper`, AOSP's MTP provider without partial + reads) or part of a larger file (an `AssetFileDescriptor` with an offset, + which `openFileDescriptor` refuses: "Not a whole file"). 1.1 failed them + with `invalid-source` and `file-not-found`. The source is opened with + `openAssetFileDescriptor`; a whole file that `lseek` accepts (the + renderer's own check; `S_ISREG` would refuse seekable descriptors that + are not regular files) is used as is, as FileProvider, MediaStore and the + Downloads provider hand out. Anything else is copied with the downloads' + 64 KB loop into the directory base64 writes to, opened, and deleted at + once like base64's file. The copy reports progress like a download + (`totalBytes` from the asset's length or the provider's + `OpenableColumns.SIZE`) and takes disk space outside `PdfCache`'s + accounting. A source change or dispose cancels the open and the size + query (`CancellationSignal`), and the signal's listener closes the descriptor, + which ends a read blocked on a stalled pipe (checked on API 37: the + thread and the partial file are gone at once). A failed read (the + provider's `closeWithError`) or write (a full disk) is `invalid-pdf`, + "The PDF could not be copied to the app's cache", with the cause in + `nativeDescription`; Android's base64 write failures now say the same. react-native-pdf reads + the whole document into memory instead (`openInputStream`). ### Props @@ -204,7 +226,7 @@ type PdfSource = | `horizontal` | `boolean` | `false` | Pages side by side; see "Layout modes". | | `enablePaging` | `boolean` | `false` | One page per swipe; see "Layout modes". | | `onLoad` | `(e: PdfLoadEvent)` | — | `{ pageCount, firstPageWidth, firstPageHeight, filePath? }` (PDF points). | -| `onLoadProgress` | `(e: PdfLoadProgressEvent)` | — | `{ loadedBytes, totalBytes? }`; remote only; see "Load progress". | +| `onLoadProgress` | `(e: PdfLoadProgressEvent)` | — | `{ loadedBytes, totalBytes? }`; remote, and Android `content://` copies; see "Load progress". | | `onPageChange` | `(e: PdfPageChangeEvent)` | — | `{ page, pageCount }`. The page under the viewport centre (the last page once scrolled to the end). | | `onError` | `(e: PdfError)` | — | Load and render errors. | | `onLinkPress` | `(e: PdfLinkPressEvent)` | — | `{ url }`. URL links only; see "Links and taps". | @@ -416,9 +438,10 @@ onLoadProgress?: (event: { loadedBytes: number; totalBytes?: number }) => void; download 1% steps still mean 100 events. The time contract bounds JS work at ~10 events per second for any file size or network speed (unit tested: 400 MB in 2 s → 21 events). -- **Remote downloads only.** Local files open without a download phase; - stored copies that are used, or confirmed unchanged by the server, open - without progress events (see "Caching and offline"). +- **Remote downloads, and Android `content://` copies** (see "Source"). + Local files open without a download phase; stored copies that are used, + or confirmed unchanged by the server, open without progress events (see + "Caching and offline"). - The throttle runs natively on the download thread (`ProgressThrottle` in Kotlin and Swift); JS never sees dropped updates. diff --git a/docs/api.md b/docs/api.md index 9e8391d..cc00368 100644 --- a/docs/api.md +++ b/docs/api.md @@ -40,7 +40,7 @@ Every component, object and type is exported from the package root. The | `searchHighlightColor` | `ColorValue` | `'rgba(255, 214, 0, 0.4)'` | background of search matches; use a translucent colour | | `searchCurrentHighlightColor` | `ColorValue` | `'rgba(255, 140, 0, 0.6)'` | background of the current match | | `onLoad` | `({ pageCount, firstPageWidth, firstPageHeight, filePath?, isStale, sources }) => void` | | first page's size in PDF points, as displayed (crop box, rotation applied); `filePath`: the local file being displayed (downloaded copy, local file or extracted asset; not for `{ base64 }`, `content://` or several sources); `isStale`: an outdated stored copy shown because the server could not be reached (see [Offline reading and caching](guides/offline-and-caching.md)); `sources`: each source's `{ pageCount, firstPage, filePath?, isStale }` | -| `onLoadProgress` | `({ loadedBytes, totalBytes? }) => void` | | remote downloads only (not when a stored copy is used); at most every 100 ms plus once on completion | +| `onLoadProgress` | `({ loadedBytes, totalBytes? }) => void` | | remote downloads (not when a stored copy is used) and, on Android, `content://` documents that are copied (see [Sources and loading](guides/sources-and-loading.md#content-uris-on-android)); at most every 100 ms plus once on completion | | `onPageChange` | `({ page, pageCount }) => void` | | zero-based | | `onError` | `(error: PdfLoadError \| PdfPageError) => void` | | without `error.page`: the load failed and nothing is shown; with `error.page` (Android only): a page could not be rendered after `onLoad`, and the document stays open; see [Errors](#errors) | | `onLinkPress` | `({ url }) => void` | | tap on a URL link; the viewer never opens URLs itself | diff --git a/docs/guides/sources-and-loading.md b/docs/guides/sources-and-loading.md index 8bf7d3a..9cb53fe 100644 --- a/docs/guides/sources-and-loading.md +++ b/docs/guides/sources-and-loading.md @@ -73,6 +73,22 @@ Both platforms follow redirects the same way: http to https on the same host is another origin too: when you send credentials, use the https URL. +## `content://` URIs on Android + +A `content://` URI (from the document picker, a share intent or another +app's provider) opens through the `ContentResolver`, with the read +permission your app was granted. Most providers (FileProvider, MediaStore, +the Downloads provider) hand out the file itself, which opens as it is. + +A document the provider streams instead (through a pipe, for example when +it generates or decrypts the document as it is read), or hands out as part +of a larger file, is first copied into the app's cache. Nothing is shown +until the whole document has arrived, and the copy takes as much disk space +as the document until the document closes or the app ends. +`onLoadProgress` reports the copy, with `totalBytes` when the provider +states the size, and changing the source stops it. `onLoad`'s `filePath` is +`undefined` for every `content://` source. + ## Base64 documents A document you have as a base64 string — made with pdfmake or pdf-lib, or diff --git a/docs/performance/audit.md b/docs/performance/audit.md index ff7dbf3..ba00866 100644 --- a/docs/performance/audit.md +++ b/docs/performance/audit.md @@ -88,7 +88,7 @@ through `BitmapPool`. iOS: PDFKit reuses its internal page views. | `getStats` | JS → native | COLD (diagnostic) | on demand | 10 numbers | | `onLoad`, `onError` | native → JS | COLD | per document | small struct | | `onPageChange` | native → JS | WARM | per page boundary crossed | 2 numbers | -| `onLoadProgress` | native → JS | WARM | ≤ 10/s during a remote download, 0 otherwise | 2 numbers | +| `onLoadProgress` | native → JS | WARM | ≤ 10/s during a remote download or an Android `content://` copy, plus one when it completes; 0 otherwise | 2 numbers | | `onLinkPress`, `onPageTap` | native → JS | COLD | per user tap | small struct | HOT crossings: **none**. No scroll, zoom, visibility or render-lifecycle data diff --git a/docs/troubleshooting.md b/docs/troubleshooting.md index 76028e0..408d59e 100644 --- a/docs/troubleshooting.md +++ b/docs/troubleshooting.md @@ -14,6 +14,7 @@ | A remote document fails with `http-error` 401 or 403 after a redirect | The server redirects to another origin (another host or port, or http to https), and `headers` are not sent there. Use the final URL, or one the target accepts without your headers, such as a signed URL. | | A signed URL or rotating token downloads the document on every open | Set `cacheKey` on the source to identify the stored copy. | | The document is a base64 string | Pass `source={{ base64 }}` (1.1+); for large documents write it to a file. See [Base64 documents](guides/sources-and-loading.md#base64-documents). | +| On Android, a `content://` document fails with `invalid-source` ("The PDF source is not seekable"), or with `file-not-found` and "Not a whole file" | The provider streams the document or hands out part of a file, which 1.1 and earlier cannot open. Later versions copy it first (see [`content://` URIs on Android](guides/sources-and-loading.md#content-uris-on-android)); on 1.1, copy it to a file and open that. | | A base64 source fails with `invalid-source` | `error.message` says why: a `data:` URI prefix to remove, URL-safe base64 (`-`, `_`) to convert, or another character that is not base64. | | Tapping a URL link does nothing | The viewer never opens URLs: open them in `onLinkPress`. | | `onPageTap` arrives about 300 ms after the tap | It waits for a possible double tap; `enableDoubleTapZoom={false}` reports taps at once. | diff --git a/example/documents.ts b/example/documents.ts index d44b4a1..a462206 100644 --- a/example/documents.ts +++ b/example/documents.ts @@ -1,4 +1,5 @@ import { Directory, File, Paths } from 'expo-file-system'; +import { Platform } from 'react-native'; import type { PdfBase64Source, PdfSource, @@ -93,6 +94,75 @@ async function base64Fixture(name: FixtureName): Promise { return { base64: await (await ensureFixture(name)).base64() }; } +/** How the example's Android provider hands a fixture out (modules/content-fixtures). */ +type ContentKind = + 'file' | 'pipe' | 'partial' | 'stalled' | 'broken' | 'slow-size'; + +/** A `content://` URI for a fixture, served by the example's own provider. */ +export async function contentFixture( + kind: ContentKind, + name: FixtureName, +): Promise { + await ensureFixture(name); + return { + uri: `content://software.gmi.betterpdf.example.fixtures/${kind}/${name}.pdf`, + }; +} + +const CONTENT_DOCUMENTS: DocumentEntry[] = [ + { + id: 'content-file', + title: 'Content file', + subtitle: 'content:// of outline, a seekable file', + resolve: () => contentFixture('file', 'outline'), + }, + { + id: 'content-pipe', + title: 'Content pipe', + subtitle: 'content:// of outline, streamed through a pipe', + resolve: () => contentFixture('pipe', 'outline'), + }, + { + id: 'content-partial', + title: 'Content partial', + subtitle: 'content:// of outline, part of a larger file', + resolve: () => contentFixture('partial', 'outline'), + }, + { + id: 'content-broken', + title: 'Content broken', + subtitle: 'A pipe whose provider fails halfway: expect invalid-pdf', + resolve: () => contentFixture('broken', 'outline'), + }, + { + id: 'content-stalled', + title: 'Content stalled', + subtitle: 'A pipe of outline-large that stops; Swap: single-page', + resolve: () => contentFixture('stalled', 'outline-large'), + alternate: () => contentFixture('pipe', 'single-page'), + }, + { + id: 'content-sources', + title: 'Content sources', + subtitle: 'links (pipe) + outline (partial) + mixed-sizes (file)', + resolve: async () => [ + await contentFixture('pipe', 'links'), + await contentFixture('partial', 'outline'), + await contentFixture('file', 'mixed-sizes'), + ], + }, + { + id: 'content-failing-sources', + title: 'Content failing sources', + subtitle: + 'outline (pipe, size query takes a minute) + outline-large (broken): expect invalid-pdf at once', + resolve: async () => [ + await contentFixture('slow-size', 'outline'), + await contentFixture('broken', 'outline-large'), + ], + }, +]; + export const DOCUMENTS: DocumentEntry[] = [ { id: 'bundled', @@ -340,4 +410,5 @@ export const DOCUMENTS: DocumentEntry[] = [ { uri: `${FIXTURE_SERVER}/does-not-exist.pdf` }, ], }, + ...(Platform.OS === 'android' ? CONTENT_DOCUMENTS : []), ]; diff --git a/example/e2e/pdf-document.yaml b/example/e2e/pdf-document.yaml index 92b01c2..1b0266c 100644 --- a/example/e2e/pdf-document.yaml +++ b/example/e2e/pdf-document.yaml @@ -6,7 +6,8 @@ # a wrong and with the right password; Android cannot read the metadata of # encrypted documents), rotated and cropped pages (`page-boxes`, as in # viewer-page-sizes), the outline fixture, the bundled asset and a file that -# is not a PDF. Screenshots show the cover and thumbnails. +# is not a PDF; on Android also a content:// pipe. Screenshots show the +# cover and thumbnails. appId: software.gmi.betterpdf.example --- - runFlow: _start.yaml @@ -46,6 +47,15 @@ appId: software.gmi.betterpdf.example - assertVisible: "sizes: 595x842 595x842" - assertVisible: "cover: 300x425" +- runFlow: + when: { platform: Android } + commands: + - tapOn: "metadata \\(content pipe\\)" + - extendedWaitUntil: { visible: "opened in .* ms", timeout: 15000 } + - assertVisible: "pages 2, stale no, file no" + - assertVisible: "title: Raport kwartalny — żółć" + - assertVisible: "cover: 300x425" + - tapOn: "metadata-encrypted" - extendedWaitUntil: { visible: "error: password-required", timeout: 15000 } - tapOn: "Wrong password" diff --git a/example/e2e/run.sh b/example/e2e/run.sh index e6f22db..b742e16 100755 --- a/example/e2e/run.sh +++ b/example/e2e/run.sh @@ -4,13 +4,14 @@ # Prerequisites: app installed, Metro running (debug) and # `node benchmarks/serve-fixtures.mjs` running (Android: adb reverse tcp:8090 tcp:8090). # The default flows need Android's newer renderer (Android 15, or 12–14 with -# SDK extension 13); without it, pass android-legacy/* and viewer-links. +# SDK extension 13); without it, pass android-legacy/*, viewer-links and +# viewer-content. set -uo pipefail cd "$(dirname "$0")" device="$1"; shift flows=("$@") if [ ${#flows[@]} -eq 0 ]; then - flows=(viewer-bundled viewer-scroll-zoom viewer-errors viewer-password viewer-layout-modes viewer-fit-policy viewer-single-page viewer-rtl viewer-outline viewer-scroll-enabled viewer-links viewer-double-tap viewer-scale-change viewer-initial viewer-text-selection viewer-accessibility viewer-search viewer-viewport viewer-page-sizes viewer-page-image viewer-offline viewer-short-document viewer-print viewer-spreads viewer-dark viewer-overlays viewer-forms viewer-inset viewer-inset-last-page viewer-base64 viewer-sources viewer-redirects pdf-document) + flows=(viewer-bundled viewer-scroll-zoom viewer-errors viewer-password viewer-layout-modes viewer-fit-policy viewer-single-page viewer-rtl viewer-outline viewer-scroll-enabled viewer-links viewer-double-tap viewer-scale-change viewer-initial viewer-text-selection viewer-accessibility viewer-search viewer-viewport viewer-page-sizes viewer-page-image viewer-offline viewer-short-document viewer-print viewer-spreads viewer-dark viewer-overlays viewer-forms viewer-inset viewer-inset-last-page viewer-base64 viewer-sources viewer-redirects viewer-content pdf-document) fi failed=0 is_android=0 diff --git a/example/e2e/viewer-content.yaml b/example/e2e/viewer-content.yaml new file mode 100644 index 0000000..d81bdf3 --- /dev/null +++ b/example/e2e/viewer-content.yaml @@ -0,0 +1,51 @@ +# content:// sources (Android only), from the example's provider +# (modules/content-fixtures): a seekable file, a pipe and part of a larger +# file all open; a pipe the provider breaks fails with invalid-pdf; a +# stalled pipe reports progress, and Swap replaces it; a broken source +# fails a set at once, although another's size query has not returned. +appId: software.gmi.betterpdf.example +--- +- runFlow: + when: { platform: Android } + commands: + - runFlow: _start.yaml + - tapOn: "Viewer" + - runFlow: { file: _open-list-entry.yaml, env: { ENTRY: "Content file" } } + - extendedWaitUntil: { visible: "1 / 12", timeout: 15000 } + - assertVisible: "file: none" + - tapOn: "‹ Back" + + - runFlow: { file: _open-list-entry.yaml, env: { ENTRY: "Content pipe" } } + - extendedWaitUntil: { visible: "1 / 12", timeout: 15000 } + - assertVisible: "file: none" + - tapOn: "Image" + - extendedWaitUntil: { visible: "image p1: 300x.*", timeout: 10000 } + - tapOn: "‹ Back" + + - runFlow: { file: _open-list-entry.yaml, env: { ENTRY: "Content partial" } } + - extendedWaitUntil: { visible: "1 / 12", timeout: 15000 } + - tapOn: "‹ Back" + + - runFlow: { file: _open-list-entry.yaml, env: { ENTRY: "Content broken" } } + - extendedWaitUntil: { visible: "invalid-pdf", timeout: 15000 } + - assertVisible: "The PDF could not be copied to the app's cache" + - tapOn: "‹ Back" + + # 64 KiB of 3.1 MB arrive, then nothing for a minute. + - runFlow: { file: _open-list-entry.yaml, env: { ENTRY: "Content stalled" } } + - extendedWaitUntil: { visible: "Downloading \\d+% \\(.* / 3\\.1 MB\\)", timeout: 15000 } + - tapOn: "Swap" + - extendedWaitUntil: { visible: "1 / 1", timeout: 15000 } + - assertVisible: "loads: 1" + - tapOn: "‹ Back" + + - runFlow: { file: _open-list-entry.yaml, env: { ENTRY: "Content sources" } } + - extendedWaitUntil: { visible: "1 / 20", timeout: 15000 } + - assertVisible: "sources: p1\\+3 p4\\+12 p16\\+5" + - tapOn: "‹ Back" + + # The broken source fails while the other waits a minute for its size. + - runFlow: { file: _open-list-entry.yaml, env: { ENTRY: "Content failing sources" } } + - extendedWaitUntil: { visible: "invalid-pdf", timeout: 15000 } + - assertVisible: "The PDF could not be copied to the app's cache" + - tapOn: "‹ Back" diff --git a/example/modules/content-fixtures/android/build.gradle b/example/modules/content-fixtures/android/build.gradle new file mode 100644 index 0000000..320176f --- /dev/null +++ b/example/modules/content-fixtures/android/build.gradle @@ -0,0 +1,15 @@ +plugins { + id 'com.android.library' + id 'expo-module-gradle-plugin' +} + +group = 'software.gmi.betterpdf.example' +version = '0.0.1' + +android { + namespace "software.gmi.betterpdf.example.content" + defaultConfig { + versionCode 1 + versionName "0.0.1" + } +} diff --git a/example/modules/content-fixtures/android/src/main/AndroidManifest.xml b/example/modules/content-fixtures/android/src/main/AndroidManifest.xml new file mode 100644 index 0000000..2e5112a --- /dev/null +++ b/example/modules/content-fixtures/android/src/main/AndroidManifest.xml @@ -0,0 +1,8 @@ + + + + + diff --git a/example/modules/content-fixtures/android/src/main/java/software/gmi/betterpdf/example/content/FixtureContentProvider.kt b/example/modules/content-fixtures/android/src/main/java/software/gmi/betterpdf/example/content/FixtureContentProvider.kt new file mode 100644 index 0000000..b566aee --- /dev/null +++ b/example/modules/content-fixtures/android/src/main/java/software/gmi/betterpdf/example/content/FixtureContentProvider.kt @@ -0,0 +1,145 @@ +package software.gmi.betterpdf.example.content + +import android.content.ContentProvider +import android.content.ContentValues +import android.content.res.AssetFileDescriptor +import android.database.Cursor +import android.database.MatrixCursor +import android.net.Uri +import android.os.CancellationSignal +import android.os.ParcelFileDescriptor +import android.provider.OpenableColumns +import android.util.Log +import java.io.File +import java.io.FileNotFoundException +import java.io.FileOutputStream +import java.io.IOException +import java.io.InputStream +import java.io.OutputStream +import kotlin.concurrent.thread + +/** + * Serves the fixtures `ensureFixture` copied to `files/fixtures/` the ways + * providers hand out documents, at `content://.fixtures//.pdf`: + * + * - `file`: the file itself, seekable, like FileProvider and MediaStore. + * - `pipe`: streamed through a pipe (`openPipeHelper`). + * - `partial`: a part of a larger file, as an asset at an offset. + * - `stalled`: a pipe that stops for a minute after its first 64 KiB. + * - `broken`: a pipe whose writer fails halfway (`closeWithError`). + * - `slow-size`: a pipe whose size query takes a minute, unless cancelled. + */ +class FixtureContentProvider : ContentProvider() { + override fun onCreate() = true + + override fun getType(uri: Uri) = "application/pdf" + + override fun query(uri: Uri, projection: Array?, selection: String?, selectionArgs: Array?, sortOrder: String?): Cursor { + val file = fixture(uri) + val columns = projection ?: arrayOf(OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE) + return MatrixCursor(columns).apply { + addRow( + columns.map { + when (it) { + OpenableColumns.DISPLAY_NAME -> file.name + OpenableColumns.SIZE -> file.length() + else -> null + } + }, + ) + } + } + + override fun query(uri: Uri, projection: Array?, selection: String?, selectionArgs: Array?, sortOrder: String?, cancellationSignal: CancellationSignal?): Cursor { + if (kind(uri) == "slow-size") { + repeat((STALL_MS / POLL_MS).toInt()) { + cancellationSignal?.throwIfCanceled() + Thread.sleep(POLL_MS) + } + } + return query(uri, projection, selection, selectionArgs, sortOrder) + } + + override fun openAssetFile(uri: Uri, mode: String): AssetFileDescriptor? { + if (kind(uri) != "partial") return super.openAssetFile(uri, mode) + val file = fixture(uri) + // More padding than the 1024 bytes PDFium searches for the header, so + // the document opens only when the offset is honoured. + val padded = File(context!!.cacheDir, "padded-${file.name}") + if (!padded.exists()) { + val temp = File.createTempFile("${padded.name}-", ".tmp", padded.parentFile) + try { + temp.outputStream().use { output -> + output.write(ByteArray(PADDING)) + file.inputStream().use { it.copyTo(output) } + output.write(ByteArray(PADDING)) + } + if (!temp.renameTo(padded)) throw FileNotFoundException("Could not rename ${temp.name} to ${padded.name}") + } finally { + // A no-op once the rename has moved it. + temp.delete() + } + } + return AssetFileDescriptor(ParcelFileDescriptor.open(padded, ParcelFileDescriptor.MODE_READ_ONLY), PADDING.toLong(), file.length()) + } + + override fun openFile(uri: Uri, mode: String): ParcelFileDescriptor { + val file = fixture(uri) + return when (kind(uri)) { + "file" -> ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY) + "pipe", "slow-size" -> pipe(uri, file) { input, output -> input.copyTo(output) } + "stalled" -> pipe(uri, file) { input, output -> + val head = ByteArray(STALL_AFTER) + output.write(head, 0, input.read(head)) + Thread.sleep(STALL_MS) + input.copyTo(output) + } + "broken" -> { + val (reader, writer) = ParcelFileDescriptor.createReliablePipe() + thread { + val bytes = file.readBytes() + try { + FileOutputStream(writer.fileDescriptor).write(bytes, 0, bytes.size / 2) + writer.closeWithError("The fixture provider failed") + } catch (e: IOException) { + Log.i(TAG, "Stopped writing ${file.name}: ${e.message}") + } + } + reader + } + else -> throw FileNotFoundException("Unknown kind in $uri") + } + } + + private fun pipe(uri: Uri, file: File, write: (InputStream, OutputStream) -> Unit): ParcelFileDescriptor = + openPipeHelper(uri, getType(uri), null, file) { output, _, _, _, _ -> + try { + file.inputStream().use { input -> FileOutputStream(output.fileDescriptor).use { write(input, it) } } + } catch (e: IOException) { + // The reader closed its end. + Log.i(TAG, "Stopped writing ${file.name}: ${e.message}") + } + } + + private fun kind(uri: Uri): String? = uri.pathSegments.firstOrNull() + + private fun fixture(uri: Uri): File { + val file = File(context!!.filesDir, "fixtures/${uri.lastPathSegment}") + if (!file.isFile) throw FileNotFoundException("No fixture for $uri") + return file + } + + override fun insert(uri: Uri, values: ContentValues?): Uri? = null + + override fun update(uri: Uri, values: ContentValues?, selection: String?, selectionArgs: Array?) = 0 + + override fun delete(uri: Uri, selection: String?, selectionArgs: Array?) = 0 + + private companion object { + const val TAG = "FixtureContentProvider" + const val PADDING = 4096 + const val STALL_AFTER = 64 * 1024 + const val STALL_MS = 60_000L + const val POLL_MS = 100L + } +} diff --git a/example/modules/content-fixtures/expo-module.config.json b/example/modules/content-fixtures/expo-module.config.json new file mode 100644 index 0000000..339d64e --- /dev/null +++ b/example/modules/content-fixtures/expo-module.config.json @@ -0,0 +1,3 @@ +{ + "platforms": ["android"] +} diff --git a/example/screens/DocumentsScreen.tsx b/example/screens/DocumentsScreen.tsx index b4535ae..d309da1 100644 --- a/example/screens/DocumentsScreen.tsx +++ b/example/screens/DocumentsScreen.tsx @@ -1,5 +1,12 @@ import { useEffect, useRef, useState } from 'react'; -import { Image, ScrollView, StyleSheet, Text, View } from 'react-native'; +import { + Image, + Platform, + ScrollView, + StyleSheet, + Text, + View, +} from 'react-native'; import { PdfDocument, type PdfDocumentMetadata, @@ -8,7 +15,12 @@ import { type PdfSource, } from 'react-native-better-pdf'; import { describeError } from '../describeError'; -import { ensureFixture, FIXTURE_SERVER, type FixtureName } from '../documents'; +import { + contentFixture, + ensureFixture, + FIXTURE_SERVER, + type FixtureName, +} from '../documents'; import { Button, colors } from '../ui'; interface Target { @@ -39,6 +51,14 @@ const TARGETS: Target[] = [ base64: await (await ensureFixture('metadata')).base64(), }), }, + ...(Platform.OS === 'android' + ? [ + { + title: 'metadata (content pipe)', + resolve: () => contentFixture('pipe', 'metadata'), + }, + ] + : []), fixture('metadata-encrypted'), fixture('page-boxes'), fixture('outline'), diff --git a/package/android/src/main/java/com/margelo/nitro/betterpdf/InputStream+copyWithProgress.kt b/package/android/src/main/java/com/margelo/nitro/betterpdf/InputStream+copyWithProgress.kt new file mode 100644 index 0000000..21b7644 --- /dev/null +++ b/package/android/src/main/java/com/margelo/nitro/betterpdf/InputStream+copyWithProgress.kt @@ -0,0 +1,31 @@ +package com.margelo.nitro.betterpdf + +import java.io.InputStream +import java.io.OutputStream + +/** + * Copies the rest of this stream to [output] in chunks, reporting the bytes + * copied after each and once more on completion; a total smaller than that + * is reported as unknown. Throws [InterruptedException] once [isCancelled]. + */ +internal fun InputStream.copyWithProgress( + output: OutputStream, + totalBytes: Long?, + isCancelled: () -> Boolean, + onProgress: (loadedBytes: Long, totalBytes: Long?, isComplete: Boolean) -> Unit, +) { + val buffer = ByteArray(COPY_CHUNK_BYTES) + var loaded = 0L + fun report(isComplete: Boolean) = onProgress(loaded, totalBytes?.takeIf { it >= loaded }, isComplete) + while (true) { + if (isCancelled()) throw InterruptedException() + val count = read(buffer) + if (count < 0) break + output.write(buffer, 0, count) + loaded += count + report(isComplete = false) + } + report(isComplete = true) +} + +private const val COPY_CHUNK_BYTES = 64 * 1024 diff --git a/package/android/src/main/java/com/margelo/nitro/betterpdf/PdfSourceResolver.kt b/package/android/src/main/java/com/margelo/nitro/betterpdf/PdfSourceResolver.kt index f03c5ca..3cfca77 100644 --- a/package/android/src/main/java/com/margelo/nitro/betterpdf/PdfSourceResolver.kt +++ b/package/android/src/main/java/com/margelo/nitro/betterpdf/PdfSourceResolver.kt @@ -1,8 +1,15 @@ package com.margelo.nitro.betterpdf import android.content.Context +import android.content.res.AssetFileDescriptor import android.net.Uri +import android.os.CancellationSignal +import android.os.OperationCanceledException import android.os.ParcelFileDescriptor +import android.provider.OpenableColumns +import android.system.ErrnoException +import android.system.Os +import android.system.OsConstants import com.margelo.nitro.core.ArrayBuffer import java.io.File import java.io.FileNotFoundException @@ -25,17 +32,20 @@ class PdfSourceResolver(private val context: Context) { @Volatile private var cancelled = false private val remoteDone = CountDownLatch(1) + private val contentCancellation = CancellationSignal() - /** Aborts an in-flight download; [open] then throws [InterruptedException]. Safe from any thread. */ + /** Aborts an in-flight download or copy; [open] then throws [InterruptedException]. Safe from any thread. */ fun cancel() { cancelled = true subscription?.cancel() remoteDone.countDown() + contentCancellation.cancel() } /** * Opens [source]. For remote sources that are downloaded (not cached), - * [onProgress] receives every chunk on a download thread; callers throttle. + * [onProgress] receives every chunk on a download thread, and for + * `content://` sources that are copied, on the calling thread; callers throttle. */ fun open( source: NativePdfSource, @@ -52,7 +62,7 @@ class PdfSourceResolver(private val context: Context) { return when (parsed.scheme?.lowercase()) { "file" -> openFile(File(parsed.path ?: throw invalidSource(uri))) "http", "https" -> openRemote(source, onProgress) - "content" -> ResolvedSource(openContent(parsed), file = null) + "content" -> openContent(parsed, onProgress) // Release builds resolve `require('./x.pdf')` to a bare resource name. null -> openRawResource(uri) else -> throw invalidSource(uri) @@ -70,42 +80,98 @@ class PdfSourceResolver(private val context: Context) { } } - private fun openContent(uri: Uri): ParcelFileDescriptor { - try { - return context.contentResolver.openFileDescriptor(uri, "r") + /** The provider's descriptor when the renderer can use it; a pipe, or part of a file, is copied. */ + private fun openContent( + uri: Uri, + onProgress: (loadedBytes: Long, totalBytes: Long?, isComplete: Boolean) -> Unit, + ): ResolvedSource { + val asset = try { + context.contentResolver.openAssetFileDescriptor(uri, "r", contentCancellation) ?: throw PdfFailure(PdfErrorCode.FILE_NOT_FOUND, "No content at $uri") } catch (e: FileNotFoundException) { throw PdfFailure(PdfErrorCode.FILE_NOT_FOUND, "No content at $uri", cause = e) } catch (e: SecurityException) { throw PdfFailure(PdfErrorCode.FILE_NOT_FOUND, "No permission to read $uri", cause = e) + } catch (_: OperationCanceledException) { + throw InterruptedException() + } + val descriptor = asset.parcelFileDescriptor + if (asset.declaredLength == AssetFileDescriptor.UNKNOWN_LENGTH && descriptor.isSeekable()) { + return ResolvedSource(descriptor, file = null) + } + // Closing it also ends a read blocked on a stalled pipe. + contentCancellation.setOnCancelListener(descriptor::closeQuietly) + try { + val totalBytes = if (asset.declaredLength >= 0) asset.declaredLength else statedSize(uri) + return openTemporaryCopy { file -> + file.outputStream().use { output -> + asset.createInputStream().use { it.copyWithProgress(output, totalBytes, { cancelled }, onProgress) } + } + } + } finally { + contentCancellation.setOnCancelListener(null) + descriptor.closeQuietly() } } - /** Like `content://`, the session needs only the descriptor, so the file is deleted at once. */ + private fun ParcelFileDescriptor.isSeekable(): Boolean = + try { + Os.lseek(fileDescriptor, 0, OsConstants.SEEK_CUR) + true + } catch (_: ErrnoException) { + false + } + + private fun statedSize(uri: Uri): Long? = + try { + context.contentResolver.query(uri, arrayOf(OpenableColumns.SIZE), null, null, null, contentCancellation)?.use { cursor -> + val column = cursor.getColumnIndex(OpenableColumns.SIZE) + if (column >= 0 && cursor.moveToFirst() && !cursor.isNull(column)) cursor.getLong(column) else null + } + } catch (_: OperationCanceledException) { + throw InterruptedException() + } catch (_: RuntimeException) { + null + } + private fun openBase64(text: ArrayBuffer): ResolvedSource { if (!text.isOwner) throw IllegalStateException("The base64 text is not in a native buffer") - val file = File(base64Directory(), "${UUID.randomUUID()}.pdf") - try { + return openTemporaryCopy { file -> val mode = ParcelFileDescriptor.MODE_WRITE_ONLY or ParcelFileDescriptor.MODE_CREATE or ParcelFileDescriptor.MODE_TRUNCATE ParcelFileDescriptor.open(file, mode).use { output -> NativeBase64Document.writeDecoded(text.getBuffer(false), output.fd)?.let { throw PdfFailure(PdfErrorCode.INVALID_SOURCE, it) } } + } + } + + /** + * Opens the file [write] fills, and deletes it at once: the session needs + * only the descriptor, so nothing stays on disk. + */ + private fun openTemporaryCopy(write: (File) -> Unit): ResolvedSource { + val file = File(openDirectory(), "${UUID.randomUUID()}.pdf") + try { + write(file) if (cancelled) throw InterruptedException() return ResolvedSource(ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY), file = null) + } catch (e: IOException) { + // Cancelling closes what a copy reads from. + if (cancelled) throw InterruptedException() + throw PdfFailure(PdfErrorCode.INVALID_PDF, "The PDF could not be copied to the app's cache", cause = e) } finally { file.delete() } } /** Emptied once per process, for files left by a crash before deletion. */ - private fun base64Directory(): File { + private fun openDirectory(): File { val directory = File(cacheDirectory(), "open") synchronized(Companion) { - if (!isBase64DirectorySwept) { + if (!isOpenDirectorySwept) { directory.listFiles()?.forEach { it.delete() } - isBase64DirectorySwept = true + isOpenDirectorySwept = true } } directory.mkdirs() @@ -181,6 +247,6 @@ class PdfSourceResolver(private val context: Context) { PdfFailure(PdfErrorCode.INVALID_SOURCE, "Unsupported PDF source: \"$uri\"") private companion object { - var isBase64DirectorySwept = false + var isOpenDirectorySwept = false } } diff --git a/package/android/src/main/java/com/margelo/nitro/betterpdf/SharedDownload.kt b/package/android/src/main/java/com/margelo/nitro/betterpdf/SharedDownload.kt index 2b636ef..13d968b 100644 --- a/package/android/src/main/java/com/margelo/nitro/betterpdf/SharedDownload.kt +++ b/package/android/src/main/java/com/margelo/nitro/betterpdf/SharedDownload.kt @@ -84,17 +84,9 @@ internal class SharedDownload( val totalBytes = connection.contentLengthLong.takeIf { it >= 0 } connection.inputStream.use { input -> partial.outputStream().use { output -> - val buffer = ByteArray(BUFFER_BYTES) - var loadedBytes = 0L - while (true) { - if (cancelled) throw InterruptedException() - val read = input.read(buffer) - if (read < 0) break - output.write(buffer, 0, read) - loadedBytes += read - onProgress(this, loadedBytes, totalBytes?.takeIf { it >= loadedBytes }, false) + input.copyWithProgress(output, totalBytes, { cancelled }) { loadedBytes, total, isComplete -> + onProgress(this, loadedBytes, total, isComplete) } - onProgress(this, loadedBytes, totalBytes?.takeIf { it >= loadedBytes }, true) } } val metadata = CacheMetadata(clock(), connection.getHeaderField("ETag"), connection.getHeaderField("Last-Modified")) @@ -168,7 +160,6 @@ internal class SharedDownload( } private companion object { - const val BUFFER_BYTES = 64 * 1024 const val TIMEOUT_MS = 30_000 /** Shorter, so a dead network shows the existing copy soon. */ diff --git a/package/android/src/test/java/com/margelo/nitro/betterpdf/InputStreamCopyWithProgressTest.kt b/package/android/src/test/java/com/margelo/nitro/betterpdf/InputStreamCopyWithProgressTest.kt new file mode 100644 index 0000000..32020f0 --- /dev/null +++ b/package/android/src/test/java/com/margelo/nitro/betterpdf/InputStreamCopyWithProgressTest.kt @@ -0,0 +1,50 @@ +package com.margelo.nitro.betterpdf + +import org.junit.Assert.assertArrayEquals +import org.junit.Assert.assertEquals +import org.junit.Assert.assertThrows +import org.junit.Test +import java.io.ByteArrayInputStream +import java.io.ByteArrayOutputStream + +class InputStreamCopyWithProgressTest { + private val bytes = ByteArray(200_000) { it.toByte() } + private val progress = mutableListOf>() + private val output = ByteArrayOutputStream() + + @Test + fun `copies every byte, reporting each chunk and then completion`() { + ByteArrayInputStream(bytes).copyWithProgress(output, bytes.size.toLong(), { false }, ::record) + + assertArrayEquals(bytes, output.toByteArray()) + assertEquals( + listOf(65_536L, 131_072L, 196_608L, 200_000L).map { Triple(it, 200_000L, false) } + Triple(200_000L, 200_000L, true), + progress, + ) + } + + @Test + fun `a total smaller than the bytes copied is reported as unknown`() { + ByteArrayInputStream(bytes).copyWithProgress(output, 100_000, { false }, ::record) + + assertEquals(listOf(100_000L, null, null, null, null), progress.map { it.second }) + } + + @Test + fun `cancelling stops the copy before the next chunk`() { + var cancelled = false + val input = object : ByteArrayInputStream(bytes) { + override fun read(b: ByteArray, off: Int, len: Int): Int = super.read(b, off, len).also { cancelled = true } + } + + assertThrows(InterruptedException::class.java) { + input.copyWithProgress(output, null, { cancelled }, ::record) + } + assertEquals(65_536, output.size()) + assertEquals(listOf(Triple(65_536L, null, false)), progress) + } + + private fun record(loadedBytes: Long, totalBytes: Long?, isComplete: Boolean) { + progress += Triple(loadedBytes, totalBytes, isComplete) + } +} diff --git a/package/src/PdfView.tsx b/package/src/PdfView.tsx index 7608cc3..87b0e2d 100644 --- a/package/src/PdfView.tsx +++ b/package/src/PdfView.tsx @@ -410,10 +410,11 @@ export interface PdfViewProps extends ViewProps { searchCurrentHighlightColor?: ColorValue; onLoad?: (event: PdfLoadEvent) => void; /** - * Download progress of a remote document, at most every 100 ms plus once on - * completion (before {@linkcode PdfViewProps.onLoad}). Not called for local - * files, or when a downloaded copy is used or confirmed unchanged by the - * server (see `PdfUriSource.cache` and `maxAgeMs`). + * Download progress of a remote document (on Android also the copy of a + * `content://` document its provider streams), at most every 100 ms plus + * once on completion (before {@linkcode PdfViewProps.onLoad}). Not called + * for local files, or when a downloaded copy is used or confirmed + * unchanged by the server (see `PdfUriSource.cache` and `maxAgeMs`). */ onLoadProgress?: (event: PdfLoadProgressEvent) => void; /** diff --git a/package/src/types/PdfLoadProgressEvent.ts b/package/src/types/PdfLoadProgressEvent.ts index f80096e..b617fad 100644 --- a/package/src/types/PdfLoadProgressEvent.ts +++ b/package/src/types/PdfLoadProgressEvent.ts @@ -1,11 +1,12 @@ /** * Delivered to `PdfView`'s `onLoadProgress` while a remote document is being - * downloaded. + * downloaded, or on Android while a `content://` document its provider + * streams is copied. * - * Emitted at most every 100 ms, plus once when the download completes (before - * `onLoad`). Local files, and downloaded copies that are used or confirmed - * unchanged by the server, load without progress events. Also delivered to - * `PdfCache.download()`'s `onProgress`. + * Emitted at most every 100 ms, plus once when the download or copy + * completes (before `onLoad`). Local files, and downloaded copies that are + * used or confirmed unchanged by the server, load without progress events. + * Also delivered to `PdfCache.download()`'s `onProgress`. * * @example * ```tsx @@ -15,12 +16,13 @@ * ``` */ export interface PdfLoadProgressEvent { - /** Bytes downloaded so far. */ + /** Bytes downloaded or copied so far. */ loadedBytes: number; /** - * Size of the download in bytes, when the server reports it. `undefined` - * when unknown, for example without a `Content-Length` header or when the - * response is transparently decompressed. + * Size of the document in bytes, when the server (or the provider) + * reports it. `undefined` when unknown, for example without a + * `Content-Length` header or when the response is transparently + * decompressed. */ totalBytes?: number; } diff --git a/package/src/types/PdfOpenOptions.ts b/package/src/types/PdfOpenOptions.ts index 873af4d..7828d01 100644 --- a/package/src/types/PdfOpenOptions.ts +++ b/package/src/types/PdfOpenOptions.ts @@ -6,8 +6,9 @@ export interface PdfOpenOptions { /** Password of a protected document, unless the source has its own. */ password?: string; /** - * Download progress of a remote document, at most every 100 ms plus once - * on completion. Not called for local files and stored copies. + * Download progress of a remote document (on Android also the copy of a + * `content://` document its provider streams), at most every 100 ms plus + * once on completion. Not called for local files and stored copies. */ onProgress?: (event: PdfLoadProgressEvent) => void; /**