Web Inspector: Remove InspectorState
[WebKit-https.git] / Source / WebCore / inspector / InspectorCanvasAgent.cpp
1 /*
2  * Copyright (C) 2012 Google 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 are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32
33 #if ENABLE(INSPECTOR)
34
35 #include "InspectorCanvasAgent.h"
36
37 #include "BindingVisitors.h"
38 #include "DOMWindow.h"
39 #include "HTMLCanvasElement.h"
40 #include "HTMLNames.h"
41 #include "InjectedScript.h"
42 #include "InjectedScriptCanvasModule.h"
43 #include "InjectedScriptManager.h"
44 #include "InspectorFrontend.h"
45 #include "InspectorPageAgent.h"
46 #include "InstrumentingAgents.h"
47 #include "MainFrame.h"
48 #include "NodeList.h"
49 #include "Page.h"
50 #include "ScriptObject.h"
51 #include "ScriptProfiler.h"
52 #include "ScriptState.h"
53
54 using WebCore::TypeBuilder::Array;
55 using WebCore::TypeBuilder::Canvas::ResourceId;
56 using WebCore::TypeBuilder::Canvas::ResourceInfo;
57 using WebCore::TypeBuilder::Canvas::ResourceState;
58 using WebCore::TypeBuilder::Canvas::TraceLog;
59 using WebCore::TypeBuilder::Canvas::TraceLogId;
60 using WebCore::TypeBuilder::Network::FrameId;
61
62 namespace WebCore {
63
64 InspectorCanvasAgent::InspectorCanvasAgent(InstrumentingAgents* instrumentingAgents, InspectorPageAgent* pageAgent, InjectedScriptManager* injectedScriptManager)
65     : InspectorBaseAgent<InspectorCanvasAgent>("Canvas", instrumentingAgents)
66     , m_pageAgent(pageAgent)
67     , m_injectedScriptManager(injectedScriptManager)
68     , m_frontend(0)
69     , m_enabled(false)
70 {
71 }
72
73 InspectorCanvasAgent::~InspectorCanvasAgent()
74 {
75 }
76
77 void InspectorCanvasAgent::setFrontend(InspectorFrontend* frontend)
78 {
79     ASSERT(frontend);
80     m_frontend = frontend->canvas();
81 }
82
83 void InspectorCanvasAgent::clearFrontend()
84 {
85     m_frontend = 0;
86     disable(0);
87 }
88
89 void InspectorCanvasAgent::enable(ErrorString*)
90 {
91     if (m_enabled)
92         return;
93     m_enabled = true;
94     m_instrumentingAgents->setInspectorCanvasAgent(this);
95     findFramesWithUninstrumentedCanvases();
96 }
97
98 void InspectorCanvasAgent::disable(ErrorString*)
99 {
100     m_enabled = false;
101     m_instrumentingAgents->setInspectorCanvasAgent(0);
102     m_framesWithUninstrumentedCanvases.clear();
103 }
104
105 void InspectorCanvasAgent::dropTraceLog(ErrorString* errorString, const TraceLogId& traceLogId)
106 {
107     InjectedScriptCanvasModule module = injectedScriptCanvasModule(errorString, traceLogId);
108     if (!module.hasNoValue())
109         module.dropTraceLog(errorString, traceLogId);
110 }
111
112 void InspectorCanvasAgent::hasUninstrumentedCanvases(ErrorString* errorString, bool* result)
113 {
114     if (!checkIsEnabled(errorString))
115         return;
116     for (FramesWithUninstrumentedCanvases::iterator it = m_framesWithUninstrumentedCanvases.begin(); it != m_framesWithUninstrumentedCanvases.end(); ++it) {
117         if (it->value) {
118             *result = true;
119             return;
120         }
121     }
122     *result = false;
123 }
124
125 void InspectorCanvasAgent::captureFrame(ErrorString* errorString, const FrameId* frameId, TraceLogId* traceLogId)
126 {
127     Frame* frame = frameId ? m_pageAgent->assertFrame(errorString, *frameId) : m_pageAgent->mainFrame();
128     if (!frame)
129         return;
130     InjectedScriptCanvasModule module = injectedScriptCanvasModule(errorString, mainWorldExecState(frame));
131     if (!module.hasNoValue())
132         module.captureFrame(errorString, traceLogId);
133 }
134
135 void InspectorCanvasAgent::startCapturing(ErrorString* errorString, const FrameId* frameId, TraceLogId* traceLogId)
136 {
137     Frame* frame = frameId ? m_pageAgent->assertFrame(errorString, *frameId) : m_pageAgent->mainFrame();
138     if (!frame)
139         return;
140     InjectedScriptCanvasModule module = injectedScriptCanvasModule(errorString, mainWorldExecState(frame));
141     if (!module.hasNoValue())
142         module.startCapturing(errorString, traceLogId);
143 }
144
145 void InspectorCanvasAgent::stopCapturing(ErrorString* errorString, const TraceLogId& traceLogId)
146 {
147     InjectedScriptCanvasModule module = injectedScriptCanvasModule(errorString, traceLogId);
148     if (!module.hasNoValue())
149         module.stopCapturing(errorString, traceLogId);
150 }
151
152 void InspectorCanvasAgent::getTraceLog(ErrorString* errorString, const TraceLogId& traceLogId, const int* startOffset, const int* maxLength, RefPtr<TraceLog>& traceLog)
153 {
154     InjectedScriptCanvasModule module = injectedScriptCanvasModule(errorString, traceLogId);
155     if (!module.hasNoValue())
156         module.traceLog(errorString, traceLogId, startOffset, maxLength, &traceLog);
157 }
158
159 void InspectorCanvasAgent::replayTraceLog(ErrorString* errorString, const TraceLogId& traceLogId, int stepNo, RefPtr<ResourceState>& result)
160 {
161     InjectedScriptCanvasModule module = injectedScriptCanvasModule(errorString, traceLogId);
162     if (!module.hasNoValue())
163         module.replayTraceLog(errorString, traceLogId, stepNo, &result);
164 }
165
166 void InspectorCanvasAgent::getResourceInfo(ErrorString* errorString, const ResourceId& resourceId, RefPtr<ResourceInfo>& result)
167 {
168     InjectedScriptCanvasModule module = injectedScriptCanvasModule(errorString, resourceId);
169     if (!module.hasNoValue())
170         module.resourceInfo(errorString, resourceId, &result);
171 }
172
173 void InspectorCanvasAgent::getResourceState(ErrorString* errorString, const TraceLogId& traceLogId, const ResourceId& resourceId, RefPtr<ResourceState>& result)
174 {
175     InjectedScriptCanvasModule module = injectedScriptCanvasModule(errorString, traceLogId);
176     if (!module.hasNoValue())
177         module.resourceState(errorString, traceLogId, resourceId, &result);
178 }
179
180 ScriptObject InspectorCanvasAgent::wrapCanvas2DRenderingContextForInstrumentation(const ScriptObject& context)
181 {
182     ErrorString error;
183     InjectedScriptCanvasModule module = injectedScriptCanvasModule(&error, context);
184     if (module.hasNoValue())
185         return ScriptObject();
186     return notifyRenderingContextWasWrapped(module.wrapCanvas2DContext(context));
187 }
188
189 #if ENABLE(WEBGL)
190 ScriptObject InspectorCanvasAgent::wrapWebGLRenderingContextForInstrumentation(const ScriptObject& glContext)
191 {
192     ErrorString error;
193     InjectedScriptCanvasModule module = injectedScriptCanvasModule(&error, glContext);
194     if (module.hasNoValue())
195         return ScriptObject();
196     return notifyRenderingContextWasWrapped(module.wrapWebGLContext(glContext));
197 }
198 #endif
199
200 ScriptObject InspectorCanvasAgent::notifyRenderingContextWasWrapped(const ScriptObject& wrappedContext)
201 {
202     ASSERT(m_frontend);
203     JSC::ExecState* scriptState = wrappedContext.scriptState();
204     DOMWindow* domWindow = scriptState ? domWindowFromExecState(scriptState) : 0;
205     Frame* frame = domWindow ? domWindow->frame() : 0;
206     if (frame && !m_framesWithUninstrumentedCanvases.contains(frame))
207         m_framesWithUninstrumentedCanvases.set(frame, false);
208     String frameId = m_pageAgent->frameId(frame);
209     if (!frameId.isEmpty())
210         m_frontend->contextCreated(frameId);
211     return wrappedContext;
212 }
213
214 InjectedScriptCanvasModule InspectorCanvasAgent::injectedScriptCanvasModule(ErrorString* errorString, JSC::ExecState* scriptState)
215 {
216     if (!checkIsEnabled(errorString))
217         return InjectedScriptCanvasModule();
218     InjectedScriptCanvasModule module = InjectedScriptCanvasModule::moduleForState(m_injectedScriptManager, scriptState);
219     if (module.hasNoValue()) {
220         ASSERT_NOT_REACHED();
221         *errorString = "Internal error: no Canvas module";
222     }
223     return module;
224 }
225
226 InjectedScriptCanvasModule InspectorCanvasAgent::injectedScriptCanvasModule(ErrorString* errorString, const ScriptObject& scriptObject)
227 {
228     if (!checkIsEnabled(errorString))
229         return InjectedScriptCanvasModule();
230     if (scriptObject.hasNoValue()) {
231         ASSERT_NOT_REACHED();
232         *errorString = "Internal error: original ScriptObject has no value";
233         return InjectedScriptCanvasModule();
234     }
235     return injectedScriptCanvasModule(errorString, scriptObject.scriptState());
236 }
237
238 InjectedScriptCanvasModule InspectorCanvasAgent::injectedScriptCanvasModule(ErrorString* errorString, const String& objectId)
239 {
240     if (!checkIsEnabled(errorString))
241         return InjectedScriptCanvasModule();
242     InjectedScript injectedScript = m_injectedScriptManager->injectedScriptForObjectId(objectId);
243     if (injectedScript.hasNoValue()) {
244         *errorString = "Inspected frame has gone";
245         return InjectedScriptCanvasModule();
246     }
247     return injectedScriptCanvasModule(errorString, injectedScript.scriptState());
248 }
249
250 void InspectorCanvasAgent::findFramesWithUninstrumentedCanvases()
251 {
252     m_framesWithUninstrumentedCanvases.clear();
253
254     for (Frame* frame = &m_pageAgent->page()->mainFrame(); frame; frame = frame->tree().traverseNext()) {
255         if (!frame->document())
256             continue;
257
258         RefPtr<NodeList> canvases = frame->document()->getElementsByTagName(HTMLNames::canvasTag.localName());
259         if (canvases) {
260             for (unsigned i = 0, length = canvases->length(); i < length; i++) {
261                 const HTMLCanvasElement* canvas = toHTMLCanvasElement(canvases->item(i));
262                 if (canvas->renderingContext()) {
263                     m_framesWithUninstrumentedCanvases.set(frame, true);
264                     break;
265                 }
266             }
267         }
268     }
269
270     for (FramesWithUninstrumentedCanvases::iterator it = m_framesWithUninstrumentedCanvases.begin(); it != m_framesWithUninstrumentedCanvases.end(); ++it) {
271         String frameId = m_pageAgent->frameId(it->key);
272         if (!frameId.isEmpty())
273             m_frontend->contextCreated(frameId);
274     }
275 }
276
277 bool InspectorCanvasAgent::checkIsEnabled(ErrorString* errorString) const
278 {
279     if (m_enabled)
280         return true;
281     *errorString = "Canvas agent is not enabled";
282     return false;
283 }
284
285 void InspectorCanvasAgent::frameNavigated(Frame* frame)
286 {
287     if (!m_enabled)
288         return;
289     if (frame == m_pageAgent->mainFrame()) {
290         for (FramesWithUninstrumentedCanvases::iterator it = m_framesWithUninstrumentedCanvases.begin(); it != m_framesWithUninstrumentedCanvases.end(); ++it)
291             m_framesWithUninstrumentedCanvases.set(it->key, false);
292         m_frontend->traceLogsRemoved(0, 0);
293     } else {
294         while (frame) {
295             if (m_framesWithUninstrumentedCanvases.contains(frame))
296                 m_framesWithUninstrumentedCanvases.set(frame, false);
297             if (m_pageAgent->hasIdForFrame(frame)) {
298                 String frameId = m_pageAgent->frameId(frame);
299                 m_frontend->traceLogsRemoved(&frameId, 0);
300             }
301             frame = frame->tree().traverseNext();
302         }
303     }
304 }
305
306 void InspectorCanvasAgent::frameDetached(Frame* frame)
307 {
308     if (m_enabled)
309         m_framesWithUninstrumentedCanvases.remove(frame);
310 }
311
312 void InspectorCanvasAgent::didBeginFrame()
313 {
314     if (!m_enabled)
315         return;
316     ErrorString error;
317     for (FramesWithUninstrumentedCanvases::iterator it = m_framesWithUninstrumentedCanvases.begin(); it != m_framesWithUninstrumentedCanvases.end(); ++it) {
318         InjectedScriptCanvasModule module = injectedScriptCanvasModule(&error, mainWorldExecState(it->key));
319         if (!module.hasNoValue())
320             module.markFrameEnd();
321     }
322 }
323
324 } // namespace WebCore
325
326 #endif // ENABLE(INSPECTOR)