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 "mozilla/Attributes.h"
11 #include "mozilla/dom/BindingUtils.h"
12 #include "mozilla/dom/TypedArray.h"
13 #include <stdint.h>
14 
15 #include "nsCycleCollectionParticipant.h"
16 #include "nsISupportsImpl.h"
17 #include "js/RootingAPI.h"
18 #include "js/StructuredClone.h"
19 
20 class nsIGlobalObject;
21 
22 namespace mozilla {
23 namespace dom {
24 
25 class ImageData final : public nsISupports {
~ImageData()26   ~ImageData() { DropData(); }
27 
28  public:
ImageData(uint32_t aWidth,uint32_t aHeight,JSObject & aData)29   ImageData(uint32_t aWidth, uint32_t aHeight, JSObject& aData)
30       : mWidth(aWidth), mHeight(aHeight), mData(&aData) {
31     HoldData();
32   }
33 
34   NS_DECL_CYCLE_COLLECTING_ISUPPORTS
35   NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(ImageData)
36 
37   static already_AddRefed<ImageData> Constructor(const GlobalObject& aGlobal,
38                                                  const uint32_t aWidth,
39                                                  const uint32_t aHeight,
40                                                  ErrorResult& aRv);
41 
42   static already_AddRefed<ImageData> Constructor(
43       const GlobalObject& aGlobal, const Uint8ClampedArray& aData,
44       const uint32_t aWidth, const Optional<uint32_t>& aHeight,
45       ErrorResult& aRv);
46 
Width()47   uint32_t Width() const { return mWidth; }
Height()48   uint32_t Height() const { return mHeight; }
GetData(JSContext * cx,JS::MutableHandle<JSObject * > aData)49   void GetData(JSContext* cx, JS::MutableHandle<JSObject*> aData) const {
50     aData.set(GetDataObject());
51   }
GetDataObject()52   JSObject* GetDataObject() const { return mData; }
53 
54   bool WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto,
55                   JS::MutableHandle<JSObject*> aReflector);
56 
57   //[Serializable] implementation
58   static already_AddRefed<ImageData> ReadStructuredClone(
59       JSContext* aCx, nsIGlobalObject* aGlobal,
60       JSStructuredCloneReader* aReader);
61   bool WriteStructuredClone(JSContext* aCx,
62                             JSStructuredCloneWriter* aWriter) const;
63 
64  private:
65   void HoldData();
66   void DropData();
67 
68   ImageData() = delete;
69 
70   uint32_t mWidth, mHeight;
71   JS::Heap<JSObject*> mData;
72 };
73 
74 }  // namespace dom
75 }  // namespace mozilla
76 
77 #endif  // mozilla_dom_ImageData_h
78