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

Side by Side Diff: third_party/WebKit/Source/core/offscreencanvas/OffscreenCanvas.cpp

Issue 2420203002: Implement convertToBlob() in OffscreenCanvas (Closed)
Patch Set: rebase and fix global-interface-listing-service-worker-expected.txt Created 4 years, 2 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
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "core/offscreencanvas/OffscreenCanvas.h" 5 #include "core/offscreencanvas/OffscreenCanvas.h"
6 6
7 #include "core/dom/ExceptionCode.h" 7 #include "core/dom/ExceptionCode.h"
8 #include "core/fileapi/Blob.h"
9 #include "core/html/ImageData.h"
10 #include "core/html/canvas/CanvasAsyncBlobCreator.h"
8 #include "core/html/canvas/CanvasContextCreationAttributes.h" 11 #include "core/html/canvas/CanvasContextCreationAttributes.h"
9 #include "core/html/canvas/CanvasRenderingContext.h" 12 #include "core/html/canvas/CanvasRenderingContext.h"
10 #include "core/html/canvas/CanvasRenderingContextFactory.h" 13 #include "core/html/canvas/CanvasRenderingContextFactory.h"
11 #include "platform/graphics/Image.h" 14 #include "platform/graphics/Image.h"
12 #include "platform/graphics/ImageBuffer.h" 15 #include "platform/graphics/ImageBuffer.h"
13 #include "platform/graphics/OffscreenCanvasFrameDispatcherImpl.h" 16 #include "platform/graphics/OffscreenCanvasFrameDispatcherImpl.h"
17 #include "platform/graphics/StaticBitmapImage.h"
18 #include "platform/image-encoders/ImageEncoderUtils.h"
14 #include "wtf/MathExtras.h" 19 #include "wtf/MathExtras.h"
15 #include <memory> 20 #include <memory>
16 21
17 namespace blink { 22 namespace blink {
18 23
19 OffscreenCanvas::OffscreenCanvas(const IntSize& size) 24 OffscreenCanvas::OffscreenCanvas(const IntSize& size)
20 : m_size(size), m_originClean(true) {} 25 : m_size(size), m_originClean(true) {}
21 26
22 OffscreenCanvas* OffscreenCanvas::create(unsigned width, unsigned height) { 27 OffscreenCanvas* OffscreenCanvas::create(unsigned width, unsigned height) {
23 return new OffscreenCanvas( 28 return new OffscreenCanvas(
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
176 if (!m_frameDispatcher) { 181 if (!m_frameDispatcher) {
177 // The frame dispatcher connects the current thread of OffscreenCanvas 182 // The frame dispatcher connects the current thread of OffscreenCanvas
178 // (either main or worker) to the browser process and remains unchanged 183 // (either main or worker) to the browser process and remains unchanged
179 // throughout the lifetime of this OffscreenCanvas. 184 // throughout the lifetime of this OffscreenCanvas.
180 m_frameDispatcher = wrapUnique(new OffscreenCanvasFrameDispatcherImpl( 185 m_frameDispatcher = wrapUnique(new OffscreenCanvasFrameDispatcherImpl(
181 m_clientId, m_sinkId, m_localId, m_nonce, width(), height())); 186 m_clientId, m_sinkId, m_localId, m_nonce, width(), height()));
182 } 187 }
183 return m_frameDispatcher.get(); 188 return m_frameDispatcher.get();
184 } 189 }
185 190
191 ScriptPromise OffscreenCanvas::convertToBlob(ScriptState* scriptState,
192 const ImageEncodeOptions& options,
193 ExceptionState& exceptionState) {
194 if (this->isNeutered()) {
195 exceptionState.throwDOMException(InvalidStateError,
196 "OffscreenCanvas object is detached.");
197 return exceptionState.reject(scriptState);
198 }
199
200 if (!this->originClean()) {
201 exceptionState.throwSecurityError(
202 "Tainted OffscreenCanvas may not be exported.");
203 return exceptionState.reject(scriptState);
204 }
205
206 if (!this->isPaintable()) {
207 return ScriptPromise();
208 }
209
210 double startTime = WTF::monotonicallyIncreasingTime();
211 String encodingMimeType = ImageEncoderUtils::toEncodingMimeType(
212 options.type(), ImageEncoderUtils::EncodeReasonConvertToBlobPromise);
213
214 ImageData* imageData = nullptr;
215 if (this->renderingContext()) {
216 imageData = this->renderingContext()->toImageData(SnapshotReasonUnknown);
217 }
218 if (!imageData) {
219 return ScriptPromise();
220 }
221
222 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
223
224 Document* document =
225 scriptState->getExecutionContext()->isDocument()
226 ? static_cast<Document*>(scriptState->getExecutionContext())
227 : nullptr;
228
229 CanvasAsyncBlobCreator* asyncCreator = CanvasAsyncBlobCreator::create(
230 imageData->data(), encodingMimeType, imageData->size(), startTime,
231 document, resolver);
232
233 asyncCreator->scheduleAsyncBlobCreation(options.quality());
234
235 return resolver->promise();
236 }
237
186 DEFINE_TRACE(OffscreenCanvas) { 238 DEFINE_TRACE(OffscreenCanvas) {
187 visitor->trace(m_context); 239 visitor->trace(m_context);
188 } 240 }
189 241
190 } // namespace blink 242 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698