ImageData.h (2794B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim:set ts=2 sw=2 et tw=78: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_dom_ImageData_h 8 #define mozilla_dom_ImageData_h 9 10 #include <cstdint> 11 12 #include "js/RootingAPI.h" 13 #include "mozilla/AlreadyAddRefed.h" 14 #include "mozilla/dom/TypedArray.h" 15 #include "nsCycleCollectionParticipant.h" 16 #include "nsISupports.h" 17 #include "nsWrapperCache.h" 18 19 class JSObject; 20 class nsIGlobalObject; 21 struct JSContext; 22 struct JSStructuredCloneReader; 23 struct JSStructuredCloneWriter; 24 25 namespace mozilla { 26 class ErrorResult; 27 28 namespace dom { 29 30 class GlobalObject; 31 template <typename T> 32 class Optional; 33 34 // ImageData extends nsWrapperCache only to support nursery allocated wrapper. 35 class ImageData final : public nsISupports, public nsWrapperCache { 36 public: 37 ImageData(nsISupports* aOwner, uint32_t aWidth, uint32_t aHeight, 38 JS::Handle<JSObject*> aData) 39 : mOwner(aOwner), mWidth(aWidth), mHeight(aHeight), mData(aData) { 40 HoldData(); 41 } 42 43 NS_DECL_CYCLE_COLLECTING_ISUPPORTS 44 NS_DECL_CYCLE_COLLECTION_SKIPPABLE_SCRIPT_HOLDER_CLASS(ImageData) 45 46 nsISupports* GetParentObject() const { return mOwner; } 47 48 static already_AddRefed<ImageData> Constructor(const GlobalObject& aGlobal, 49 const uint32_t aWidth, 50 const uint32_t aHeight, 51 ErrorResult& aRv); 52 53 static already_AddRefed<ImageData> Constructor( 54 const GlobalObject& aGlobal, const Uint8ClampedArray& aData, 55 const uint32_t aWidth, const Optional<uint32_t>& aHeight, 56 ErrorResult& aRv); 57 58 uint32_t Width() const { return mWidth; } 59 uint32_t Height() const { return mHeight; } 60 void GetData(JSContext* cx, JS::MutableHandle<JSObject*> aData) const { 61 aData.set(GetDataObject()); 62 } 63 JSObject* GetDataObject() const { return mData; } 64 65 JSObject* WrapObject(JSContext*, JS::Handle<JSObject*> aGivenProto) override; 66 67 //[Serializable] implementation 68 static already_AddRefed<ImageData> ReadStructuredClone( 69 JSContext* aCx, nsIGlobalObject* aGlobal, 70 JSStructuredCloneReader* aReader); 71 bool WriteStructuredClone(JSContext* aCx, 72 JSStructuredCloneWriter* aWriter) const; 73 74 private: 75 void HoldData(); 76 void DropData(); 77 78 ImageData() = delete; 79 ~ImageData() { DropData(); } 80 81 nsCOMPtr<nsISupports> mOwner; 82 83 const uint32_t mWidth; 84 const uint32_t mHeight; 85 86 JS::Heap<JSObject*> mData; 87 }; 88 89 } // namespace dom 90 } // namespace mozilla 91 92 #endif // mozilla_dom_ImageData_h