Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(666)

Side by Side Diff: Source/core/html/ImageData.cpp

Issue 196343032: Implement ImageData constructors. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Restrict feature test to the actual constructor(s) Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Source/core/html/ImageData.h ('k') | Source/core/html/ImageData.idl » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 11 matching lines...) Expand all
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
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 "config.h" 29 #include "config.h"
30 #include "core/html/ImageData.h" 30 #include "core/html/ImageData.h"
31 31
32 #include "RuntimeEnabledFeatures.h"
33 #include "bindings/v8/ExceptionState.h"
34 #include "core/dom/ExceptionCode.h"
35
32 namespace WebCore { 36 namespace WebCore {
33 37
34 PassRefPtr<ImageData> ImageData::create(const IntSize& size) 38 PassRefPtr<ImageData> ImageData::create(const IntSize& size)
35 { 39 {
36 Checked<int, RecordOverflow> dataSize = 4; 40 Checked<int, RecordOverflow> dataSize = 4;
37 dataSize *= size.width(); 41 dataSize *= size.width();
38 dataSize *= size.height(); 42 dataSize *= size.height();
39 if (dataSize.hasOverflowed()) 43 if (dataSize.hasOverflowed())
40 return nullptr; 44 return nullptr;
41 45
42 return adoptRef(new ImageData(size)); 46 return adoptRef(new ImageData(size));
43 } 47 }
44 48
45 PassRefPtr<ImageData> ImageData::create(const IntSize& size, PassRefPtr<Uint8Cla mpedArray> byteArray) 49 PassRefPtr<ImageData> ImageData::create(const IntSize& size, PassRefPtr<Uint8Cla mpedArray> byteArray)
46 { 50 {
47 Checked<int, RecordOverflow> dataSize = 4; 51 Checked<int, RecordOverflow> dataSize = 4;
48 dataSize *= size.width(); 52 dataSize *= size.width();
49 dataSize *= size.height(); 53 dataSize *= size.height();
50 if (dataSize.hasOverflowed()) 54 if (dataSize.hasOverflowed())
51 return nullptr; 55 return nullptr;
52 56
53 if (dataSize.unsafeGet() < 0 57 if (dataSize.unsafeGet() < 0
54 || static_cast<unsigned>(dataSize.unsafeGet()) > byteArray->length()) 58 || static_cast<unsigned>(dataSize.unsafeGet()) > byteArray->length())
55 return nullptr; 59 return nullptr;
56 60
57 return adoptRef(new ImageData(size, byteArray)); 61 return adoptRef(new ImageData(size, byteArray));
58 } 62 }
59 63
64 PassRefPtr<ImageData> ImageData::create(unsigned width, unsigned height, Excepti onState& exceptionState)
65 {
66 if (!RuntimeEnabledFeatures::imageDataConstructorEnabled()) {
67 exceptionState.throwTypeError("Illegal constructor");
68 return nullptr;
69 }
70 if (!width || !height) {
71 exceptionState.throwDOMException(IndexSizeError, String::format("The sou rce %s is zero or not a number.", width ? "height" : "width"));
72 return nullptr;
73 }
74
75 Checked<unsigned, RecordOverflow> dataSize = 4;
76 dataSize *= width;
77 dataSize *= height;
78 if (dataSize.hasOverflowed()) {
79 exceptionState.throwDOMException(IndexSizeError, "The requested image si ze exceeds the supported range.");
80 return nullptr;
81 }
82
83 RefPtr<ImageData> imageData = adoptRef(new ImageData(IntSize(width, height)) );
84 imageData->data()->zeroFill();
85 return imageData.release();
86 }
87
88 PassRefPtr<ImageData> ImageData::create(Uint8ClampedArray* data, unsigned width, unsigned height, ExceptionState& exceptionState)
89 {
90 if (!RuntimeEnabledFeatures::imageDataConstructorEnabled()) {
91 exceptionState.throwTypeError("Illegal constructor");
92 return nullptr;
93 }
94 if (!data) {
95 exceptionState.throwTypeError("Expected a Uint8ClampedArray as first arg ument.");
96 return nullptr;
97 }
98 if (!width) {
99 exceptionState.throwDOMException(IndexSizeError, "The source width is ze ro or not a number.");
100 return nullptr;
101 }
102
103 unsigned length = data->length();
104 if (!length) {
105 exceptionState.throwDOMException(IndexSizeError, "The input data has a z ero byte length.");
106 return nullptr;
107 }
108 if (length % 4) {
109 exceptionState.throwDOMException(IndexSizeError, "The input data byte le ngth is not a multiple of 4.");
110 return nullptr;
111 }
112 length /= 4;
113 if (length % width) {
114 exceptionState.throwDOMException(IndexSizeError, "The input data byte le ngth is not a multiple of (4 * width).");
115 return nullptr;
116 }
117 if (!height) {
118 height = length / width;
119 } else if (height != length / width) {
120 exceptionState.throwDOMException(IndexSizeError, "The input data byte le ngth is not equal to (4 * width * height).");
121 return nullptr;
122 }
123
124 return adoptRef(new ImageData(IntSize(width, height), data));
125 }
126
60 ImageData::ImageData(const IntSize& size) 127 ImageData::ImageData(const IntSize& size)
61 : m_size(size) 128 : m_size(size)
62 , m_data(Uint8ClampedArray::createUninitialized(size.width() * size.height() * 4)) 129 , m_data(Uint8ClampedArray::createUninitialized(size.width() * size.height() * 4))
63 { 130 {
64 ScriptWrappable::init(this); 131 ScriptWrappable::init(this);
65 } 132 }
66 133
67 ImageData::ImageData(const IntSize& size, PassRefPtr<Uint8ClampedArray> byteArra y) 134 ImageData::ImageData(const IntSize& size, PassRefPtr<Uint8ClampedArray> byteArra y)
68 : m_size(size) 135 : m_size(size)
69 , m_data(byteArray) 136 , m_data(byteArray)
70 { 137 {
71 ASSERT_WITH_SECURITY_IMPLICATION(static_cast<unsigned>(size.width() * size.h eight() * 4) <= m_data->length()); 138 ASSERT_WITH_SECURITY_IMPLICATION(static_cast<unsigned>(size.width() * size.h eight() * 4) <= m_data->length());
72 ScriptWrappable::init(this); 139 ScriptWrappable::init(this);
73 } 140 }
74 141
75 } 142 }
76 143
OLDNEW
« no previous file with comments | « Source/core/html/ImageData.h ('k') | Source/core/html/ImageData.idl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698