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

Side by Side Diff: Source/core/html/HTMLCanvasElement.cpp

Issue 1257253004: [HTMLCanvasElement.toBlob] Default callback version without scheduler (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Removed unnecesary rebaseline Created 5 years, 4 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
« no previous file with comments | « Source/core/html/HTMLCanvasElement.h ('k') | Source/core/html/HTMLCanvasElement.idl » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2004, 2006, 2007 Apple Inc. All rights reserved. 2 * Copyright (C) 2004, 2006, 2007 Apple Inc. All rights reserved.
3 * Copyright (C) 2007 Alp Toker <alp@atoker.com> 3 * Copyright (C) 2007 Alp Toker <alp@atoker.com>
4 * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved. 4 * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved.
5 * 5 *
6 * Redistribution and use in source and binary forms, with or without 6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions 7 * modification, are permitted provided that the following conditions
8 * are met: 8 * are met:
9 * 1. Redistributions of source code must retain the above copyright 9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer. 10 * notice, this list of conditions and the following disclaimer.
(...skipping 16 matching lines...) Expand all
27 27
28 #include "config.h" 28 #include "config.h"
29 #include "core/html/HTMLCanvasElement.h" 29 #include "core/html/HTMLCanvasElement.h"
30 30
31 #include "bindings/core/v8/ExceptionMessages.h" 31 #include "bindings/core/v8/ExceptionMessages.h"
32 #include "bindings/core/v8/ExceptionState.h" 32 #include "bindings/core/v8/ExceptionState.h"
33 #include "bindings/core/v8/ScriptController.h" 33 #include "bindings/core/v8/ScriptController.h"
34 #include "core/HTMLNames.h" 34 #include "core/HTMLNames.h"
35 #include "core/dom/Document.h" 35 #include "core/dom/Document.h"
36 #include "core/dom/ExceptionCode.h" 36 #include "core/dom/ExceptionCode.h"
37 #include "core/fileapi/File.h"
37 #include "core/frame/LocalFrame.h" 38 #include "core/frame/LocalFrame.h"
38 #include "core/frame/Settings.h" 39 #include "core/frame/Settings.h"
39 #include "core/html/ImageData.h" 40 #include "core/html/ImageData.h"
40 #include "core/html/canvas/CanvasContextCreationAttributes.h" 41 #include "core/html/canvas/CanvasContextCreationAttributes.h"
41 #include "core/html/canvas/CanvasFontCache.h" 42 #include "core/html/canvas/CanvasFontCache.h"
42 #include "core/html/canvas/CanvasRenderingContext.h" 43 #include "core/html/canvas/CanvasRenderingContext.h"
43 #include "core/html/canvas/CanvasRenderingContextFactory.h" 44 #include "core/html/canvas/CanvasRenderingContextFactory.h"
44 #include "core/layout/LayoutHTMLCanvas.h" 45 #include "core/layout/LayoutHTMLCanvas.h"
45 #include "core/paint/DeprecatedPaintLayer.h" 46 #include "core/paint/DeprecatedPaintLayer.h"
46 #include "platform/MIMETypeRegistry.h" 47 #include "platform/MIMETypeRegistry.h"
47 #include "platform/RuntimeEnabledFeatures.h" 48 #include "platform/RuntimeEnabledFeatures.h"
49 #include "platform/ThreadSafeFunctional.h"
48 #include "platform/graphics/Canvas2DImageBufferSurface.h" 50 #include "platform/graphics/Canvas2DImageBufferSurface.h"
49 #include "platform/graphics/ExpensiveCanvasHeuristicParameters.h" 51 #include "platform/graphics/ExpensiveCanvasHeuristicParameters.h"
50 #include "platform/graphics/ImageBuffer.h" 52 #include "platform/graphics/ImageBuffer.h"
51 #include "platform/graphics/RecordingImageBufferSurface.h" 53 #include "platform/graphics/RecordingImageBufferSurface.h"
52 #include "platform/graphics/StaticBitmapImage.h" 54 #include "platform/graphics/StaticBitmapImage.h"
53 #include "platform/graphics/UnacceleratedImageBufferSurface.h" 55 #include "platform/graphics/UnacceleratedImageBufferSurface.h"
54 #include "platform/graphics/gpu/AcceleratedImageBufferSurface.h" 56 #include "platform/graphics/gpu/AcceleratedImageBufferSurface.h"
55 #include "platform/transforms/AffineTransform.h" 57 #include "platform/transforms/AffineTransform.h"
56 #include "public/platform/Platform.h" 58 #include "public/platform/Platform.h"
59 #include "public/platform/WebTraceLocation.h"
57 #include <math.h> 60 #include <math.h>
58 #include <v8.h> 61 #include <v8.h>
59 62
60 namespace blink { 63 namespace blink {
61 64
62 using namespace HTMLNames; 65 using namespace HTMLNames;
63 66
64 namespace { 67 namespace {
65 68
66 // These values come from the WhatWG spec. 69 // These values come from the WhatWG spec.
(...skipping 434 matching lines...) Expand 10 before | Expand all | Expand 10 after
501 return String("data:,"); 504 return String("data:,");
502 505
503 String encodingMimeType = toEncodingMimeType(mimeType); 506 String encodingMimeType = toEncodingMimeType(mimeType);
504 507
505 ImageData* imageData = toImageData(sourceBuffer); 508 ImageData* imageData = toImageData(sourceBuffer);
506 ScopedDisposal<ImageData> disposer(imageData); 509 ScopedDisposal<ImageData> disposer(imageData);
507 510
508 return ImageDataBuffer(imageData->size(), imageData->data()->data()).toDataU RL(encodingMimeType, quality); 511 return ImageDataBuffer(imageData->size(), imageData->data()->data()).toDataU RL(encodingMimeType, quality);
509 } 512 }
510 513
514 static void invokeFileCallbackWithBlob(FileCallback* callback, File* blob)
515 {
516 callback->handleEvent(blob);
517 }
518
511 String HTMLCanvasElement::toDataURL(const String& mimeType, const ScriptValue& q ualityArgument, ExceptionState& exceptionState) const 519 String HTMLCanvasElement::toDataURL(const String& mimeType, const ScriptValue& q ualityArgument, ExceptionState& exceptionState) const
512 { 520 {
513 if (!originClean()) { 521 if (!originClean()) {
514 exceptionState.throwSecurityError("Tainted canvases may not be exported. "); 522 exceptionState.throwSecurityError("Tainted canvases may not be exported. ");
515 return String(); 523 return String();
516 } 524 }
517 double quality; 525 double quality;
518 double* qualityPtr = nullptr; 526 double* qualityPtr = nullptr;
519 if (!qualityArgument.isEmpty()) { 527 if (!qualityArgument.isEmpty()) {
520 v8::Local<v8::Value> v8Value = qualityArgument.v8Value(); 528 v8::Local<v8::Value> v8Value = qualityArgument.v8Value();
521 if (v8Value->IsNumber()) { 529 if (v8Value->IsNumber()) {
522 quality = v8Value.As<v8::Number>()->Value(); 530 quality = v8Value.As<v8::Number>()->Value();
523 qualityPtr = &quality; 531 qualityPtr = &quality;
524 } 532 }
525 } 533 }
526 return toDataURLInternal(mimeType, qualityPtr, BackBuffer); 534 return toDataURLInternal(mimeType, qualityPtr, BackBuffer);
527 } 535 }
528 536
537 void HTMLCanvasElement::toBlob(FileCallback* callback, const String& mimeType, c onst ScriptValue& qualityArgument, ExceptionState& exceptionState) const
538 {
539 if (!originClean()) {
540 exceptionState.throwSecurityError("Tainted canvases may not be exported. ");
541 return;
542 }
543
544 File* resultBlob = nullptr;
545 if (!isPaintable()) {
546 // If the canvas element's bitmap has no pixels
547 return;
548 }
549
550 double quality;
551 if (!qualityArgument.isEmpty()) {
552 v8::Local<v8::Value> v8Value = qualityArgument.v8Value();
553 if (v8Value->IsNumber()) {
554 quality = v8Value.As<v8::Number>()->Value();
555 }
556 }
557
558 String encodingMimeType = toEncodingMimeType(mimeType);
559
560 ImageData* imageData = toImageData(BackBuffer);
561 ScopedDisposal<ImageData> disposer(imageData);
562
563 // Perform image encoding
564 Vector<char> encodedImage;
565 ImageDataBuffer(imageData->size(), imageData->data()->data()).encodeImage(en codingMimeType, &quality, &encodedImage);
566 resultBlob = File::create(encodedImage.data(), encodedImage.size(), encoding MimeType);
567
568 Platform::current()->mainThread()->postTask(FROM_HERE, threadSafeBind(&invok eFileCallbackWithBlob, callback, resultBlob));
kinuko 2015/08/26 06:28:30 nit: if you're posting on from the main thread jus
xlai (Olivia) 2015/08/27 18:41:50 Done.
569
570 }
571
529 SecurityOrigin* HTMLCanvasElement::securityOrigin() const 572 SecurityOrigin* HTMLCanvasElement::securityOrigin() const
530 { 573 {
531 return document().securityOrigin(); 574 return document().securityOrigin();
532 } 575 }
533 576
534 bool HTMLCanvasElement::originClean() const 577 bool HTMLCanvasElement::originClean() const
535 { 578 {
536 if (document().settings() && document().settings()->disableReadingFromCanvas ()) 579 if (document().settings() && document().settings()->disableReadingFromCanvas ())
537 return false; 580 return false;
538 return m_originClean; 581 return m_originClean;
(...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after
886 { 929 {
887 return FloatSize(width(), height()); 930 return FloatSize(width(), height());
888 } 931 }
889 932
890 bool HTMLCanvasElement::isOpaque() const 933 bool HTMLCanvasElement::isOpaque() const
891 { 934 {
892 return m_context && !m_context->hasAlpha(); 935 return m_context && !m_context->hasAlpha();
893 } 936 }
894 937
895 } // blink 938 } // blink
OLDNEW
« no previous file with comments | « Source/core/html/HTMLCanvasElement.h ('k') | Source/core/html/HTMLCanvasElement.idl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698