| 1 | /* |
| 2 | * Copyright (C) 2017-2018 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #pragma once |
| 27 | |
| 28 | #include "CallTracerTypes.h" |
| 29 | #include <JavaScriptCore/InspectorProtocolObjects.h> |
| 30 | #include <JavaScriptCore/ScriptCallFrame.h> |
| 31 | #include <JavaScriptCore/ScriptCallStack.h> |
| 32 | #include <wtf/Variant.h> |
| 33 | #include <wtf/Vector.h> |
| 34 | #include <wtf/text/WTFString.h> |
| 35 | |
| 36 | namespace WebCore { |
| 37 | |
| 38 | class CanvasGradient; |
| 39 | class CanvasPattern; |
| 40 | class CanvasRenderingContext; |
| 41 | class HTMLCanvasElement; |
| 42 | class HTMLImageElement; |
| 43 | class HTMLVideoElement; |
| 44 | class ImageBitmap; |
| 45 | class ImageData; |
| 46 | #if ENABLE(CSS_TYPED_OM) |
| 47 | class TypedOMCSSImageValue; |
| 48 | #endif |
| 49 | |
| 50 | typedef String ErrorString; |
| 51 | |
| 52 | class InspectorCanvas final : public RefCounted<InspectorCanvas> { |
| 53 | public: |
| 54 | static Ref<InspectorCanvas> create(CanvasRenderingContext&); |
| 55 | |
| 56 | const String& identifier() { return m_identifier; } |
| 57 | CanvasRenderingContext& context() { return m_context; } |
| 58 | |
| 59 | HTMLCanvasElement* canvasElement(); |
| 60 | |
| 61 | void canvasChanged(); |
| 62 | |
| 63 | void resetRecordingData(); |
| 64 | bool hasRecordingData() const; |
| 65 | bool currentFrameHasData() const; |
| 66 | void recordAction(const String&, Vector<RecordCanvasActionVariant>&& = { }); |
| 67 | |
| 68 | Ref<JSON::ArrayOf<Inspector::Protocol::Recording::Frame>> releaseFrames() { return m_frames.releaseNonNull(); } |
| 69 | |
| 70 | void finalizeFrame(); |
| 71 | void markCurrentFrameIncomplete(); |
| 72 | |
| 73 | void setRecordingName(const String& name) { m_recordingName = name; } |
| 74 | |
| 75 | void setBufferLimit(long); |
| 76 | bool hasBufferSpace() const; |
| 77 | long bufferUsed() const { return m_bufferUsed; } |
| 78 | |
| 79 | void setFrameCount(long); |
| 80 | bool overFrameCount() const; |
| 81 | |
| 82 | Ref<Inspector::Protocol::Canvas::Canvas> buildObjectForCanvas(bool captureBacktrace); |
| 83 | Ref<Inspector::Protocol::Recording::Recording> releaseObjectForRecording(); |
| 84 | |
| 85 | String getCanvasContentAsDataURL(ErrorString&); |
| 86 | |
| 87 | private: |
| 88 | InspectorCanvas(CanvasRenderingContext&); |
| 89 | void appendActionSnapshotIfNeeded(); |
| 90 | |
| 91 | using DuplicateDataVariant = Variant< |
| 92 | RefPtr<CanvasGradient>, |
| 93 | RefPtr<CanvasPattern>, |
| 94 | RefPtr<HTMLCanvasElement>, |
| 95 | RefPtr<HTMLImageElement>, |
| 96 | #if ENABLE(VIDEO) |
| 97 | RefPtr<HTMLVideoElement>, |
| 98 | #endif |
| 99 | RefPtr<ImageData>, |
| 100 | RefPtr<ImageBitmap>, |
| 101 | RefPtr<Inspector::ScriptCallStack>, |
| 102 | #if ENABLE(CSS_TYPED_OM) |
| 103 | RefPtr<TypedOMCSSImageValue>, |
| 104 | #endif |
| 105 | Inspector::ScriptCallFrame, |
| 106 | String |
| 107 | >; |
| 108 | |
| 109 | int indexForData(DuplicateDataVariant); |
| 110 | String stringIndexForKey(const String&); |
| 111 | Ref<Inspector::Protocol::Recording::InitialState> buildInitialState(); |
| 112 | Ref<JSON::ArrayOf<JSON::Value>> buildAction(const String&, Vector<RecordCanvasActionVariant>&& = { }); |
| 113 | Ref<JSON::ArrayOf<JSON::Value>> buildArrayForCanvasGradient(const CanvasGradient&); |
| 114 | Ref<JSON::ArrayOf<JSON::Value>> buildArrayForCanvasPattern(const CanvasPattern&); |
| 115 | Ref<JSON::ArrayOf<JSON::Value>> buildArrayForImageData(const ImageData&); |
| 116 | |
| 117 | String m_identifier; |
| 118 | CanvasRenderingContext& m_context; |
| 119 | |
| 120 | RefPtr<Inspector::Protocol::Recording::InitialState> m_initialState; |
| 121 | RefPtr<JSON::ArrayOf<Inspector::Protocol::Recording::Frame>> m_frames; |
| 122 | RefPtr<JSON::ArrayOf<JSON::Value>> m_currentActions; |
| 123 | RefPtr<JSON::ArrayOf<JSON::Value>> m_lastRecordedAction; |
| 124 | RefPtr<JSON::ArrayOf<JSON::Value>> m_serializedDuplicateData; |
| 125 | Vector<DuplicateDataVariant> m_indexedDuplicateData; |
| 126 | |
| 127 | String m_recordingName; |
| 128 | MonotonicTime m_currentFrameStartTime { MonotonicTime::nan() }; |
| 129 | size_t m_bufferLimit { 100 * 1024 * 1024 }; |
| 130 | size_t m_bufferUsed { 0 }; |
| 131 | Optional<size_t> m_frameCount; |
| 132 | size_t m_framesCaptured { 0 }; |
| 133 | bool m_contentChanged { false }; |
| 134 | }; |
| 135 | |
| 136 | } // namespace WebCore |
| 137 | |