-
Notifications
You must be signed in to change notification settings - Fork 134
feat: adds a progress/cancel feature to Context for the c2pa-rs sdk #1927
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+1,696
−92
Merged
Changes from 3 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
20b7883
Progress callback API and cancellation
gpeacock 5ff7ae7
feat: api update
gpeacock 24a4771
Merge branch 'main' into gpeacock/progress_cancel
gpeacock 44d5485
add ProgressPhase::Writing
gpeacock e65f5ea
Updates progress/cancel documentation and adds unit tests
gpeacock 36a8894
Merge branch 'main' into gpeacock/progress_cancel
gpeacock 0e689dd
Add granular progress callbacks to hashing and verification paths
gpeacock 1fab325
chore: clippy fix
gpeacock 94684b6
Merge branch 'main' into gpeacock/progress_cancel
gpeacock 59e9879
adds progress.rs example
gpeacock fb64aa5
Merge branch 'main' into gpeacock/progress_cancel
gpeacock aee3a03
chore: fmt/clippy /wasm for progress example
gpeacock 2983876
chore: review feedback, fix ordering on cancel
gpeacock dc5b7ca
Merge branch 'main' into gpeacock/progress_cancel
gpeacock 897abee
Merge branch 'main' into gpeacock/progress_cancel
gpeacock ce47091
Update docs/progress_callbacks.md
gpeacock File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| # Progress Callback API | ||
|
|
||
| ## Overview | ||
|
|
||
| The SDK reports progress during long-running operations (signing, reading, verification) via an optional callback on `Context`. The callback receives the current phase and a completion fraction. Returning `false` (or `0` in C) requests cancellation; the operation stops at the next safe point and returns `Error::OperationCancelled`. | ||
|
|
||
| ## Rust API | ||
|
|
||
| **Types:** | ||
| - `ProgressPhase` – enum of phases: `Ingredients`, `Thumbnail`, `Hashing`, `Signing`, `Embedding`, `Verification`, `RemoteFetch` (`#[repr(u8)]` for FFI) | ||
| - `ProgressCallbackFunc` – `dyn Fn(ProgressPhase, f32) -> bool` (closure type) | ||
|
|
||
| **Context methods:** | ||
| - `with_progress_callback(callback)` – set the callback (builder pattern) | ||
| - `set_progress_callback(callback)` – set the callback (mutable) | ||
| - `cancel()` – request cancellation from any thread | ||
| - `is_cancelled()` – check if cancellation was requested | ||
|
|
||
| **Callback signature:** `(phase: ProgressPhase, pct: f32) -> bool` | ||
| - `phase` – current phase (caller maps to localized text) | ||
| - `pct` – fraction complete in range 0.0–1.0 (e.g. 0.75 = 75%) | ||
| - Return `true` to continue, `false` to cancel | ||
|
|
||
| **Example:** | ||
| ```rust | ||
| let ctx = Context::new() | ||
| .with_progress_callback(|phase, pct| { | ||
| println!("{:?} {:.0}%", phase, pct * 100.0); | ||
| true // return false to cancel | ||
| }); | ||
|
|
||
| let ctx = Arc::new(ctx); | ||
| // From another thread: ctx.cancel(); | ||
| ``` | ||
|
|
||
| ## C FFI | ||
|
|
||
| - `c2pa_context_builder_set_progress_callback(builder, user_data, callback)` – attach callback and user data | ||
| - `c2pa_context_cancel(ctx)` – request cancellation | ||
|
|
||
| **C callback:** `int (*)(void* context, uint8_t phase, float pct)` | ||
| - Return non-zero to continue, zero to cancel | ||
| - `phase` matches `ProgressPhase` discriminants (0–6) | ||
| - `pct` is 0.0–1.0 | ||
|
|
||
| ## When It Fires | ||
gpeacock marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Callbacks fire at phase boundaries during Builder and Reader operations (e.g. after ingredients, thumbnail, hashing, signing, embedding). There are no callbacks inside long I/O loops yet; finer-grained progress would require additional implementation work. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.