#include "third_party/blink/renderer/modules/imagecapture/image_capture.h"
#include <algorithm>
#include <utility>
#include "base/containers/contains.h"
#include "base/functional/callback_helpers.h"
#include "base/time/time.h"
#include "base/trace_event/trace_event.h"
#include "base/types/strong_alias.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "third_party/blink/public/mojom/permissions/permission_status.mojom-blink.h"
#include "third_party/blink/public/platform/browser_interface_broker_proxy.h"
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/renderer/bindings/core/v8/callback_promise_adapter.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise_resolver.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_union_string_stringsequence.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_constrain_boolean_parameters.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_constrain_dom_string_parameters.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_constrain_double_range.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_constrain_point_2d_parameters.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_fill_light_mode.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_media_settings_range.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_media_track_capabilities.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_media_track_constraints.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_media_track_settings.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_photo_capabilities.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_photo_settings.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_point_2d.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_union_boolean_constrainbooleanparameters.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_union_boolean_constraindoublerange_double.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_union_constraindomstringparameters_string_stringsequence.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_union_constraindoublerange_double.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_union_constrainpoint2dparameters_point2dsequence.h"
#include "third_party/blink/renderer/core/dom/dom_exception.h"
#include "third_party/blink/renderer/core/fileapi/blob.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/core/imagebitmap/image_bitmap.h"
#include "third_party/blink/renderer/modules/imagecapture/image_capture_frame_grabber.h"
#include "third_party/blink/renderer/modules/mediastream/media_stream_track.h"
#include "third_party/blink/renderer/modules/mediastream/media_stream_video_track.h"
#include "third_party/blink/renderer/modules/mediastream/overconstrained_error.h"
#include "third_party/blink/renderer/modules/permissions/permission_utils.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/heap/persistent.h"
#include "third_party/blink/renderer/platform/instrumentation/use_counter.h"
#include "third_party/blink/renderer/platform/mediastream/media_stream_component.h"
#include "third_party/blink/renderer/platform/wtf/functional.h"
namespace blink {
enum class ImageCapture::MediaTrackConstraintSetType { … };
namespace {
BackgroundBlurMode;
EyeGazeCorrectionMode;
FillLightMode;
MeteringMode;
RedEyeReduction;
MediaTrackConstraintSetType;
const char kNoServiceError[] = …;
const char kInvalidStateTrackError[] = …;
class AllConstraintSets { … };
CopyPanTiltZoom;
template <typename T>
void CopyCommonMembers(const T* source,
T* destination,
CopyPanTiltZoom copy_pan_tilt_zoom) { … }
void CopyCapabilities(const MediaTrackCapabilities* source,
MediaTrackCapabilities* destination,
CopyPanTiltZoom copy_pan_tilt_zoom) { … }
void CopyConstraintSet(const MediaTrackConstraintSet* source,
MediaTrackConstraintSet* destination) { … }
void CopyConstraints(const MediaTrackConstraints* source,
MediaTrackConstraints* destination) { … }
void CopySettings(const MediaTrackSettings* source,
MediaTrackSettings* destination,
CopyPanTiltZoom copy_pan_tilt_zoom) { … }
MediaSettingsRange* DuplicateRange(const MediaSettingsRange* range) { … }
enum class ConstraintType { … };
bool IsEmptySequence(bool ) { … }
bool IsEmptySequence(const HeapVector<Member<Point2D>>& constraint) { … }
bool IsEmptySequence(const V8UnionStringOrStringSequence* constraint) { … }
template <typename Constraint>
ConstraintType GetConstraintType(const Constraint* constraint) { … }
ConstraintType GetConstraintType(const ConstrainDoubleRange* constraint) { … }
ConstraintType GetConstraintType(
const V8UnionBooleanOrConstrainBooleanParameters* constraint) { … }
ConstraintType GetConstraintType(
const V8UnionBooleanOrConstrainDoubleRangeOrDouble* constraint) { … }
ConstraintType GetConstraintType(
const V8UnionConstrainDOMStringParametersOrStringOrStringSequence*
constraint) { … }
ConstraintType GetConstraintType(
const V8UnionConstrainDoubleRangeOrDouble* constraint) { … }
ConstraintType GetConstraintType(
const V8UnionConstrainPoint2DParametersOrPoint2DSequence* constraint) { … }
MediaTrackConstraintSetType GetMediaTrackConstraintSetType(
const MediaTrackConstraintSet* constraint_set,
const MediaTrackConstraints* constraints) { … }
bool IsBareValueToBeTreatedAsExact(
MediaTrackConstraintSetType constraint_set_type) { … }
bool IsBooleanFalseConstraint(
V8UnionBooleanOrConstrainDoubleRangeOrDouble* constraint) { … }
bool IsValueConstraintType(ConstraintType constraint_type,
MediaTrackConstraintSetType constraint_set_type) { … }
template <typename Constraint>
bool IsValueConstraint(const Constraint* constraint,
MediaTrackConstraintSetType constraint_set_type) { … }
bool MayRejectWithOverconstrainedError(
MediaTrackConstraintSetType constraint_set_type) { … }
bool TrackIsInactive(const MediaStreamTrack& track) { … }
BackgroundBlurMode ParseBackgroundBlur(bool blink_mode) { … }
EyeGazeCorrectionMode ParseEyeGazeCorrection(bool blink_mode) { … }
MeteringMode ParseFaceFraming(bool blink_mode) { … }
MeteringMode ParseMeteringMode(const String& blink_mode) { … }
FillLightMode ParseFillLightMode(const String& blink_mode) { … }
bool ToBooleanMode(BackgroundBlurMode mode) { … }
bool ToBooleanMode(EyeGazeCorrectionMode mode) { … }
WebString ToString(MeteringMode value) { … }
V8FillLightMode ToV8FillLightMode(FillLightMode value) { … }
WebString ToString(RedEyeReduction value) { … }
MediaSettingsRange* ToMediaSettingsRange(
const media::mojom::blink::Range& range) { … }
bool CheckExactValueConstraint(
const HeapVector<Member<Point2D>>* effective_setting,
const HeapVector<Member<Point2D>>& exact_constraint) { … }
bool CheckExactValueConstraint(const MediaSettingsRange* effective_capability,
double exact_constraint) { … }
bool CheckExactValueConstraint(const Vector<String>& effective_capability,
const String& exact_constraint) { … }
bool CheckExactValueConstraint(const Vector<String>& effective_capability,
const Vector<String>& exact_constraints) { … }
CapabilityExists;
bool CheckIfCapabilityExistenceSatisfiesConstraintType(
ConstraintType constraint_type,
CapabilityExists capability_exists,
MediaTrackConstraintSetType constraint_set_type) { … }
template <typename Constraint>
bool CheckIfCapabilityExistenceSatisfiesConstraint(
const Constraint* constraint,
CapabilityExists capability_exists,
MediaTrackConstraintSetType constraint_set_type) { … }
bool CheckValueConstraint(
const HeapVector<Member<Point2D>>* effective_setting,
const V8UnionConstrainPoint2DParametersOrPoint2DSequence* constraint,
MediaTrackConstraintSetType constraint_set_type) { … }
bool CheckValueConstraint(const MediaSettingsRange* effective_capability,
const V8UnionConstrainDoubleRangeOrDouble* constraint,
MediaTrackConstraintSetType constraint_set_type) { … }
bool CheckValueConstraint(
const MediaSettingsRange* effective_capability,
const V8UnionBooleanOrConstrainDoubleRangeOrDouble* constraint,
MediaTrackConstraintSetType constraint_set_type) { … }
bool CheckValueConstraint(
const Vector<bool>& effective_capability,
const V8UnionBooleanOrConstrainBooleanParameters* constraint,
MediaTrackConstraintSetType constraint_set_type) { … }
bool CheckValueConstraint(
const Vector<String>& effective_capability,
const V8UnionConstrainDOMStringParametersOrStringOrStringSequence*
constraint,
MediaTrackConstraintSetType constraint_set_type) { … }
Vector<bool> ApplyExactValueConstraint(bool* has_setting_ptr,
bool* setting_ptr,
const Vector<bool>& effective_capability,
bool exact_constraint) { … }
MediaSettingsRange* ApplyExactValueConstraint(
bool* has_setting_ptr,
double* setting_ptr,
const MediaSettingsRange* effective_capability,
double exact_constraint) { … }
Vector<String> ApplyExactValueConstraint(
bool* has_setting_ptr,
MeteringMode* setting_ptr,
const Vector<String>& effective_capability,
const String& exact_constraint) { … }
Vector<String> ApplyExactValueConstraint(
bool* has_setting_ptr,
MeteringMode* setting_ptr,
const Vector<String>& effective_capability,
const Vector<String>& exact_constraints) { … }
Vector<bool> ApplyIdealValueConstraint(bool* has_setting_ptr,
bool* setting_ptr,
const Vector<bool>& effective_capability,
bool ideal_constraint) { … }
MediaSettingsRange* ApplyIdealValueConstraint(
bool* has_setting_ptr,
double* setting_ptr,
MediaSettingsRange* effective_capability,
std::optional<double> ideal_constraint,
double current_setting) { … }
Vector<String> ApplyIdealValueConstraint(
bool* has_setting_ptr,
MeteringMode* setting_ptr,
const Vector<String>& effective_capability,
const String& ideal_constraint,
const String& current_setting) { … }
Vector<String> ApplyIdealValueConstraint(
bool* has_setting_ptr,
MeteringMode* setting_ptr,
const Vector<String>& effective_capability,
const Vector<String>& ideal_constraints,
const String& current_setting) { … }
Vector<bool> ApplyValueConstraint(
bool* has_setting_ptr,
bool* setting_ptr,
const Vector<bool>& effective_capability,
const V8UnionBooleanOrConstrainBooleanParameters* constraint,
MediaTrackConstraintSetType constraint_set_type) { … }
MediaSettingsRange* ApplyValueConstraint(
bool* has_setting_ptr,
double* setting_ptr,
const MediaSettingsRange* effective_capability,
const V8UnionConstrainDoubleRangeOrDouble* constraint,
MediaTrackConstraintSetType constraint_set_type,
double current_setting) { … }
MediaSettingsRange* ApplyValueConstraint(
bool* has_setting_ptr,
double* setting_ptr,
const MediaSettingsRange* effective_capability,
const V8UnionBooleanOrConstrainDoubleRangeOrDouble* constraint,
MediaTrackConstraintSetType constraint_set_type,
double current_setting) { … }
Vector<String> ApplyValueConstraint(
bool* has_setting_ptr,
MeteringMode* setting_ptr,
const Vector<String>& effective_capability,
const V8UnionConstrainDOMStringParametersOrStringOrStringSequence*
constraint,
MediaTrackConstraintSetType constraint_set_type,
const String& current_setting) { … }
void ApplyValueConstraint(bool* has_setting_ptr,
Vector<media::mojom::blink::Point2DPtr>* setting_ptr,
const HeapVector<Member<Point2D>>* effective_setting,
const HeapVector<Member<Point2D>>& constraint) { … }
std::optional<HeapVector<Member<Point2D>>> ApplyValueConstraint(
bool* has_setting_ptr,
Vector<media::mojom::blink::Point2DPtr>* setting_ptr,
const HeapVector<Member<Point2D>>* effective_setting,
const V8UnionConstrainPoint2DParametersOrPoint2DSequence* constraint,
MediaTrackConstraintSetType constraint_set_type) { … }
void MaybeSetBackgroundBlurSetting(bool value,
const Vector<bool>& capability,
bool& has_setting,
BackgroundBlurMode& setting) { … }
void MaybeSetBoolSetting(bool value,
const Vector<bool>& capability,
std::optional<bool>& setting) { … }
void MaybeSetBoolSetting(bool value,
const Vector<bool>& capability,
bool& has_setting,
bool& setting) { … }
void MaybeSetEyeGazeCorrectionSetting(
bool value,
const Vector<bool>& capability,
std::optional<EyeGazeCorrectionMode>& setting) { … }
void MaybeSetFaceFramingSetting(bool value,
const Vector<bool>& capability,
bool& has_setting,
MeteringMode& setting) { … }
void MaybeSetDoubleSetting(double value,
const MediaSettingsRange& capability,
bool& has_setting,
double& setting) { … }
}
ImageCapture* ImageCapture::Create(ExecutionContext* context,
MediaStreamTrack* track,
ExceptionState& exception_state) { … }
ImageCapture::~ImageCapture() { … }
void ImageCapture::ContextDestroyed() { … }
ScriptPromise<PhotoCapabilities> ImageCapture::getPhotoCapabilities(
ScriptState* script_state) { … }
ScriptPromise<PhotoSettings> ImageCapture::getPhotoSettings(
ScriptState* script_state) { … }
ScriptPromise<Blob> ImageCapture::takePhoto(
ScriptState* script_state,
const PhotoSettings* photo_settings) { … }
ScriptPromise<ImageBitmap> ImageCapture::grabFrame(ScriptState* script_state) { … }
void ImageCapture::UpdateAndCheckMediaTrackSettingsAndCapabilities(
base::OnceCallback<void(bool)> callback) { … }
void ImageCapture::GotPhotoState(
base::OnceCallback<void(bool)> callback,
media::mojom::blink::PhotoStatePtr photo_state) { … }
bool ImageCapture::CheckAndApplyMediaTrackConstraintsToSettings(
media::mojom::blink::PhotoSettings* settings,
const MediaTrackConstraints* constraints,
ScriptPromiseResolverBase* resolver) const { … }
void ImageCapture::GetMediaTrackCapabilities(
MediaTrackCapabilities* capabilities) const { … }
void ImageCapture::SetMediaTrackConstraints(
ScriptPromiseResolverBase* resolver,
const MediaTrackConstraints* constraints) { … }
void ImageCapture::SetVideoTrackDeviceSettingsFromTrack(
base::OnceClosure initialized_callback,
media::mojom::blink::PhotoStatePtr photo_state) { … }
void ImageCapture::OnSetVideoTrackDeviceSettingsFromTrack(
base::OnceClosure done_callback,
bool result) { … }
MediaTrackConstraints* ImageCapture::GetMediaTrackConstraints() const { … }
void ImageCapture::ClearMediaTrackConstraints() { … }
void ImageCapture::GetMediaTrackSettings(MediaTrackSettings* settings) const { … }
ImageCapture::ImageCapture(ExecutionContext* context,
MediaStreamTrack* track,
bool pan_tilt_zoom_allowed,
base::OnceClosure initialized_callback,
base::TimeDelta grab_frame_timeout)
: … { … }
void ImageCapture::ApplyMediaTrackConstraintSetToSettings(
media::mojom::blink::PhotoSettings* settings,
MediaTrackCapabilities* effective_capabilities,
MediaTrackSettings* effective_settings,
const MediaTrackConstraintSet* constraint_set,
MediaTrackConstraintSetType constraint_set_type) const { … }
bool ImageCapture::CheckMediaTrackConstraintSet(
const MediaTrackCapabilities* effective_capabilities,
const MediaTrackSettings* effective_settings,
const MediaTrackConstraintSet* constraint_set,
MediaTrackConstraintSetType constraint_set_type,
ScriptPromiseResolverBase* resolver) const { … }
void ImageCapture::OnPermissionStatusChange(
mojom::blink::PermissionStatus status) { … }
bool ImageCapture::HasPanTiltZoomPermissionGranted() const { … }
void ImageCapture::GetMojoPhotoState(ScriptPromiseResolverBase* resolver,
PromiseResolverFunction resolver_cb) { … }
void ImageCapture::OnMojoGetPhotoState(
ScriptPromiseResolverBase* resolver,
PromiseResolverFunction resolve_function,
bool trigger_take_photo,
media::mojom::blink::PhotoStatePtr photo_state) { … }
void ImageCapture::OnMojoSetPhotoOptions(ScriptPromiseResolverBase* resolver,
bool trigger_take_photo,
bool result) { … }
void ImageCapture::OnMojoTakePhoto(ScriptPromiseResolverBase* resolver,
media::mojom::blink::BlobPtr blob) { … }
void ImageCapture::UpdateMediaTrackSettingsAndCapabilities(
base::OnceClosure initialized_callback,
media::mojom::blink::PhotoStatePtr photo_state) { … }
void ImageCapture::OnServiceConnectionError() { … }
void ImageCapture::MaybeRejectWithOverconstrainedError(
ScriptPromiseResolverBase* resolver,
const char* constraint,
const char* message) const { … }
void ImageCapture::ResolveWithNothing(ScriptPromiseResolverBase* resolver) { … }
void ImageCapture::ResolveWithPhotoSettings(
ScriptPromiseResolverBase* resolver) { … }
void ImageCapture::ResolveWithPhotoCapabilities(
ScriptPromiseResolverBase* resolver) { … }
bool ImageCapture::IsPageVisible() const { … }
const String& ImageCapture::SourceId() const { … }
const std::optional<const char*>
ImageCapture::GetConstraintWithCapabilityExistenceMismatch(
const MediaTrackConstraintSet* constraint_set,
MediaTrackConstraintSetType constraint_set_type) const { … }
ImageCapture* ImageCapture::Clone() const { … }
void ImageCapture::Trace(Visitor* visitor) const { … }
}