OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (c) 2013, Google Inc. All rights reserved. | |
3 * | |
4 * Redistribution and use in source and binary forms, with or without | |
5 * modification, are permitted provided that the following conditions are | |
6 * met: | |
7 * | |
8 * * Redistributions of source code must retain the above copyright | |
9 * notice, this list of conditions and the following disclaimer. | |
10 * * Redistributions in binary form must reproduce the above | |
11 * copyright notice, this list of conditions and the following disclaimer | |
12 * in the documentation and/or other materials provided with the | |
13 * distribution. | |
14 * * Neither the name of Google Inc. nor the names of its | |
15 * contributors may be used to endorse or promote products derived from | |
16 * this software without specific prior written permission. | |
17 * | |
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
29 */ | |
30 | |
31 #include "config.h" | |
32 #include "modules/imagebitmap/WindowImageBitmapFactories.h" | |
33 | |
34 #include "bindings/core/v8/ExceptionState.h" | |
35 #include "bindings/core/v8/ScriptPromiseResolver.h" | |
36 #include "core/dom/ExceptionCode.h" | |
37 #include "core/dom/ExecutionContext.h" | |
38 #include "core/fileapi/Blob.h" | |
39 #include "core/frame/ImageBitmap.h" | |
40 #include "core/frame/LocalDOMWindow.h" | |
41 #include "core/html/HTMLCanvasElement.h" | |
42 #include "core/html/HTMLImageElement.h" | |
43 #include "core/html/HTMLVideoElement.h" | |
44 #include "core/html/ImageData.h" | |
45 #include "core/workers/WorkerGlobalScope.h" | |
46 #include "modules/canvas2d/CanvasRenderingContext2D.h" | |
47 #include "platform/SharedBuffer.h" | |
48 #include "platform/graphics/BitmapImage.h" | |
49 #include "platform/graphics/ImageSource.h" | |
50 #include "public/platform/WebSize.h" | |
51 #include <v8.h> | |
52 | |
53 namespace blink { | |
54 | |
55 static LayoutSize sizeFor(HTMLImageElement* image) | |
56 { | |
57 if (ImageResource* cachedImage = image->cachedImage()) | |
58 return cachedImage->imageSizeForLayoutObject(image->layoutObject(), 1.0f
); // FIXME: Not sure about this. | |
59 return LayoutSize(); | |
60 } | |
61 | |
62 static IntSize sizeFor(HTMLVideoElement* video) | |
63 { | |
64 if (WebMediaPlayer* webMediaPlayer = video->webMediaPlayer()) | |
65 return webMediaPlayer->naturalSize(); | |
66 return IntSize(); | |
67 } | |
68 | |
69 static ScriptPromise fulfillImageBitmap(ScriptState* scriptState, PassRefPtrWill
BeRawPtr<ImageBitmap> imageBitmap) | |
70 { | |
71 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | |
72 ScriptPromise promise = resolver->promise(); | |
73 if (imageBitmap) { | |
74 resolver->resolve(imageBitmap); | |
75 } else { | |
76 resolver->reject(ScriptValue(scriptState, v8::Null(scriptState->isolate(
)))); | |
77 } | |
78 return promise; | |
79 } | |
80 | |
81 ScriptPromise WindowImageBitmapFactories::createImageBitmap(ScriptState* scriptS
tate, EventTarget& eventTarget, HTMLImageElement* image, ExceptionState& excepti
onState) | |
82 { | |
83 LayoutSize s = sizeFor(image); | |
84 return createImageBitmap(scriptState, eventTarget, image, 0, 0, s.width(), s
.height(), exceptionState); | |
85 } | |
86 | |
87 ScriptPromise WindowImageBitmapFactories::createImageBitmap(ScriptState* scriptS
tate, EventTarget& eventTarget, HTMLImageElement* image, int sx, int sy, int sw,
int sh, ExceptionState& exceptionState) | |
88 { | |
89 // This variant does not work in worker threads. | |
90 ASSERT(eventTarget.toDOMWindow()); | |
91 | |
92 if (!image->cachedImage()) { | |
93 exceptionState.throwDOMException(InvalidStateError, "No image can be ret
rieved from the provided element."); | |
94 return ScriptPromise(); | |
95 } | |
96 if (image->cachedImage()->image()->isSVGImage()) { | |
97 exceptionState.throwDOMException(InvalidStateError, "The image element c
ontains an SVG image, which is unsupported."); | |
98 return ScriptPromise(); | |
99 } | |
100 if (!sw || !sh) { | |
101 exceptionState.throwDOMException(IndexSizeError, String::format("The sou
rce %s provided is 0.", sw ? "height" : "width")); | |
102 return ScriptPromise(); | |
103 } | |
104 if (!image->cachedImage()->image()->currentFrameHasSingleSecurityOrigin()) { | |
105 exceptionState.throwSecurityError("The source image contains image data
from multiple origins."); | |
106 return ScriptPromise(); | |
107 } | |
108 Document* document = eventTarget.toDOMWindow()->document(); | |
109 if (!image->cachedImage()->passesAccessControlCheck(document->securityOrigin
()) && document->securityOrigin()->taintsCanvas(image->src())) { | |
110 exceptionState.throwSecurityError("Cross-origin access to the source ima
ge is denied."); | |
111 return ScriptPromise(); | |
112 } | |
113 // FIXME: make ImageBitmap creation asynchronous crbug.com/258082 | |
114 return fulfillImageBitmap(scriptState, ImageBitmap::create(image, IntRect(sx
, sy, sw, sh))); | |
115 } | |
116 | |
117 ScriptPromise WindowImageBitmapFactories::createImageBitmap(ScriptState* scriptS
tate, EventTarget& eventTarget, HTMLVideoElement* video, ExceptionState& excepti
onState) | |
118 { | |
119 IntSize s = sizeFor(video); | |
120 return createImageBitmap(scriptState, eventTarget, video, 0, 0, s.width(), s
.height(), exceptionState); | |
121 } | |
122 | |
123 ScriptPromise WindowImageBitmapFactories::createImageBitmap(ScriptState* scriptS
tate, EventTarget& eventTarget, HTMLVideoElement* video, int sx, int sy, int sw,
int sh, ExceptionState& exceptionState) | |
124 { | |
125 // This variant does not work in worker threads. | |
126 ASSERT(eventTarget.toDOMWindow()); | |
127 | |
128 if (video->networkState() == HTMLMediaElement::NETWORK_EMPTY) { | |
129 exceptionState.throwDOMException(InvalidStateError, "The provided elemen
t has not retrieved data."); | |
130 return ScriptPromise(); | |
131 } | |
132 if (video->readyState() <= HTMLMediaElement::HAVE_METADATA) { | |
133 exceptionState.throwDOMException(InvalidStateError, "The provided elemen
t's player has no current data."); | |
134 return ScriptPromise(); | |
135 } | |
136 if (!sw || !sh) { | |
137 exceptionState.throwDOMException(IndexSizeError, String::format("The sou
rce %s provided is 0.", sw ? "height" : "width")); | |
138 return ScriptPromise(); | |
139 } | |
140 if (!video->hasSingleSecurityOrigin()) { | |
141 exceptionState.throwSecurityError("The source video contains image data
from multiple origins."); | |
142 return ScriptPromise(); | |
143 } | |
144 if (!video->webMediaPlayer()->didPassCORSAccessCheck() | |
145 && eventTarget.toDOMWindow()->document()->securityOrigin()->taintsCanvas
(video->currentSrc())) { | |
146 exceptionState.throwSecurityError("Cross-origin access to the source vid
eo is denied."); | |
147 return ScriptPromise(); | |
148 } | |
149 // FIXME: make ImageBitmap creation asynchronous crbug.com/258082 | |
150 return fulfillImageBitmap(scriptState, ImageBitmap::create(video, IntRect(sx
, sy, sw, sh))); | |
151 } | |
152 | |
153 ScriptPromise WindowImageBitmapFactories::createImageBitmap(ScriptState* scriptS
tate, EventTarget& eventTarget, CanvasRenderingContext2D* context, ExceptionStat
e& exceptionState) | |
154 { | |
155 return createImageBitmap(scriptState, eventTarget, context->canvas(), except
ionState); | |
156 } | |
157 | |
158 ScriptPromise WindowImageBitmapFactories::createImageBitmap(ScriptState* scriptS
tate, EventTarget& eventTarget, CanvasRenderingContext2D* context, int sx, int s
y, int sw, int sh, ExceptionState& exceptionState) | |
159 { | |
160 return createImageBitmap(scriptState, eventTarget, context->canvas(), sx, sy
, sw, sh, exceptionState); | |
161 } | |
162 | |
163 ScriptPromise WindowImageBitmapFactories::createImageBitmap(ScriptState* scriptS
tate, EventTarget& eventTarget, HTMLCanvasElement* canvas, ExceptionState& excep
tionState) | |
164 { | |
165 return createImageBitmap(scriptState, eventTarget, canvas, 0, 0, canvas->wid
th(), canvas->height(), exceptionState); | |
166 } | |
167 | |
168 ScriptPromise WindowImageBitmapFactories::createImageBitmap(ScriptState* scriptS
tate, EventTarget& eventTarget, HTMLCanvasElement* canvas, int sx, int sy, int s
w, int sh, ExceptionState& exceptionState) | |
169 { | |
170 // This variant does not work in worker threads. | |
171 ASSERT(eventTarget.toDOMWindow()); | |
172 | |
173 if (!canvas->originClean()) { | |
174 exceptionState.throwSecurityError("The canvas element provided is tainte
d with cross-origin data."); | |
175 return ScriptPromise(); | |
176 } | |
177 if (!sw || !sh) { | |
178 exceptionState.throwDOMException(IndexSizeError, String::format("The sou
rce %s provided is 0.", sw ? "height" : "width")); | |
179 return ScriptPromise(); | |
180 } | |
181 | |
182 // FIXME: make ImageBitmap creation asynchronous crbug.com/258082 | |
183 return fulfillImageBitmap(scriptState, canvas->isPaintable() ? ImageBitmap::
create(canvas, IntRect(sx, sy, sw, sh)) : nullptr); | |
184 } | |
185 | |
186 } // namespace blink | |
OLD | NEW |