1/*
2 * Copyright (C) 2010 Apple Inc. All rights reserved.
3 * Copyright (C) 2010 Google Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1.  Redistributions of source code must retain the above copyright
10 *     notice, this list of conditions and the following disclaimer.
11 * 2.  Redistributions in binary form must reproduce the above copyright
12 *     notice, this list of conditions and the following disclaimer in the
13 *     documentation and/or other materials provided with the distribution.
14 * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
15 *     its contributors may be used to endorse or promote products derived
16 *     from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#ifndef InspectorProfilerAgent_h
31#define InspectorProfilerAgent_h
32
33#if ENABLE(JAVASCRIPT_DEBUGGER) && ENABLE(INSPECTOR)
34
35#include "InspectorBaseAgent.h"
36#include "InspectorFrontend.h"
37#include <wtf/Forward.h>
38#include <wtf/HashMap.h>
39#include <wtf/Noncopyable.h>
40#include <wtf/PassOwnPtr.h>
41#include <wtf/text/WTFString.h>
42
43namespace WebCore {
44
45class InjectedScriptManager;
46class InspectorArray;
47class InspectorConsoleAgent;
48class InspectorFrontend;
49class InspectorObject;
50class InspectorState;
51class InstrumentingAgents;
52class Page;
53class ScriptHeapSnapshot;
54class ScriptProfile;
55class WorkerContext;
56
57typedef String ErrorString;
58
59class InspectorProfilerAgent : public InspectorBaseAgent<InspectorProfilerAgent>, public InspectorBackendDispatcher::ProfilerCommandHandler {
60    WTF_MAKE_NONCOPYABLE(InspectorProfilerAgent); WTF_MAKE_FAST_ALLOCATED;
61public:
62    static PassOwnPtr<InspectorProfilerAgent> create(InstrumentingAgents*, InspectorConsoleAgent*, Page*, InspectorCompositeState*, InjectedScriptManager*);
63#if ENABLE(WORKERS)
64    static PassOwnPtr<InspectorProfilerAgent> create(InstrumentingAgents*, InspectorConsoleAgent*, WorkerContext*, InspectorCompositeState*, InjectedScriptManager*);
65#endif
66    virtual ~InspectorProfilerAgent();
67
68    void addProfile(PassRefPtr<ScriptProfile> prpProfile, unsigned lineNumber, unsigned columnNumber, const String& sourceURL);
69    void addProfileFinishedMessageToConsole(PassRefPtr<ScriptProfile>, unsigned lineNumber, unsigned columnNumber, const String& sourceURL);
70    void addStartProfilingMessageToConsole(const String& title, unsigned lineNumber, unsigned columnNumber, const String& sourceURL);
71    virtual void collectGarbage(ErrorString*);
72    virtual void clearProfiles(ErrorString*) { resetState(); }
73    void resetState();
74
75    virtual void causesRecompilation(ErrorString*, bool*);
76    virtual void recompileScript() = 0;
77    virtual void isSampling(ErrorString*, bool*);
78    virtual void hasHeapProfiler(ErrorString*, bool*);
79
80    virtual void enable(ErrorString*);
81    virtual void disable(ErrorString*);
82    virtual void start(ErrorString* = 0);
83    virtual void stop(ErrorString* = 0);
84
85    void disable();
86    void enable(bool skipRecompile);
87    bool enabled() { return m_enabled; }
88    String getCurrentUserInitiatedProfileName(bool incrementProfileNumber = false);
89    virtual void getProfileHeaders(ErrorString*, RefPtr<TypeBuilder::Array<TypeBuilder::Profiler::ProfileHeader> >&);
90    virtual void getCPUProfile(ErrorString*, int uid, RefPtr<TypeBuilder::Profiler::CPUProfile>&);
91    virtual void getHeapSnapshot(ErrorString*, int uid);
92    virtual void removeProfile(ErrorString*, const String& type, int uid);
93
94    virtual void setFrontend(InspectorFrontend*);
95    virtual void clearFrontend();
96    virtual void restore();
97
98    virtual void takeHeapSnapshot(ErrorString*, const bool* reportProgress);
99    void toggleRecordButton(bool isProfiling);
100
101    virtual void getObjectByHeapObjectId(ErrorString*, const String& heapSnapshotObjectId, const String* objectGroup, RefPtr<TypeBuilder::Runtime::RemoteObject>& result);
102    virtual void getHeapObjectId(ErrorString*, const String& objectId, String* heapSnapshotObjectId);
103
104    void willProcessTask();
105    void didProcessTask();
106
107protected:
108    InspectorProfilerAgent(InstrumentingAgents*, InspectorConsoleAgent*, InspectorCompositeState*, InjectedScriptManager*);
109    virtual void startProfiling(const String& title) = 0;
110    virtual PassRefPtr<ScriptProfile> stopProfiling(const String& title) = 0;
111
112private:
113    typedef HashMap<unsigned int, RefPtr<ScriptProfile> > ProfilesMap;
114    typedef HashMap<unsigned int, RefPtr<ScriptHeapSnapshot> > HeapSnapshotsMap;
115
116    void resetFrontendProfiles();
117    void restoreEnablement();
118
119    PassRefPtr<TypeBuilder::Profiler::ProfileHeader> createProfileHeader(const ScriptProfile&);
120    PassRefPtr<TypeBuilder::Profiler::ProfileHeader> createSnapshotHeader(const ScriptHeapSnapshot&);
121
122    InspectorConsoleAgent* m_consoleAgent;
123    InjectedScriptManager* m_injectedScriptManager;
124    InspectorFrontend::Profiler* m_frontend;
125    bool m_enabled;
126    bool m_recordingCPUProfile;
127    int m_currentUserInitiatedProfileNumber;
128    unsigned m_nextUserInitiatedProfileNumber;
129    unsigned m_nextUserInitiatedHeapSnapshotNumber;
130    ProfilesMap m_profiles;
131    HeapSnapshotsMap m_snapshots;
132
133    typedef HashMap<String, double> ProfileNameIdleTimeMap;
134    ProfileNameIdleTimeMap* m_profileNameIdleTimeMap;
135    double m_previousTaskEndTime;
136};
137
138} // namespace WebCore
139
140#endif // ENABLE(JAVASCRIPT_DEBUGGER) && ENABLE(INSPECTOR)
141
142#endif // !defined(InspectorProfilerAgent_h)
143