Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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" | |
|
Justin Novosad
2015/08/13 21:03:40
File is in core. I think FileCallback should be a
xlai (Olivia)
2015/08/20 19:46:44
Acknowledged. Great! This sounds a simpler solutio
| |
| 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 Loading... | |
| 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. "); | |
|
Justin Novosad
2015/08/13 21:03:40
Excellent. But this need to be covered by a test.
xlai (Olivia)
2015/08/20 19:46:44
Acknowledged. One more layout test in my to-do lis
| |
| 541 return; | |
| 542 } | |
| 543 | |
| 544 File* resultBlob; | |
|
Justin Novosad
2015/08/13 21:03:40
Code is less fragile if you initialize to nullptr
| |
| 545 if (!isPaintable()) { | |
| 546 // If the canvas element's bitmap has no pixels | |
| 547 resultBlob = nullptr; | |
| 548 return; | |
| 549 } | |
| 550 | |
| 551 double quality; | |
| 552 if (!qualityArgument.isEmpty()) { | |
| 553 v8::Local<v8::Value> v8Value = qualityArgument.v8Value(); | |
| 554 if (v8Value->IsNumber()) { | |
| 555 quality = v8Value.As<v8::Number>()->Value(); | |
| 556 } | |
| 557 } | |
| 558 | |
| 559 String encodingMimeType = toEncodingMimeType(mimeType); | |
| 560 | |
| 561 ImageData* imageData = toImageData(BackBuffer); | |
| 562 ScopedDisposal<ImageData> disposer(imageData); | |
| 563 | |
| 564 // Perform image encoding | |
| 565 Vector<char> encodedImage; | |
| 566 ImageDataBuffer(imageData->size(), imageData->data()->data()).toDataVector(e ncodingMimeType, &quality, encodedImage); | |
| 567 resultBlob = File::create(&encodedImage, encodingMimeType); | |
| 568 | |
| 569 Platform::current()->mainThread()->postDelayedTask(FROM_HERE, threadSafeBind (&invokeFileCallbackWithBlob, callback, resultBlob), 1); | |
|
Justin Novosad
2015/08/13 21:03:40
Why delayed?
xlai (Olivia)
2015/08/20 19:46:44
Done. Changed back to postTask. It was my layout t
| |
| 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 Loading... | |
| 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 |
| OLD | NEW |