[camera_web] Fix TypeError when reading the torch capability - #12647
Open
0xharkirat wants to merge 1 commit into
Open
[camera_web] Fix TypeError when reading the torch capability#126470xharkirat wants to merge 1 commit into
0xharkirat wants to merge 1 commit into
Conversation
Browsers do not report the `torch` capability in the shape the Image Capture specification describes. The specification declares `sequence<boolean> torch`, and `package:web` is generated from it, but both Chromium and WebKit declare a bare `boolean torch` in their IDL. `NonStandardFieldsOnMediaTrackCapabilities.torchNullable` mirrored the specification, so reading it on a camera that has a flash threw before the torch could ever be applied. It surfaced differently per compiler: DDC threw a `TypeError`, dart2js a `NoSuchMethodError`, and dart2wasm an `ArgumentError`. Both `setFlashMode` and `takePicture` reach it. Reads the capability as `JSAny?` and adds `canEnableTorch`, which accepts either shape and reports false for anything else, so a browser that reports something unexpected raises `torchModeNotSupported` rather than crashing. Reading a sequence now also answers with `any` rather than `first`, which was both wrong for `[false, true]` and threw a `StateError` on an empty sequence. Fixes flutter/flutter#191384
Contributor
There was a problem hiding this comment.
Code Review
This pull request updates the camera_web package to handle different browser implementations of the torch capability. In pkg_web_tweaks.dart, torchNullable is changed from JSArray<JSBoolean>? to JSAny? to accommodate both bare booleans and boolean sequences. A new canEnableTorch getter is added to parse these types, and integration tests are introduced to verify the capability parsing across various formats. No review comments were provided, so there is no feedback to address.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What broke
setFlashModeandtakePicturethrew on any camera that has a flash.The Image Capture specification declares
sequence<boolean> torchinMediaTrackCapabilities.package:webis generated from that specification, sotorchNullablewas typedJSArray<JSBoolean>?.No browser ships that shape. Chromium and WebKit both declare a bare
boolean torch.Reading the property threw before the torch could be applied. The error depends on
the compiler:
TypeError: true: type 'bool' is not a subtype of type 'JSArray<Object?>?'NoSuchMethodError: method not found: 'gq'ArgumentError: Invalid argument(s)The same line held two more defects:
.firstgave the wrong answer for[false, true]. A specification-shaped sequencelists the values the source accepts, so that means the torch can be turned on.
.firstthrewStateErroron an empty sequence.The fix
Read
torchasJSAny?and addcanEnableTorch, which accepts either shape andreturns false for anything else.
A sequence is answered with
anyinstead offirst, and is only trusted when everyelement is a boolean. Anything unrecognised raises
torchModeNotSupportedinstead ofcrashing, and prints a debug-only warning so the shape can be reported.
Test matrix
truefalse[false, true][true][false][]'yes'['yes']Nine cases are covered by integration tests. Six of them fail without this change.
Devices tested
All nine shapes were run through the real plugin code on each of these. All passed.
On the Moto G56 the rear camera reports
torch: trueas a bare boolean, and the flashnow turns on. The front camera does not report the key at all and raises
torchModeNotSupportedas expected.What was added
canEnableTorchonNonStandardFieldsOnMediaTrackCapabilities, which reads either shape.debugPrintwarning for a shape that is neither, so an unknown browser canbe reported rather than failing quietly.
0.3.5+6.What was removed
JSArray<JSBoolean>?type ontorchNullable, which no browser matches.?.toDart.first.toDart ?? falsechain in_setTorchMode.Issues fixed by this PR
Fixes flutter/flutter#191384
Pre-Review Checklist
[shared_preferences]///).🤖 Generated with Claude Code