Skip to content

Commit

Permalink
Remove kConstraintError and kDOMException from MediaErrorState
Browse files Browse the repository at this point in the history
This CL removes DOMException and ConstraintError from media_error_state.h as they had no usage. It also renames
ThrowTypeError to MarkTypeError as the name is more appropriate for what
it does.

This CL also starts recording results using SPRwT for MediaErrorState
exceptions.

Bug: 1373398
Change-Id: Idd7019807422cd961ad2d3f7be122f6221d454a3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4130274
Reviewed-by: Guido Urdaneta <guidou@chromium.org>
Commit-Queue: Palak Agarwal <agpalak@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1098496}
  • Loading branch information
palak8669 authored and Chromium LUCI CQ committed Jan 30, 2023
1 parent ff8497c commit aee8540
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,6 @@ const char kDAEchoCancellation[] = "googDAEchoCancellation";
// Google-specific constraint keys for a local video source (getUserMedia).
const char kNoiseReduction[] = "googNoiseReduction";

// Names used for testing.
const char kTestConstraint1[] = "valid_and_supported_1";
const char kTestConstraint2[] = "valid_and_supported_2";

static bool ParseMandatoryConstraintsDictionary(
const Dictionary& mandatory_constraints_dictionary,
Vector<NameValueStringConstraint>& mandatory) {
Expand Down Expand Up @@ -179,8 +175,7 @@ static bool ToBoolean(const WebString& as_web_string) {
static void ParseOldStyleNames(
ExecutionContext* context,
const Vector<NameValueStringConstraint>& old_names,
MediaTrackConstraintSetPlatform& result,
MediaErrorState& error_state) {
MediaTrackConstraintSetPlatform& result) {
if (old_names.size() > 0) {
UseCounter::Count(context, WebFeature::kOldConstraintsParsed);
}
Expand Down Expand Up @@ -240,14 +235,6 @@ static void ParseOldStyleNames(
result.goog_da_echo_cancellation.SetExact(ToBoolean(constraint.value_));
} else if (constraint.name_.Equals(kNoiseReduction)) {
result.goog_noise_reduction.SetExact(ToBoolean(constraint.value_));
} else if (constraint.name_.Equals(kTestConstraint1) ||
constraint.name_.Equals(kTestConstraint2)) {
// These constraints are only for testing parsing.
// Values 0 and 1 are legal, all others are a ConstraintError.
if (!constraint.value_.Equals("0") && !constraint.value_.Equals("1")) {
error_state.ThrowConstraintError("Illegal value for constraint",
constraint.name_);
}
}
// else: Nothing. Unrecognized constraints are simply ignored.
}
Expand All @@ -256,22 +243,17 @@ static void ParseOldStyleNames(
static MediaConstraints CreateFromNamedConstraints(
ExecutionContext* context,
Vector<NameValueStringConstraint>& mandatory,
const Vector<NameValueStringConstraint>& optional,
MediaErrorState& error_state) {
const Vector<NameValueStringConstraint>& optional) {
MediaTrackConstraintSetPlatform basic;
MediaTrackConstraintSetPlatform advanced;
MediaConstraints constraints;
ParseOldStyleNames(context, mandatory, basic, error_state);
if (error_state.HadException())
return constraints;
// We ignore unknow names and syntax errors in optional constraints.
MediaErrorState ignored_error_state;
ParseOldStyleNames(context, mandatory, basic);
// We ignore unknown names and syntax errors in optional constraints.
Vector<MediaTrackConstraintSetPlatform> advanced_vector;
for (const auto& optional_constraint : optional) {
MediaTrackConstraintSetPlatform advanced_element;
Vector<NameValueStringConstraint> element_as_list(1, optional_constraint);
ParseOldStyleNames(context, element_as_list, advanced_element,
ignored_error_state);
ParseOldStyleNames(context, element_as_list, advanced_element);
if (!advanced_element.IsUnconstrained())
advanced_vector.push_back(advanced_element);
}
Expand Down Expand Up @@ -367,7 +349,7 @@ bool ValidateString(const String& str, MediaErrorState& error_state) {
DCHECK(!error_state.HadException());

if (str.length() > kMaxConstraintStringLength) {
error_state.ThrowTypeError("Constraint string too long.");
error_state.MarkTypeError("Constraint string too long.");
return false;
}
return true;
Expand All @@ -378,7 +360,7 @@ bool ValidateStringSeq(const Vector<String>& strs,
DCHECK(!error_state.HadException());

if (strs.size() > kMaxConstraintStringSeqLength) {
error_state.ThrowTypeError("Constraint string sequence too long.");
error_state.MarkTypeError("Constraint string sequence too long.");
return false;
}

Expand Down Expand Up @@ -699,20 +681,19 @@ MediaConstraints Create(ExecutionContext* context,
if (constraints_in->hasOptional() || constraints_in->hasMandatory()) {
if (!standard_form.IsUnconstrained()) {
UseCounter::Count(context, WebFeature::kMediaStreamConstraintsOldAndNew);
error_state.ThrowTypeError(
error_state.MarkTypeError(
"Malformed constraint: Cannot use both optional/mandatory and "
"specific or advanced constraints.");
return MediaConstraints();
}
Vector<NameValueStringConstraint> optional;
Vector<NameValueStringConstraint> mandatory;
if (!Parse(constraints_in, optional, mandatory)) {
error_state.ThrowTypeError("Malformed constraints object.");
error_state.MarkTypeError("Malformed constraints object.");
return MediaConstraints();
}
UseCounter::Count(context, WebFeature::kMediaStreamConstraintsNameValue);
return CreateFromNamedConstraints(context, mandatory, optional,
error_state);
return CreateFromNamedConstraints(context, mandatory, optional);
}
UseCounter::Count(context, WebFeature::kMediaStreamConstraintsConformant);
return standard_form;
Expand Down
13 changes: 4 additions & 9 deletions third_party/blink/renderer/modules/mediastream/media_devices.cc
Original file line number Diff line number Diff line change
Expand Up @@ -435,18 +435,13 @@ ScriptPromise MediaDevices::SendUserMediaRequest(
callbacks, error_state, surface);
if (!request) {
DCHECK(error_state.HadException());
if (error_state.CanGenerateException()) {
// TODO(crbug.com/1373398): Change this to use
// ScriptPromiseResolverWithTracker.
error_state.RaiseException(exception_state);
return ScriptPromise();
}
resolver->RecordAndThrowTypeError(
exception_state, error_state.GetErrorMessage(),
UserMediaRequestResult::kInvalidConstraints);
RecordIdentifiabilityMetric(
surface, GetExecutionContext(),
IdentifiabilityBenignStringToken(error_state.GetErrorMessage()));
resolver->Reject(error_state.CreateError(),
UserMediaRequestResult::kInvalidConstraints);
return promise;
return ScriptPromise();
}

String error_message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,35 +30,18 @@

#include "third_party/blink/renderer/modules/mediastream/media_error_state.h"

#include "third_party/blink/renderer/bindings/modules/v8/v8_union_domexception_overconstrainederror.h"
#include "third_party/blink/renderer/modules/mediastream/overconstrained_error.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"

namespace blink {

MediaErrorState::MediaErrorState()
: error_type_(kNoError), code_(DOMExceptionCode::kNoError) {}
MediaErrorState::MediaErrorState() = default;

void MediaErrorState::ThrowTypeError(const String& message) {
void MediaErrorState::MarkTypeError(const String& message) {
error_type_ = kTypeError;
message_ = message;
}

void MediaErrorState::ThrowDOMException(DOMExceptionCode code,
const String& message) {
error_type_ = kDOMException;
code_ = code;
message_ = message;
}

void MediaErrorState::ThrowConstraintError(const String& message,
const String& constraint) {
error_type_ = kConstraintError;
message_ = message;
constraint_ = constraint;
}

void MediaErrorState::Reset() {
error_type_ = kNoError;
}
Expand All @@ -67,61 +50,14 @@ bool MediaErrorState::HadException() {
return error_type_ != kNoError;
}

bool MediaErrorState::CanGenerateException() {
return error_type_ == kTypeError || error_type_ == kDOMException;
}

void MediaErrorState::RaiseException(ExceptionState& target) {
switch (error_type_) {
case kNoError:
NOTREACHED();
break;
case kTypeError:
target.ThrowTypeError(message_);
break;
case kDOMException:
target.ThrowDOMException(code_, message_);
break;
case kConstraintError:
// This is for the cases where we can't pass back a
// NavigatorUserMediaError.
// So far, we have this in the constructor of RTCPeerConnection,
// which is due to be deprecated.
// TODO(hta): Remove this code. https://crbug.com/576581
target.ThrowDOMException(DOMExceptionCode::kNotSupportedError,
"Unsatisfiable constraint " + constraint_);
break;
default:
NOTREACHED();
}
void MediaErrorState::Throw(ExceptionState& target) {
DCHECK_EQ(error_type_, kTypeError);
target.ThrowTypeError(message_);
}

String MediaErrorState::GetErrorMessage() {
switch (error_type_) {
case kNoError:
NOTREACHED();
break;
case kTypeError:
case kDOMException:
return message_;
case kConstraintError:
// This is for the cases where we can't pass back a
// NavigatorUserMediaError.
// So far, we have this in the constructor of RTCPeerConnection,
// which is due to be deprecated.
// TODO(hta): Remove this code. https://crbug.com/576581
return "Unsatisfiable constraint " + constraint_;
default:
NOTREACHED();
}

return String();
}

V8UnionDOMExceptionOrOverconstrainedError* MediaErrorState::CreateError() {
DCHECK_EQ(error_type_, kConstraintError);
return MakeGarbageCollected<V8UnionDOMExceptionOrOverconstrainedError>(
MakeGarbageCollected<OverconstrainedError>(constraint_, message_));
DCHECK_EQ(error_type_, kTypeError);
return message_;
}

} // namespace blink
17 changes: 4 additions & 13 deletions third_party/blink/renderer/modules/mediastream/media_error_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,12 @@
#define THIRD_PARTY_BLINK_RENDERER_MODULES_MEDIASTREAM_MEDIA_ERROR_STATE_H_

#include "third_party/blink/renderer/modules/modules_export.h"
#include "third_party/blink/renderer/platform/bindings/exception_code.h"
#include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"

namespace blink {

class ExceptionState;
class OverconstrainedError;
class V8UnionDOMExceptionOrOverconstrainedError;

// A class that is able to be used like ExceptionState for carrying
// information about an error up the stack, but it is up to the higher
Expand All @@ -50,24 +47,18 @@ class MODULES_EXPORT MediaErrorState {

public:
MediaErrorState();
void ThrowTypeError(const String& message);
void ThrowDOMException(DOMExceptionCode, const String& message);
void ThrowConstraintError(const String& message, const String& constraint);
void MarkTypeError(const String& message);
void Reset();

bool HadException();
bool CanGenerateException();
void RaiseException(ExceptionState&);
void Throw(ExceptionState&);
String GetErrorMessage();
V8UnionDOMExceptionOrOverconstrainedError* CreateError();

private:
enum ErrorType { kNoError, kTypeError, kDOMException, kConstraintError };
ErrorType error_type_;
enum ErrorType { kNoError, kTypeError };
ErrorType error_type_ = kNoError;
String name_;
DOMExceptionCode code_;
String message_;
String constraint_;
};

} // namespace blink
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,7 @@ void NavigatorMediaStream::getUserMedia(
error_state, surface);
if (!request) {
DCHECK(error_state.HadException());
if (error_state.CanGenerateException()) {
error_state.RaiseException(exception_state);
} else {
error_callback->InvokeAndReportException(nullptr,
error_state.CreateError());
}
error_state.Throw(exception_state);
RecordIdentifiabilityMetric(
surface, navigator.GetExecutionContext(),
IdentifiabilityBenignStringToken(error_state.GetErrorMessage()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,14 +427,14 @@ UserMediaRequest* UserMediaRequest::Create(

if (media_type == UserMediaRequestType::kUserMedia) {
if (audio.IsNull() && video.IsNull()) {
error_state.ThrowTypeError(
error_state.MarkTypeError(
"At least one of audio and video must be requested");
return nullptr;
} else if (!video.IsNull()) {
if (video.Basic().pan.HasMandatory() ||
video.Basic().tilt.HasMandatory() ||
video.Basic().zoom.HasMandatory()) {
error_state.ThrowTypeError(
error_state.MarkTypeError(
"Mandatory pan-tilt-zoom constraints are not supported");
return nullptr;
}
Expand All @@ -456,33 +456,33 @@ UserMediaRequest* UserMediaRequest::Create(
// either a dictionary value or a value of true.
if (media_type == UserMediaRequestType::kDisplayMediaSet) {
if (!audio.IsNull()) {
error_state.ThrowTypeError("Audio requests are not supported");
error_state.MarkTypeError("Audio requests are not supported");
return nullptr;
} else if (options->preferCurrentTab()) {
error_state.ThrowTypeError("preferCurrentTab is not supported");
error_state.MarkTypeError("preferCurrentTab is not supported");
return nullptr;
}
}

if (video.IsNull()) {
error_state.ThrowTypeError("video must be requested");
error_state.MarkTypeError("video must be requested");
return nullptr;
}

if ((!audio.IsNull() && !audio.Advanced().empty()) ||
!video.Advanced().empty()) {
error_state.ThrowTypeError("Advanced constraints are not supported");
error_state.MarkTypeError("Advanced constraints are not supported");
return nullptr;
}

if ((!audio.IsNull() && audio.Basic().HasMin()) || video.Basic().HasMin()) {
error_state.ThrowTypeError("min constraints are not supported");
error_state.MarkTypeError("min constraints are not supported");
return nullptr;
}

if ((!audio.IsNull() && audio.Basic().HasExact()) ||
video.Basic().HasExact()) {
error_state.ThrowTypeError("exact constraints are not supported");
error_state.MarkTypeError("exact constraints are not supported");
return nullptr;
}

Expand Down Expand Up @@ -524,7 +524,7 @@ UserMediaRequest* UserMediaRequest::Create(
options->selfBrowserSurface().AsEnum() ==
V8SelfCapturePreferenceEnum::Enum::kExclude;
if (exclude_self_browser_surface && options->preferCurrentTab()) {
error_state.ThrowTypeError(
error_state.MarkTypeError(
"Self-contradictory configuration (preferCurrentTab and "
"selfBrowserSurface=exclude).");
return nullptr;
Expand Down

0 comments on commit aee8540

Please sign in to comment.