1/*
2 * Copyright (C) 2013 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(JAVASCRIPT_DEBUGGER) && ENABLE(INSPECTOR)
34
35#include "InspectorHeapProfilerAgent.h"
36
37#include "InjectedScript.h"
38#include "InjectedScriptHost.h"
39#include "InspectorState.h"
40#include "InstrumentingAgents.h"
41#include "ScriptProfiler.h"
42
43namespace WebCore {
44
45namespace HeapProfilerAgentState {
46static const char profileHeadersRequested[] = "profileHeadersRequested";
47}
48
49static const char* const UserInitiatedProfileNameHeap = "org.webkit.profiles.user-initiated";
50
51PassOwnPtr<InspectorHeapProfilerAgent> InspectorHeapProfilerAgent::create(InstrumentingAgents* instrumentingAgents, InspectorCompositeState* inspectorState, InjectedScriptManager* injectedScriptManager)
52{
53    return adoptPtr(new InspectorHeapProfilerAgent(instrumentingAgents, inspectorState, injectedScriptManager));
54}
55
56InspectorHeapProfilerAgent::InspectorHeapProfilerAgent(InstrumentingAgents* instrumentingAgents, InspectorCompositeState* inspectorState, InjectedScriptManager* injectedScriptManager)
57    : InspectorBaseAgent<InspectorHeapProfilerAgent>("HeapProfiler", instrumentingAgents, inspectorState)
58    , m_injectedScriptManager(injectedScriptManager)
59    , m_frontend(0)
60    , m_nextUserInitiatedHeapSnapshotNumber(1)
61{
62    m_instrumentingAgents->setInspectorHeapProfilerAgent(this);
63}
64
65InspectorHeapProfilerAgent::~InspectorHeapProfilerAgent()
66{
67    m_instrumentingAgents->setInspectorHeapProfilerAgent(0);
68}
69
70void InspectorHeapProfilerAgent::resetState()
71{
72    m_snapshots.clear();
73    m_nextUserInitiatedHeapSnapshotNumber = 1;
74    resetFrontendProfiles();
75    m_injectedScriptManager->injectedScriptHost()->clearInspectedObjects();
76}
77
78void InspectorHeapProfilerAgent::resetFrontendProfiles()
79{
80    if (!m_frontend)
81        return;
82    if (!m_state->getBoolean(HeapProfilerAgentState::profileHeadersRequested))
83        return;
84    if (m_snapshots.isEmpty())
85        m_frontend->resetProfiles();
86}
87
88void InspectorHeapProfilerAgent::setFrontend(InspectorFrontend* frontend)
89{
90    m_frontend = frontend->heapprofiler();
91}
92
93void InspectorHeapProfilerAgent::clearFrontend()
94{
95    m_state->setBoolean(HeapProfilerAgentState::profileHeadersRequested, false);
96    m_frontend = 0;
97}
98
99void InspectorHeapProfilerAgent::restore()
100{
101    resetFrontendProfiles();
102}
103
104void InspectorHeapProfilerAgent::collectGarbage(WebCore::ErrorString*)
105{
106    ScriptProfiler::collectGarbage();
107}
108
109PassRefPtr<TypeBuilder::HeapProfiler::ProfileHeader> InspectorHeapProfilerAgent::createSnapshotHeader(const ScriptHeapSnapshot& snapshot)
110{
111    RefPtr<TypeBuilder::HeapProfiler::ProfileHeader> header = TypeBuilder::HeapProfiler::ProfileHeader::create()
112        .setUid(snapshot.uid())
113        .setTitle(snapshot.title());
114    header->setMaxJSObjectId(snapshot.maxSnapshotJSObjectId());
115    return header.release();
116}
117
118void InspectorHeapProfilerAgent::hasHeapProfiler(ErrorString*, bool* result)
119{
120    *result = ScriptProfiler::hasHeapProfiler();
121}
122
123void InspectorHeapProfilerAgent::getProfileHeaders(ErrorString*, RefPtr<TypeBuilder::Array<TypeBuilder::HeapProfiler::ProfileHeader> >& headers)
124{
125    m_state->setBoolean(HeapProfilerAgentState::profileHeadersRequested, true);
126    headers = TypeBuilder::Array<TypeBuilder::HeapProfiler::ProfileHeader>::create();
127
128    IdToHeapSnapshotMap::iterator snapshotsEnd = m_snapshots.end();
129    for (IdToHeapSnapshotMap::iterator it = m_snapshots.begin(); it != snapshotsEnd; ++it)
130        headers->addItem(createSnapshotHeader(*it->value));
131}
132
133void InspectorHeapProfilerAgent::getHeapSnapshot(ErrorString* errorString, int rawUid)
134{
135    class OutputStream : public ScriptHeapSnapshot::OutputStream {
136    public:
137        OutputStream(InspectorFrontend::HeapProfiler* frontend, unsigned uid)
138            : m_frontend(frontend), m_uid(uid) { }
139        void Write(const String& chunk) { m_frontend->addHeapSnapshotChunk(m_uid, chunk); }
140        void Close() { m_frontend->finishHeapSnapshot(m_uid); }
141    private:
142        InspectorFrontend::HeapProfiler* m_frontend;
143        int m_uid;
144    };
145
146    unsigned uid = static_cast<unsigned>(rawUid);
147    IdToHeapSnapshotMap::iterator it = m_snapshots.find(uid);
148    if (it == m_snapshots.end()) {
149        *errorString = "Profile wasn't found";
150        return;
151    }
152    RefPtr<ScriptHeapSnapshot> snapshot = it->value;
153    if (m_frontend) {
154        OutputStream stream(m_frontend, uid);
155        snapshot->writeJSON(&stream);
156    }
157}
158
159void InspectorHeapProfilerAgent::removeProfile(ErrorString*, int rawUid)
160{
161    unsigned uid = static_cast<unsigned>(rawUid);
162    if (m_snapshots.contains(uid))
163        m_snapshots.remove(uid);
164}
165
166void InspectorHeapProfilerAgent::takeHeapSnapshot(ErrorString*, const bool* reportProgress)
167{
168    class HeapSnapshotProgress: public ScriptProfiler::HeapSnapshotProgress {
169    public:
170        explicit HeapSnapshotProgress(InspectorFrontend::HeapProfiler* frontend)
171            : m_frontend(frontend) { }
172        void Start(int totalWork)
173        {
174            m_totalWork = totalWork;
175        }
176        void Worked(int workDone)
177        {
178            if (m_frontend)
179                m_frontend->reportHeapSnapshotProgress(workDone, m_totalWork);
180        }
181        void Done() { }
182        bool isCanceled() { return false; }
183    private:
184        InspectorFrontend::HeapProfiler* m_frontend;
185        int m_totalWork;
186    };
187
188    String title = makeString(UserInitiatedProfileNameHeap, '.', String::number(m_nextUserInitiatedHeapSnapshotNumber));
189    ++m_nextUserInitiatedHeapSnapshotNumber;
190
191    HeapSnapshotProgress progress(reportProgress && *reportProgress ? m_frontend : 0);
192    RefPtr<ScriptHeapSnapshot> snapshot = ScriptProfiler::takeHeapSnapshot(title, &progress);
193    if (snapshot) {
194        m_snapshots.add(snapshot->uid(), snapshot);
195        if (m_frontend)
196            m_frontend->addProfileHeader(createSnapshotHeader(*snapshot));
197    }
198}
199
200void InspectorHeapProfilerAgent::getObjectByHeapObjectId(ErrorString* error, const String& heapSnapshotObjectId, const String* objectGroup, RefPtr<TypeBuilder::Runtime::RemoteObject>& result)
201{
202    bool ok;
203    unsigned id = heapSnapshotObjectId.toUInt(&ok);
204    if (!ok) {
205        *error = "Invalid heap snapshot object id";
206        return;
207    }
208    ScriptObject heapObject = ScriptProfiler::objectByHeapObjectId(id);
209    if (heapObject.hasNoValue()) {
210        *error = "Object is not available";
211        return;
212    }
213    InjectedScript injectedScript = m_injectedScriptManager->injectedScriptFor(heapObject.scriptState());
214    if (injectedScript.hasNoValue()) {
215        *error = "Object is not available. Inspected context is gone";
216        return;
217    }
218    result = injectedScript.wrapObject(heapObject, objectGroup ? *objectGroup : "");
219    if (!result)
220        *error = "Failed to wrap object";
221}
222
223void InspectorHeapProfilerAgent::getHeapObjectId(ErrorString* errorString, const String& objectId, String* heapSnapshotObjectId)
224{
225    InjectedScript injectedScript = m_injectedScriptManager->injectedScriptForObjectId(objectId);
226    if (injectedScript.hasNoValue()) {
227        *errorString = "Inspected context has gone";
228        return;
229    }
230    ScriptValue value = injectedScript.findObjectById(objectId);
231    if (value.hasNoValue() || value.isUndefined()) {
232        *errorString = "Object with given id not found";
233        return;
234    }
235    unsigned id = ScriptProfiler::getHeapObjectId(value);
236    *heapSnapshotObjectId = String::number(id);
237}
238
239} // namespace WebCore
240
241#endif // ENABLE(JAVASCRIPT_DEBUGGER) && ENABLE(INSPECTOR)
242