Web Inspector: We should have a way to capture heap snapshots programatically.
[WebKit-https.git] / Source / JavaScriptCore / inspector / agents / InspectorConsoleAgent.cpp
index 814a0c1..3116b06 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2014 Apple Inc. All rights reserved.
+ * Copyright (C) 2014, 2015 Apple Inc. All rights reserved.
  * Copyright (C) 2011 Google Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -28,6 +28,8 @@
 
 #include "ConsoleMessage.h"
 #include "InjectedScriptManager.h"
+#include "InspectorFrontendRouter.h"
+#include "InspectorHeapAgent.h"
 #include "ScriptArguments.h"
 #include "ScriptCallFrame.h"
 #include "ScriptCallStack.h"
 
 namespace Inspector {
 
-static const unsigned maximumConsoleMessages = 1000;
-static const int expireConsoleMessagesStep = 100;
+static const unsigned maximumConsoleMessages = 100;
+static const int expireConsoleMessagesStep = 10;
 
-InspectorConsoleAgent::InspectorConsoleAgent(InjectedScriptManager* injectedScriptManager)
+InspectorConsoleAgent::InspectorConsoleAgent(AgentContext& context, InspectorHeapAgent* heapAgent)
     : InspectorAgentBase(ASCIILiteral("Console"))
-    , m_injectedScriptManager(injectedScriptManager)
-    , m_previousMessage(nullptr)
-    , m_expiredConsoleMessageCount(0)
-    , m_enabled(false)
+    , m_injectedScriptManager(context.injectedScriptManager)
+    , m_frontendDispatcher(std::make_unique<ConsoleFrontendDispatcher>(context.frontendRouter))
+    , m_backendDispatcher(ConsoleBackendDispatcher::create(context.backendDispatcher, this))
+    , m_heapAgent(heapAgent)
 {
 }
 
@@ -55,17 +57,12 @@ InspectorConsoleAgent::~InspectorConsoleAgent()
 {
 }
 
-void InspectorConsoleAgent::didCreateFrontendAndBackend(FrontendChannel* frontendChannel, BackendDispatcher* backendDispatcher)
+void InspectorConsoleAgent::didCreateFrontendAndBackend(FrontendRouter*, BackendDispatcher*)
 {
-    m_frontendDispatcher = std::make_unique<ConsoleFrontendDispatcher>(frontendChannel);
-    m_backendDispatcher = ConsoleBackendDispatcher::create(backendDispatcher, this);
 }
 
 void InspectorConsoleAgent::willDestroyFrontendAndBackend(DisconnectReason)
 {
-    m_frontendDispatcher = nullptr;
-    m_backendDispatcher.clear();
-
     String errorString;
     disable(errorString);
 }
@@ -79,12 +76,12 @@ void InspectorConsoleAgent::enable(ErrorString&)
 
     if (m_expiredConsoleMessageCount) {
         ConsoleMessage expiredMessage(MessageSource::Other, MessageType::Log, MessageLevel::Warning, String::format("%d console messages are not shown.", m_expiredConsoleMessageCount));
-        expiredMessage.addToFrontend(m_frontendDispatcher.get(), m_injectedScriptManager, false);
+        expiredMessage.addToFrontend(*m_frontendDispatcher, m_injectedScriptManager, false);
     }
 
     size_t messageCount = m_consoleMessages.size();
     for (size_t i = 0; i < messageCount; ++i)
-        m_consoleMessages[i]->addToFrontend(m_frontendDispatcher.get(), m_injectedScriptManager, false);
+        m_consoleMessages[i]->addToFrontend(*m_frontendDispatcher, m_injectedScriptManager, false);
 }
 
 void InspectorConsoleAgent::disable(ErrorString&)
@@ -101,9 +98,9 @@ void InspectorConsoleAgent::clearMessages(ErrorString&)
     m_expiredConsoleMessageCount = 0;
     m_previousMessage = nullptr;
 
-    m_injectedScriptManager->releaseObjectGroup(ASCIILiteral("console"));
+    m_injectedScriptManager.releaseObjectGroup(ASCIILiteral("console"));
 
-    if (m_frontendDispatcher && m_enabled)
+    if (m_enabled)
         m_frontendDispatcher->messagesCleared();
 }
 
@@ -118,7 +115,7 @@ void InspectorConsoleAgent::reset()
 
 void InspectorConsoleAgent::addMessageToConsole(std::unique_ptr<ConsoleMessage> message)
 {
-    if (!m_injectedScriptManager->inspectorEnvironment().developerExtrasEnabled())
+    if (!m_injectedScriptManager.inspectorEnvironment().developerExtrasEnabled())
         return;
 
     if (message->type() == MessageType::Clear) {
@@ -126,15 +123,7 @@ void InspectorConsoleAgent::addMessageToConsole(std::unique_ptr<ConsoleMessage>
         clearMessages(unused);
     }
 
-    addConsoleMessage(WTF::move(message));
-}
-
-Vector<unsigned> InspectorConsoleAgent::consoleMessageArgumentCounts() const
-{
-    Vector<unsigned> result(m_consoleMessages.size());
-    for (size_t i = 0; i < m_consoleMessages.size(); i++)
-        result[i] = m_consoleMessages[i]->argumentCount();
-    return result;
+    addConsoleMessage(WTFMove(message));
 }
 
 void InspectorConsoleAgent::startTiming(const String& title)
@@ -166,6 +155,19 @@ void InspectorConsoleAgent::stopTiming(const String& title, PassRefPtr<ScriptCal
     addMessageToConsole(std::make_unique<ConsoleMessage>(MessageSource::ConsoleAPI, MessageType::Timing, MessageLevel::Debug, message, callStack));
 }
 
+void InspectorConsoleAgent::takeHeapSnapshot(const String& title)
+{
+    if (!m_injectedScriptManager.inspectorEnvironment().developerExtrasEnabled())
+        return;
+
+    ErrorString ignored;
+    double timestamp;
+    String snapshotData;
+    m_heapAgent->snapshot(ignored, &timestamp, &snapshotData);
+
+    m_frontendDispatcher->heapSnapshot(timestamp, snapshotData, title.isEmpty() ? nullptr : &title);
+}
+
 void InspectorConsoleAgent::count(JSC::ExecState* state, PassRefPtr<ScriptArguments> arguments)
 {
     RefPtr<ScriptCallStack> callStack(createScriptCallStackForConsole(state, ScriptCallStack::maxCallStackSizeToCapture));
@@ -187,7 +189,12 @@ void InspectorConsoleAgent::count(JSC::ExecState* state, PassRefPtr<ScriptArgume
 
     m_counts.add(identifier, count);
 
-    String message = title + ": " + String::number(count);
+    String message;
+    if (title.isEmpty())
+        message = "<no label>: " + String::number(count);
+    else
+        message = title + ": " + String::number(count);
+
     addMessageToConsole(std::make_unique<ConsoleMessage>(MessageSource::ConsoleAPI, MessageType::Log, MessageLevel::Debug, message, callStack));
 }
 
@@ -200,21 +207,21 @@ static bool isGroupMessage(MessageType type)
 
 void InspectorConsoleAgent::addConsoleMessage(std::unique_ptr<ConsoleMessage> consoleMessage)
 {
-    ASSERT(m_injectedScriptManager->inspectorEnvironment().developerExtrasEnabled());
+    ASSERT(m_injectedScriptManager.inspectorEnvironment().developerExtrasEnabled());
     ASSERT_ARG(consoleMessage, consoleMessage);
 
     if (m_previousMessage && !isGroupMessage(m_previousMessage->type()) && m_previousMessage->isEqual(consoleMessage.get())) {
         m_previousMessage->incrementCount();
-        if (m_frontendDispatcher && m_enabled)
-            m_previousMessage->updateRepeatCountInConsole(m_frontendDispatcher.get());
+        if (m_enabled)
+            m_previousMessage->updateRepeatCountInConsole(*m_frontendDispatcher);
     } else {
         m_previousMessage = consoleMessage.get();
-        m_consoleMessages.append(WTF::move(consoleMessage));
-        if (m_frontendDispatcher && m_enabled)
-            m_previousMessage->addToFrontend(m_frontendDispatcher.get(), m_injectedScriptManager, true);
+        m_consoleMessages.append(WTFMove(consoleMessage));
+        if (m_enabled)
+            m_previousMessage->addToFrontend(*m_frontendDispatcher, m_injectedScriptManager, true);
     }
 
-    if (!m_frontendDispatcher && m_consoleMessages.size() >= maximumConsoleMessages) {
+    if (m_consoleMessages.size() >= maximumConsoleMessages) {
         m_expiredConsoleMessageCount += expireConsoleMessagesStep;
         m_consoleMessages.remove(0, expireConsoleMessagesStep);
     }