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"
|
|
|
|
|
2014-01-28 17:04:40 +04:00
|
|
|
#include "mozilla/CheckedInt.h"
|
2013-09-10 19:29:43 +04:00
|
|
|
#include "mozilla/HoldDropJSObjects.h"
|
2012-10-22 21:08:52 +04:00
|
|
|
#include "mozilla/dom/ImageDataBinding.h"
|
2012-03-16 13:44:08 +04:00
|
|
|
|
|
|
|
#include "jsapi.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
CheckedInt<uint32_t> length = CheckedInt<uint32_t>(aWidth) * aHeight * 4;
|
|
|
|
if (!length.isValid()) {
|
|
|
|
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) {
|
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
|
|
|
aData.ComputeLengthAndData();
|
|
|
|
|
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-11-25 21:04:50 +03:00
|
|
|
if (JS_GetTypedArraySharedness(aData.Obj())) {
|
|
|
|
// Throw if the object is mapping shared memory (must opt in).
|
|
|
|
aRv.ThrowTypeError<MSG_TYPEDARRAY_IS_SHARED>(
|
|
|
|
NS_LITERAL_STRING("Argument of ImageData constructor"));
|
|
|
|
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-04-03 05:14:24 +04:00
|
|
|
mData = nullptr;
|
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);
|
|
|
|
}
|
|
|
|
|
2012-03-16 13:44:08 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|