Title: [219634] trunk
Revision
219634
Author
[email protected]
Date
2017-07-18 15:24:54 -0700 (Tue, 18 Jul 2017)

Log Message

Web Inspector: Refactoring: replace InspectorCanvasAgent::CanvasEntry with a helper class
https://bugs.webkit.org/show_bug.cgi?id=174311

Reviewed by Devin Rousso.

Source/WebCore:

This patch adds a helper class for tracking canvases and their data. The
current approach of defining a helper structure in the agent's header is
awkward to use and maintain, and won't scale well as more canvas instrumentation
points and data are added.

* CMakeLists.txt:
* WebCore.xcodeproj/project.pbxproj:
New file.

* inspector/InspectorCanvas.cpp: Added.
(WebCore::InspectorCanvas::create):
(WebCore::InspectorCanvas::InspectorCanvas):
(WebCore::InspectorCanvas::buildObjectForCanvas):
* inspector/InspectorCanvas.h: Added.

* inspector/InspectorCanvasAgent.cpp:
(WebCore::InspectorCanvasAgent::InspectorCanvasAgent):
(WebCore::InspectorCanvasAgent::enable):
(WebCore::InspectorCanvasAgent::requestNode):
(WebCore::InspectorCanvasAgent::requestContent):
(WebCore::InspectorCanvasAgent::requestCSSCanvasClientNodes):
(WebCore::InspectorCanvasAgent::resolveCanvasContext):
(WebCore::InspectorCanvasAgent::frameNavigated):
(WebCore::InspectorCanvasAgent::didCreateCSSCanvas):
(WebCore::InspectorCanvasAgent::didChangeCSSCanvasClientNodes):
(WebCore::InspectorCanvasAgent::didCreateCanvasRenderingContext):
(WebCore::InspectorCanvasAgent::didChangeCanvasMemory):
(WebCore::InspectorCanvasAgent::canvasDestroyed):
(WebCore::InspectorCanvasAgent::canvasDestroyedTimerFired):
(WebCore::InspectorCanvasAgent::clearCanvasData):
(WebCore::InspectorCanvasAgent::unbindCanvas):
(WebCore::InspectorCanvasAgent::assertInspectorCanvas):
(WebCore::InspectorCanvasAgent::findInspectorCanvas):
(WebCore::InspectorCanvasAgent::getCanvasEntry): Deleted.
(WebCore::InspectorCanvasAgent::buildObjectForCanvas): Deleted.
* inspector/InspectorCanvasAgent.h:

* inspector/InspectorController.cpp:
(WebCore::InspectorController::InspectorController):
Remove unused constructor argument.

LayoutTests:

Updated test expectations for new error strings.

* inspector/canvas/css-canvas-clients-expected.txt:
* inspector/canvas/requestContent-2d-expected.txt:
* inspector/canvas/requestNode-expected.txt:
* inspector/canvas/resolveCanvasContext-2d-expected.txt:

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (219633 => 219634)


--- trunk/LayoutTests/ChangeLog	2017-07-18 22:07:43 UTC (rev 219633)
+++ trunk/LayoutTests/ChangeLog	2017-07-18 22:24:54 UTC (rev 219634)
@@ -1,3 +1,17 @@
+2017-07-18  Matt Baker  <[email protected]>
+
+        Web Inspector: Refactoring: replace InspectorCanvasAgent::CanvasEntry with a helper class
+        https://bugs.webkit.org/show_bug.cgi?id=174311
+
+        Reviewed by Devin Rousso.
+
+        Updated test expectations for new error strings.
+
+        * inspector/canvas/css-canvas-clients-expected.txt:
+        * inspector/canvas/requestContent-2d-expected.txt:
+        * inspector/canvas/requestNode-expected.txt:
+        * inspector/canvas/resolveCanvasContext-2d-expected.txt:
+
 2017-07-18  Antoine Quint  <[email protected]>
 
         Turn tests at media/modern-media-controls/playback-support back on

Modified: trunk/LayoutTests/inspector/canvas/css-canvas-clients-expected.txt (219633 => 219634)


--- trunk/LayoutTests/inspector/canvas/css-canvas-clients-expected.txt	2017-07-18 22:07:43 UTC (rev 219633)
+++ trunk/LayoutTests/inspector/canvas/css-canvas-clients-expected.txt	2017-07-18 22:24:54 UTC (rev 219634)
@@ -18,5 +18,5 @@
 
 -- Running test case: Canvas.CSSCanvasClients.InvalidCanvasId
 PASS: Should produce an error.
-Error: Invalid canvas identifier
+Error: No canvas for given identifier.
 

Modified: trunk/LayoutTests/inspector/canvas/requestContent-2d-expected.txt (219633 => 219634)


--- trunk/LayoutTests/inspector/canvas/requestContent-2d-expected.txt	2017-07-18 22:07:43 UTC (rev 219633)
+++ trunk/LayoutTests/inspector/canvas/requestContent-2d-expected.txt	2017-07-18 22:24:54 UTC (rev 219634)
@@ -7,5 +7,5 @@
 
 -- Running test case: Canvas.requestContent.invalidCanvasId
 PASS: Should produce an error.
-Error: Invalid canvas identifier
+Error: No canvas for given identifier.
 

Modified: trunk/LayoutTests/inspector/canvas/requestNode-expected.txt (219633 => 219634)


--- trunk/LayoutTests/inspector/canvas/requestNode-expected.txt	2017-07-18 22:07:43 UTC (rev 219633)
+++ trunk/LayoutTests/inspector/canvas/requestNode-expected.txt	2017-07-18 22:24:54 UTC (rev 219634)
@@ -17,5 +17,5 @@
 
 -- Running test case: Canvas.requestNode.invalidCanvasId
 PASS: Should produce an error.
-Error: Invalid canvas identifier
+Error: No canvas for given identifier.
 

Modified: trunk/LayoutTests/inspector/canvas/resolveCanvasContext-2d-expected.txt (219633 => 219634)


--- trunk/LayoutTests/inspector/canvas/resolveCanvasContext-2d-expected.txt	2017-07-18 22:07:43 UTC (rev 219633)
+++ trunk/LayoutTests/inspector/canvas/resolveCanvasContext-2d-expected.txt	2017-07-18 22:24:54 UTC (rev 219634)
@@ -8,5 +8,5 @@
 
 -- Running test case: Canvas.resolveCanvasContext.invalidIdentifier
 PASS: Should produce an error.
-Error: Invalid canvas identifier
+Error: No canvas for given identifier.
 

Modified: trunk/Source/WebCore/CMakeLists.txt (219633 => 219634)


--- trunk/Source/WebCore/CMakeLists.txt	2017-07-18 22:07:43 UTC (rev 219633)
+++ trunk/Source/WebCore/CMakeLists.txt	2017-07-18 22:24:54 UTC (rev 219634)
@@ -1901,6 +1901,7 @@
     inspector/DOMPatchSupport.cpp
     inspector/InspectorApplicationCacheAgent.cpp
     inspector/InspectorCSSAgent.cpp
+    inspector/InspectorCanvas.cpp
     inspector/InspectorCanvasAgent.cpp
     inspector/InspectorClient.cpp
     inspector/InspectorController.cpp

Modified: trunk/Source/WebCore/ChangeLog (219633 => 219634)


--- trunk/Source/WebCore/ChangeLog	2017-07-18 22:07:43 UTC (rev 219633)
+++ trunk/Source/WebCore/ChangeLog	2017-07-18 22:24:54 UTC (rev 219634)
@@ -1,3 +1,51 @@
+2017-07-18  Matt Baker  <[email protected]>
+
+        Web Inspector: Refactoring: replace InspectorCanvasAgent::CanvasEntry with a helper class
+        https://bugs.webkit.org/show_bug.cgi?id=174311
+
+        Reviewed by Devin Rousso.
+
+        This patch adds a helper class for tracking canvases and their data. The
+        current approach of defining a helper structure in the agent's header is
+        awkward to use and maintain, and won't scale well as more canvas instrumentation
+        points and data are added.
+
+        * CMakeLists.txt:
+        * WebCore.xcodeproj/project.pbxproj:
+        New file.
+
+        * inspector/InspectorCanvas.cpp: Added.
+        (WebCore::InspectorCanvas::create):
+        (WebCore::InspectorCanvas::InspectorCanvas):
+        (WebCore::InspectorCanvas::buildObjectForCanvas):
+        * inspector/InspectorCanvas.h: Added.
+
+        * inspector/InspectorCanvasAgent.cpp:
+        (WebCore::InspectorCanvasAgent::InspectorCanvasAgent):
+        (WebCore::InspectorCanvasAgent::enable):
+        (WebCore::InspectorCanvasAgent::requestNode):
+        (WebCore::InspectorCanvasAgent::requestContent):
+        (WebCore::InspectorCanvasAgent::requestCSSCanvasClientNodes):
+        (WebCore::InspectorCanvasAgent::resolveCanvasContext):
+        (WebCore::InspectorCanvasAgent::frameNavigated):
+        (WebCore::InspectorCanvasAgent::didCreateCSSCanvas):
+        (WebCore::InspectorCanvasAgent::didChangeCSSCanvasClientNodes):
+        (WebCore::InspectorCanvasAgent::didCreateCanvasRenderingContext):
+        (WebCore::InspectorCanvasAgent::didChangeCanvasMemory):
+        (WebCore::InspectorCanvasAgent::canvasDestroyed):
+        (WebCore::InspectorCanvasAgent::canvasDestroyedTimerFired):
+        (WebCore::InspectorCanvasAgent::clearCanvasData):
+        (WebCore::InspectorCanvasAgent::unbindCanvas):
+        (WebCore::InspectorCanvasAgent::assertInspectorCanvas):
+        (WebCore::InspectorCanvasAgent::findInspectorCanvas):
+        (WebCore::InspectorCanvasAgent::getCanvasEntry): Deleted.
+        (WebCore::InspectorCanvasAgent::buildObjectForCanvas): Deleted.
+        * inspector/InspectorCanvasAgent.h:
+
+        * inspector/InspectorController.cpp:
+        (WebCore::InspectorController::InspectorController):
+        Remove unused constructor argument.
+
 2017-07-18  Basuke Suzuki  <[email protected]>
 
         [Curl] Move detail implementation from ResourceHandle to ResourceHandleInternal

Modified: trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj (219633 => 219634)


--- trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj	2017-07-18 22:07:43 UTC (rev 219633)
+++ trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj	2017-07-18 22:24:54 UTC (rev 219634)
@@ -2879,6 +2879,8 @@
 		65FEA86909833ADE00BED4AB /* Page.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 65FEA86809833ADE00BED4AB /* Page.cpp */; };
 		69A6CBAC1C6BE42C00B836E9 /* AccessibilitySVGElement.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 697101071C6BE1550018C7F1 /* AccessibilitySVGElement.cpp */; };
 		69A6CBAD1C6BE42C00B836E9 /* AccessibilitySVGElement.h in Headers */ = {isa = PBXBuildFile; fileRef = 697101081C6BE1550018C7F1 /* AccessibilitySVGElement.h */; };
+		6A22E8701F10418600F546C3 /* InspectorCanvas.h in Headers */ = {isa = PBXBuildFile; fileRef = 6A22E86F1F10418600F546C3 /* InspectorCanvas.h */; };
+		6A22E8731F1042C400F546C3 /* InspectorCanvas.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6A22E8721F1042C400F546C3 /* InspectorCanvas.cpp */; };
 		6A32D7CE1A16D8C000412F0B /* InspectorCanvasAgent.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6A4B6D6619D225D8006F11D3 /* InspectorCanvasAgent.cpp */; };
 		6A4B6D6519D22519006F11D3 /* InspectorCanvasAgent.h in Headers */ = {isa = PBXBuildFile; fileRef = 6A4B6D6419D22519006F11D3 /* InspectorCanvasAgent.h */; };
 		6B3480940EEF50D400AC1B41 /* NativeImage.h in Headers */ = {isa = PBXBuildFile; fileRef = 6B3480920EEF50D400AC1B41 /* NativeImage.h */; settings = {ATTRIBUTES = (Private, ); }; };
@@ -10703,6 +10705,8 @@
 		65FEA86809833ADE00BED4AB /* Page.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = Page.cpp; sourceTree = "<group>"; };
 		697101071C6BE1550018C7F1 /* AccessibilitySVGElement.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = AccessibilitySVGElement.cpp; sourceTree = "<group>"; };
 		697101081C6BE1550018C7F1 /* AccessibilitySVGElement.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AccessibilitySVGElement.h; sourceTree = "<group>"; };
+		6A22E86F1F10418600F546C3 /* InspectorCanvas.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = InspectorCanvas.h; sourceTree = "<group>"; };
+		6A22E8721F1042C400F546C3 /* InspectorCanvas.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = InspectorCanvas.cpp; sourceTree = "<group>"; };
 		6A4B6D6419D22519006F11D3 /* InspectorCanvasAgent.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = InspectorCanvasAgent.h; sourceTree = "<group>"; };
 		6A4B6D6619D225D8006F11D3 /* InspectorCanvasAgent.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = InspectorCanvasAgent.cpp; sourceTree = "<group>"; };
 		6B3480920EEF50D400AC1B41 /* NativeImage.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = NativeImage.h; sourceTree = "<group>"; };
@@ -17191,6 +17195,8 @@
 				7A54881514E432A1006AE05A /* DOMPatchSupport.h */,
 				B885E8D211E06DD2009FFBF4 /* InspectorApplicationCacheAgent.cpp */,
 				B885E8D311E06DD2009FFBF4 /* InspectorApplicationCacheAgent.h */,
+				6A22E8721F1042C400F546C3 /* InspectorCanvas.cpp */,
+				6A22E86F1F10418600F546C3 /* InspectorCanvas.h */,
 				6A4B6D6619D225D8006F11D3 /* InspectorCanvasAgent.cpp */,
 				6A4B6D6419D22519006F11D3 /* InspectorCanvasAgent.h */,
 				7A1F2B51126C61B20006A7E6 /* InspectorClient.cpp */,
@@ -27708,6 +27714,7 @@
 				93309DF0099E64920056E581 /* InsertParagraphSeparatorCommand.h in Headers */,
 				93309DF2099E64920056E581 /* InsertTextCommand.h in Headers */,
 				B885E8D511E06DD2009FFBF4 /* InspectorApplicationCacheAgent.h in Headers */,
+				6A22E8701F10418600F546C3 /* InspectorCanvas.h in Headers */,
 				6A4B6D6519D22519006F11D3 /* InspectorCanvasAgent.h in Headers */,
 				1C81B95C0E97330800266E07 /* InspectorClient.h in Headers */,
 				1C81B95A0E97330800266E07 /* InspectorController.h in Headers */,
@@ -31689,6 +31696,7 @@
 				93309DEF099E64920056E581 /* InsertParagraphSeparatorCommand.cpp in Sources */,
 				93309DF1099E64920056E581 /* InsertTextCommand.cpp in Sources */,
 				B885E8D411E06DD2009FFBF4 /* InspectorApplicationCacheAgent.cpp in Sources */,
+				6A22E8731F1042C400F546C3 /* InspectorCanvas.cpp in Sources */,
 				6A32D7CE1A16D8C000412F0B /* InspectorCanvasAgent.cpp in Sources */,
 				7A1F2B52126C61B20006A7E6 /* InspectorClient.cpp in Sources */,
 				1C81B95B0E97330800266E07 /* InspectorController.cpp in Sources */,

Added: trunk/Source/WebCore/inspector/InspectorCanvas.cpp (0 => 219634)


--- trunk/Source/WebCore/inspector/InspectorCanvas.cpp	                        (rev 0)
+++ trunk/Source/WebCore/inspector/InspectorCanvas.cpp	2017-07-18 22:24:54 UTC (rev 219634)
@@ -0,0 +1,134 @@
+/*
+ * Copyright (C) 2017 Apple Inc.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "InspectorCanvas.h"
+
+#include "CanvasRenderingContext2D.h"
+#include "Document.h"
+#include "Frame.h"
+#include "HTMLCanvasElement.h"
+#include "InspectorDOMAgent.h"
+#include "InspectorPageAgent.h"
+#include "InstrumentingAgents.h"
+#if ENABLE(WEBGL)
+#include "WebGLRenderingContext.h"
+#endif
+#if ENABLE(WEBGL2)
+#include "WebGL2RenderingContext.h"
+#endif
+#if ENABLE(WEBGPU)
+#include "WebGPURenderingContext.h"
+#endif
+#include <inspector/IdentifiersFactory.h>
+
+using namespace Inspector;
+
+namespace WebCore {
+
+Ref<InspectorCanvas> InspectorCanvas::create(HTMLCanvasElement& canvas, const String& cssCanvasName)
+{
+    return adoptRef(*new InspectorCanvas(canvas, cssCanvasName));
+}
+
+InspectorCanvas::InspectorCanvas(HTMLCanvasElement& canvas, const String& cssCanvasName)
+    : m_identifier("canvas:" + IdentifiersFactory::createIdentifier())
+    , m_canvas(canvas)
+    , m_cssCanvasName(cssCanvasName)
+{
+}
+
+Ref<Inspector::Protocol::Canvas::Canvas> InspectorCanvas::buildObjectForCanvas(InstrumentingAgents& instrumentingAgents)
+{
+    Document& document = m_canvas.document();
+    Frame* frame = document.frame();
+    CanvasRenderingContext* context = m_canvas.renderingContext();
+
+    Inspector::Protocol::Canvas::ContextType contextType;
+    if (is<CanvasRenderingContext2D>(context))
+        contextType = Inspector::Protocol::Canvas::ContextType::Canvas2D;
+#if ENABLE(WEBGL)
+    else if (is<WebGLRenderingContext>(context))
+        contextType = Inspector::Protocol::Canvas::ContextType::WebGL;
+#endif
+#if ENABLE(WEBGL2)
+    else if (is<WebGL2RenderingContext>(context))
+        contextType = Inspector::Protocol::Canvas::ContextType::WebGL2;
+#endif
+#if ENABLE(WEBGPU)
+    else if (is<WebGPURenderingContext>(context))
+        contextType = Inspector::Protocol::Canvas::ContextType::WebGPU;
+#endif
+    else {
+        ASSERT_NOT_REACHED();
+        contextType = Inspector::Protocol::Canvas::ContextType::Canvas2D;
+    }
+
+    auto canvas = Inspector::Protocol::Canvas::Canvas::create()
+        .setCanvasId(m_identifier)
+        .setFrameId(instrumentingAgents.inspectorPageAgent()->frameId(frame))
+        .setContextType(contextType)
+        .release();
+
+    if (!m_cssCanvasName.isEmpty())
+        canvas->setCssCanvasName(m_cssCanvasName);
+    else {
+        InspectorDOMAgent* domAgent = instrumentingAgents.inspectorDOMAgent();
+        int nodeId = domAgent->boundNodeId(&m_canvas);
+        if (!nodeId) {
+            if (int documentNodeId = domAgent->boundNodeId(&m_canvas.document())) {
+                ErrorString ignored;
+                nodeId = domAgent->pushNodeToFrontend(ignored, documentNodeId, &m_canvas);
+            }
+        }
+
+        if (nodeId)
+            canvas->setNodeId(nodeId);
+    }
+
+#if ENABLE(WEBGL)
+    if (is<WebGLRenderingContextBase>(context)) {
+        if (std::optional<WebGLContextAttributes> attributes = downcast<WebGLRenderingContextBase>(context)->getContextAttributes()) {
+            canvas->setContextAttributes(Inspector::Protocol::Canvas::ContextAttributes::create()
+                .setAlpha(attributes->alpha)
+                .setDepth(attributes->depth)
+                .setStencil(attributes->stencil)
+                .setAntialias(attributes->antialias)
+                .setPremultipliedAlpha(attributes->premultipliedAlpha)
+                .setPreserveDrawingBuffer(attributes->preserveDrawingBuffer)
+                .setFailIfMajorPerformanceCaveat(attributes->failIfMajorPerformanceCaveat)
+                .release());
+        }
+    }
+#endif
+
+    if (size_t memoryCost = m_canvas.memoryCost())
+        canvas->setMemoryCost(memoryCost);
+
+    return canvas;
+}
+
+} // namespace WebCore
+

Added: trunk/Source/WebCore/inspector/InspectorCanvas.h (0 => 219634)


--- trunk/Source/WebCore/inspector/InspectorCanvas.h	                        (rev 0)
+++ trunk/Source/WebCore/inspector/InspectorCanvas.h	2017-07-18 22:24:54 UTC (rev 219634)
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2017 Apple Inc.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#include <inspector/InspectorProtocolObjects.h>
+#include <inspector/InspectorValues.h>
+#include <wtf/HashMap.h>
+
+namespace WebCore {
+
+class HTMLCanvasElement;
+class InstrumentingAgents;
+
+typedef String ErrorString;
+
+class InspectorCanvas final : public RefCounted<InspectorCanvas> {
+public:
+    static Ref<InspectorCanvas> create(HTMLCanvasElement&, const String& cssCanvasName);
+
+    const String& identifier() { return m_identifier; }
+    HTMLCanvasElement& canvas() { return m_canvas; }
+    const String& cssCanvasName() { return m_cssCanvasName; }
+
+    Ref<Inspector::Protocol::Canvas::Canvas> buildObjectForCanvas(InstrumentingAgents&);
+
+    ~InspectorCanvas() { }
+
+private:
+    InspectorCanvas(HTMLCanvasElement&, const String& cssCanvasName);
+
+    String m_identifier;
+    HTMLCanvasElement& m_canvas;
+    String m_cssCanvasName;
+};
+
+} // namespace WebCore
+

Modified: trunk/Source/WebCore/inspector/InspectorCanvasAgent.cpp (219633 => 219634)


--- trunk/Source/WebCore/inspector/InspectorCanvasAgent.cpp	2017-07-18 22:07:43 UTC (rev 219633)
+++ trunk/Source/WebCore/inspector/InspectorCanvasAgent.cpp	2017-07-18 22:24:54 UTC (rev 219634)
@@ -31,7 +31,6 @@
 #include "Element.h"
 #include "Frame.h"
 #include "InspectorDOMAgent.h"
-#include "InspectorPageAgent.h"
 #include "InstrumentingAgents.h"
 #include "JSCanvasRenderingContext2D.h"
 #include "JSMainThreadExecState.h"
@@ -60,12 +59,11 @@
 
 namespace WebCore {
 
-InspectorCanvasAgent::InspectorCanvasAgent(WebAgentContext& context, InspectorPageAgent* pageAgent)
+InspectorCanvasAgent::InspectorCanvasAgent(WebAgentContext& context)
     : InspectorAgentBase(ASCIILiteral("Canvas"), context)
     , m_frontendDispatcher(std::make_unique<Inspector::CanvasFrontendDispatcher>(context.frontendRouter))
     , m_backendDispatcher(Inspector::CanvasBackendDispatcher::create(context.backendDispatcher, this))
     , m_injectedScriptManager(context.injectedScriptManager)
-    , m_pageAgent(pageAgent)
     , m_timer(*this, &InspectorCanvasAgent::canvasDestroyedTimerFired)
 {
 }
@@ -92,11 +90,8 @@
 
     m_enabled = true;
 
-    for (const auto& pair : m_canvasEntries) {
-        auto* canvasElement = pair.key;
-        auto& canvasEntry = pair.value;
-        m_frontendDispatcher->canvasAdded(buildObjectForCanvas(canvasEntry, *canvasElement));
-    }
+    for (auto& inspectorCanvas : m_identifierToInspectorCanvas.values())
+        m_frontendDispatcher->canvasAdded(inspectorCanvas->buildObjectForCanvas(m_instrumentingAgents));
 }
 
 void InspectorCanvasAgent::disable(ErrorString&)
@@ -114,32 +109,28 @@
 
 void InspectorCanvasAgent::requestNode(ErrorString& errorString, const String& canvasId, int* nodeId)
 {
-    const CanvasEntry* canvasEntry = getCanvasEntry(canvasId);
-    if (!canvasEntry) {
-        errorString = ASCIILiteral("Invalid canvas identifier");
+    auto* inspectorCanvas = assertInspectorCanvas(errorString, canvasId);
+    if (!inspectorCanvas)
         return;
-    }
 
-    int documentNodeId = m_instrumentingAgents.inspectorDOMAgent()->boundNodeId(&canvasEntry->element->document());
+    int documentNodeId = m_instrumentingAgents.inspectorDOMAgent()->boundNodeId(&inspectorCanvas->canvas().document());
     if (!documentNodeId) {
         errorString = ASCIILiteral("Document has not been requested");
         return;
     }
 
-    *nodeId = m_instrumentingAgents.inspectorDOMAgent()->pushNodeToFrontend(errorString, documentNodeId, canvasEntry->element);
+    *nodeId = m_instrumentingAgents.inspectorDOMAgent()->pushNodeToFrontend(errorString, documentNodeId, &inspectorCanvas->canvas());
 }
 
 void InspectorCanvasAgent::requestContent(ErrorString& errorString, const String& canvasId, String* content)
 {
-    const CanvasEntry* canvasEntry = getCanvasEntry(canvasId);
-    if (!canvasEntry) {
-        errorString = ASCIILiteral("Invalid canvas identifier");
+    auto* inspectorCanvas = assertInspectorCanvas(errorString, canvasId);
+    if (!inspectorCanvas)
         return;
-    }
 
-    CanvasRenderingContext* context = canvasEntry->element->renderingContext();
+    CanvasRenderingContext* context = inspectorCanvas->canvas().renderingContext();
     if (is<CanvasRenderingContext2D>(context)) {
-        auto result = canvasEntry->element->toDataURL(ASCIILiteral("image/png"));
+        auto result = inspectorCanvas->canvas().toDataURL(ASCIILiteral("image/png"));
         if (result.hasException()) {
             errorString = result.releaseException().releaseMessage();
             return;
@@ -151,7 +142,7 @@
         WebGLRenderingContextBase* gl = downcast<WebGLRenderingContextBase>(context);
 
         gl->setPreventBufferClearForInspector(true);
-        auto result = canvasEntry->element->toDataURL(ASCIILiteral("image/png"));
+        auto result = inspectorCanvas->canvas().toDataURL(ASCIILiteral("image/png"));
         gl->setPreventBufferClearForInspector(false);
 
         if (result.hasException()) {
@@ -168,14 +159,12 @@
 
 void InspectorCanvasAgent::requestCSSCanvasClientNodes(ErrorString& errorString, const String& canvasId, RefPtr<Inspector::Protocol::Array<int>>& result)
 {
-    const CanvasEntry* canvasEntry = getCanvasEntry(canvasId);
-    if (!canvasEntry) {
-        errorString = ASCIILiteral("Invalid canvas identifier");
+    auto* inspectorCanvas = assertInspectorCanvas(errorString, canvasId);
+    if (!inspectorCanvas)
         return;
-    }
 
     result = Inspector::Protocol::Array<int>::create();
-    for (Element* element : canvasEntry->element->cssCanvasClients()) {
+    for (Element* element : inspectorCanvas->canvas().cssCanvasClients()) {
         if (int documentNodeId = m_instrumentingAgents.inspectorDOMAgent()->boundNodeId(&element->document()))
             result->addItem(m_instrumentingAgents.inspectorDOMAgent()->pushNodeToFrontend(errorString, documentNodeId, element));
     }
@@ -205,13 +194,11 @@
 
 void InspectorCanvasAgent::resolveCanvasContext(ErrorString& errorString, const String& canvasId, const String* const objectGroup, RefPtr<Inspector::Protocol::Runtime::RemoteObject>& result)
 {
-    const CanvasEntry* canvasEntry = getCanvasEntry(canvasId);
-    if (!canvasEntry) {
-        errorString = ASCIILiteral("Invalid canvas identifier");
+    auto* inspectorCanvas = assertInspectorCanvas(errorString, canvasId);
+    if (!inspectorCanvas)
         return;
-    }
 
-    Frame* frame = canvasEntry->element->document().frame();
+    Frame* frame = inspectorCanvas->canvas().document().frame();
     if (!frame) {
         errorString = ASCIILiteral("Canvas belongs to a document without a frame");
         return;
@@ -221,7 +208,7 @@
     auto injectedScript = m_injectedScriptManager.injectedScriptFor(&state);
     ASSERT(!injectedScript.hasNoValue());
 
-    CanvasRenderingContext* context = canvasEntry->element->renderingContext();
+    CanvasRenderingContext* context = inspectorCanvas->canvas().renderingContext();
     JSC::JSValue value = contextAsScriptValue(state, context);
     if (!value) {
         ASSERT_NOT_REACHED();
@@ -240,73 +227,74 @@
         return;
     }
 
-    Vector<HTMLCanvasElement*> canvasesForFrame;
-    for (const auto& canvasElement : m_canvasEntries.keys()) {
-        if (canvasElement->document().frame() == &frame)
-            canvasesForFrame.append(canvasElement);
+    Vector<InspectorCanvas*> inspectorCanvases;
+    for (RefPtr<InspectorCanvas>& inspectorCanvas : m_identifierToInspectorCanvas.values()) {
+        if (inspectorCanvas->canvas().document().frame() == &frame)
+            inspectorCanvases.append(inspectorCanvas.get());
     }
 
-    for (auto* canvasElement : canvasesForFrame) {
-        auto canvasEntry = m_canvasEntries.take(canvasElement);
+    for (auto* inspectorCanvas : inspectorCanvases) {
+        String identifier = unbindCanvas(*inspectorCanvas);
         if (m_enabled)
-            m_frontendDispatcher->canvasRemoved(canvasEntry.identifier);
+            m_frontendDispatcher->canvasRemoved(identifier);
     }
 }
 
 void InspectorCanvasAgent::didCreateCSSCanvas(HTMLCanvasElement& canvasElement, const String& name)
 {
-    ASSERT(!m_canvasToCSSCanvasId.contains(&canvasElement));
-    ASSERT(!m_canvasEntries.contains(&canvasElement));
+    if (findInspectorCanvas(canvasElement)) {
+        ASSERT_NOT_REACHED();
+        return;
+    }
 
-    m_canvasToCSSCanvasId.set(&canvasElement, name);
+    ASSERT(!m_canvasToCSSCanvasName.contains(&canvasElement));
+    m_canvasToCSSCanvasName.set(&canvasElement, name);
 }
 
 void InspectorCanvasAgent::didChangeCSSCanvasClientNodes(HTMLCanvasElement& canvasElement)
 {
-    const CanvasEntry* canvasEntry = getCanvasEntry(canvasElement);
-    if (!canvasEntry)
+    auto* inspectorCanvas = findInspectorCanvas(canvasElement);
+    if (!inspectorCanvas)
         return;
 
-    m_frontendDispatcher->cssCanvasClientNodesChanged(canvasEntry->identifier);
+    m_frontendDispatcher->cssCanvasClientNodesChanged(inspectorCanvas->identifier());
 }
 
 void InspectorCanvasAgent::didCreateCanvasRenderingContext(HTMLCanvasElement& canvasElement)
 {
-    if (m_canvasEntries.contains(&canvasElement)) {
+    if (findInspectorCanvas(canvasElement)) {
         ASSERT_NOT_REACHED();
         return;
     }
 
-    CanvasEntry newCanvasEntry("canvas:" + IdentifiersFactory::createIdentifier(), &canvasElement);
-    newCanvasEntry.cssCanvasName = m_canvasToCSSCanvasId.take(&canvasElement);
-
-    m_canvasEntries.set(&canvasElement, newCanvasEntry);
     canvasElement.addObserver(*this);
 
-    if (!m_enabled)
-        return;
+    String cssCanvasName = m_canvasToCSSCanvasName.take(&canvasElement);
+    auto inspectorCanvas = InspectorCanvas::create(canvasElement, cssCanvasName);
 
-    m_frontendDispatcher->canvasAdded(buildObjectForCanvas(newCanvasEntry, canvasElement));
+    if (m_enabled)
+        m_frontendDispatcher->canvasAdded(inspectorCanvas->buildObjectForCanvas(m_instrumentingAgents));
+
+    m_identifierToInspectorCanvas.set(inspectorCanvas->identifier(), WTFMove(inspectorCanvas));
 }
 
 void InspectorCanvasAgent::didChangeCanvasMemory(HTMLCanvasElement& canvasElement)
 {
-    CanvasEntry* canvasEntry = getCanvasEntry(canvasElement);
-    if (!canvasEntry)
+    auto* inspectorCanvas = findInspectorCanvas(canvasElement);
+    if (!inspectorCanvas)
         return;
 
-    m_frontendDispatcher->canvasMemoryChanged(canvasEntry->identifier, canvasElement.memoryCost());
+    m_frontendDispatcher->canvasMemoryChanged(inspectorCanvas->identifier(), canvasElement.memoryCost());
 }
 
 void InspectorCanvasAgent::canvasDestroyed(HTMLCanvasElement& canvasElement)
 {
-    auto it = m_canvasEntries.find(&canvasElement);
-    if (it == m_canvasEntries.end())
+    auto* inspectorCanvas = findInspectorCanvas(canvasElement);
+    ASSERT(inspectorCanvas);
+    if (!inspectorCanvas)
         return;
 
-    String canvasIdentifier = it->value.identifier;
-    m_canvasEntries.remove(it);
-
+    String identifier = unbindCanvas(*inspectorCanvas);
     if (!m_enabled)
         return;
 
@@ -313,7 +301,7 @@
     // WebCore::CanvasObserver::canvasDestroyed is called in response to the GC destroying the HTMLCanvasElement.
     // Due to the single-process model used in WebKit1, the event must be dispatched from a timer to prevent
     // the frontend from making JS allocations while the GC is still active.
-    m_removedCanvasIdentifiers.append(canvasIdentifier);
+    m_removedCanvasIdentifiers.append(identifier);
 
     if (!m_timer.isActive())
         m_timer.startOneShot(0_s);
@@ -324,8 +312,10 @@
     if (!m_removedCanvasIdentifiers.size())
         return;
 
-    for (const auto& identifier : m_removedCanvasIdentifiers)
-        m_frontendDispatcher->canvasRemoved(identifier);
+    if (m_enabled) {
+        for (auto& identifier : m_removedCanvasIdentifiers)
+            m_frontendDispatcher->canvasRemoved(identifier);
+    }
 
     m_removedCanvasIdentifiers.clear();
 }
@@ -332,11 +322,11 @@
 
 void InspectorCanvasAgent::clearCanvasData()
 {
-    for (auto* canvasElement : m_canvasEntries.keys())
-        canvasElement->removeObserver(*this);
+    for (auto& inspectorCanvas : m_identifierToInspectorCanvas.values())
+        inspectorCanvas->canvas().removeObserver(*this);
 
-    m_canvasEntries.clear();
-    m_canvasToCSSCanvasId.clear();
+    m_identifierToInspectorCanvas.clear();
+    m_canvasToCSSCanvasName.clear();
     m_removedCanvasIdentifiers.clear();
 
     if (m_timer.isActive())
@@ -343,92 +333,35 @@
         m_timer.stop();
 }
 
-InspectorCanvasAgent::CanvasEntry* InspectorCanvasAgent::getCanvasEntry(HTMLCanvasElement& canvasElement)
+String InspectorCanvasAgent::unbindCanvas(InspectorCanvas& inspectorCanvas)
 {
-    auto findResult = m_canvasEntries.find(&canvasElement);
-    if (findResult != m_canvasEntries.end())
-        return &findResult->value;
+    ASSERT(!m_canvasToCSSCanvasName.contains(&inspectorCanvas.canvas()));
 
-    return nullptr;
+    String identifier = inspectorCanvas.identifier();
+    m_identifierToInspectorCanvas.remove(identifier);
+
+    return identifier;
 }
 
-InspectorCanvasAgent::CanvasEntry* InspectorCanvasAgent::getCanvasEntry(const String& canvasIdentifier)
+InspectorCanvas* InspectorCanvasAgent::assertInspectorCanvas(ErrorString& errorString, const String& identifier)
 {
-    for (auto& canvasEntry : m_canvasEntries.values()) {
-        if (canvasEntry.identifier == canvasIdentifier)
-            return &canvasEntry;
+    RefPtr<InspectorCanvas> inspectorCanvas = m_identifierToInspectorCanvas.get(identifier);
+    if (!inspectorCanvas) {
+        errorString = ASCIILiteral("No canvas for given identifier.");
+        return nullptr;
     }
 
-    return nullptr;
+    return inspectorCanvas.get();
 }
 
-Ref<Inspector::Protocol::Canvas::Canvas> InspectorCanvasAgent::buildObjectForCanvas(const CanvasEntry& canvasEntry, HTMLCanvasElement& canvasElement)
+InspectorCanvas* InspectorCanvasAgent::findInspectorCanvas(HTMLCanvasElement& canvasElement)
 {
-    Frame* frame = canvasElement.document().frame();
-    CanvasRenderingContext* context = canvasElement.renderingContext();
-
-    Inspector::Protocol::Canvas::ContextType contextType;
-    if (is<CanvasRenderingContext2D>(context))
-        contextType = Inspector::Protocol::Canvas::ContextType::Canvas2D;
-#if ENABLE(WEBGL)
-    else if (is<WebGLRenderingContext>(context))
-        contextType = Inspector::Protocol::Canvas::ContextType::WebGL;
-#endif
-#if ENABLE(WEBGL2)
-    else if (is<WebGL2RenderingContext>(context))
-        contextType = Inspector::Protocol::Canvas::ContextType::WebGL2;
-#endif
-#if ENABLE(WEBGPU)
-    else if (is<WebGPURenderingContext>(context))
-        contextType = Inspector::Protocol::Canvas::ContextType::WebGPU;
-#endif
-    else {
-        ASSERT_NOT_REACHED();
-        contextType = Inspector::Protocol::Canvas::ContextType::Canvas2D;
+    for (auto& inspectorCanvas : m_identifierToInspectorCanvas.values()) {
+        if (&inspectorCanvas->canvas() == &canvasElement)
+            return inspectorCanvas.get();
     }
 
-    auto canvas = Inspector::Protocol::Canvas::Canvas::create()
-        .setCanvasId(canvasEntry.identifier)
-        .setFrameId(m_pageAgent->frameId(frame))
-        .setContextType(contextType)
-        .release();
-
-    if (!canvasEntry.cssCanvasName.isEmpty())
-        canvas->setCssCanvasName(canvasEntry.cssCanvasName);
-    else {
-        InspectorDOMAgent* domAgent = m_instrumentingAgents.inspectorDOMAgent();
-        int nodeId = domAgent->boundNodeId(&canvasElement);
-        if (!nodeId) {
-            if (int documentNodeId = domAgent->boundNodeId(&canvasElement.document())) {
-                ErrorString ignored;
-                nodeId = domAgent->pushNodeToFrontend(ignored, documentNodeId, &canvasElement);
-            }
-        }
-
-        if (nodeId)
-            canvas->setNodeId(nodeId);
-    }
-
-#if ENABLE(WEBGL)
-    if (is<WebGLRenderingContextBase>(context)) {
-        if (std::optional<WebGLContextAttributes> attributes = downcast<WebGLRenderingContextBase>(context)->getContextAttributes()) {
-            canvas->setContextAttributes(Inspector::Protocol::Canvas::ContextAttributes::create()
-                .setAlpha(attributes->alpha)
-                .setDepth(attributes->depth)
-                .setStencil(attributes->stencil)
-                .setAntialias(attributes->antialias)
-                .setPremultipliedAlpha(attributes->premultipliedAlpha)
-                .setPreserveDrawingBuffer(attributes->preserveDrawingBuffer)
-                .setFailIfMajorPerformanceCaveat(attributes->failIfMajorPerformanceCaveat)
-                .release());
-        }
-    }
-#endif
-
-    if (size_t memoryCost = canvasElement.memoryCost())
-        canvas->setMemoryCost(memoryCost);
-
-    return canvas;
+    return nullptr;
 }
 
 } // namespace WebCore

Modified: trunk/Source/WebCore/inspector/InspectorCanvasAgent.h (219633 => 219634)


--- trunk/Source/WebCore/inspector/InspectorCanvasAgent.h	2017-07-18 22:07:43 UTC (rev 219633)
+++ trunk/Source/WebCore/inspector/InspectorCanvasAgent.h	2017-07-18 22:24:54 UTC (rev 219634)
@@ -26,6 +26,7 @@
 #pragma once
 
 #include "HTMLCanvasElement.h"
+#include "InspectorCanvas.h"
 #include "InspectorWebAgentBase.h"
 #include "Timer.h"
 #include <inspector/InspectorBackendDispatchers.h>
@@ -42,7 +43,6 @@
 
 namespace WebCore {
 
-class InspectorPageAgent;
 class WebGLRenderingContextBase;
 
 typedef String ErrorString;
@@ -51,7 +51,7 @@
     WTF_MAKE_NONCOPYABLE(InspectorCanvasAgent);
     WTF_MAKE_FAST_ALLOCATED;
 public:
-    InspectorCanvasAgent(WebAgentContext&, InspectorPageAgent*);
+    explicit InspectorCanvasAgent(WebAgentContext&);
     virtual ~InspectorCanvasAgent() { }
 
     void didCreateFrontendAndBackend(Inspector::FrontendRouter*, Inspector::BackendDispatcher*) override;
@@ -79,33 +79,18 @@
     void canvasDestroyed(HTMLCanvasElement&) override;
 
 private:
-    struct CanvasEntry {
-        String identifier;
-        String cssCanvasName;
-        HTMLCanvasElement* element = { nullptr };
-
-        CanvasEntry() { }
-
-        CanvasEntry(const String& identifier, HTMLCanvasElement* canvasElement)
-            : identifier(identifier)
-            , element(canvasElement)
-        {
-        }
-    };
-
     void canvasDestroyedTimerFired();
     void clearCanvasData();
-    CanvasEntry* getCanvasEntry(HTMLCanvasElement&);
-    CanvasEntry* getCanvasEntry(const String&);
-    Ref<Inspector::Protocol::Canvas::Canvas> buildObjectForCanvas(const CanvasEntry&, HTMLCanvasElement&);
+    String unbindCanvas(InspectorCanvas&);
+    InspectorCanvas* assertInspectorCanvas(ErrorString&, const String&);
+    InspectorCanvas* findInspectorCanvas(HTMLCanvasElement&);
 
     std::unique_ptr<Inspector::CanvasFrontendDispatcher> m_frontendDispatcher;
     RefPtr<Inspector::CanvasBackendDispatcher> m_backendDispatcher;
     Inspector::InjectedScriptManager& m_injectedScriptManager;
-    InspectorPageAgent* m_pageAgent;
 
-    HashMap<HTMLCanvasElement*, CanvasEntry> m_canvasEntries;
-    HashMap<HTMLCanvasElement*, String> m_canvasToCSSCanvasId;
+    HashMap<String, RefPtr<InspectorCanvas>> m_identifierToInspectorCanvas;
+    HashMap<HTMLCanvasElement*, String> m_canvasToCSSCanvasName;
     Vector<String> m_removedCanvasIdentifiers;
     Timer m_timer;
     bool m_enabled { false };

Modified: trunk/Source/WebCore/inspector/InspectorController.cpp (219633 => 219634)


--- trunk/Source/WebCore/inspector/InspectorController.cpp	2017-07-18 22:07:43 UTC (rev 219633)
+++ trunk/Source/WebCore/inspector/InspectorController.cpp	2017-07-18 22:24:54 UTC (rev 219634)
@@ -179,7 +179,7 @@
     m_agents.append(std::make_unique<InspectorLayerTreeAgent>(pageContext));
     m_agents.append(std::make_unique<InspectorWorkerAgent>(pageContext));
 
-    auto canvasAgentPtr = std::make_unique<InspectorCanvasAgent>(pageContext, pageAgent);
+    auto canvasAgentPtr = std::make_unique<InspectorCanvasAgent>(pageContext);
     m_instrumentingAgents->setInspectorCanvasAgent(canvasAgentPtr.get());
     m_agents.append(WTFMove(canvasAgentPtr));
 
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to