OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2008 Apple Inc. All rights reserved. | 2 * Copyright (C) 2008 Apple Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * | 7 * |
8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
(...skipping 12 matching lines...) Expand all Loading... |
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | 23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 */ | 27 */ |
28 | 28 |
29 #include "sky/engine/config.h" | 29 #include "sky/engine/config.h" |
30 #include "sky/engine/core/html/ImageData.h" | 30 #include "sky/engine/core/html/ImageData.h" |
31 | 31 |
32 #include "gen/sky/platform/RuntimeEnabledFeatures.h" | 32 #include "gen/sky/platform/RuntimeEnabledFeatures.h" |
33 #include "sky/engine/bindings/core/v8/ExceptionState.h" | 33 #include "sky/engine/bindings2/exception_state.h" |
34 #include "sky/engine/bindings/core/v8/custom/V8Uint8ClampedArrayCustom.h" | |
35 #include "sky/engine/core/dom/ExceptionCode.h" | 34 #include "sky/engine/core/dom/ExceptionCode.h" |
36 | 35 |
37 namespace blink { | 36 namespace blink { |
38 | 37 |
39 PassRefPtr<ImageData> ImageData::create(const IntSize& size) | 38 PassRefPtr<ImageData> ImageData::create(const IntSize& size) |
40 { | 39 { |
41 Checked<int, RecordOverflow> dataSize = 4; | 40 Checked<int, RecordOverflow> dataSize = 4; |
42 dataSize *= size.width(); | 41 dataSize *= size.width(); |
43 dataSize *= size.height(); | 42 dataSize *= size.height(); |
44 if (dataSize.hasOverflowed()) | 43 if (dataSize.hasOverflowed()) |
(...skipping 13 matching lines...) Expand all Loading... |
58 if (dataSize.unsafeGet() < 0 | 57 if (dataSize.unsafeGet() < 0 |
59 || static_cast<unsigned>(dataSize.unsafeGet()) > byteArray->length()) | 58 || static_cast<unsigned>(dataSize.unsafeGet()) > byteArray->length()) |
60 return nullptr; | 59 return nullptr; |
61 | 60 |
62 return adoptRef(new ImageData(size, byteArray)); | 61 return adoptRef(new ImageData(size, byteArray)); |
63 } | 62 } |
64 | 63 |
65 PassRefPtr<ImageData> ImageData::create(unsigned width, unsigned height, Excepti
onState& exceptionState) | 64 PassRefPtr<ImageData> ImageData::create(unsigned width, unsigned height, Excepti
onState& exceptionState) |
66 { | 65 { |
67 if (!RuntimeEnabledFeatures::imageDataConstructorEnabled()) { | 66 if (!RuntimeEnabledFeatures::imageDataConstructorEnabled()) { |
68 exceptionState.throwTypeError("Illegal constructor"); | 67 exceptionState.ThrowTypeError("Illegal constructor"); |
69 return nullptr; | 68 return nullptr; |
70 } | 69 } |
71 if (!width || !height) { | 70 if (!width || !height) { |
72 exceptionState.throwDOMException(IndexSizeError, String::format("The sou
rce %s is zero or not a number.", width ? "height" : "width")); | 71 exceptionState.ThrowDOMException(IndexSizeError, String::format("The sou
rce %s is zero or not a number.", width ? "height" : "width")); |
73 return nullptr; | 72 return nullptr; |
74 } | 73 } |
75 | 74 |
76 Checked<unsigned, RecordOverflow> dataSize = 4; | 75 Checked<unsigned, RecordOverflow> dataSize = 4; |
77 dataSize *= width; | 76 dataSize *= width; |
78 dataSize *= height; | 77 dataSize *= height; |
79 if (dataSize.hasOverflowed()) { | 78 if (dataSize.hasOverflowed()) { |
80 exceptionState.throwDOMException(IndexSizeError, "The requested image si
ze exceeds the supported range."); | 79 exceptionState.ThrowDOMException(IndexSizeError, "The requested image si
ze exceeds the supported range."); |
81 return nullptr; | 80 return nullptr; |
82 } | 81 } |
83 | 82 |
84 RefPtr<ImageData> imageData = adoptRef(new ImageData(IntSize(width, height))
); | 83 RefPtr<ImageData> imageData = adoptRef(new ImageData(IntSize(width, height))
); |
85 imageData->data()->zeroFill(); | 84 imageData->data()->zeroFill(); |
86 return imageData.release(); | 85 return imageData.release(); |
87 } | 86 } |
88 | 87 |
89 PassRefPtr<ImageData> ImageData::create(Uint8ClampedArray* data, unsigned width,
unsigned height, ExceptionState& exceptionState) | 88 PassRefPtr<ImageData> ImageData::create(Uint8ClampedArray* data, unsigned width,
unsigned height, ExceptionState& exceptionState) |
90 { | 89 { |
91 if (!RuntimeEnabledFeatures::imageDataConstructorEnabled()) { | 90 if (!RuntimeEnabledFeatures::imageDataConstructorEnabled()) { |
92 exceptionState.throwTypeError("Illegal constructor"); | 91 exceptionState.ThrowTypeError("Illegal constructor"); |
93 return nullptr; | 92 return nullptr; |
94 } | 93 } |
95 if (!data) { | 94 if (!data) { |
96 exceptionState.throwTypeError("Expected a Uint8ClampedArray as first arg
ument."); | 95 exceptionState.ThrowTypeError("Expected a Uint8ClampedArray as first arg
ument."); |
97 return nullptr; | 96 return nullptr; |
98 } | 97 } |
99 if (!width) { | 98 if (!width) { |
100 exceptionState.throwDOMException(IndexSizeError, "The source width is ze
ro or not a number."); | 99 exceptionState.ThrowDOMException(IndexSizeError, "The source width is ze
ro or not a number."); |
101 return nullptr; | 100 return nullptr; |
102 } | 101 } |
103 | 102 |
104 unsigned length = data->length(); | 103 unsigned length = data->length(); |
105 if (!length) { | 104 if (!length) { |
106 exceptionState.throwDOMException(IndexSizeError, "The input data has a z
ero byte length."); | 105 exceptionState.ThrowDOMException(IndexSizeError, "The input data has a z
ero byte length."); |
107 return nullptr; | 106 return nullptr; |
108 } | 107 } |
109 if (length % 4) { | 108 if (length % 4) { |
110 exceptionState.throwDOMException(IndexSizeError, "The input data byte le
ngth is not a multiple of 4."); | 109 exceptionState.ThrowDOMException(IndexSizeError, "The input data byte le
ngth is not a multiple of 4."); |
111 return nullptr; | 110 return nullptr; |
112 } | 111 } |
113 length /= 4; | 112 length /= 4; |
114 if (length % width) { | 113 if (length % width) { |
115 exceptionState.throwDOMException(IndexSizeError, "The input data byte le
ngth is not a multiple of (4 * width)."); | 114 exceptionState.ThrowDOMException(IndexSizeError, "The input data byte le
ngth is not a multiple of (4 * width)."); |
116 return nullptr; | 115 return nullptr; |
117 } | 116 } |
118 if (!height) { | 117 if (!height) { |
119 height = length / width; | 118 height = length / width; |
120 } else if (height != length / width) { | 119 } else if (height != length / width) { |
121 exceptionState.throwDOMException(IndexSizeError, "The input data byte le
ngth is not equal to (4 * width * height)."); | 120 exceptionState.ThrowDOMException(IndexSizeError, "The input data byte le
ngth is not equal to (4 * width * height)."); |
122 return nullptr; | 121 return nullptr; |
123 } | 122 } |
124 | 123 |
125 return adoptRef(new ImageData(IntSize(width, height), data)); | 124 return adoptRef(new ImageData(IntSize(width, height), data)); |
126 } | 125 } |
127 | 126 |
128 v8::Handle<v8::Object> ImageData::wrap(v8::Handle<v8::Object> creationContext, v
8::Isolate* isolate) | |
129 { | |
130 v8::Handle<v8::Object> wrapper = ScriptWrappable::wrap(creationContext, isol
ate); | |
131 if (!wrapper.IsEmpty()) { | |
132 // Create a V8 Uint8ClampedArray object. | |
133 v8::Handle<v8::Value> pixelArray = toV8(data(), creationContext, isolate
); | |
134 // Set the "data" property of the ImageData object to | |
135 // the created v8 object, eliminating the C++ callback | |
136 // when accessing the "data" property. | |
137 if (!pixelArray.IsEmpty()) | |
138 wrapper->ForceSet(v8AtomicString(isolate, "data"), pixelArray, v8::R
eadOnly); | |
139 } | |
140 return wrapper; | |
141 } | |
142 | |
143 ImageData::ImageData(const IntSize& size) | 127 ImageData::ImageData(const IntSize& size) |
144 : m_size(size) | 128 : m_size(size) |
145 , m_data(Uint8ClampedArray::create(size.width() * size.height() * 4)) | 129 , m_data(Uint8ClampedArray::create(size.width() * size.height() * 4)) |
146 { | 130 { |
147 } | 131 } |
148 | 132 |
149 ImageData::ImageData(const IntSize& size, PassRefPtr<Uint8ClampedArray> byteArra
y) | 133 ImageData::ImageData(const IntSize& size, PassRefPtr<Uint8ClampedArray> byteArra
y) |
150 : m_size(size) | 134 : m_size(size) |
151 , m_data(byteArray) | 135 , m_data(byteArray) |
152 { | 136 { |
153 ASSERT_WITH_SECURITY_IMPLICATION(static_cast<unsigned>(size.width() * size.h
eight() * 4) <= m_data->length()); | 137 ASSERT_WITH_SECURITY_IMPLICATION(static_cast<unsigned>(size.width() * size.h
eight() * 4) <= m_data->length()); |
154 } | 138 } |
155 | 139 |
156 } | 140 } |
OLD | NEW |