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#ifndef InspectorCanvasAgent_h
32#define InspectorCanvasAgent_h
33
34#if ENABLE(INSPECTOR)
35
36#include "InspectorBaseAgent.h"
37#include "InspectorFrontend.h"
38#include "InspectorTypeBuilder.h"
39#include "ScriptState.h"
40#include <wtf/HashMap.h>
41#include <wtf/PassOwnPtr.h>
42#include <wtf/PassRefPtr.h>
43#include <wtf/text/WTFString.h>
44
45namespace WebCore {
46
47class Frame;
48class InjectedScriptCanvasModule;
49class InjectedScriptManager;
50class InspectorPageAgent;
51class InspectorState;
52class InstrumentingAgents;
53class ScriptObject;
54
55typedef String ErrorString;
56
57class InspectorCanvasAgent : public InspectorBaseAgent<InspectorCanvasAgent>, public InspectorBackendDispatcher::CanvasCommandHandler {
58public:
59    static PassOwnPtr<InspectorCanvasAgent> create(InstrumentingAgents* instrumentingAgents, InspectorCompositeState* state, InspectorPageAgent* pageAgent, InjectedScriptManager* injectedScriptManager)
60    {
61        return adoptPtr(new InspectorCanvasAgent(instrumentingAgents, state, pageAgent, injectedScriptManager));
62    }
63    ~InspectorCanvasAgent();
64
65    virtual void setFrontend(InspectorFrontend*);
66    virtual void clearFrontend();
67    virtual void restore();
68
69    void frameNavigated(Frame*);
70    void frameDetached(Frame*);
71    void didBeginFrame();
72
73    // Called from InspectorCanvasInstrumentation.
74    ScriptObject wrapCanvas2DRenderingContextForInstrumentation(const ScriptObject&);
75#if ENABLE(WEBGL)
76    ScriptObject wrapWebGLRenderingContextForInstrumentation(const ScriptObject&);
77#endif
78
79    // Called from the front-end.
80    virtual void enable(ErrorString*);
81    virtual void disable(ErrorString*);
82    virtual void dropTraceLog(ErrorString*, const TypeBuilder::Canvas::TraceLogId&);
83    virtual void hasUninstrumentedCanvases(ErrorString*, bool*);
84    virtual void captureFrame(ErrorString*, const TypeBuilder::Network::FrameId*, TypeBuilder::Canvas::TraceLogId*);
85    virtual void startCapturing(ErrorString*, const TypeBuilder::Network::FrameId*, TypeBuilder::Canvas::TraceLogId*);
86    virtual void stopCapturing(ErrorString*, const TypeBuilder::Canvas::TraceLogId&);
87    virtual void getTraceLog(ErrorString*, const TypeBuilder::Canvas::TraceLogId&, const int*, const int*, RefPtr<TypeBuilder::Canvas::TraceLog>&);
88    virtual void replayTraceLog(ErrorString*, const TypeBuilder::Canvas::TraceLogId&, int, RefPtr<TypeBuilder::Canvas::ResourceState>&);
89    virtual void getResourceInfo(ErrorString*, const TypeBuilder::Canvas::ResourceId&, RefPtr<TypeBuilder::Canvas::ResourceInfo>&);
90    virtual void getResourceState(ErrorString*, const TypeBuilder::Canvas::TraceLogId&, const TypeBuilder::Canvas::ResourceId&, RefPtr<TypeBuilder::Canvas::ResourceState>&);
91
92private:
93    InspectorCanvasAgent(InstrumentingAgents*, InspectorCompositeState*, InspectorPageAgent*, InjectedScriptManager*);
94
95    InjectedScriptCanvasModule injectedScriptCanvasModule(ErrorString*, ScriptState*);
96    InjectedScriptCanvasModule injectedScriptCanvasModule(ErrorString*, const ScriptObject&);
97    InjectedScriptCanvasModule injectedScriptCanvasModule(ErrorString*, const String&);
98
99    void findFramesWithUninstrumentedCanvases();
100    bool checkIsEnabled(ErrorString*) const;
101    ScriptObject notifyRenderingContextWasWrapped(const ScriptObject&);
102
103    InspectorPageAgent* m_pageAgent;
104    InjectedScriptManager* m_injectedScriptManager;
105    InspectorFrontend::Canvas* m_frontend;
106    bool m_enabled;
107    // Contains all frames with canvases, value is true only for frames that have an uninstrumented canvas.
108    typedef HashMap<Frame*, bool> FramesWithUninstrumentedCanvases;
109    FramesWithUninstrumentedCanvases m_framesWithUninstrumentedCanvases;
110};
111
112} // namespace WebCore
113
114#endif // ENABLE(INSPECTOR)
115
116#endif // !defined(InspectorCanvasAgent_h)
117