Commit eddfb152 authored by Christopher Cameron's avatar Christopher Cameron Committed by Chromium LUCI CQ

ImageData: Clean up ValidateConstructorArguments

Pass all creation arguments by pointer instead of also passing in
a sometimes-redundant parameters bitfield.

TBR=fserb

Bug: 1160105
Change-Id: I7d631c9fd98b7f6a65118cc95e41148c3223848f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2597929Reviewed-by: default avatarccameron <ccameron@chromium.org>
Commit-Queue: ccameron <ccameron@chromium.org>
Cr-Commit-Position: refs/heads/master@{#838548}
parent 9c9135d6
...@@ -53,37 +53,36 @@ bool RaiseDOMExceptionAndReturnFalse(ExceptionState* exception_state, ...@@ -53,37 +53,36 @@ bool RaiseDOMExceptionAndReturnFalse(ExceptionState* exception_state,
} // namespace } // namespace
bool ImageData::ValidateConstructorArguments( bool ImageData::ValidateConstructorArguments(
const unsigned& param_flags,
const IntSize* size, const IntSize* size,
const unsigned& width, const unsigned* width,
const unsigned& height, const unsigned* height,
const NotShared<DOMArrayBufferView> data, const NotShared<DOMArrayBufferView>* data,
const ImageDataSettings* settings, const ImageDataSettings* settings,
ExceptionState* exception_state) { ExceptionState* exception_state) {
// We accept all the combinations of colorSpace and storageFormat in an // We accept all the combinations of colorSpace and storageFormat in an
// ImageDataSettings to be stored in an ImageData. Therefore, we don't // ImageDataSettings to be stored in an ImageData. Therefore, we don't
// check the color settings in this function. // check the color settings in this function.
if ((param_flags & kParamWidth) && !width) { if (width && !*width) {
return RaiseDOMExceptionAndReturnFalse( return RaiseDOMExceptionAndReturnFalse(
exception_state, DOMExceptionCode::kIndexSizeError, exception_state, DOMExceptionCode::kIndexSizeError,
"The source width is zero or not a number."); "The source width is zero or not a number.");
} }
if ((param_flags & kParamHeight) && !height) { if (height && !*height) {
return RaiseDOMExceptionAndReturnFalse( return RaiseDOMExceptionAndReturnFalse(
exception_state, DOMExceptionCode::kIndexSizeError, exception_state, DOMExceptionCode::kIndexSizeError,
"The source height is zero or not a number."); "The source height is zero or not a number.");
} }
if (param_flags & (kParamWidth | kParamHeight)) { if (width || height) {
base::CheckedNumeric<unsigned> data_size = base::CheckedNumeric<unsigned> data_size =
StorageFormatBytesPerPixel(kUint8ClampedArrayStorageFormatName); StorageFormatBytesPerPixel(kUint8ClampedArrayStorageFormatName);
if (settings) { if (settings) {
data_size = StorageFormatBytesPerPixel(settings->storageFormat()); data_size = StorageFormatBytesPerPixel(settings->storageFormat());
} }
data_size *= width; data_size *= width ? *width : 0;
data_size *= height; data_size *= height ? *height : 0;
if (!data_size.IsValid()) { if (!data_size.IsValid()) {
return RaiseDOMExceptionAndReturnFalse( return RaiseDOMExceptionAndReturnFalse(
exception_state, DOMExceptionCode::kIndexSizeError, exception_state, DOMExceptionCode::kIndexSizeError,
...@@ -100,11 +99,11 @@ bool ImageData::ValidateConstructorArguments( ...@@ -100,11 +99,11 @@ bool ImageData::ValidateConstructorArguments(
} }
unsigned data_length = 0; unsigned data_length = 0;
if (param_flags & kParamData) { if (data) {
DCHECK(data); DCHECK(data);
if (data->GetType() != DOMArrayBufferView::ViewType::kTypeUint8Clamped && if ((*data)->GetType() != DOMArrayBufferView::ViewType::kTypeUint8Clamped &&
data->GetType() != DOMArrayBufferView::ViewType::kTypeUint16 && (*data)->GetType() != DOMArrayBufferView::ViewType::kTypeUint16 &&
data->GetType() != DOMArrayBufferView::ViewType::kTypeFloat32) { (*data)->GetType() != DOMArrayBufferView::ViewType::kTypeFloat32) {
return RaiseDOMExceptionAndReturnFalse( return RaiseDOMExceptionAndReturnFalse(
exception_state, DOMExceptionCode::kNotSupportedError, exception_state, DOMExceptionCode::kNotSupportedError,
"The input data type is not supported."); "The input data type is not supported.");
...@@ -115,7 +114,7 @@ bool ImageData::ValidateConstructorArguments( ...@@ -115,7 +114,7 @@ bool ImageData::ValidateConstructorArguments(
std::numeric_limits<uint32_t>::max(), std::numeric_limits<uint32_t>::max(),
"We use UINT32_MAX as the upper bound of the input size and expect " "We use UINT32_MAX as the upper bound of the input size and expect "
"that the result fits into an `unsigned`."); "that the result fits into an `unsigned`.");
if (!base::CheckedNumeric<uint32_t>(data->byteLength()) if (!base::CheckedNumeric<uint32_t>((*data)->byteLength())
.AssignIfValid(&data_length)) { .AssignIfValid(&data_length)) {
return RaiseDOMExceptionAndReturnFalse( return RaiseDOMExceptionAndReturnFalse(
exception_state, DOMExceptionCode::kNotSupportedError, exception_state, DOMExceptionCode::kNotSupportedError,
...@@ -126,27 +125,26 @@ bool ImageData::ValidateConstructorArguments( ...@@ -126,27 +125,26 @@ bool ImageData::ValidateConstructorArguments(
exception_state, DOMExceptionCode::kInvalidStateError, exception_state, DOMExceptionCode::kInvalidStateError,
"The input data has zero elements."); "The input data has zero elements.");
} }
data_length /= data->TypeSize(); data_length /= (*data)->TypeSize();
if (data_length % 4) { if (data_length % 4) {
return RaiseDOMExceptionAndReturnFalse( return RaiseDOMExceptionAndReturnFalse(
exception_state, DOMExceptionCode::kInvalidStateError, exception_state, DOMExceptionCode::kInvalidStateError,
"The input data length is not a multiple of 4."); "The input data length is not a multiple of 4.");
} }
if ((param_flags & kParamWidth) && (data_length / 4) % width) { if (width && (data_length / 4) % *width) {
return RaiseDOMExceptionAndReturnFalse( return RaiseDOMExceptionAndReturnFalse(
exception_state, DOMExceptionCode::kIndexSizeError, exception_state, DOMExceptionCode::kIndexSizeError,
"The input data length is not a multiple of (4 * width)."); "The input data length is not a multiple of (4 * width).");
} }
if ((param_flags & kParamWidth) && (param_flags & kParamHeight) && if (width && height && *height != data_length / (4 * *width))
height != data_length / (4 * width))
return RaiseDOMExceptionAndReturnFalse( return RaiseDOMExceptionAndReturnFalse(
exception_state, DOMExceptionCode::kIndexSizeError, exception_state, DOMExceptionCode::kIndexSizeError,
"The input data length is not equal to (4 * width * height)."); "The input data length is not equal to (4 * width * height).");
} }
if (param_flags & kParamSize) { if (size) {
if (size->Width() <= 0 || size->Height() <= 0) if (size->Width() <= 0 || size->Height() <= 0)
return false; return false;
base::CheckedNumeric<unsigned> data_size = 4; base::CheckedNumeric<unsigned> data_size = 4;
...@@ -155,7 +153,7 @@ bool ImageData::ValidateConstructorArguments( ...@@ -155,7 +153,7 @@ bool ImageData::ValidateConstructorArguments(
if (!data_size.IsValid() || if (!data_size.IsValid() ||
data_size.ValueOrDie() > v8::TypedArray::kMaxLength) data_size.ValueOrDie() > v8::TypedArray::kMaxLength)
return false; return false;
if (param_flags & kParamData) { if (data) {
if (data_size.ValueOrDie() > data_length) if (data_size.ValueOrDie() > data_length)
return false; return false;
} }
...@@ -203,8 +201,7 @@ NotShared<DOMArrayBufferView> ImageData::AllocateAndValidateDataArray( ...@@ -203,8 +201,7 @@ NotShared<DOMArrayBufferView> ImageData::AllocateAndValidateDataArray(
ImageData* ImageData::Create(const IntSize& size, ImageData* ImageData::Create(const IntSize& size,
const ImageDataSettings* settings) { const ImageDataSettings* settings) {
if (!ValidateConstructorArguments(kParamSize, &size, 0, 0, if (!ValidateConstructorArguments(&size, nullptr, nullptr, nullptr, settings))
NotShared<DOMArrayBufferView>(), settings))
return nullptr; return nullptr;
ImageDataStorageFormat storage_format = kUint8ClampedArrayStorageFormat; ImageDataStorageFormat storage_format = kUint8ClampedArrayStorageFormat;
if (settings) { if (settings) {
...@@ -254,8 +251,9 @@ ImageData* ImageData::Create(const IntSize& size, ...@@ -254,8 +251,9 @@ ImageData* ImageData::Create(const IntSize& size,
ImageData* ImageData::Create(const IntSize& size, ImageData* ImageData::Create(const IntSize& size,
NotShared<DOMArrayBufferView> data_array, NotShared<DOMArrayBufferView> data_array,
const ImageDataSettings* settings) { const ImageDataSettings* settings) {
if (!ValidateConstructorArguments(kParamSize | kParamData, &size, 0, 0, NotShared<DOMArrayBufferView> buffer_view = data_array;
data_array, settings)) if (!ValidateConstructorArguments(&size, nullptr, nullptr, &buffer_view,
settings))
return nullptr; return nullptr;
return MakeGarbageCollected<ImageData>(size, data_array, settings); return MakeGarbageCollected<ImageData>(size, data_array, settings);
} }
...@@ -263,9 +261,8 @@ ImageData* ImageData::Create(const IntSize& size, ...@@ -263,9 +261,8 @@ ImageData* ImageData::Create(const IntSize& size,
ImageData* ImageData::Create(unsigned width, ImageData* ImageData::Create(unsigned width,
unsigned height, unsigned height,
ExceptionState& exception_state) { ExceptionState& exception_state) {
if (!ValidateConstructorArguments(kParamWidth | kParamHeight, nullptr, width, if (!ValidateConstructorArguments(nullptr, &width, &height, nullptr, nullptr,
height, NotShared<DOMArrayBufferView>(), &exception_state))
nullptr, &exception_state))
return nullptr; return nullptr;
NotShared<DOMArrayBufferView> byte_array = AllocateAndValidateDataArray( NotShared<DOMArrayBufferView> byte_array = AllocateAndValidateDataArray(
...@@ -278,8 +275,9 @@ ImageData* ImageData::Create(unsigned width, ...@@ -278,8 +275,9 @@ ImageData* ImageData::Create(unsigned width,
ImageData* ImageData::Create(NotShared<DOMUint8ClampedArray> data, ImageData* ImageData::Create(NotShared<DOMUint8ClampedArray> data,
unsigned width, unsigned width,
ExceptionState& exception_state) { ExceptionState& exception_state) {
if (!ValidateConstructorArguments(kParamData | kParamWidth, nullptr, width, 0, NotShared<DOMArrayBufferView> buffer_view = data;
data, nullptr, &exception_state)) if (!ValidateConstructorArguments(nullptr, &width, nullptr, &buffer_view,
nullptr, &exception_state))
return nullptr; return nullptr;
unsigned height = base::checked_cast<unsigned>(data->length()) / (width * 4); unsigned height = base::checked_cast<unsigned>(data->length()) / (width * 4);
...@@ -290,9 +288,9 @@ ImageData* ImageData::Create(NotShared<DOMUint8ClampedArray> data, ...@@ -290,9 +288,9 @@ ImageData* ImageData::Create(NotShared<DOMUint8ClampedArray> data,
unsigned width, unsigned width,
unsigned height, unsigned height,
ExceptionState& exception_state) { ExceptionState& exception_state) {
if (!ValidateConstructorArguments(kParamData | kParamWidth | kParamHeight, NotShared<DOMArrayBufferView> buffer_view = data;
nullptr, width, height, data, nullptr, if (!ValidateConstructorArguments(nullptr, &width, &height, &buffer_view,
&exception_state)) nullptr, &exception_state))
return nullptr; return nullptr;
return MakeGarbageCollected<ImageData>(IntSize(width, height), data); return MakeGarbageCollected<ImageData>(IntSize(width, height), data);
...@@ -302,9 +300,8 @@ ImageData* ImageData::CreateImageData(unsigned width, ...@@ -302,9 +300,8 @@ ImageData* ImageData::CreateImageData(unsigned width,
unsigned height, unsigned height,
const ImageDataSettings* settings, const ImageDataSettings* settings,
ExceptionState& exception_state) { ExceptionState& exception_state) {
if (!ValidateConstructorArguments(kParamWidth | kParamHeight, nullptr, width, if (!ValidateConstructorArguments(nullptr, &width, &height, nullptr, settings,
height, NotShared<DOMArrayBufferView>(), &exception_state))
settings, &exception_state))
return nullptr; return nullptr;
ImageDataStorageFormat storage_format = ImageDataStorageFormat storage_format =
...@@ -347,8 +344,7 @@ ImageData* ImageData::CreateImageData(ImageDataArray& data, ...@@ -347,8 +344,7 @@ ImageData* ImageData::CreateImageData(ImageDataArray& data,
if (settings->storageFormat() != storage_format_name) if (settings->storageFormat() != storage_format_name)
settings->setStorageFormat(storage_format_name); settings->setStorageFormat(storage_format_name);
if (!ValidateConstructorArguments(kParamData | kParamWidth | kParamHeight, if (!ValidateConstructorArguments(nullptr, &width, &height, &buffer_view,
nullptr, width, height, buffer_view,
settings, &exception_state)) settings, &exception_state))
return nullptr; return nullptr;
......
...@@ -143,15 +143,6 @@ class CORE_EXPORT ImageData final : public ScriptWrappable, ...@@ -143,15 +143,6 @@ class CORE_EXPORT ImageData final : public ScriptWrappable,
const WrapperTypeInfo*, const WrapperTypeInfo*,
v8::Local<v8::Object> wrapper) override; v8::Local<v8::Object> wrapper) override;
static bool ValidateConstructorArguments(
const unsigned&,
const IntSize* = nullptr,
const unsigned& = 0,
const unsigned& = 0,
const NotShared<DOMArrayBufferView> = NotShared<DOMArrayBufferView>(),
const ImageDataSettings* = nullptr,
ExceptionState* = nullptr);
private: private:
IntSize size_; IntSize size_;
Member<ImageDataSettings> settings_; Member<ImageDataSettings> settings_;
...@@ -160,6 +151,14 @@ class CORE_EXPORT ImageData final : public ScriptWrappable, ...@@ -160,6 +151,14 @@ class CORE_EXPORT ImageData final : public ScriptWrappable,
NotShared<DOMUint16Array> data_u16_; NotShared<DOMUint16Array> data_u16_;
NotShared<DOMFloat32Array> data_f32_; NotShared<DOMFloat32Array> data_f32_;
static bool ValidateConstructorArguments(
const IntSize* size,
const unsigned* width,
const unsigned* height,
const NotShared<DOMArrayBufferView>* data,
const ImageDataSettings* settings,
ExceptionState* = nullptr);
static NotShared<DOMArrayBufferView> AllocateAndValidateDataArray( static NotShared<DOMArrayBufferView> AllocateAndValidateDataArray(
const unsigned&, const unsigned&,
ImageDataStorageFormat, ImageDataStorageFormat,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment