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 "InspectorFrontend.h"
36 #include "PlatformString.h"
37 #include <wtf/Forward.h>
38 #include <wtf/HashMap.h>
39 #include <wtf/Noncopyable.h>
40 #include <wtf/PassOwnPtr.h>
41 
42 namespace WebCore {
43 
44 class InspectorArray;
45 class InspectorConsoleAgent;
46 class InspectorFrontend;
47 class InspectorObject;
48 class InspectorState;
49 class InstrumentingAgents;
50 class Page;
51 class ScriptHeapSnapshot;
52 class ScriptProfile;
53 
54 typedef String ErrorString;
55 
56 class InspectorProfilerAgent {
57     WTF_MAKE_NONCOPYABLE(InspectorProfilerAgent); WTF_MAKE_FAST_ALLOCATED;
58 public:
59     static PassOwnPtr<InspectorProfilerAgent> create(InstrumentingAgents*, InspectorConsoleAgent*, Page*, InspectorState*);
60     virtual ~InspectorProfilerAgent();
61 
62     void addProfile(PassRefPtr<ScriptProfile> prpProfile, unsigned lineNumber, const String& sourceURL);
63     void addProfileFinishedMessageToConsole(PassRefPtr<ScriptProfile>, unsigned lineNumber, const String& sourceURL);
64     void addStartProfilingMessageToConsole(const String& title, unsigned lineNumber, const String& sourceURL);
65     void collectGarbage(ErrorString*);
clearProfiles(ErrorString *)66     void clearProfiles(ErrorString*) { resetState(); }
67     void resetState();
68 
69     void enable(ErrorString*);
70     void disable(ErrorString*);
isEnabled(ErrorString *,bool * result)71     void isEnabled(ErrorString*, bool* result) { *result = enabled(); }
start(ErrorString *)72     void start(ErrorString*) { startUserInitiatedProfiling(); }
stop(ErrorString *)73     void stop(ErrorString*) { stopUserInitiatedProfiling(); }
74 
75     void disable();
76     void enable(bool skipRecompile);
enabled()77     bool enabled() { return m_enabled; }
78     String getCurrentUserInitiatedProfileName(bool incrementProfileNumber = false);
79     void getProfileHeaders(ErrorString* error, RefPtr<InspectorArray>* headers);
80     void getProfile(ErrorString* error, const String& type, unsigned uid, RefPtr<InspectorObject>* profileObject);
isRecordingUserInitiatedProfile()81     bool isRecordingUserInitiatedProfile() { return m_recordingUserInitiatedProfile; }
82     void removeProfile(ErrorString* error, const String& type, unsigned uid);
83 
84     void setFrontend(InspectorFrontend*);
85     void clearFrontend();
86     void restore();
87 
88     void startUserInitiatedProfiling();
89     void stopUserInitiatedProfiling(bool ignoreProfile = false);
90     void takeHeapSnapshot(ErrorString* error, bool detailed);
91     void toggleRecordButton(bool isProfiling);
92 
93 private:
94     typedef HashMap<unsigned int, RefPtr<ScriptProfile> > ProfilesMap;
95     typedef HashMap<unsigned int, RefPtr<ScriptHeapSnapshot> > HeapSnapshotsMap;
96 
97     void resetFrontendProfiles();
98     void restoreEnablement();
99 
100     InspectorProfilerAgent(InstrumentingAgents*, InspectorConsoleAgent*, Page*, InspectorState*);
101     PassRefPtr<InspectorObject> createProfileHeader(const ScriptProfile& profile);
102     PassRefPtr<InspectorObject> createSnapshotHeader(const ScriptHeapSnapshot& snapshot);
103 
104     InstrumentingAgents* m_instrumentingAgents;
105     InspectorConsoleAgent* m_consoleAgent;
106     Page* m_inspectedPage;
107     InspectorState* m_inspectorState;
108     InspectorFrontend::Profiler* m_frontend;
109     bool m_enabled;
110     bool m_recordingUserInitiatedProfile;
111     int m_currentUserInitiatedProfileNumber;
112     unsigned m_nextUserInitiatedProfileNumber;
113     unsigned m_nextUserInitiatedHeapSnapshotNumber;
114     ProfilesMap m_profiles;
115     HeapSnapshotsMap m_snapshots;
116 };
117 
118 } // namespace WebCore
119 
120 #endif // ENABLE(JAVASCRIPT_DEBUGGER) && ENABLE(INSPECTOR)
121 
122 #endif // !defined(InspectorProfilerAgent_h)
123