contentauth / c2pa-js

JavaScript SDK for displaying and validating C2PA data
https://opensource.contentauthenticity.org
MIT License
74 stars 40 forks source link

C2pa in Safari extension problem #152

Open Kaven01 opened 8 months ago

Kaven01 commented 8 months ago

Hello, I am trying to create Safari extension on Mac using c2pa-js library. I have created a minimal test extension, which I paste here. It works as it is in Chrome on Windows - as a content script, when loading any page it just shows a manifest of test image in console.

But then I create a multiplatform Safari extension in XCode, replace its original Resource contents with this extension and run. Just minimal setup. Run it, enable in safari, and reload any page. I am getting the following error in console:

[Debug] c2pa:workers Fetched worker from safari-web-extension://E3BAC506-B5D9-474E-B99B-6452024AE624/scripts/thirdparty/c2pa/c2pa.worker.min.js (18594 bytes) +0ms (c2pa.esm.min.js, line 9) [Debug] c2pa:wasm Fetching WASM binary from url safari-web-extension://E3BAC506-B5D9-474E-B99B-6452024AE624/scripts/thirdparty/c2pa/toolkit_bg.wasm +0ms – {expectedIntegrity: "sha512-vdvTW4Xzi3QAhgk4nGIc/FFSrRAuQgI1G6eKq8hcgOEqL2LfvRUrX85nPKlyEPzM8fFwtRtzRs78QqBl4iFMCw=="} (c2pa.esm.min.js, line 9) [Debug] c2pa:wasm Sending WASM binary buffer to worker for compilation +33ms – {size: 3672108} (c2pa.esm.min.js, line 9) [Error] TypeError: null is not an object (evaluating 'e.data.type') (anonymous function) (c2pa.esm.min.js:18:163475)

C2pa object is not created - during "WASM compilation" in createC2pa() something receives null data in message and script crashes. It never gets to the point where it would extract manifest from image.

Am I doing something wrong, or is there any other problem?

Mac OS Sonoma 14.4.1

SafariTest.zip

Kaven01 commented 7 months ago

After some testing ... it seems that in Safari extension, Worker cannot pass compiled wasm Module as a part of data in outgoing postMessage(). If you try to do it, no error is displayed, but resulting message has null data. Which is problem when using worker for compileWasm operation.

A simple workaround (with unfortunate neccessity to change sourcode) is to find function fetchWasm() in c2pa.esm.js, and change the line const wasm = await pool.compileWasm(buffer); to const wasm = WebAssembly.compile(buffer);

Which makes the compiles directly, not through Worker. Not exactly ideal solution, but it works.

dkozma commented 6 months ago

@Kaven01 good to know - we are working on a V2 API for this package and will keep this in mind.