| 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 18 matching lines...) Expand all Loading... |
| 29 #include "config.h" | 29 #include "config.h" |
| 30 #include "core/html/ImageData.h" | 30 #include "core/html/ImageData.h" |
| 31 | 31 |
| 32 #include "bindings/core/v8/ExceptionState.h" | 32 #include "bindings/core/v8/ExceptionState.h" |
| 33 #include "bindings/core/v8/V8Uint8ClampedArray.h" | 33 #include "bindings/core/v8/V8Uint8ClampedArray.h" |
| 34 #include "core/dom/ExceptionCode.h" | 34 #include "core/dom/ExceptionCode.h" |
| 35 #include "platform/RuntimeEnabledFeatures.h" | 35 #include "platform/RuntimeEnabledFeatures.h" |
| 36 | 36 |
| 37 namespace blink { | 37 namespace blink { |
| 38 | 38 |
| 39 PassRefPtrWillBeRawPtr<ImageData> ImageData::create(const IntSize& size) | 39 ImageData* ImageData::create(const IntSize& size) |
| 40 { | 40 { |
| 41 Checked<int, RecordOverflow> dataSize = 4; | 41 Checked<int, RecordOverflow> dataSize = 4; |
| 42 dataSize *= size.width(); | 42 dataSize *= size.width(); |
| 43 dataSize *= size.height(); | 43 dataSize *= size.height(); |
| 44 if (dataSize.hasOverflowed()) | 44 if (dataSize.hasOverflowed()) |
| 45 return nullptr; | 45 return nullptr; |
| 46 | 46 |
| 47 return adoptRefWillBeNoop(new ImageData(size)); | 47 return new ImageData(size); |
| 48 } | 48 } |
| 49 | 49 |
| 50 PassRefPtrWillBeRawPtr<ImageData> ImageData::create(const IntSize& size, PassRef
Ptr<DOMUint8ClampedArray> byteArray) | 50 ImageData* ImageData::create(const IntSize& size, PassRefPtr<DOMUint8ClampedArra
y> byteArray) |
| 51 { | 51 { |
| 52 Checked<int, RecordOverflow> dataSize = 4; | 52 Checked<int, RecordOverflow> dataSize = 4; |
| 53 dataSize *= size.width(); | 53 dataSize *= size.width(); |
| 54 dataSize *= size.height(); | 54 dataSize *= size.height(); |
| 55 if (dataSize.hasOverflowed()) | 55 if (dataSize.hasOverflowed()) |
| 56 return nullptr; | 56 return nullptr; |
| 57 | 57 |
| 58 if (dataSize.unsafeGet() < 0 | 58 if (dataSize.unsafeGet() < 0 |
| 59 || static_cast<unsigned>(dataSize.unsafeGet()) > byteArray->length()) | 59 || static_cast<unsigned>(dataSize.unsafeGet()) > byteArray->length()) |
| 60 return nullptr; | 60 return nullptr; |
| 61 | 61 |
| 62 return adoptRefWillBeNoop(new ImageData(size, byteArray)); | 62 return new ImageData(size, byteArray); |
| 63 } | 63 } |
| 64 | 64 |
| 65 PassRefPtrWillBeRawPtr<ImageData> ImageData::create(unsigned width, unsigned hei
ght, ExceptionState& exceptionState) | 65 ImageData* ImageData::create(unsigned width, unsigned height, ExceptionState& ex
ceptionState) |
| 66 { | 66 { |
| 67 if (!RuntimeEnabledFeatures::imageDataConstructorEnabled()) { | 67 if (!RuntimeEnabledFeatures::imageDataConstructorEnabled()) { |
| 68 exceptionState.throwTypeError("Illegal constructor"); | 68 exceptionState.throwTypeError("Illegal constructor"); |
| 69 return nullptr; | 69 return nullptr; |
| 70 } | 70 } |
| 71 if (!width || !height) { | 71 if (!width || !height) { |
| 72 exceptionState.throwDOMException(IndexSizeError, String::format("The sou
rce %s is zero or not a number.", width ? "height" : "width")); | 72 exceptionState.throwDOMException(IndexSizeError, String::format("The sou
rce %s is zero or not a number.", width ? "height" : "width")); |
| 73 return nullptr; | 73 return nullptr; |
| 74 } | 74 } |
| 75 | 75 |
| 76 Checked<unsigned, RecordOverflow> dataSize = 4; | 76 Checked<unsigned, RecordOverflow> dataSize = 4; |
| 77 dataSize *= width; | 77 dataSize *= width; |
| 78 dataSize *= height; | 78 dataSize *= height; |
| 79 if (dataSize.hasOverflowed()) { | 79 if (dataSize.hasOverflowed()) { |
| 80 exceptionState.throwDOMException(IndexSizeError, "The requested image si
ze exceeds the supported range."); | 80 exceptionState.throwDOMException(IndexSizeError, "The requested image si
ze exceeds the supported range."); |
| 81 return nullptr; | 81 return nullptr; |
| 82 } | 82 } |
| 83 | 83 |
| 84 return adoptRefWillBeNoop(new ImageData(IntSize(width, height))); | 84 return new ImageData(IntSize(width, height)); |
| 85 } | 85 } |
| 86 | 86 |
| 87 bool ImageData::validateConstructorArguments(DOMUint8ClampedArray* data, unsigne
d width, unsigned& lengthInPixels, ExceptionState& exceptionState) | 87 bool ImageData::validateConstructorArguments(DOMUint8ClampedArray* data, unsigne
d width, unsigned& lengthInPixels, ExceptionState& exceptionState) |
| 88 { | 88 { |
| 89 if (!width) { | 89 if (!width) { |
| 90 exceptionState.throwDOMException(IndexSizeError, "The source width is ze
ro or not a number."); | 90 exceptionState.throwDOMException(IndexSizeError, "The source width is ze
ro or not a number."); |
| 91 return false; | 91 return false; |
| 92 } | 92 } |
| 93 ASSERT(data); | 93 ASSERT(data); |
| 94 unsigned length = data->length(); | 94 unsigned length = data->length(); |
| 95 if (!length) { | 95 if (!length) { |
| 96 exceptionState.throwDOMException(IndexSizeError, "The input data has a z
ero byte length."); | 96 exceptionState.throwDOMException(IndexSizeError, "The input data has a z
ero byte length."); |
| 97 return false; | 97 return false; |
| 98 } | 98 } |
| 99 if (length % 4) { | 99 if (length % 4) { |
| 100 exceptionState.throwDOMException(IndexSizeError, "The input data byte le
ngth is not a multiple of 4."); | 100 exceptionState.throwDOMException(IndexSizeError, "The input data byte le
ngth is not a multiple of 4."); |
| 101 return false; | 101 return false; |
| 102 } | 102 } |
| 103 length /= 4; | 103 length /= 4; |
| 104 if (length % width) { | 104 if (length % width) { |
| 105 exceptionState.throwDOMException(IndexSizeError, "The input data byte le
ngth is not a multiple of (4 * width)."); | 105 exceptionState.throwDOMException(IndexSizeError, "The input data byte le
ngth is not a multiple of (4 * width)."); |
| 106 return false; | 106 return false; |
| 107 } | 107 } |
| 108 lengthInPixels = length; | 108 lengthInPixels = length; |
| 109 return true; | 109 return true; |
| 110 } | 110 } |
| 111 | 111 |
| 112 PassRefPtrWillBeRawPtr<ImageData> ImageData::create(DOMUint8ClampedArray* data,
unsigned width, ExceptionState& exceptionState) | 112 ImageData* ImageData::create(DOMUint8ClampedArray* data, unsigned width, Excepti
onState& exceptionState) |
| 113 { | 113 { |
| 114 if (!RuntimeEnabledFeatures::imageDataConstructorEnabled()) { | 114 if (!RuntimeEnabledFeatures::imageDataConstructorEnabled()) { |
| 115 exceptionState.throwTypeError("Illegal constructor"); | 115 exceptionState.throwTypeError("Illegal constructor"); |
| 116 return nullptr; | 116 return nullptr; |
| 117 } | 117 } |
| 118 unsigned lengthInPixels = 0; | 118 unsigned lengthInPixels = 0; |
| 119 if (!validateConstructorArguments(data, width, lengthInPixels, exceptionStat
e)) { | 119 if (!validateConstructorArguments(data, width, lengthInPixels, exceptionStat
e)) { |
| 120 ASSERT(exceptionState.hadException()); | 120 ASSERT(exceptionState.hadException()); |
| 121 return nullptr; | 121 return nullptr; |
| 122 } | 122 } |
| 123 ASSERT(lengthInPixels && width); | 123 ASSERT(lengthInPixels && width); |
| 124 unsigned height = lengthInPixels / width; | 124 unsigned height = lengthInPixels / width; |
| 125 return adoptRefWillBeNoop(new ImageData(IntSize(width, height), data)); | 125 return new ImageData(IntSize(width, height), data); |
| 126 } | 126 } |
| 127 | 127 |
| 128 PassRefPtrWillBeRawPtr<ImageData> ImageData::create(DOMUint8ClampedArray* data,
unsigned width, unsigned height, ExceptionState& exceptionState) | 128 ImageData* ImageData::create(DOMUint8ClampedArray* data, unsigned width, unsigne
d height, ExceptionState& exceptionState) |
| 129 { | 129 { |
| 130 if (!RuntimeEnabledFeatures::imageDataConstructorEnabled()) { | 130 if (!RuntimeEnabledFeatures::imageDataConstructorEnabled()) { |
| 131 exceptionState.throwTypeError("Illegal constructor"); | 131 exceptionState.throwTypeError("Illegal constructor"); |
| 132 return nullptr; | 132 return nullptr; |
| 133 } | 133 } |
| 134 unsigned lengthInPixels = 0; | 134 unsigned lengthInPixels = 0; |
| 135 if (!validateConstructorArguments(data, width, lengthInPixels, exceptionStat
e)) { | 135 if (!validateConstructorArguments(data, width, lengthInPixels, exceptionStat
e)) { |
| 136 ASSERT(exceptionState.hadException()); | 136 ASSERT(exceptionState.hadException()); |
| 137 return nullptr; | 137 return nullptr; |
| 138 } | 138 } |
| 139 ASSERT(lengthInPixels && width); | 139 ASSERT(lengthInPixels && width); |
| 140 if (height != lengthInPixels / width) { | 140 if (height != lengthInPixels / width) { |
| 141 exceptionState.throwDOMException(IndexSizeError, "The input data byte le
ngth is not equal to (4 * width * height)."); | 141 exceptionState.throwDOMException(IndexSizeError, "The input data byte le
ngth is not equal to (4 * width * height)."); |
| 142 return nullptr; | 142 return nullptr; |
| 143 } | 143 } |
| 144 return adoptRefWillBeNoop(new ImageData(IntSize(width, height), data)); | 144 return new ImageData(IntSize(width, height), data); |
| 145 } | 145 } |
| 146 | 146 |
| 147 v8::Local<v8::Object> ImageData::associateWithWrapper(v8::Isolate* isolate, cons
t WrapperTypeInfo* wrapperType, v8::Local<v8::Object> wrapper) | 147 v8::Local<v8::Object> ImageData::associateWithWrapper(v8::Isolate* isolate, cons
t WrapperTypeInfo* wrapperType, v8::Local<v8::Object> wrapper) |
| 148 { | 148 { |
| 149 ScriptWrappable::associateWithWrapper(isolate, wrapperType, wrapper); | 149 ScriptWrappable::associateWithWrapper(isolate, wrapperType, wrapper); |
| 150 | 150 |
| 151 if (!wrapper.IsEmpty() && m_data.get()) { | 151 if (!wrapper.IsEmpty() && m_data.get()) { |
| 152 // Create a V8 Uint8ClampedArray object and set the "data" property | 152 // Create a V8 Uint8ClampedArray object and set the "data" property |
| 153 // of the ImageData object to the created v8 object, eliminating the | 153 // of the ImageData object to the created v8 object, eliminating the |
| 154 // C++ callback when accessing the "data" property. | 154 // C++ callback when accessing the "data" property. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 165 { | 165 { |
| 166 } | 166 } |
| 167 | 167 |
| 168 ImageData::ImageData(const IntSize& size, PassRefPtr<DOMUint8ClampedArray> byteA
rray) | 168 ImageData::ImageData(const IntSize& size, PassRefPtr<DOMUint8ClampedArray> byteA
rray) |
| 169 : m_size(size) | 169 : m_size(size) |
| 170 , m_data(byteArray) | 170 , m_data(byteArray) |
| 171 { | 171 { |
| 172 ASSERT_WITH_SECURITY_IMPLICATION(static_cast<unsigned>(size.width() * size.h
eight() * 4) <= m_data->length()); | 172 ASSERT_WITH_SECURITY_IMPLICATION(static_cast<unsigned>(size.width() * size.h
eight() * 4) <= m_data->length()); |
| 173 } | 173 } |
| 174 | 174 |
| 175 void ImageData::dispose() |
| 176 { |
| 177 m_data.clear(); |
| 178 } |
| 179 |
| 175 } // namespace blink | 180 } // namespace blink |
| OLD | NEW |