Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim:set ts=2 sw=2 et tw=78: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 5 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include "mozilla/dom/ImageData.h" |
michael@0 | 8 | |
michael@0 | 9 | #include "mozilla/CheckedInt.h" |
michael@0 | 10 | #include "mozilla/HoldDropJSObjects.h" |
michael@0 | 11 | #include "mozilla/dom/ImageDataBinding.h" |
michael@0 | 12 | |
michael@0 | 13 | #include "jsapi.h" |
michael@0 | 14 | |
michael@0 | 15 | namespace mozilla { |
michael@0 | 16 | namespace dom { |
michael@0 | 17 | |
michael@0 | 18 | NS_IMPL_CYCLE_COLLECTING_ADDREF(ImageData) |
michael@0 | 19 | NS_IMPL_CYCLE_COLLECTING_RELEASE(ImageData) |
michael@0 | 20 | |
michael@0 | 21 | NS_IMPL_CYCLE_COLLECTION_CLASS(ImageData) |
michael@0 | 22 | |
michael@0 | 23 | NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ImageData) |
michael@0 | 24 | NS_INTERFACE_MAP_ENTRY(nsISupports) |
michael@0 | 25 | NS_INTERFACE_MAP_END |
michael@0 | 26 | |
michael@0 | 27 | NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(ImageData) |
michael@0 | 28 | NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mData) |
michael@0 | 29 | NS_IMPL_CYCLE_COLLECTION_TRACE_END |
michael@0 | 30 | |
michael@0 | 31 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(ImageData) |
michael@0 | 32 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS |
michael@0 | 33 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END |
michael@0 | 34 | |
michael@0 | 35 | NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(ImageData) |
michael@0 | 36 | tmp->DropData(); |
michael@0 | 37 | NS_IMPL_CYCLE_COLLECTION_UNLINK_END |
michael@0 | 38 | |
michael@0 | 39 | //static |
michael@0 | 40 | ImageData* |
michael@0 | 41 | ImageData::Constructor(const GlobalObject& aGlobal, |
michael@0 | 42 | const uint32_t aWidth, |
michael@0 | 43 | const uint32_t aHeight, |
michael@0 | 44 | ErrorResult& aRv) |
michael@0 | 45 | { |
michael@0 | 46 | if (aWidth == 0 || aHeight == 0) { |
michael@0 | 47 | aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR); |
michael@0 | 48 | return nullptr; |
michael@0 | 49 | } |
michael@0 | 50 | CheckedInt<uint32_t> length = CheckedInt<uint32_t>(aWidth) * aHeight * 4; |
michael@0 | 51 | if (!length.isValid()) { |
michael@0 | 52 | aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR); |
michael@0 | 53 | return nullptr; |
michael@0 | 54 | } |
michael@0 | 55 | js::AssertSameCompartment(aGlobal.GetContext(), aGlobal.Get()); |
michael@0 | 56 | JSObject* data = Uint8ClampedArray::Create(aGlobal.GetContext(), |
michael@0 | 57 | length.value()); |
michael@0 | 58 | if (!data) { |
michael@0 | 59 | aRv.Throw(NS_ERROR_OUT_OF_MEMORY); |
michael@0 | 60 | return nullptr; |
michael@0 | 61 | } |
michael@0 | 62 | return new ImageData(aWidth, aHeight, *data); |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | //static |
michael@0 | 66 | ImageData* |
michael@0 | 67 | ImageData::Constructor(const GlobalObject& aGlobal, |
michael@0 | 68 | const Uint8ClampedArray& aData, |
michael@0 | 69 | const uint32_t aWidth, |
michael@0 | 70 | const Optional<uint32_t>& aHeight, |
michael@0 | 71 | ErrorResult& aRv) |
michael@0 | 72 | { |
michael@0 | 73 | aData.ComputeLengthAndData(); |
michael@0 | 74 | |
michael@0 | 75 | uint32_t length = aData.Length(); |
michael@0 | 76 | if (length == 0 || length % 4) { |
michael@0 | 77 | aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR); |
michael@0 | 78 | return nullptr; |
michael@0 | 79 | } |
michael@0 | 80 | length /= 4; |
michael@0 | 81 | if (aWidth == 0) { |
michael@0 | 82 | aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR); |
michael@0 | 83 | return nullptr; |
michael@0 | 84 | } |
michael@0 | 85 | uint32_t height = length / aWidth; |
michael@0 | 86 | if (length != aWidth * height || |
michael@0 | 87 | (aHeight.WasPassed() && aHeight.Value() != height)) { |
michael@0 | 88 | aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR); |
michael@0 | 89 | return nullptr; |
michael@0 | 90 | } |
michael@0 | 91 | return new ImageData(aWidth, height, *aData.Obj()); |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | void |
michael@0 | 95 | ImageData::HoldData() |
michael@0 | 96 | { |
michael@0 | 97 | mozilla::HoldJSObjects(this); |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | void |
michael@0 | 101 | ImageData::DropData() |
michael@0 | 102 | { |
michael@0 | 103 | if (mData) { |
michael@0 | 104 | mData = nullptr; |
michael@0 | 105 | mozilla::DropJSObjects(this); |
michael@0 | 106 | } |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | JSObject* |
michael@0 | 110 | ImageData::WrapObject(JSContext* cx) |
michael@0 | 111 | { |
michael@0 | 112 | return ImageDataBinding::Wrap(cx, this); |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | } // namespace dom |
michael@0 | 116 | } // namespace mozilla |