]> git.saurik.com Git - apple/javascriptcore.git/blob - inspector/agents/InspectorConsoleAgent.cpp
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / inspector / agents / InspectorConsoleAgent.cpp
1 /*
2 * Copyright (C) 2014 Apple Inc. All rights reserved.
3 * Copyright (C) 2011 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 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
21 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include "config.h"
27 #include "InspectorConsoleAgent.h"
28
29 #include "ConsoleMessage.h"
30 #include "InjectedScriptManager.h"
31 #include "ScriptArguments.h"
32 #include "ScriptCallFrame.h"
33 #include "ScriptCallStack.h"
34 #include "ScriptCallStackFactory.h"
35 #include "ScriptObject.h"
36 #include <wtf/CurrentTime.h>
37 #include <wtf/text/StringBuilder.h>
38 #include <wtf/text/WTFString.h>
39
40 namespace Inspector {
41
42 static const unsigned maximumConsoleMessages = 1000;
43 static const int expireConsoleMessagesStep = 100;
44
45 InspectorConsoleAgent::InspectorConsoleAgent(InjectedScriptManager* injectedScriptManager)
46 : InspectorAgentBase(ASCIILiteral("Console"))
47 , m_injectedScriptManager(injectedScriptManager)
48 , m_previousMessage(nullptr)
49 , m_expiredConsoleMessageCount(0)
50 , m_enabled(false)
51 {
52 }
53
54 InspectorConsoleAgent::~InspectorConsoleAgent()
55 {
56 }
57
58 void InspectorConsoleAgent::didCreateFrontendAndBackend(FrontendChannel* frontendChannel, BackendDispatcher* backendDispatcher)
59 {
60 m_frontendDispatcher = std::make_unique<ConsoleFrontendDispatcher>(frontendChannel);
61 m_backendDispatcher = ConsoleBackendDispatcher::create(backendDispatcher, this);
62 }
63
64 void InspectorConsoleAgent::willDestroyFrontendAndBackend(DisconnectReason)
65 {
66 m_frontendDispatcher = nullptr;
67 m_backendDispatcher = nullptr;
68
69 String errorString;
70 disable(errorString);
71 }
72
73 void InspectorConsoleAgent::enable(ErrorString&)
74 {
75 if (m_enabled)
76 return;
77
78 m_enabled = true;
79
80 if (m_expiredConsoleMessageCount) {
81 ConsoleMessage expiredMessage(MessageSource::Other, MessageType::Log, MessageLevel::Warning, String::format("%d console messages are not shown.", m_expiredConsoleMessageCount));
82 expiredMessage.addToFrontend(m_frontendDispatcher.get(), m_injectedScriptManager, false);
83 }
84
85 size_t messageCount = m_consoleMessages.size();
86 for (size_t i = 0; i < messageCount; ++i)
87 m_consoleMessages[i]->addToFrontend(m_frontendDispatcher.get(), m_injectedScriptManager, false);
88 }
89
90 void InspectorConsoleAgent::disable(ErrorString&)
91 {
92 if (!m_enabled)
93 return;
94
95 m_enabled = false;
96 }
97
98 void InspectorConsoleAgent::clearMessages(ErrorString&)
99 {
100 m_consoleMessages.clear();
101 m_expiredConsoleMessageCount = 0;
102 m_previousMessage = nullptr;
103
104 m_injectedScriptManager->releaseObjectGroup(ASCIILiteral("console"));
105
106 if (m_frontendDispatcher && m_enabled)
107 m_frontendDispatcher->messagesCleared();
108 }
109
110 void InspectorConsoleAgent::reset()
111 {
112 ErrorString unused;
113 clearMessages(unused);
114
115 m_times.clear();
116 m_counts.clear();
117 }
118
119 void InspectorConsoleAgent::addMessageToConsole(std::unique_ptr<ConsoleMessage> message)
120 {
121 if (!m_injectedScriptManager->inspectorEnvironment().developerExtrasEnabled())
122 return;
123
124 if (message->type() == MessageType::Clear) {
125 ErrorString unused;
126 clearMessages(unused);
127 }
128
129 addConsoleMessage(WTF::move(message));
130 }
131
132 Vector<unsigned> InspectorConsoleAgent::consoleMessageArgumentCounts() const
133 {
134 Vector<unsigned> result(m_consoleMessages.size());
135 for (size_t i = 0; i < m_consoleMessages.size(); i++)
136 result[i] = m_consoleMessages[i]->argumentCount();
137 return result;
138 }
139
140 void InspectorConsoleAgent::startTiming(const String& title)
141 {
142 // Follow Firebug's behavior of requiring a title that is not null or
143 // undefined for timing functions
144 if (title.isNull())
145 return;
146
147 m_times.add(title, monotonicallyIncreasingTime());
148 }
149
150 void InspectorConsoleAgent::stopTiming(const String& title, PassRefPtr<ScriptCallStack> callStack)
151 {
152 // Follow Firebug's behavior of requiring a title that is not null or
153 // undefined for timing functions
154 if (title.isNull())
155 return;
156
157 HashMap<String, double>::iterator it = m_times.find(title);
158 if (it == m_times.end())
159 return;
160
161 double startTime = it->value;
162 m_times.remove(it);
163
164 double elapsed = monotonicallyIncreasingTime() - startTime;
165 String message = title + String::format(": %.3fms", elapsed * 1000);
166 addMessageToConsole(std::make_unique<ConsoleMessage>(MessageSource::ConsoleAPI, MessageType::Timing, MessageLevel::Debug, message, callStack));
167 }
168
169 void InspectorConsoleAgent::count(JSC::ExecState* state, PassRefPtr<ScriptArguments> arguments)
170 {
171 RefPtr<ScriptCallStack> callStack(createScriptCallStackForConsole(state, ScriptCallStack::maxCallStackSizeToCapture));
172 const ScriptCallFrame& lastCaller = callStack->at(0);
173 // Follow Firebug's behavior of counting with null and undefined title in
174 // the same bucket as no argument
175 String title;
176 arguments->getFirstArgumentAsString(title);
177 String identifier = title + '@' + lastCaller.sourceURL() + ':' + String::number(lastCaller.lineNumber());
178
179 HashMap<String, unsigned>::iterator it = m_counts.find(identifier);
180 int count;
181 if (it == m_counts.end())
182 count = 1;
183 else {
184 count = it->value + 1;
185 m_counts.remove(it);
186 }
187
188 m_counts.add(identifier, count);
189
190 String message = title + ": " + String::number(count);
191 addMessageToConsole(std::make_unique<ConsoleMessage>(MessageSource::ConsoleAPI, MessageType::Log, MessageLevel::Debug, message, callStack));
192 }
193
194 static bool isGroupMessage(MessageType type)
195 {
196 return type == MessageType::StartGroup
197 || type == MessageType::StartGroupCollapsed
198 || type == MessageType::EndGroup;
199 }
200
201 void InspectorConsoleAgent::addConsoleMessage(std::unique_ptr<ConsoleMessage> consoleMessage)
202 {
203 ASSERT(m_injectedScriptManager->inspectorEnvironment().developerExtrasEnabled());
204 ASSERT_ARG(consoleMessage, consoleMessage);
205
206 if (m_previousMessage && !isGroupMessage(m_previousMessage->type()) && m_previousMessage->isEqual(consoleMessage.get())) {
207 m_previousMessage->incrementCount();
208 if (m_frontendDispatcher && m_enabled)
209 m_previousMessage->updateRepeatCountInConsole(m_frontendDispatcher.get());
210 } else {
211 m_previousMessage = consoleMessage.get();
212 m_consoleMessages.append(WTF::move(consoleMessage));
213 if (m_frontendDispatcher && m_enabled)
214 m_previousMessage->addToFrontend(m_frontendDispatcher.get(), m_injectedScriptManager, true);
215 }
216
217 if (!m_frontendDispatcher && m_consoleMessages.size() >= maximumConsoleMessages) {
218 m_expiredConsoleMessageCount += expireConsoleMessagesStep;
219 m_consoleMessages.remove(0, expireConsoleMessagesStep);
220 }
221 }
222
223 } // namespace Inspector