2 * Copyright (C) 2014 Apple Inc. All rights reserved.
3 * Copyright (C) 2011 Google Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 #include "InspectorConsoleAgent.h"
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>
42 static const unsigned maximumConsoleMessages
= 1000;
43 static const int expireConsoleMessagesStep
= 100;
45 InspectorConsoleAgent::InspectorConsoleAgent(InjectedScriptManager
* injectedScriptManager
)
46 : InspectorAgentBase(ASCIILiteral("Console"))
47 , m_injectedScriptManager(injectedScriptManager
)
48 , m_previousMessage(nullptr)
49 , m_expiredConsoleMessageCount(0)
54 InspectorConsoleAgent::~InspectorConsoleAgent()
58 void InspectorConsoleAgent::didCreateFrontendAndBackend(FrontendChannel
* frontendChannel
, BackendDispatcher
* backendDispatcher
)
60 m_frontendDispatcher
= std::make_unique
<ConsoleFrontendDispatcher
>(frontendChannel
);
61 m_backendDispatcher
= ConsoleBackendDispatcher::create(backendDispatcher
, this);
64 void InspectorConsoleAgent::willDestroyFrontendAndBackend(DisconnectReason
)
66 m_frontendDispatcher
= nullptr;
67 m_backendDispatcher
= nullptr;
73 void InspectorConsoleAgent::enable(ErrorString
&)
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);
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);
90 void InspectorConsoleAgent::disable(ErrorString
&)
98 void InspectorConsoleAgent::clearMessages(ErrorString
&)
100 m_consoleMessages
.clear();
101 m_expiredConsoleMessageCount
= 0;
102 m_previousMessage
= nullptr;
104 m_injectedScriptManager
->releaseObjectGroup(ASCIILiteral("console"));
106 if (m_frontendDispatcher
&& m_enabled
)
107 m_frontendDispatcher
->messagesCleared();
110 void InspectorConsoleAgent::reset()
113 clearMessages(unused
);
119 void InspectorConsoleAgent::addMessageToConsole(std::unique_ptr
<ConsoleMessage
> message
)
121 if (!m_injectedScriptManager
->inspectorEnvironment().developerExtrasEnabled())
124 if (message
->type() == MessageType::Clear
) {
126 clearMessages(unused
);
129 addConsoleMessage(WTF::move(message
));
132 Vector
<unsigned> InspectorConsoleAgent::consoleMessageArgumentCounts() const
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();
140 void InspectorConsoleAgent::startTiming(const String
& title
)
142 // Follow Firebug's behavior of requiring a title that is not null or
143 // undefined for timing functions
147 m_times
.add(title
, monotonicallyIncreasingTime());
150 void InspectorConsoleAgent::stopTiming(const String
& title
, PassRefPtr
<ScriptCallStack
> callStack
)
152 // Follow Firebug's behavior of requiring a title that is not null or
153 // undefined for timing functions
157 HashMap
<String
, double>::iterator it
= m_times
.find(title
);
158 if (it
== m_times
.end())
161 double startTime
= it
->value
;
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
));
169 void InspectorConsoleAgent::count(JSC::ExecState
* state
, PassRefPtr
<ScriptArguments
> arguments
)
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
176 arguments
->getFirstArgumentAsString(title
);
177 String identifier
= title
+ '@' + lastCaller
.sourceURL() + ':' + String::number(lastCaller
.lineNumber());
179 HashMap
<String
, unsigned>::iterator it
= m_counts
.find(identifier
);
181 if (it
== m_counts
.end())
184 count
= it
->value
+ 1;
188 m_counts
.add(identifier
, count
);
190 String message
= title
+ ": " + String::number(count
);
191 addMessageToConsole(std::make_unique
<ConsoleMessage
>(MessageSource::ConsoleAPI
, MessageType::Log
, MessageLevel::Debug
, message
, callStack
));
194 static bool isGroupMessage(MessageType type
)
196 return type
== MessageType::StartGroup
197 || type
== MessageType::StartGroupCollapsed
198 || type
== MessageType::EndGroup
;
201 void InspectorConsoleAgent::addConsoleMessage(std::unique_ptr
<ConsoleMessage
> consoleMessage
)
203 ASSERT(m_injectedScriptManager
->inspectorEnvironment().developerExtrasEnabled());
204 ASSERT_ARG(consoleMessage
, consoleMessage
);
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());
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);
217 if (!m_frontendDispatcher
&& m_consoleMessages
.size() >= maximumConsoleMessages
) {
218 m_expiredConsoleMessageCount
+= expireConsoleMessagesStep
;
219 m_consoleMessages
.remove(0, expireConsoleMessagesStep
);
223 } // namespace Inspector