2010-11-29  Mikhail Naganov  <mnaganov@chromium.org>

        Reviewed by Pavel Feldman.

        WebInspector: Request JSON-serialized heap snapshot from JS engine.
        This simplifies heap snapshots interaction and API. Instead of
        having objects representing snapshot entities, the whole snapshot
        is transferred to WebInspector and parsed there.

        https://bugs.webkit.org/show_bug.cgi?id=49974

        * bindings/js/ScriptHeapSnapshot.h:
        (WebCore::ScriptHeapSnapshot::OutputStream::~OutputStream):
        (WebCore::ScriptHeapSnapshot::~ScriptHeapSnapshot):
        (WebCore::ScriptHeapSnapshot::writeJSON):
        (WebCore::ScriptHeapSnapshot::ScriptHeapSnapshot):
        * bindings/v8/ScriptHeapSnapshot.cpp:
        (WebCore::ScriptHeapSnapshot::writeJSON):
        * bindings/v8/ScriptHeapSnapshot.h:
        (WebCore::ScriptHeapSnapshot::OutputStream::~OutputStream):
        * inspector/Inspector.idl:
        * inspector/InspectorProfilerAgent.cpp:
        (WebCore::InspectorProfilerAgent::getProfile):
        * inspector/front-end/HeapSnapshotView.js:
        (WebInspector.HeapSnapshotEdgesIterator):
        (WebInspector.HeapSnapshotEdgesIterator.prototype.get done):
        (WebInspector.HeapSnapshotEdgesIterator.prototype.get isElement):
        (WebInspector.HeapSnapshotEdgesIterator.prototype.get isHidden):
        (WebInspector.HeapSnapshotEdgesIterator.prototype.get name):
        (WebInspector.HeapSnapshotEdgesIterator.prototype.next):
        (WebInspector.HeapSnapshotEdgesIterator.prototype.get node):
        (WebInspector.HeapSnapshotEdgesIterator.prototype.get nodeIndex):
        (WebInspector.HeapSnapshotEdgesIterator.prototype._getNameOrIndex):
        (WebInspector.HeapSnapshotEdgesIterator.prototype._getType):
        (WebInspector.HeapSnapshotNodeWrapper):
        (WebInspector.HeapSnapshotNodeWrapper.prototype.get edges):
        (WebInspector.HeapSnapshotNodeWrapper.prototype.get edgesCount):
        (WebInspector.HeapSnapshotNodeWrapper.prototype.get instancesCount):
        (WebInspector.HeapSnapshotNodeWrapper.prototype.get isHidden):
        (WebInspector.HeapSnapshotNodeWrapper.prototype.get name):
        (WebInspector.HeapSnapshotNodeWrapper.prototype.get selfSize):
        (WebInspector.HeapSnapshotNodeWrapper.prototype._getName):
        (WebInspector.HeapSnapshotNodeWrapper.prototype._getEdges):
        (WebInspector.HeapSnapshotNodeWrapper.prototype._getType):
        (WebInspector.HeapSnapshot):
        (WebInspector.HeapSnapshot.prototype._init):
        (WebInspector.HeapSnapshot.prototype.get rootEdges):
        (WebInspector.HeapSnapshotView.prototype.snapshotLoaded):
        (WebInspector.HeapSnapshotView.prototype._loadProfile.processLoadedSnapshot):
        (WebInspector.HeapSnapshotView.prototype._loadProfile):
        (WebInspector.HeapSnapshotView.prototype._convertSnapshot):
        (WebInspector.HeapSnapshotView.prototype._prepareProfile.mergeRetainers):
        (WebInspector.HeapSnapshotView.prototype._prepareProfile):
        (WebInspector.HeapSnapshotView.prototype._sortData):
        * inspector/front-end/ProfilesPanel.js:
        (WebInspector.ProfilesPanel):
        (WebInspector.ProfilesPanel.prototype.addHeapSnapshotChunk):
        (WebInspector.ProfilesPanel.prototype.finishHeapSnapshot):
        * inspector/front-end/inspector.js:
        (WebInspector.addHeapSnapshotChunk):
        (WebInspector.finishHeapSnapshot):

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@72843 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/WebCore/inspector/InspectorProfilerAgent.cpp b/WebCore/inspector/InspectorProfilerAgent.cpp
index 1248677..6d5364f 100644
--- a/WebCore/inspector/InspectorProfilerAgent.cpp
+++ b/WebCore/inspector/InspectorProfilerAgent.cpp
@@ -156,6 +156,21 @@
         (*headers)->pushObject(createSnapshotHeader(*it->second));
 }
 
+namespace {
+
+class OutputStream : public ScriptHeapSnapshot::OutputStream {
+public:
+    OutputStream(InspectorFrontend* frontend, unsigned long uid)
+        : m_frontend(frontend), m_uid(uid) { }
+    void Write(const String& chunk) { m_frontend->addHeapSnapshotChunk(m_uid, chunk); }
+    void Close() { m_frontend->finishHeapSnapshot(m_uid); }
+private:
+    InspectorFrontend* m_frontend;
+    unsigned long m_uid;
+};
+
+} // namespace
+
 void InspectorProfilerAgent::getProfile(const String& type, unsigned uid, RefPtr<InspectorObject>* profileObject)
 {
     if (type == CPUProfileType) {
@@ -167,8 +182,12 @@
     } else if (type == HeapProfileType) {
         HeapSnapshotsMap::iterator it = m_snapshots.find(uid);
         if (it != m_snapshots.end()) {
-            *profileObject = createSnapshotHeader(*it->second);
-            (*profileObject)->setObject("head", it->second->buildInspectorObjectForHead());
+            RefPtr<ScriptHeapSnapshot> snapshot = it->second;
+            *profileObject = createSnapshotHeader(*snapshot);
+            if (m_frontend) {
+                OutputStream stream(m_frontend, uid);
+                snapshot->writeJSON(&stream);
+            }
         }
     }
 }