2 * Copyright (c) 2013, Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "core/imagebitmap/ImageBitmapFactories.h"
34 #include "bindings/core/v8/ExceptionState.h"
35 #include "bindings/core/v8/ScriptPromiseResolver.h"
36 #include "core/dom/ExecutionContext.h"
37 #include "core/fileapi/Blob.h"
38 #include "core/frame/ImageBitmap.h"
39 #include "core/frame/LocalDOMWindow.h"
40 #include "core/html/HTMLCanvasElement.h"
41 #include "core/html/HTMLImageElement.h"
42 #include "core/html/HTMLVideoElement.h"
43 #include "core/html/ImageData.h"
44 #include "core/html/canvas/CanvasRenderingContext2D.h"
45 #include "core/workers/WorkerGlobalScope.h"
46 #include "platform/SharedBuffer.h"
47 #include "platform/graphics/BitmapImage.h"
48 #include "platform/graphics/ImageSource.h"
49 #include "platform/graphics/skia/NativeImageSkia.h"
54 static LayoutSize sizeFor(HTMLImageElement* image)
56 if (ImageResource* cachedImage = image->cachedImage())
57 return cachedImage->imageSizeForRenderer(image->renderer(), 1.0f); // FIXME: Not sure about this.
61 static IntSize sizeFor(HTMLVideoElement* video)
63 if (blink::WebMediaPlayer* webMediaPlayer = video->webMediaPlayer())
64 return webMediaPlayer->naturalSize();
68 static ScriptPromise fulfillImageBitmap(ScriptState* scriptState, PassRefPtrWillBeRawPtr<ImageBitmap> imageBitmap)
70 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState);
71 ScriptPromise promise = resolver->promise();
73 resolver->resolve(imageBitmap);
75 resolver->reject(ScriptValue(scriptState, v8::Null(scriptState->isolate())));
80 ScriptPromise ImageBitmapFactories::createImageBitmap(ScriptState* scriptState, EventTarget& eventTarget, HTMLImageElement* image, ExceptionState& exceptionState)
82 LayoutSize s = sizeFor(image);
83 return createImageBitmap(scriptState, eventTarget, image, 0, 0, s.width(), s.height(), exceptionState);
86 ScriptPromise ImageBitmapFactories::createImageBitmap(ScriptState* scriptState, EventTarget& eventTarget, HTMLImageElement* image, int sx, int sy, int sw, int sh, ExceptionState& exceptionState)
88 // This variant does not work in worker threads.
89 ASSERT(eventTarget.toDOMWindow());
91 if (!image->cachedImage()) {
92 exceptionState.throwDOMException(InvalidStateError, "No image can be retrieved from the provided element.");
93 return ScriptPromise();
95 if (image->cachedImage()->image()->isSVGImage()) {
96 exceptionState.throwDOMException(InvalidStateError, "The image element contains an SVG image, which is unsupported.");
97 return ScriptPromise();
100 exceptionState.throwDOMException(IndexSizeError, String::format("The source %s provided is 0.", sw ? "height" : "width"));
101 return ScriptPromise();
103 if (!image->cachedImage()->image()->currentFrameHasSingleSecurityOrigin()) {
104 exceptionState.throwSecurityError("The source image contains image data from multiple origins.");
105 return ScriptPromise();
107 if (!image->cachedImage()->passesAccessControlCheck(eventTarget.toDOMWindow()->document()->securityOrigin()) && eventTarget.toDOMWindow()->document()->securityOrigin()->taintsCanvas(image->src())) {
108 exceptionState.throwSecurityError("Cross-origin access to the source image is denied.");
109 return ScriptPromise();
111 // FIXME: make ImageBitmap creation asynchronous crbug.com/258082
112 return fulfillImageBitmap(scriptState, ImageBitmap::create(image, IntRect(sx, sy, sw, sh)));
115 ScriptPromise ImageBitmapFactories::createImageBitmap(ScriptState* scriptState, EventTarget& eventTarget, HTMLVideoElement* video, ExceptionState& exceptionState)
117 IntSize s = sizeFor(video);
118 return createImageBitmap(scriptState, eventTarget, video, 0, 0, s.width(), s.height(), exceptionState);
121 ScriptPromise ImageBitmapFactories::createImageBitmap(ScriptState* scriptState, EventTarget& eventTarget, HTMLVideoElement* video, int sx, int sy, int sw, int sh, ExceptionState& exceptionState)
123 // This variant does not work in worker threads.
124 ASSERT(eventTarget.toDOMWindow());
126 if (video->networkState() == HTMLMediaElement::NETWORK_EMPTY) {
127 exceptionState.throwDOMException(InvalidStateError, "The provided element has not retrieved data.");
128 return ScriptPromise();
130 if (video->readyState() <= HTMLMediaElement::HAVE_METADATA) {
131 exceptionState.throwDOMException(InvalidStateError, "The provided element's player has no current data.");
132 return ScriptPromise();
135 exceptionState.throwDOMException(IndexSizeError, String::format("The source %s provided is 0.", sw ? "height" : "width"));
136 return ScriptPromise();
138 if (!video->hasSingleSecurityOrigin()) {
139 exceptionState.throwSecurityError("The source video contains image data from multiple origins.");
140 return ScriptPromise();
142 if (!video->webMediaPlayer()->didPassCORSAccessCheck()
143 && eventTarget.toDOMWindow()->document()->securityOrigin()->taintsCanvas(video->currentSrc())) {
144 exceptionState.throwSecurityError("Cross-origin access to the source video is denied.");
145 return ScriptPromise();
147 // FIXME: make ImageBitmap creation asynchronous crbug.com/258082
148 return fulfillImageBitmap(scriptState, ImageBitmap::create(video, IntRect(sx, sy, sw, sh)));
151 ScriptPromise ImageBitmapFactories::createImageBitmap(ScriptState* scriptState, EventTarget& eventTarget, CanvasRenderingContext2D* context, ExceptionState& exceptionState)
153 return createImageBitmap(scriptState, eventTarget, context->canvas(), exceptionState);
156 ScriptPromise ImageBitmapFactories::createImageBitmap(ScriptState* scriptState, EventTarget& eventTarget, CanvasRenderingContext2D* context, int sx, int sy, int sw, int sh, ExceptionState& exceptionState)
158 return createImageBitmap(scriptState, eventTarget, context->canvas(), sx, sy, sw, sh, exceptionState);
161 ScriptPromise ImageBitmapFactories::createImageBitmap(ScriptState* scriptState, EventTarget& eventTarget, HTMLCanvasElement* canvas, ExceptionState& exceptionState)
163 return createImageBitmap(scriptState, eventTarget, canvas, 0, 0, canvas->width(), canvas->height(), exceptionState);
166 ScriptPromise ImageBitmapFactories::createImageBitmap(ScriptState* scriptState, EventTarget& eventTarget, HTMLCanvasElement* canvas, int sx, int sy, int sw, int sh, ExceptionState& exceptionState)
168 // This variant does not work in worker threads.
169 ASSERT(eventTarget.toDOMWindow());
171 if (!canvas->originClean()) {
172 exceptionState.throwSecurityError("The canvas element provided is tainted with cross-origin data.");
173 return ScriptPromise();
176 exceptionState.throwDOMException(IndexSizeError, String::format("The source %s provided is 0.", sw ? "height" : "width"));
177 return ScriptPromise();
180 // FIXME: make ImageBitmap creation asynchronous crbug.com/258082
181 return fulfillImageBitmap(scriptState, canvas->buffer() ? ImageBitmap::create(canvas, IntRect(sx, sy, sw, sh)) : nullptr);
184 ScriptPromise ImageBitmapFactories::createImageBitmap(ScriptState* scriptState, EventTarget& eventTarget, Blob* blob, ExceptionState& exceptionState)
186 ImageBitmapLoader* loader = ImageBitmapFactories::ImageBitmapLoader::create(from(eventTarget), IntRect(), scriptState);
187 ScriptPromise promise = loader->promise();
188 from(eventTarget).addLoader(loader);
189 loader->loadBlobAsync(eventTarget.executionContext(), blob);
193 ScriptPromise ImageBitmapFactories::createImageBitmap(ScriptState* scriptState, EventTarget& eventTarget, Blob* blob, int sx, int sy, int sw, int sh, ExceptionState& exceptionState)
196 exceptionState.throwDOMException(IndexSizeError, String::format("The source %s provided is 0.", sw ? "height" : "width"));
197 return ScriptPromise();
199 ImageBitmapLoader* loader = ImageBitmapFactories::ImageBitmapLoader::create(from(eventTarget), IntRect(sx, sy, sw, sh), scriptState);
200 ScriptPromise promise = loader->promise();
201 from(eventTarget).addLoader(loader);
202 loader->loadBlobAsync(eventTarget.executionContext(), blob);
206 ScriptPromise ImageBitmapFactories::createImageBitmap(ScriptState* scriptState, EventTarget& eventTarget, ImageData* data, ExceptionState& exceptionState)
208 return createImageBitmap(scriptState, eventTarget, data, 0, 0, data->width(), data->height(), exceptionState);
211 ScriptPromise ImageBitmapFactories::createImageBitmap(ScriptState* scriptState, EventTarget& eventTarget, ImageData* data, int sx, int sy, int sw, int sh, ExceptionState& exceptionState)
214 exceptionState.throwDOMException(IndexSizeError, String::format("The source %s provided is 0.", sw ? "height" : "width"));
215 return ScriptPromise();
217 // FIXME: make ImageBitmap creation asynchronous crbug.com/258082
218 return fulfillImageBitmap(scriptState, ImageBitmap::create(data, IntRect(sx, sy, sw, sh)));
221 ScriptPromise ImageBitmapFactories::createImageBitmap(ScriptState* scriptState, EventTarget& eventTarget, ImageBitmap* bitmap, ExceptionState& exceptionState)
223 return createImageBitmap(scriptState, eventTarget, bitmap, 0, 0, bitmap->width(), bitmap->height(), exceptionState);
226 ScriptPromise ImageBitmapFactories::createImageBitmap(ScriptState* scriptState, EventTarget& eventTarget, ImageBitmap* bitmap, int sx, int sy, int sw, int sh, ExceptionState& exceptionState)
229 exceptionState.throwDOMException(IndexSizeError, String::format("The source %s provided is 0.", sw ? "height" : "width"));
230 return ScriptPromise();
232 // FIXME: make ImageBitmap creation asynchronous crbug.com/258082
233 return fulfillImageBitmap(scriptState, ImageBitmap::create(bitmap, IntRect(sx, sy, sw, sh)));
236 const char* ImageBitmapFactories::supplementName()
238 return "ImageBitmapFactories";
241 ImageBitmapFactories& ImageBitmapFactories::from(EventTarget& eventTarget)
243 if (LocalDOMWindow* window = eventTarget.toDOMWindow())
244 return fromInternal(*window);
246 ASSERT(eventTarget.executionContext()->isWorkerGlobalScope());
247 return ImageBitmapFactories::fromInternal(*toWorkerGlobalScope(eventTarget.executionContext()));
250 template<class GlobalObject>
251 ImageBitmapFactories& ImageBitmapFactories::fromInternal(GlobalObject& object)
253 ImageBitmapFactories* supplement = static_cast<ImageBitmapFactories*>(WillBeHeapSupplement<GlobalObject>::from(object, supplementName()));
255 supplement = new ImageBitmapFactories();
256 WillBeHeapSupplement<GlobalObject>::provideTo(object, supplementName(), adoptPtrWillBeNoop(supplement));
261 void ImageBitmapFactories::addLoader(ImageBitmapLoader* loader)
263 m_pendingLoaders.add(loader);
266 void ImageBitmapFactories::didFinishLoading(ImageBitmapLoader* loader)
268 ASSERT(m_pendingLoaders.contains(loader));
269 m_pendingLoaders.remove(loader);
272 ImageBitmapFactories::ImageBitmapLoader::ImageBitmapLoader(ImageBitmapFactories& factory, const IntRect& cropRect, ScriptState* scriptState)
273 : m_loader(FileReaderLoader::ReadAsArrayBuffer, this)
274 , m_factory(&factory)
275 , m_resolver(ScriptPromiseResolver::create(scriptState))
276 , m_cropRect(cropRect)
280 void ImageBitmapFactories::ImageBitmapLoader::loadBlobAsync(ExecutionContext* context, Blob* blob)
282 m_loader.start(context, blob->blobDataHandle());
285 void ImageBitmapFactories::trace(Visitor* visitor)
287 visitor->trace(m_pendingLoaders);
288 WillBeHeapSupplement<LocalDOMWindow>::trace(visitor);
289 WillBeHeapSupplement<WorkerGlobalScope>::trace(visitor);
292 void ImageBitmapFactories::ImageBitmapLoader::rejectPromise()
294 m_resolver->reject(ScriptValue(m_resolver->scriptState(), v8::Null(m_resolver->scriptState()->isolate())));
295 m_factory->didFinishLoading(this);
298 void ImageBitmapFactories::ImageBitmapLoader::didFinishLoading()
300 if (!m_loader.arrayBufferResult()) {
304 RefPtr<SharedBuffer> sharedBuffer = SharedBuffer::create((char*)m_loader.arrayBufferResult()->data(), m_loader.arrayBufferResult()->byteLength());
306 OwnPtr<ImageSource> source = adoptPtr(new ImageSource());
307 source->setData(*sharedBuffer, true);
308 RefPtr<NativeImageSkia> imageSkia = source->createFrameAtIndex(0);
314 RefPtr<Image> image = BitmapImage::create(imageSkia);
315 if (!image->width() || !image->height()) {
319 if (!m_cropRect.width() && !m_cropRect.height()) {
320 // No cropping variant was called.
321 m_cropRect = IntRect(IntPoint(), image->size());
324 RefPtrWillBeRawPtr<ImageBitmap> imageBitmap = ImageBitmap::create(image.get(), m_cropRect);
325 m_resolver->resolve(imageBitmap.release());
326 m_factory->didFinishLoading(this);
329 void ImageBitmapFactories::ImageBitmapLoader::didFail(FileError::ErrorCode)
334 void ImageBitmapFactories::ImageBitmapLoader::trace(Visitor* visitor)
336 visitor->trace(m_factory);