michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 et tw=78: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "mozilla/dom/ImageData.h" michael@0: michael@0: #include "mozilla/CheckedInt.h" michael@0: #include "mozilla/HoldDropJSObjects.h" michael@0: #include "mozilla/dom/ImageDataBinding.h" michael@0: michael@0: #include "jsapi.h" michael@0: michael@0: namespace mozilla { michael@0: namespace dom { michael@0: michael@0: NS_IMPL_CYCLE_COLLECTING_ADDREF(ImageData) michael@0: NS_IMPL_CYCLE_COLLECTING_RELEASE(ImageData) michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_CLASS(ImageData) michael@0: michael@0: NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ImageData) michael@0: NS_INTERFACE_MAP_ENTRY(nsISupports) michael@0: NS_INTERFACE_MAP_END michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(ImageData) michael@0: NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mData) michael@0: NS_IMPL_CYCLE_COLLECTION_TRACE_END michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(ImageData) michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(ImageData) michael@0: tmp->DropData(); michael@0: NS_IMPL_CYCLE_COLLECTION_UNLINK_END michael@0: michael@0: //static michael@0: ImageData* michael@0: ImageData::Constructor(const GlobalObject& aGlobal, michael@0: const uint32_t aWidth, michael@0: const uint32_t aHeight, michael@0: ErrorResult& aRv) michael@0: { michael@0: if (aWidth == 0 || aHeight == 0) { michael@0: aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR); michael@0: return nullptr; michael@0: } michael@0: CheckedInt length = CheckedInt(aWidth) * aHeight * 4; michael@0: if (!length.isValid()) { michael@0: aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR); michael@0: return nullptr; michael@0: } michael@0: js::AssertSameCompartment(aGlobal.GetContext(), aGlobal.Get()); michael@0: JSObject* data = Uint8ClampedArray::Create(aGlobal.GetContext(), michael@0: length.value()); michael@0: if (!data) { michael@0: aRv.Throw(NS_ERROR_OUT_OF_MEMORY); michael@0: return nullptr; michael@0: } michael@0: return new ImageData(aWidth, aHeight, *data); michael@0: } michael@0: michael@0: //static michael@0: ImageData* michael@0: ImageData::Constructor(const GlobalObject& aGlobal, michael@0: const Uint8ClampedArray& aData, michael@0: const uint32_t aWidth, michael@0: const Optional& aHeight, michael@0: ErrorResult& aRv) michael@0: { michael@0: aData.ComputeLengthAndData(); michael@0: michael@0: uint32_t length = aData.Length(); michael@0: if (length == 0 || length % 4) { michael@0: aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR); michael@0: return nullptr; michael@0: } michael@0: length /= 4; michael@0: if (aWidth == 0) { michael@0: aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR); michael@0: return nullptr; michael@0: } michael@0: uint32_t height = length / aWidth; michael@0: if (length != aWidth * height || michael@0: (aHeight.WasPassed() && aHeight.Value() != height)) { michael@0: aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR); michael@0: return nullptr; michael@0: } michael@0: return new ImageData(aWidth, height, *aData.Obj()); michael@0: } michael@0: michael@0: void michael@0: ImageData::HoldData() michael@0: { michael@0: mozilla::HoldJSObjects(this); michael@0: } michael@0: michael@0: void michael@0: ImageData::DropData() michael@0: { michael@0: if (mData) { michael@0: mData = nullptr; michael@0: mozilla::DropJSObjects(this); michael@0: } michael@0: } michael@0: michael@0: JSObject* michael@0: ImageData::WrapObject(JSContext* cx) michael@0: { michael@0: return ImageDataBinding::Wrap(cx, this); michael@0: } michael@0: michael@0: } // namespace dom michael@0: } // namespace mozilla