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/layout/LayoutObject.h" | |
46 #include "core/workers/WorkerGlobalScope.h" | |
47 #include "modules/canvas2d/CanvasRenderingContext2D.h" | |
48 #include "platform/SharedBuffer.h" | |
49 #include "platform/graphics/BitmapImage.h" | |
50 #include "platform/graphics/ImageSource.h" | |
51 #include "public/platform/WebSize.h" | |
52 #include <v8.h> | |
53 | |
54 namespace blink { | |
55 | |
56 static LayoutSize sizeFor(HTMLImageElement* image) | |
57 { | |
58 if (ImageResource* cachedImage = image->cachedImage()) | |
59 return cachedImage->imageSize(LayoutObject::shouldRespectImageOrientatio
n(image->layoutObject()), 1.0f); // FIXME: Not sure about this. | |
60 return LayoutSize(); | |
61 } | |
62 | |
63 static IntSize sizeFor(HTMLVideoElement* video) | |
64 { | |
65 if (WebMediaPlayer* webMediaPlayer = video->webMediaPlayer()) | |
66 return webMediaPlayer->naturalSize(); | |
67 return IntSize(); | |
68 } | |
69 | |
70 static ScriptPromise fulfillImageBitmap(ScriptState* scriptState, PassRefPtrWill
BeRawPtr<ImageBitmap> imageBitmap) | |
71 { | |
72 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | |
73 ScriptPromise promise = resolver->promise(); | |
74 if (imageBitmap) { | |
75 resolver->resolve(imageBitmap); | |
76 } else { | |
77 resolver->reject(ScriptValue(scriptState, v8::Null(scriptState->isolate(
)))); | |
78 } | |
79 return promise; | |
80 } | |
81 | |
82 ScriptPromise WindowImageBitmapFactories::createImageBitmap(ScriptState* scriptS
tate, EventTarget& eventTarget, HTMLImageElement* image, ExceptionState& excepti
onState) | |
83 { | |
84 LayoutSize s = sizeFor(image); | |
85 return createImageBitmap(scriptState, eventTarget, image, 0, 0, s.width(), s
.height(), exceptionState); | |
86 } | |
87 | |
88 ScriptPromise WindowImageBitmapFactories::createImageBitmap(ScriptState* scriptS
tate, EventTarget& eventTarget, HTMLImageElement* image, int sx, int sy, int sw,
int sh, ExceptionState& exceptionState) | |
89 { | |
90 // This variant does not work in worker threads. | |
91 ASSERT(eventTarget.toDOMWindow()); | |
92 | |
93 if (!image->cachedImage()) { | |
94 exceptionState.throwDOMException(InvalidStateError, "No image can be ret
rieved from the provided element."); | |
95 return ScriptPromise(); | |
96 } | |
97 if (image->cachedImage()->image()->isSVGImage()) { | |
98 exceptionState.throwDOMException(InvalidStateError, "The image element c
ontains an SVG image, which is unsupported."); | |
99 return ScriptPromise(); | |
100 } | |
101 if (!sw || !sh) { | |
102 exceptionState.throwDOMException(IndexSizeError, String::format("The sou
rce %s provided is 0.", sw ? "height" : "width")); | |
103 return ScriptPromise(); | |
104 } | |
105 if (!image->cachedImage()->image()->currentFrameHasSingleSecurityOrigin()) { | |
106 exceptionState.throwSecurityError("The source image contains image data
from multiple origins."); | |
107 return ScriptPromise(); | |
108 } | |
109 Document* document = eventTarget.toDOMWindow()->document(); | |
110 if (!image->cachedImage()->passesAccessControlCheck(document->securityOrigin
()) && document->securityOrigin()->taintsCanvas(image->src())) { | |
111 exceptionState.throwSecurityError("Cross-origin access to the source ima
ge is denied."); | |
112 return ScriptPromise(); | |
113 } | |
114 // FIXME: make ImageBitmap creation asynchronous crbug.com/258082 | |
115 return fulfillImageBitmap(scriptState, ImageBitmap::create(image, IntRect(sx
, sy, sw, sh))); | |
116 } | |
117 | |
118 ScriptPromise WindowImageBitmapFactories::createImageBitmap(ScriptState* scriptS
tate, EventTarget& eventTarget, HTMLVideoElement* video, ExceptionState& excepti
onState) | |
119 { | |
120 IntSize s = sizeFor(video); | |
121 return createImageBitmap(scriptState, eventTarget, video, 0, 0, s.width(), s
.height(), exceptionState); | |
122 } | |
123 | |
124 ScriptPromise WindowImageBitmapFactories::createImageBitmap(ScriptState* scriptS
tate, EventTarget& eventTarget, HTMLVideoElement* video, int sx, int sy, int sw,
int sh, ExceptionState& exceptionState) | |
125 { | |
126 // This variant does not work in worker threads. | |
127 ASSERT(eventTarget.toDOMWindow()); | |
128 | |
129 if (video->networkState() == HTMLMediaElement::NETWORK_EMPTY) { | |
130 exceptionState.throwDOMException(InvalidStateError, "The provided elemen
t has not retrieved data."); | |
131 return ScriptPromise(); | |
132 } | |
133 if (video->readyState() <= HTMLMediaElement::HAVE_METADATA) { | |
134 exceptionState.throwDOMException(InvalidStateError, "The provided elemen
t's player has no current data."); | |
135 return ScriptPromise(); | |
136 } | |
137 if (!sw || !sh) { | |
138 exceptionState.throwDOMException(IndexSizeError, String::format("The sou
rce %s provided is 0.", sw ? "height" : "width")); | |
139 return ScriptPromise(); | |
140 } | |
141 if (!video->hasSingleSecurityOrigin()) { | |
142 exceptionState.throwSecurityError("The source video contains image data
from multiple origins."); | |
143 return ScriptPromise(); | |
144 } | |
145 if (!video->webMediaPlayer()->didPassCORSAccessCheck() | |
146 && eventTarget.toDOMWindow()->document()->securityOrigin()->taintsCanvas
(video->currentSrc())) { | |
147 exceptionState.throwSecurityError("Cross-origin access to the source vid
eo is denied."); | |
148 return ScriptPromise(); | |
149 } | |
150 // FIXME: make ImageBitmap creation asynchronous crbug.com/258082 | |
151 return fulfillImageBitmap(scriptState, ImageBitmap::create(video, IntRect(sx
, sy, sw, sh))); | |
152 } | |
153 | |
154 ScriptPromise WindowImageBitmapFactories::createImageBitmap(ScriptState* scriptS
tate, EventTarget& eventTarget, CanvasRenderingContext2D* context, ExceptionStat
e& exceptionState) | |
155 { | |
156 return createImageBitmap(scriptState, eventTarget, context->canvas(), except
ionState); | |
157 } | |
158 | |
159 ScriptPromise WindowImageBitmapFactories::createImageBitmap(ScriptState* scriptS
tate, EventTarget& eventTarget, CanvasRenderingContext2D* context, int sx, int s
y, int sw, int sh, ExceptionState& exceptionState) | |
160 { | |
161 return createImageBitmap(scriptState, eventTarget, context->canvas(), sx, sy
, sw, sh, exceptionState); | |
162 } | |
163 | |
164 ScriptPromise WindowImageBitmapFactories::createImageBitmap(ScriptState* scriptS
tate, EventTarget& eventTarget, HTMLCanvasElement* canvas, ExceptionState& excep
tionState) | |
165 { | |
166 return createImageBitmap(scriptState, eventTarget, canvas, 0, 0, canvas->wid
th(), canvas->height(), exceptionState); | |
167 } | |
168 | |
169 ScriptPromise WindowImageBitmapFactories::createImageBitmap(ScriptState* scriptS
tate, EventTarget& eventTarget, HTMLCanvasElement* canvas, int sx, int sy, int s
w, int sh, ExceptionState& exceptionState) | |
170 { | |
171 // This variant does not work in worker threads. | |
172 ASSERT(eventTarget.toDOMWindow()); | |
173 | |
174 if (!canvas->originClean()) { | |
175 exceptionState.throwSecurityError("The canvas element provided is tainte
d with cross-origin data."); | |
176 return ScriptPromise(); | |
177 } | |
178 if (!sw || !sh) { | |
179 exceptionState.throwDOMException(IndexSizeError, String::format("The sou
rce %s provided is 0.", sw ? "height" : "width")); | |
180 return ScriptPromise(); | |
181 } | |
182 | |
183 // FIXME: make ImageBitmap creation asynchronous crbug.com/258082 | |
184 return fulfillImageBitmap(scriptState, canvas->isPaintable() ? ImageBitmap::
create(canvas, IntRect(sx, sy, sw, sh)) : nullptr); | |
185 } | |
186 | |
187 } // namespace blink | |
OLD | NEW |