<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[242594] trunk/Source/WebCore</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/242594">242594</a></dd>
<dt>Author</dt> <dd>drousso@apple.com</dd>
<dt>Date</dt> <dd>2019-03-07 00:41:25 -0800 (Thu, 07 Mar 2019)</dd>
</dl>

<h3>Log Message</h3>
<pre>Web Inspector: Canvas: lazily create the agent
https://bugs.webkit.org/show_bug.cgi?id=195241

Reviewed by Joseph Pecoraro.

No functionality change.

* html/canvas/CanvasRenderingContext.h:
* html/canvas/CanvasRenderingContext.cpp:
(WebCore::CanvasRenderingContext::instances): Added.
(WebCore::CanvasRenderingContext::instancesMutex): Added.
(WebCore::CanvasRenderingContext::CanvasRenderingContext):
(WebCore::CanvasRenderingContext::~CanvasRenderingContext):

* html/canvas/WebGLProgram.h:
* html/canvas/WebGLProgram.cpp:
(WebCore::WebGLProgram::instances): Added.
(WebCore::WebGLProgram::instancesMutex): Added.
(WebCore::WebGLProgram::WebGLProgram):
(WebCore::WebGLProgram::~WebGLProgram):
* html/canvas/WebGLRenderingContextBase.cpp:
(WebCore::WebGLRenderingContextBase::~WebGLRenderingContextBase):

* inspector/InspectorController.cpp:
(WebCore::InspectorController::InspectorController):
(WebCore::InspectorController::createLazyAgents):

* inspector/agents/InspectorCanvasAgent.h:
* inspector/agents/InspectorCanvasAgent.cpp:
(WebCore::InspectorCanvasAgent::enable):
(WebCore::InspectorCanvasAgent::disable):
(WebCore::InspectorCanvasAgent::frameNavigated):
(WebCore::InspectorCanvasAgent::didCreateCanvasRenderingContext):
(WebCore::InspectorCanvasAgent::canvasDestroyed):
(WebCore::InspectorCanvasAgent::didCreateProgram):
(WebCore::InspectorCanvasAgent::willDeleteProgram):
(WebCore::InspectorCanvasAgent::bindCanvas): Added.
(WebCore::InspectorCanvasAgent::canvasDestroyedTimerFired):

* inspector/InspectorInstrumentation.h:
(WebCore::InspectorInstrumentation::didCreateCanvasRenderingContext):
(WebCore::InspectorInstrumentation::didCreateProgram):
(WebCore::InspectorInstrumentation::willDeleteProgram):</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCorehtmlcanvasCanvasRenderingContextcpp">trunk/Source/WebCore/html/canvas/CanvasRenderingContext.cpp</a></li>
<li><a href="#trunkSourceWebCorehtmlcanvasCanvasRenderingContexth">trunk/Source/WebCore/html/canvas/CanvasRenderingContext.h</a></li>
<li><a href="#trunkSourceWebCorehtmlcanvasWebGLProgramcpp">trunk/Source/WebCore/html/canvas/WebGLProgram.cpp</a></li>
<li><a href="#trunkSourceWebCorehtmlcanvasWebGLProgramh">trunk/Source/WebCore/html/canvas/WebGLProgram.h</a></li>
<li><a href="#trunkSourceWebCorehtmlcanvasWebGLRenderingContextBasecpp">trunk/Source/WebCore/html/canvas/WebGLRenderingContextBase.cpp</a></li>
<li><a href="#trunkSourceWebCoreinspectorInspectorControllercpp">trunk/Source/WebCore/inspector/InspectorController.cpp</a></li>
<li><a href="#trunkSourceWebCoreinspectorInspectorInstrumentationh">trunk/Source/WebCore/inspector/InspectorInstrumentation.h</a></li>
<li><a href="#trunkSourceWebCoreinspectoragentsInspectorCanvasAgentcpp">trunk/Source/WebCore/inspector/agents/InspectorCanvasAgent.cpp</a></li>
<li><a href="#trunkSourceWebCoreinspectoragentsInspectorCanvasAgenth">trunk/Source/WebCore/inspector/agents/InspectorCanvasAgent.h</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (242593 => 242594)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog   2019-03-07 08:06:36 UTC (rev 242593)
+++ trunk/Source/WebCore/ChangeLog      2019-03-07 08:41:25 UTC (rev 242594)
</span><span class="lines">@@ -1,3 +1,49 @@
</span><ins>+2019-03-07  Devin Rousso  <drousso@apple.com>
+
+        Web Inspector: Canvas: lazily create the agent
+        https://bugs.webkit.org/show_bug.cgi?id=195241
+
+        Reviewed by Joseph Pecoraro.
+
+        No functionality change.
+
+        * html/canvas/CanvasRenderingContext.h:
+        * html/canvas/CanvasRenderingContext.cpp:
+        (WebCore::CanvasRenderingContext::instances): Added.
+        (WebCore::CanvasRenderingContext::instancesMutex): Added.
+        (WebCore::CanvasRenderingContext::CanvasRenderingContext):
+        (WebCore::CanvasRenderingContext::~CanvasRenderingContext):
+
+        * html/canvas/WebGLProgram.h:
+        * html/canvas/WebGLProgram.cpp:
+        (WebCore::WebGLProgram::instances): Added.
+        (WebCore::WebGLProgram::instancesMutex): Added.
+        (WebCore::WebGLProgram::WebGLProgram):
+        (WebCore::WebGLProgram::~WebGLProgram):
+        * html/canvas/WebGLRenderingContextBase.cpp:
+        (WebCore::WebGLRenderingContextBase::~WebGLRenderingContextBase):
+
+        * inspector/InspectorController.cpp:
+        (WebCore::InspectorController::InspectorController):
+        (WebCore::InspectorController::createLazyAgents):
+
+        * inspector/agents/InspectorCanvasAgent.h:
+        * inspector/agents/InspectorCanvasAgent.cpp:
+        (WebCore::InspectorCanvasAgent::enable):
+        (WebCore::InspectorCanvasAgent::disable):
+        (WebCore::InspectorCanvasAgent::frameNavigated):
+        (WebCore::InspectorCanvasAgent::didCreateCanvasRenderingContext):
+        (WebCore::InspectorCanvasAgent::canvasDestroyed):
+        (WebCore::InspectorCanvasAgent::didCreateProgram):
+        (WebCore::InspectorCanvasAgent::willDeleteProgram):
+        (WebCore::InspectorCanvasAgent::bindCanvas): Added.
+        (WebCore::InspectorCanvasAgent::canvasDestroyedTimerFired):
+
+        * inspector/InspectorInstrumentation.h:
+        (WebCore::InspectorInstrumentation::didCreateCanvasRenderingContext):
+        (WebCore::InspectorInstrumentation::didCreateProgram):
+        (WebCore::InspectorInstrumentation::willDeleteProgram):
+
</ins><span class="cx"> 2019-03-06  Ross Kirsling  <ross.kirsling@sony.com>
</span><span class="cx"> 
</span><span class="cx">         [Win] Remove -DUCHAR_TYPE=wchar_t stopgap and learn to live with char16_t.
</span></span></pre></div>
<a id="trunkSourceWebCorehtmlcanvasCanvasRenderingContextcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/html/canvas/CanvasRenderingContext.cpp (242593 => 242594)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/html/canvas/CanvasRenderingContext.cpp      2019-03-07 08:06:36 UTC (rev 242593)
+++ trunk/Source/WebCore/html/canvas/CanvasRenderingContext.cpp 2019-03-07 08:41:25 UTC (rev 242594)
</span><span class="lines">@@ -34,16 +34,44 @@
</span><span class="cx"> #include "Image.h"
</span><span class="cx"> #include "ImageBitmap.h"
</span><span class="cx"> #include "OffscreenCanvas.h"
</span><ins>+#include "SecurityOrigin.h"
+#include <wtf/HashSet.h>
+#include <wtf/Lock.h>
+#include <wtf/NeverDestroyed.h>
</ins><span class="cx"> #include <wtf/URL.h>
</span><del>-#include "SecurityOrigin.h"
</del><span class="cx"> 
</span><span class="cx"> namespace WebCore {
</span><span class="cx"> 
</span><ins>+HashSet<CanvasRenderingContext*>& CanvasRenderingContext::instances(const LockHolder&)
+{
+    static NeverDestroyed<HashSet<CanvasRenderingContext*>> instances;
+    return instances;
+}
+
+Lock& CanvasRenderingContext::instancesMutex()
+{
+    static LazyNeverDestroyed<Lock> mutex;
+    static std::once_flag initializeMutex;
+    std::call_once(initializeMutex, [] {
+        mutex.construct();
+    });
+    return mutex.get();
+}
+
</ins><span class="cx"> CanvasRenderingContext::CanvasRenderingContext(CanvasBase& canvas)
</span><span class="cx">     : m_canvas(canvas)
</span><span class="cx"> {
</span><ins>+    LockHolder lock(instancesMutex());
+    instances(lock).add(this);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><ins>+CanvasRenderingContext::~CanvasRenderingContext()
+{
+    LockHolder lock(instancesMutex());
+    ASSERT(instances(lock).contains(this));
+    instances(lock).remove(this);
+}
+
</ins><span class="cx"> void CanvasRenderingContext::ref()
</span><span class="cx"> {
</span><span class="cx">     m_canvas.refCanvasBase();
</span></span></pre></div>
<a id="trunkSourceWebCorehtmlcanvasCanvasRenderingContexth"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/html/canvas/CanvasRenderingContext.h (242593 => 242594)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/html/canvas/CanvasRenderingContext.h        2019-03-07 08:06:36 UTC (rev 242593)
+++ trunk/Source/WebCore/html/canvas/CanvasRenderingContext.h   2019-03-07 08:41:25 UTC (rev 242594)
</span><span class="lines">@@ -28,6 +28,7 @@
</span><span class="cx"> #include "CanvasBase.h"
</span><span class="cx"> #include "GraphicsLayer.h"
</span><span class="cx"> #include "ScriptWrappable.h"
</span><ins>+#include <wtf/Forward.h>
</ins><span class="cx"> #include <wtf/Noncopyable.h>
</span><span class="cx"> #include <wtf/text/StringHash.h>
</span><span class="cx"> 
</span><span class="lines">@@ -44,8 +45,11 @@
</span><span class="cx"> class CanvasRenderingContext : public ScriptWrappable {
</span><span class="cx">     WTF_MAKE_NONCOPYABLE(CanvasRenderingContext); WTF_MAKE_FAST_ALLOCATED;
</span><span class="cx"> public:
</span><del>-    virtual ~CanvasRenderingContext() = default;
</del><ins>+    virtual ~CanvasRenderingContext();
</ins><span class="cx"> 
</span><ins>+    static HashSet<CanvasRenderingContext*>& instances(const LockHolder&);
+    static Lock& instancesMutex();
+
</ins><span class="cx">     void ref();
</span><span class="cx">     WEBCORE_EXPORT void deref();
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebCorehtmlcanvasWebGLProgramcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/html/canvas/WebGLProgram.cpp (242593 => 242594)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/html/canvas/WebGLProgram.cpp        2019-03-07 08:06:36 UTC (rev 242593)
+++ trunk/Source/WebCore/html/canvas/WebGLProgram.cpp   2019-03-07 08:41:25 UTC (rev 242594)
</span><span class="lines">@@ -31,9 +31,28 @@
</span><span class="cx"> #include "WebGLContextGroup.h"
</span><span class="cx"> #include "WebGLRenderingContextBase.h"
</span><span class="cx"> #include "WebGLShader.h"
</span><ins>+#include <wtf/HashMap.h>
+#include <wtf/Lock.h>
+#include <wtf/NeverDestroyed.h>
</ins><span class="cx"> 
</span><span class="cx"> namespace WebCore {
</span><span class="cx"> 
</span><ins>+HashMap<WebGLProgram*, WebGLRenderingContextBase*>& WebGLProgram::instances(const LockHolder&)
+{
+    static NeverDestroyed<HashMap<WebGLProgram*, WebGLRenderingContextBase*>> instances;
+    return instances;
+}
+
+Lock& WebGLProgram::instancesMutex()
+{
+    static LazyNeverDestroyed<Lock> mutex;
+    static std::once_flag initializeMutex;
+    std::call_once(initializeMutex, [] {
+        mutex.construct();
+    });
+    return mutex.get();
+}
+
</ins><span class="cx"> Ref<WebGLProgram> WebGLProgram::create(WebGLRenderingContextBase& ctx)
</span><span class="cx"> {
</span><span class="cx">     return adoptRef(*new WebGLProgram(ctx));
</span><span class="lines">@@ -42,6 +61,11 @@
</span><span class="cx"> WebGLProgram::WebGLProgram(WebGLRenderingContextBase& ctx)
</span><span class="cx">     : WebGLSharedObject(ctx)
</span><span class="cx"> {
</span><ins>+    {
+        LockHolder lock(instancesMutex());
+        instances(lock).add(this, &ctx);
+    }
+
</ins><span class="cx">     setObject(ctx.graphicsContext3D()->createProgram());
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -48,6 +72,12 @@
</span><span class="cx"> WebGLProgram::~WebGLProgram()
</span><span class="cx"> {
</span><span class="cx">     deleteObject(0);
</span><ins>+
+    {
+        LockHolder lock(instancesMutex());
+        ASSERT(instances(lock).contains(this));
+        instances(lock).remove(this);
+    }
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void WebGLProgram::deleteObjectImpl(GraphicsContext3D* context3d, Platform3DObject obj)
</span></span></pre></div>
<a id="trunkSourceWebCorehtmlcanvasWebGLProgramh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/html/canvas/WebGLProgram.h (242593 => 242594)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/html/canvas/WebGLProgram.h  2019-03-07 08:06:36 UTC (rev 242593)
+++ trunk/Source/WebCore/html/canvas/WebGLProgram.h     2019-03-07 08:41:25 UTC (rev 242594)
</span><span class="lines">@@ -26,6 +26,7 @@
</span><span class="cx"> #pragma once
</span><span class="cx"> 
</span><span class="cx"> #include "WebGLSharedObject.h"
</span><ins>+#include <wtf/Forward.h>
</ins><span class="cx"> 
</span><span class="cx"> namespace WebCore {
</span><span class="cx"> 
</span><span class="lines">@@ -36,6 +37,9 @@
</span><span class="cx">     static Ref<WebGLProgram> create(WebGLRenderingContextBase&);
</span><span class="cx">     virtual ~WebGLProgram();
</span><span class="cx"> 
</span><ins>+    static HashMap<WebGLProgram*, WebGLRenderingContextBase*>& instances(const LockHolder&);
+    static Lock& instancesMutex();
+
</ins><span class="cx">     unsigned numActiveAttribLocations();
</span><span class="cx">     GC3Dint getActiveAttribLocation(GC3Duint index);
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebCorehtmlcanvasWebGLRenderingContextBasecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/html/canvas/WebGLRenderingContextBase.cpp (242593 => 242594)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/html/canvas/WebGLRenderingContextBase.cpp   2019-03-07 08:06:36 UTC (rev 242593)
+++ trunk/Source/WebCore/html/canvas/WebGLRenderingContextBase.cpp      2019-03-07 08:41:25 UTC (rev 242594)
</span><span class="lines">@@ -98,7 +98,9 @@
</span><span class="cx"> #include <JavaScriptCore/TypedArrayInlines.h>
</span><span class="cx"> #include <JavaScriptCore/Uint32Array.h>
</span><span class="cx"> #include <wtf/CheckedArithmetic.h>
</span><ins>+#include <wtf/HashMap.h>
</ins><span class="cx"> #include <wtf/HexNumber.h>
</span><ins>+#include <wtf/Lock.h>
</ins><span class="cx"> #include <wtf/StdLibExtras.h>
</span><span class="cx"> #include <wtf/UniqueArray.h>
</span><span class="cx"> #include <wtf/text/CString.h>
</span><span class="lines">@@ -891,6 +893,17 @@
</span><span class="cx">         destroyGraphicsContext3D();
</span><span class="cx">         m_contextGroup->removeContext(*this);
</span><span class="cx">     }
</span><ins>+
+    {
+        LockHolder lock(WebGLProgram::instancesMutex());
+        for (auto& entry : WebGLProgram::instances(lock)) {
+            if (entry.value == this) {
+                // Don't remove any WebGLProgram from the instances list, as they may still exist.
+                // Only remove the association with a WebGL context.
+                entry.value = nullptr;
+            }
+        }
+    }
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void WebGLRenderingContextBase::destroyGraphicsContext3D()
</span></span></pre></div>
<a id="trunkSourceWebCoreinspectorInspectorControllercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/inspector/InspectorController.cpp (242593 => 242594)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/inspector/InspectorController.cpp   2019-03-07 08:06:36 UTC (rev 242593)
+++ trunk/Source/WebCore/inspector/InspectorController.cpp      2019-03-07 08:41:25 UTC (rev 242594)
</span><span class="lines">@@ -146,10 +146,6 @@
</span><span class="cx"> 
</span><span class="cx">     m_agents.append(std::make_unique<InspectorTimelineAgent>(pageContext, scriptProfilerAgent, heapAgent, pageAgent));
</span><span class="cx"> 
</span><del>-    auto canvasAgentPtr = std::make_unique<InspectorCanvasAgent>(pageContext);
-    m_instrumentingAgents->setInspectorCanvasAgent(canvasAgentPtr.get());
-    m_agents.append(WTFMove(canvasAgentPtr));
-
</del><span class="cx">     ASSERT(m_injectedScriptManager->commandLineAPIHost());
</span><span class="cx">     if (CommandLineAPIHost* commandLineAPIHost = m_injectedScriptManager->commandLineAPIHost())
</span><span class="cx">         commandLineAPIHost->init(m_inspectorAgent, consoleAgent, m_domAgent, domStorageAgent, databaseAgent);
</span><span class="lines">@@ -210,6 +206,7 @@
</span><span class="cx">     m_agents.append(std::make_unique<InspectorMemoryAgent>(pageContext));
</span><span class="cx"> #endif
</span><span class="cx">     m_agents.append(std::make_unique<PageAuditAgent>(pageContext));
</span><ins>+    m_agents.append(std::make_unique<InspectorCanvasAgent>(pageContext));
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void InspectorController::inspectedPageDestroyed()
</span></span></pre></div>
<a id="trunkSourceWebCoreinspectorInspectorInstrumentationh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/inspector/InspectorInstrumentation.h (242593 => 242594)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/inspector/InspectorInstrumentation.h        2019-03-07 08:06:36 UTC (rev 242593)
+++ trunk/Source/WebCore/inspector/InspectorInstrumentation.h   2019-03-07 08:41:25 UTC (rev 242594)
</span><span class="lines">@@ -1272,6 +1272,7 @@
</span><span class="cx"> 
</span><span class="cx"> inline void InspectorInstrumentation::didCreateCanvasRenderingContext(CanvasRenderingContext& context)
</span><span class="cx"> {
</span><ins>+    FAST_RETURN_IF_NO_FRONTENDS(void());
</ins><span class="cx">     if (InstrumentingAgents* instrumentingAgents = instrumentingAgentsForContext(context.canvasBase().scriptExecutionContext()))
</span><span class="cx">         didCreateCanvasRenderingContextImpl(*instrumentingAgents, context);
</span><span class="cx"> }
</span><span class="lines">@@ -1307,6 +1308,7 @@
</span><span class="cx"> 
</span><span class="cx"> inline void InspectorInstrumentation::didCreateProgram(WebGLRenderingContextBase& contextWebGLBase, WebGLProgram& program)
</span><span class="cx"> {
</span><ins>+    FAST_RETURN_IF_NO_FRONTENDS(void());
</ins><span class="cx">     if (InstrumentingAgents* instrumentingAgents = instrumentingAgentsForContext(contextWebGLBase.canvasBase().scriptExecutionContext()))
</span><span class="cx">         didCreateProgramImpl(*instrumentingAgents, contextWebGLBase, program);
</span><span class="cx"> }
</span><span class="lines">@@ -1313,6 +1315,7 @@
</span><span class="cx"> 
</span><span class="cx"> inline void InspectorInstrumentation::willDeleteProgram(WebGLRenderingContextBase& contextWebGLBase, WebGLProgram& program)
</span><span class="cx"> {
</span><ins>+    FAST_RETURN_IF_NO_FRONTENDS(void());
</ins><span class="cx">     if (InstrumentingAgents* instrumentingAgents = instrumentingAgentsForContext(contextWebGLBase.canvasBase().scriptExecutionContext()))
</span><span class="cx">         willDeleteProgramImpl(*instrumentingAgents, program);
</span><span class="cx"> }
</span></span></pre></div>
<a id="trunkSourceWebCoreinspectoragentsInspectorCanvasAgentcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/inspector/agents/InspectorCanvasAgent.cpp (242593 => 242594)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/inspector/agents/InspectorCanvasAgent.cpp   2019-03-07 08:06:36 UTC (rev 242593)
+++ trunk/Source/WebCore/inspector/agents/InspectorCanvasAgent.cpp      2019-03-07 08:41:25 UTC (rev 242594)
</span><span class="lines">@@ -31,8 +31,8 @@
</span><span class="cx"> #include "Document.h"
</span><span class="cx"> #include "Element.h"
</span><span class="cx"> #include "Frame.h"
</span><ins>+#include "HTMLCanvasElement.h"
</ins><span class="cx"> #include "ImageBitmapRenderingContext.h"
</span><del>-#include "InspectorDOMAgent.h"
</del><span class="cx"> #include "InstrumentingAgents.h"
</span><span class="cx"> #include "JSCanvasRenderingContext2D.h"
</span><span class="cx"> #include "JSExecState.h"
</span><span class="lines">@@ -45,6 +45,8 @@
</span><span class="cx"> #include <JavaScriptCore/InjectedScriptManager.h>
</span><span class="cx"> #include <JavaScriptCore/InspectorProtocolObjects.h>
</span><span class="cx"> #include <JavaScriptCore/JSCInlines.h>
</span><ins>+#include <wtf/HashSet.h>
+#include <wtf/Lock.h>
</ins><span class="cx"> 
</span><span class="cx"> #if ENABLE(WEBGL)
</span><span class="cx"> #include "JSWebGLRenderingContext.h"
</span><span class="lines">@@ -92,34 +94,43 @@
</span><span class="cx"> 
</span><span class="cx"> void InspectorCanvasAgent::enable(ErrorString&)
</span><span class="cx"> {
</span><del>-    if (m_enabled)
</del><ins>+    if (m_instrumentingAgents.inspectorCanvasAgent() == this)
</ins><span class="cx">         return;
</span><span class="cx"> 
</span><del>-    m_recordingAutoCaptureFrameCount = WTF::nullopt;
</del><ins>+    m_instrumentingAgents.setInspectorCanvasAgent(this);
</ins><span class="cx"> 
</span><del>-    m_enabled = true;
</del><ins>+    const auto canvasExistsInCurrentPage = [&] (CanvasRenderingContext* canvasRenderingContext) {
+        if (!canvasRenderingContext)
+            return false;
</ins><span class="cx"> 
</span><del>-    const bool captureBacktrace = false;
-    for (auto& inspectorCanvas : m_identifierToInspectorCanvas.values()) {
-        m_frontendDispatcher->canvasAdded(inspectorCanvas->buildObjectForCanvas(captureBacktrace));
</del><ins>+        auto* scriptExecutionContext = canvasRenderingContext->canvasBase().scriptExecutionContext();
+        if (!is<Document>(scriptExecutionContext))
+            return false;
</ins><span class="cx"> 
</span><del>-#if ENABLE(WEBGL)
-        if (is<WebGLRenderingContextBase>(inspectorCanvas->context())) {
-            WebGLRenderingContextBase& contextWebGL = downcast<WebGLRenderingContextBase>(inspectorCanvas->context());
-            if (Optional<Vector<String>> extensions = contextWebGL.getSupportedExtensions()) {
-                for (const String& extension : *extensions) {
-                    if (contextWebGL.extensionIsEnabled(extension))
-                        m_frontendDispatcher->extensionEnabled(inspectorCanvas->identifier(), extension);
-                }
-            }
</del><ins>+        if (auto* inspectorPageAgent = m_instrumentingAgents.inspectorPageAgent()) {
+            // FIXME: <https://webkit.org/b/168475> Web Inspector: Correctly display iframe's WebSockets
+            auto* document = downcast<Document>(scriptExecutionContext);
+            return document->page() == &inspectorPageAgent->page();
</ins><span class="cx">         }
</span><del>-#endif
</del><ins>+
+        return false;
+    };
+
+    {
+        LockHolder lock(CanvasRenderingContext::instancesMutex());
+        for (auto* canvasRenderingContext : CanvasRenderingContext::instances(lock)) {
+            if (canvasExistsInCurrentPage(canvasRenderingContext))
+                bindCanvas(*canvasRenderingContext, false);
+        }
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx"> #if ENABLE(WEBGL)
</span><del>-    for (auto& inspectorProgram : m_identifierToInspectorProgram.values()) {
-        auto& inspectorCanvas = inspectorProgram->canvas();
-        m_frontendDispatcher->programCreated(inspectorCanvas.identifier(), inspectorProgram->identifier());
</del><ins>+    {
+        LockHolder lock(WebGLProgram::instancesMutex());
+        for (auto& entry : WebGLProgram::instances(lock)) {
+            if (canvasExistsInCurrentPage(entry.value))
+                didCreateProgram(*entry.value, *entry.key);
+        }
</ins><span class="cx">     }
</span><span class="cx"> #endif
</span><span class="cx"> }
</span><span class="lines">@@ -126,23 +137,22 @@
</span><span class="cx"> 
</span><span class="cx"> void InspectorCanvasAgent::disable(ErrorString&)
</span><span class="cx"> {
</span><del>-    if (!m_enabled)
-        return;
</del><ins>+    m_instrumentingAgents.setInspectorCanvasAgent(nullptr);
</ins><span class="cx"> 
</span><ins>+#if ENABLE(WEBGL)
+    m_identifierToInspectorProgram.clear();
+#endif
+
+    m_identifierToInspectorCanvas.clear();
+    m_removedCanvasIdentifiers.clear();
+
</ins><span class="cx">     if (m_canvasDestroyedTimer.isActive())
</span><span class="cx">         m_canvasDestroyedTimer.stop();
</span><span class="cx"> 
</span><del>-    m_removedCanvasIdentifiers.clear();
-
</del><span class="cx">     if (m_canvasRecordingTimer.isActive())
</span><span class="cx">         m_canvasRecordingTimer.stop();
</span><span class="cx"> 
</span><del>-    for (auto& inspectorCanvas : m_identifierToInspectorCanvas.values())
-        inspectorCanvas->resetRecordingData();
-
</del><span class="cx">     m_recordingAutoCaptureFrameCount = WTF::nullopt;
</span><del>-
-    m_enabled = false;
</del><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void InspectorCanvasAgent::requestNode(ErrorString& errorString, const String& canvasId, int* nodeId)
</span><span class="lines">@@ -408,8 +418,7 @@
</span><span class="cx">         inspectorCanvas->context().canvasBase().removeObserver(*this);
</span><span class="cx"> 
</span><span class="cx">         String identifier = unbindCanvas(*inspectorCanvas);
</span><del>-        if (m_enabled)
-            m_frontendDispatcher->canvasRemoved(identifier);
</del><ins>+        m_frontendDispatcher->canvasRemoved(identifier);
</ins><span class="cx">     }
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -436,21 +445,12 @@
</span><span class="cx">         return;
</span><span class="cx">     }
</span><span class="cx"> 
</span><del>-    context.canvasBase().addObserver(*this);
</del><ins>+    auto& inspectorCanvas = bindCanvas(context, true);
</ins><span class="cx"> 
</span><del>-    auto inspectorCanvas = InspectorCanvas::create(context);
-    m_identifierToInspectorCanvas.set(inspectorCanvas->identifier(), inspectorCanvas.copyRef());
-
-    if (!m_enabled)
-        return;
-
-    const bool captureBacktrace = true;
-    m_frontendDispatcher->canvasAdded(inspectorCanvas->buildObjectForCanvas(captureBacktrace));
-
</del><span class="cx">     if (m_recordingAutoCaptureFrameCount) {
</span><span class="cx">         RecordingOptions recordingOptions;
</span><span class="cx">         recordingOptions.frameCount = m_recordingAutoCaptureFrameCount.value();
</span><del>-        startRecording(inspectorCanvas.get(), Inspector::Protocol::Recording::Initiator::AutoCapture, WTFMove(recordingOptions));
</del><ins>+        startRecording(inspectorCanvas, Inspector::Protocol::Recording::Initiator::AutoCapture, WTFMove(recordingOptions));
</ins><span class="cx">     }
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -490,18 +490,14 @@
</span><span class="cx"> void InspectorCanvasAgent::canvasDestroyed(CanvasBase& canvasBase)
</span><span class="cx"> {
</span><span class="cx">     auto* context = canvasBase.renderingContext();
</span><del>-    ASSERT(context);
</del><span class="cx">     if (!context)
</span><span class="cx">         return;
</span><span class="cx"> 
</span><span class="cx">     auto* inspectorCanvas = findInspectorCanvas(*context);
</span><del>-    ASSERT(inspectorCanvas);
</del><span class="cx">     if (!inspectorCanvas)
</span><span class="cx">         return;
</span><span class="cx"> 
</span><span class="cx">     String identifier = unbindCanvas(*inspectorCanvas);
</span><del>-    if (!m_enabled)
-        return;
</del><span class="cx"> 
</span><span class="cx">     // WebCore::CanvasObserver::canvasDestroyed is called in response to the GC destroying the CanvasBase.
</span><span class="cx">     // Due to the single-process model used in WebKit1, the event must be dispatched from a timer to prevent
</span><span class="lines">@@ -615,9 +611,7 @@
</span><span class="cx">     auto inspectorProgram = InspectorShaderProgram::create(program, *inspectorCanvas);
</span><span class="cx">     String programIdentifier = inspectorProgram->identifier();
</span><span class="cx">     m_identifierToInspectorProgram.set(programIdentifier, WTFMove(inspectorProgram));
</span><del>-
-    if (m_enabled)
-        m_frontendDispatcher->programCreated(inspectorCanvas->identifier(), programIdentifier);
</del><ins>+    m_frontendDispatcher->programCreated(inspectorCanvas->identifier(), programIdentifier);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void InspectorCanvasAgent::willDeleteProgram(WebGLProgram& program)
</span><span class="lines">@@ -627,8 +621,7 @@
</span><span class="cx">         return;
</span><span class="cx"> 
</span><span class="cx">     String identifier = unbindProgram(*inspectorProgram);
</span><del>-    if (m_enabled)
-        m_frontendDispatcher->programDeleted(identifier);
</del><ins>+    m_frontendDispatcher->programDeleted(identifier);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> bool InspectorCanvasAgent::isShaderProgramDisabled(WebGLProgram& program)
</span><span class="lines">@@ -681,10 +674,8 @@
</span><span class="cx">     if (!m_removedCanvasIdentifiers.size())
</span><span class="cx">         return;
</span><span class="cx"> 
</span><del>-    if (m_enabled) {
-        for (auto& identifier : m_removedCanvasIdentifiers)
-            m_frontendDispatcher->canvasRemoved(identifier);
-    }
</del><ins>+    for (auto& identifier : m_removedCanvasIdentifiers)
+        m_frontendDispatcher->canvasRemoved(identifier);
</ins><span class="cx"> 
</span><span class="cx">     m_removedCanvasIdentifiers.clear();
</span><span class="cx"> }
</span><span class="lines">@@ -717,6 +708,30 @@
</span><span class="cx">         m_canvasDestroyedTimer.stop();
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+InspectorCanvas& InspectorCanvasAgent::bindCanvas(CanvasRenderingContext& context, bool captureBacktrace)
+{
+    context.canvasBase().addObserver(*this);
+
+    auto inspectorCanvas = InspectorCanvas::create(context);
+    m_identifierToInspectorCanvas.set(inspectorCanvas->identifier(), inspectorCanvas.copyRef());
+
+    m_frontendDispatcher->canvasAdded(inspectorCanvas->buildObjectForCanvas(captureBacktrace));
+
+#if ENABLE(WEBGL)
+    if (is<WebGLRenderingContextBase>(inspectorCanvas->context())) {
+        WebGLRenderingContextBase& contextWebGL = downcast<WebGLRenderingContextBase>(inspectorCanvas->context());
+        if (Optional<Vector<String>> extensions = contextWebGL.getSupportedExtensions()) {
+            for (const String& extension : *extensions) {
+                if (contextWebGL.extensionIsEnabled(extension))
+                    m_frontendDispatcher->extensionEnabled(inspectorCanvas->identifier(), extension);
+            }
+        }
+    }
+#endif
+
+    return inspectorCanvas;
+}
+
</ins><span class="cx"> String InspectorCanvasAgent::unbindCanvas(InspectorCanvas& inspectorCanvas)
</span><span class="cx"> {
</span><span class="cx"> #if ENABLE(WEBGL)
</span></span></pre></div>
<a id="trunkSourceWebCoreinspectoragentsInspectorCanvasAgenth"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/inspector/agents/InspectorCanvasAgent.h (242593 => 242594)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/inspector/agents/InspectorCanvasAgent.h     2019-03-07 08:06:36 UTC (rev 242593)
+++ trunk/Source/WebCore/inspector/agents/InspectorCanvasAgent.h        2019-03-07 08:41:25 UTC (rev 242594)
</span><span class="lines">@@ -114,6 +114,7 @@
</span><span class="cx">     void canvasDestroyedTimerFired();
</span><span class="cx">     void canvasRecordingTimerFired();
</span><span class="cx">     void clearCanvasData();
</span><ins>+    InspectorCanvas& bindCanvas(CanvasRenderingContext&, bool captureBacktrace);
</ins><span class="cx">     String unbindCanvas(InspectorCanvas&);
</span><span class="cx">     InspectorCanvas* assertInspectorCanvas(ErrorString&, const String& identifier);
</span><span class="cx">     InspectorCanvas* findInspectorCanvas(CanvasRenderingContext&);
</span><span class="lines">@@ -133,8 +134,6 @@
</span><span class="cx">     Timer m_canvasDestroyedTimer;
</span><span class="cx">     Timer m_canvasRecordingTimer;
</span><span class="cx">     Optional<size_t> m_recordingAutoCaptureFrameCount;
</span><del>-
-    bool m_enabled { false };
</del><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> } // namespace WebCore
</span></span></pre>
</div>
</div>

</body>
</html>