Index: third_party/WebKit/Source/core/offscreencanvas/OffscreenCanvas.cpp |
diff --git a/third_party/WebKit/Source/core/offscreencanvas/OffscreenCanvas.cpp b/third_party/WebKit/Source/core/offscreencanvas/OffscreenCanvas.cpp |
index 816d7e968fb6a8d98423feab9b38795546a49659..4a76b803e2834e17aba2ee6c6246523b8c87e63b 100644 |
--- a/third_party/WebKit/Source/core/offscreencanvas/OffscreenCanvas.cpp |
+++ b/third_party/WebKit/Source/core/offscreencanvas/OffscreenCanvas.cpp |
@@ -5,12 +5,17 @@ |
#include "core/offscreencanvas/OffscreenCanvas.h" |
#include "core/dom/ExceptionCode.h" |
+#include "core/fileapi/Blob.h" |
+#include "core/html/ImageData.h" |
+#include "core/html/canvas/CanvasAsyncBlobCreator.h" |
#include "core/html/canvas/CanvasContextCreationAttributes.h" |
#include "core/html/canvas/CanvasRenderingContext.h" |
#include "core/html/canvas/CanvasRenderingContextFactory.h" |
#include "platform/graphics/Image.h" |
#include "platform/graphics/ImageBuffer.h" |
#include "platform/graphics/OffscreenCanvasFrameDispatcherImpl.h" |
+#include "platform/graphics/StaticBitmapImage.h" |
+#include "platform/image-encoders/ImageEncoderUtils.h" |
#include "wtf/MathExtras.h" |
#include <memory> |
@@ -183,6 +188,53 @@ OffscreenCanvasFrameDispatcher* OffscreenCanvas::getOrCreateFrameDispatcher() { |
return m_frameDispatcher.get(); |
} |
+ScriptPromise OffscreenCanvas::convertToBlob(ScriptState* scriptState, |
+ const ImageEncodeOptions& options, |
+ ExceptionState& exceptionState) { |
+ if (this->isNeutered()) { |
+ exceptionState.throwDOMException(InvalidStateError, |
+ "OffscreenCanvas object is detached."); |
+ return exceptionState.reject(scriptState); |
+ } |
+ |
+ if (!this->originClean()) { |
+ exceptionState.throwSecurityError( |
+ "Tainted OffscreenCanvas may not be exported."); |
+ return exceptionState.reject(scriptState); |
+ } |
+ |
+ if (!this->isPaintable()) { |
+ return ScriptPromise(); |
+ } |
+ |
+ double startTime = WTF::monotonicallyIncreasingTime(); |
+ String encodingMimeType = ImageEncoderUtils::toEncodingMimeType( |
+ options.type(), ImageEncoderUtils::EncodeReasonConvertToBlobPromise); |
+ |
+ ImageData* imageData = nullptr; |
+ if (this->renderingContext()) { |
+ imageData = this->renderingContext()->toImageData(SnapshotReasonUnknown); |
+ } |
+ if (!imageData) { |
+ return ScriptPromise(); |
+ } |
+ |
+ ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
+ |
+ Document* document = |
+ scriptState->getExecutionContext()->isDocument() |
+ ? static_cast<Document*>(scriptState->getExecutionContext()) |
+ : nullptr; |
+ |
+ CanvasAsyncBlobCreator* asyncCreator = CanvasAsyncBlobCreator::create( |
+ imageData->data(), encodingMimeType, imageData->size(), startTime, |
+ document, resolver); |
+ |
+ asyncCreator->scheduleAsyncBlobCreation(options.quality()); |
+ |
+ return resolver->promise(); |
+} |
+ |
DEFINE_TRACE(OffscreenCanvas) { |
visitor->trace(m_context); |
} |