2012-03-16 13:44:08 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 et tw=78: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "mozilla/dom/ImageData.h"
|
|
|
|
|
2020-11-23 19:06:52 +03:00
|
|
|
#include "ErrorList.h"
|
|
|
|
#include "js/StructuredClone.h"
|
|
|
|
#include "js/Value.h"
|
|
|
|
#include "jsapi.h"
|
|
|
|
#include "jsfriendapi.h"
|
2014-01-28 17:04:40 +04:00
|
|
|
#include "mozilla/CheckedInt.h"
|
2020-11-23 19:06:52 +03:00
|
|
|
#include "mozilla/ErrorResult.h"
|
2013-09-10 19:29:43 +04:00
|
|
|
#include "mozilla/HoldDropJSObjects.h"
|
2020-11-23 19:06:52 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
|
|
|
#include "mozilla/dom/BindingDeclarations.h"
|
2012-10-22 21:08:52 +04:00
|
|
|
#include "mozilla/dom/ImageDataBinding.h"
|
2020-11-23 19:06:52 +03:00
|
|
|
#include "nsCycleCollectionNoteChild.h"
|
2012-03-16 13:44:08 +04:00
|
|
|
|
2020-11-04 20:04:01 +03:00
|
|
|
namespace mozilla::dom {
|
2012-03-16 13:44:08 +04:00
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(ImageData)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(ImageData)
|
|
|
|
|
2013-08-02 05:29:05 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(ImageData)
|
|
|
|
|
2012-03-16 13:44:08 +04:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ImageData)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(ImageData)
|
2012-06-11 03:44:50 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mData)
|
2012-03-16 13:44:08 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(ImageData)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(ImageData)
|
|
|
|
tmp->DropData();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
2014-01-28 17:04:40 +04:00
|
|
|
// static
|
|
|
|
already_AddRefed<ImageData> ImageData::Constructor(const GlobalObject& aGlobal,
|
|
|
|
const uint32_t aWidth,
|
|
|
|
const uint32_t aHeight,
|
|
|
|
ErrorResult& aRv) {
|
|
|
|
if (aWidth == 0 || aHeight == 0) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2022-04-07 22:46:47 +03:00
|
|
|
|
|
|
|
// Restrict the typed array length to INT32_MAX because that's all we support
|
|
|
|
// in dom::TypedArray::ComputeState.
|
2014-01-28 17:04:40 +04:00
|
|
|
CheckedInt<uint32_t> length = CheckedInt<uint32_t>(aWidth) * aHeight * 4;
|
2022-04-07 22:46:47 +03:00
|
|
|
if (!length.isValid() || length.value() > INT32_MAX) {
|
2014-01-28 17:04:40 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-06-16 22:08:00 +04:00
|
|
|
js::AssertSameCompartment(aGlobal.Context(), aGlobal.Get());
|
|
|
|
JSObject* data = Uint8ClampedArray::Create(aGlobal.Context(), length.value());
|
2014-01-28 17:04:40 +04:00
|
|
|
if (!data) {
|
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ImageData> imageData = new ImageData(aWidth, aHeight, *data);
|
2014-08-28 02:43:55 +04:00
|
|
|
return imageData.forget();
|
2014-01-28 17:04:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2014-08-28 02:43:55 +04:00
|
|
|
already_AddRefed<ImageData> ImageData::Constructor(
|
2014-01-28 17:04:40 +04:00
|
|
|
const GlobalObject& aGlobal, const Uint8ClampedArray& aData,
|
|
|
|
const uint32_t aWidth, const Optional<uint32_t>& aHeight,
|
|
|
|
ErrorResult& aRv) {
|
2020-01-23 06:22:06 +03:00
|
|
|
aData.ComputeState();
|
Bug 999651, bug 995679, bug 1009952, bug 1011007, bug 991981. r=sfink, r=shu, r=jandem, r=jdm, r=luke, r=bbouvier, r=nmatsakis, r=bz, r=ehsan, r=jgilbert, r=smaug, r=sicking, r=terrence, r=bholley, r=bent, r=efaust, r=jorendorff
2014-05-28 01:32:41 +04:00
|
|
|
|
2014-01-28 17:04:40 +04:00
|
|
|
uint32_t length = aData.Length();
|
|
|
|
if (length == 0 || length % 4) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
length /= 4;
|
|
|
|
if (aWidth == 0) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
uint32_t height = length / aWidth;
|
|
|
|
if (length != aWidth * height ||
|
|
|
|
(aHeight.WasPassed() && aHeight.Value() != height)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ImageData> imageData = new ImageData(aWidth, height, *aData.Obj());
|
2014-08-28 02:43:55 +04:00
|
|
|
return imageData.forget();
|
2014-01-28 17:04:40 +04:00
|
|
|
}
|
|
|
|
|
2013-08-17 00:10:17 +04:00
|
|
|
void ImageData::HoldData() { mozilla::HoldJSObjects(this); }
|
2012-03-16 13:44:08 +04:00
|
|
|
|
|
|
|
void ImageData::DropData() {
|
|
|
|
if (mData) {
|
2013-08-17 00:10:17 +04:00
|
|
|
mozilla::DropJSObjects(this);
|
2012-03-16 13:44:08 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Bug 1117172 part 2. Change the non-wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, Codegen.py, and
StructuredClone.cpp. The rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/WrapObject\((JSContext *\* *(?:aCx|cx)),(\s*)(JS::MutableHandle<JSObject\*> aReflector)/WrapObject(\1,\2JS::Handle<JSObject*> aGivenProto,\2\3/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx)), this, aReflector/\1, this, aGivenProto, aReflector/'
2015-03-19 17:13:32 +03:00
|
|
|
bool ImageData::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto,
|
|
|
|
JS::MutableHandle<JSObject*> aReflector) {
|
2018-06-26 00:20:54 +03:00
|
|
|
return ImageData_Binding::Wrap(aCx, this, aGivenProto, aReflector);
|
2012-10-22 21:08:52 +04:00
|
|
|
}
|
|
|
|
|
2019-06-25 09:44:38 +03:00
|
|
|
// static
|
|
|
|
already_AddRefed<ImageData> ImageData::ReadStructuredClone(
|
|
|
|
JSContext* aCx, nsIGlobalObject* aGlobal,
|
|
|
|
JSStructuredCloneReader* aReader) {
|
|
|
|
// Read the information out of the stream.
|
|
|
|
uint32_t width, height;
|
|
|
|
JS::Rooted<JS::Value> dataArray(aCx);
|
|
|
|
if (!JS_ReadUint32Pair(aReader, &width, &height) ||
|
|
|
|
!JS_ReadTypedArray(aReader, &dataArray)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(dataArray.isObject());
|
|
|
|
|
|
|
|
RefPtr<ImageData> imageData =
|
|
|
|
new ImageData(width, height, dataArray.toObject());
|
|
|
|
return imageData.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ImageData::WriteStructuredClone(JSContext* aCx,
|
|
|
|
JSStructuredCloneWriter* aWriter) const {
|
|
|
|
JS::Rooted<JS::Value> arrayValue(aCx, JS::ObjectValue(*GetDataObject()));
|
|
|
|
if (!JS_WrapValue(aCx, &arrayValue)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return JS_WriteUint32Pair(aWriter, Width(), Height()) &&
|
|
|
|
JS_WriteTypedArray(aWriter, arrayValue);
|
|
|
|
}
|
|
|
|
|
2020-11-04 20:04:01 +03:00
|
|
|
} // namespace mozilla::dom
|