This source file includes following definitions.
- m_enabled
- setFrontend
- clearFrontend
- restore
- enable
- disable
- dropTraceLog
- hasUninstrumentedCanvases
- captureFrame
- startCapturing
- stopCapturing
- getTraceLog
- replayTraceLog
- getResourceState
- evaluateTraceLogCallArgument
- wrapCanvas2DRenderingContextForInstrumentation
- wrapWebGLRenderingContextForInstrumentation
- notifyRenderingContextWasWrapped
- injectedScriptCanvasModule
- injectedScriptCanvasModule
- injectedScriptCanvasModule
- findFramesWithUninstrumentedCanvases
- m_framesWithUninstrumentedCanvases
- visitNode
- checkIsEnabled
- didCommitLoad
- frameDetachedFromParent
- didBeginFrame
#include "config.h"
#include "core/inspector/InspectorCanvasAgent.h"
#include "bindings/v8/ScriptObject.h"
#include "bindings/v8/ScriptProfiler.h"
#include "core/html/HTMLCanvasElement.h"
#include "core/inspector/BindingVisitors.h"
#include "core/inspector/InjectedScript.h"
#include "core/inspector/InjectedScriptCanvasModule.h"
#include "core/inspector/InjectedScriptManager.h"
#include "core/inspector/InspectorPageAgent.h"
#include "core/inspector/InspectorState.h"
#include "core/inspector/InstrumentingAgents.h"
#include "core/loader/DocumentLoader.h"
#include "core/frame/DOMWindow.h"
#include "core/frame/LocalFrame.h"
using WebCore::TypeBuilder::Array;
using WebCore::TypeBuilder::Canvas::ResourceId;
using WebCore::TypeBuilder::Canvas::ResourceState;
using WebCore::TypeBuilder::Canvas::TraceLog;
using WebCore::TypeBuilder::Canvas::TraceLogId;
using WebCore::TypeBuilder::Page::FrameId;
using WebCore::TypeBuilder::Runtime::RemoteObject;
namespace WebCore {
namespace CanvasAgentState {
static const char canvasAgentEnabled[] = "canvasAgentEnabled";
};
InspectorCanvasAgent::InspectorCanvasAgent(InspectorPageAgent* pageAgent, InjectedScriptManager* injectedScriptManager)
: InspectorBaseAgent<InspectorCanvasAgent>("Canvas")
, m_pageAgent(pageAgent)
, m_injectedScriptManager(injectedScriptManager)
, m_frontend(0)
, m_enabled(false)
{
}
InspectorCanvasAgent::~InspectorCanvasAgent()
{
}
void InspectorCanvasAgent::setFrontend(InspectorFrontend* frontend)
{
ASSERT(frontend);
m_frontend = frontend->canvas();
}
void InspectorCanvasAgent::clearFrontend()
{
m_frontend = 0;
disable(0);
}
void InspectorCanvasAgent::restore()
{
if (m_state->getBoolean(CanvasAgentState::canvasAgentEnabled)) {
ErrorString error;
enable(&error);
}
}
void InspectorCanvasAgent::enable(ErrorString*)
{
if (m_enabled)
return;
m_enabled = true;
m_state->setBoolean(CanvasAgentState::canvasAgentEnabled, m_enabled);
m_instrumentingAgents->setInspectorCanvasAgent(this);
findFramesWithUninstrumentedCanvases();
}
void InspectorCanvasAgent::disable(ErrorString*)
{
m_enabled = false;
m_state->setBoolean(CanvasAgentState::canvasAgentEnabled, m_enabled);
m_instrumentingAgents->setInspectorCanvasAgent(0);
m_framesWithUninstrumentedCanvases.clear();
if (m_frontend)
m_frontend->traceLogsRemoved(0, 0);
}
void InspectorCanvasAgent::dropTraceLog(ErrorString* errorString, const TraceLogId& traceLogId)
{
InjectedScriptCanvasModule module = injectedScriptCanvasModule(errorString, traceLogId);
if (!module.hasNoValue())
module.dropTraceLog(errorString, traceLogId);
}
void InspectorCanvasAgent::hasUninstrumentedCanvases(ErrorString* errorString, bool* result)
{
if (!checkIsEnabled(errorString))
return;
for (FramesWithUninstrumentedCanvases::const_iterator it = m_framesWithUninstrumentedCanvases.begin(); it != m_framesWithUninstrumentedCanvases.end(); ++it) {
if (it->value) {
*result = true;
return;
}
}
*result = false;
}
void InspectorCanvasAgent::captureFrame(ErrorString* errorString, const FrameId* frameId, TraceLogId* traceLogId)
{
LocalFrame* frame = frameId ? m_pageAgent->assertFrame(errorString, *frameId) : m_pageAgent->mainFrame();
if (!frame)
return;
InjectedScriptCanvasModule module = injectedScriptCanvasModule(errorString, mainWorldScriptState(frame));
if (!module.hasNoValue())
module.captureFrame(errorString, traceLogId);
}
void InspectorCanvasAgent::startCapturing(ErrorString* errorString, const FrameId* frameId, TraceLogId* traceLogId)
{
LocalFrame* frame = frameId ? m_pageAgent->assertFrame(errorString, *frameId) : m_pageAgent->mainFrame();
if (!frame)
return;
InjectedScriptCanvasModule module = injectedScriptCanvasModule(errorString, mainWorldScriptState(frame));
if (!module.hasNoValue())
module.startCapturing(errorString, traceLogId);
}
void InspectorCanvasAgent::stopCapturing(ErrorString* errorString, const TraceLogId& traceLogId)
{
InjectedScriptCanvasModule module = injectedScriptCanvasModule(errorString, traceLogId);
if (!module.hasNoValue())
module.stopCapturing(errorString, traceLogId);
}
void InspectorCanvasAgent::getTraceLog(ErrorString* errorString, const TraceLogId& traceLogId, const int* startOffset, const int* maxLength, RefPtr<TraceLog>& traceLog)
{
InjectedScriptCanvasModule module = injectedScriptCanvasModule(errorString, traceLogId);
if (!module.hasNoValue())
module.traceLog(errorString, traceLogId, startOffset, maxLength, &traceLog);
}
void InspectorCanvasAgent::replayTraceLog(ErrorString* errorString, const TraceLogId& traceLogId, int stepNo, RefPtr<ResourceState>& result, double* replayTime)
{
InjectedScriptCanvasModule module = injectedScriptCanvasModule(errorString, traceLogId);
if (!module.hasNoValue())
module.replayTraceLog(errorString, traceLogId, stepNo, &result, replayTime);
}
void InspectorCanvasAgent::getResourceState(ErrorString* errorString, const TraceLogId& traceLogId, const ResourceId& resourceId, RefPtr<ResourceState>& result)
{
InjectedScriptCanvasModule module = injectedScriptCanvasModule(errorString, traceLogId);
if (!module.hasNoValue())
module.resourceState(errorString, traceLogId, resourceId, &result);
}
void InspectorCanvasAgent::evaluateTraceLogCallArgument(ErrorString* errorString, const TraceLogId& traceLogId, int callIndex, int argumentIndex, const String* objectGroup, RefPtr<RemoteObject>& result, RefPtr<ResourceState>& resourceState)
{
InjectedScriptCanvasModule module = injectedScriptCanvasModule(errorString, traceLogId);
if (!module.hasNoValue())
module.evaluateTraceLogCallArgument(errorString, traceLogId, callIndex, argumentIndex, objectGroup ? *objectGroup : String(), &result, &resourceState);
}
ScriptObject InspectorCanvasAgent::wrapCanvas2DRenderingContextForInstrumentation(const ScriptObject& context)
{
ErrorString error;
InjectedScriptCanvasModule module = injectedScriptCanvasModule(&error, context);
if (module.hasNoValue())
return ScriptObject();
return notifyRenderingContextWasWrapped(module.wrapCanvas2DContext(context));
}
ScriptObject InspectorCanvasAgent::wrapWebGLRenderingContextForInstrumentation(const ScriptObject& glContext)
{
ErrorString error;
InjectedScriptCanvasModule module = injectedScriptCanvasModule(&error, glContext);
if (module.hasNoValue())
return ScriptObject();
return notifyRenderingContextWasWrapped(module.wrapWebGLContext(glContext));
}
ScriptObject InspectorCanvasAgent::notifyRenderingContextWasWrapped(const ScriptObject& wrappedContext)
{
ASSERT(m_frontend);
ScriptState* scriptState = wrappedContext.scriptState();
DOMWindow* domWindow = 0;
if (scriptState)
domWindow = scriptState->domWindow();
LocalFrame* frame = domWindow ? domWindow->frame() : 0;
if (frame && !m_framesWithUninstrumentedCanvases.contains(frame))
m_framesWithUninstrumentedCanvases.set(frame, false);
String frameId = m_pageAgent->frameId(frame);
if (!frameId.isEmpty())
m_frontend->contextCreated(frameId);
return wrappedContext;
}
InjectedScriptCanvasModule InspectorCanvasAgent::injectedScriptCanvasModule(ErrorString* errorString, ScriptState* scriptState)
{
if (!checkIsEnabled(errorString))
return InjectedScriptCanvasModule();
InjectedScriptCanvasModule module = InjectedScriptCanvasModule::moduleForState(m_injectedScriptManager, scriptState);
if (module.hasNoValue()) {
ASSERT_NOT_REACHED();
*errorString = "Internal error: no Canvas module";
}
return module;
}
InjectedScriptCanvasModule InspectorCanvasAgent::injectedScriptCanvasModule(ErrorString* errorString, const ScriptObject& scriptObject)
{
if (!checkIsEnabled(errorString))
return InjectedScriptCanvasModule();
if (scriptObject.hasNoValue()) {
ASSERT_NOT_REACHED();
*errorString = "Internal error: original ScriptObject has no value";
return InjectedScriptCanvasModule();
}
return injectedScriptCanvasModule(errorString, scriptObject.scriptState());
}
InjectedScriptCanvasModule InspectorCanvasAgent::injectedScriptCanvasModule(ErrorString* errorString, const String& objectId)
{
if (!checkIsEnabled(errorString))
return InjectedScriptCanvasModule();
InjectedScript injectedScript = m_injectedScriptManager->injectedScriptForObjectId(objectId);
if (injectedScript.hasNoValue()) {
*errorString = "Inspected frame has gone";
return InjectedScriptCanvasModule();
}
return injectedScriptCanvasModule(errorString, injectedScript.scriptState());
}
void InspectorCanvasAgent::findFramesWithUninstrumentedCanvases()
{
class NodeVisitor FINAL : public WrappedNodeVisitor {
public:
NodeVisitor(Page* page, FramesWithUninstrumentedCanvases& result)
: m_page(page)
, m_framesWithUninstrumentedCanvases(result)
{
}
virtual void visitNode(Node* node) OVERRIDE
{
ASSERT(node);
if (!isHTMLCanvasElement(*node) || !node->document().frame())
return;
LocalFrame* frame = node->document().frame();
if (frame->page() != m_page)
return;
if (toHTMLCanvasElement(node)->renderingContext())
m_framesWithUninstrumentedCanvases.set(frame, true);
}
private:
Page* m_page;
FramesWithUninstrumentedCanvases& m_framesWithUninstrumentedCanvases;
} nodeVisitor(m_pageAgent->page(), m_framesWithUninstrumentedCanvases);
m_framesWithUninstrumentedCanvases.clear();
ScriptProfiler::visitNodeWrappers(&nodeVisitor);
if (m_frontend) {
for (FramesWithUninstrumentedCanvases::const_iterator it = m_framesWithUninstrumentedCanvases.begin(); it != m_framesWithUninstrumentedCanvases.end(); ++it) {
String frameId = m_pageAgent->frameId(it->key);
if (!frameId.isEmpty())
m_frontend->contextCreated(frameId);
}
}
}
bool InspectorCanvasAgent::checkIsEnabled(ErrorString* errorString) const
{
if (m_enabled)
return true;
*errorString = "Canvas agent is not enabled";
return false;
}
void InspectorCanvasAgent::didCommitLoad(LocalFrame*, DocumentLoader* loader)
{
if (!m_enabled)
return;
LocalFrame* frame = loader->frame();
if (frame == m_pageAgent->mainFrame()) {
for (FramesWithUninstrumentedCanvases::iterator it = m_framesWithUninstrumentedCanvases.begin(); it != m_framesWithUninstrumentedCanvases.end(); ++it)
it->value = false;
m_frontend->traceLogsRemoved(0, 0);
} else {
while (frame) {
if (m_framesWithUninstrumentedCanvases.contains(frame))
m_framesWithUninstrumentedCanvases.set(frame, false);
if (m_pageAgent->hasIdForFrame(frame)) {
String frameId = m_pageAgent->frameId(frame);
m_frontend->traceLogsRemoved(&frameId, 0);
}
frame = frame->tree().traverseNext();
}
}
}
void InspectorCanvasAgent::frameDetachedFromParent(LocalFrame* frame)
{
if (m_enabled)
m_framesWithUninstrumentedCanvases.remove(frame);
}
void InspectorCanvasAgent::didBeginFrame()
{
if (!m_enabled)
return;
ErrorString error;
for (FramesWithUninstrumentedCanvases::const_iterator it = m_framesWithUninstrumentedCanvases.begin(); it != m_framesWithUninstrumentedCanvases.end(); ++it) {
InjectedScriptCanvasModule module = injectedScriptCanvasModule(&error, mainWorldScriptState(it->key));
if (!module.hasNoValue())
module.markFrameEnd();
}
}
}