1/*
2 * Copyright (C) 2014 Apple 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
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "JSGlobalObjectConsoleClient.h"
28
29#include "ConsoleMessage.h"
30#include "InspectorConsoleAgent.h"
31#include "InspectorDebuggerAgent.h"
32#include "InspectorScriptProfilerAgent.h"
33#include "ScriptArguments.h"
34#include "ScriptCallStack.h"
35#include "ScriptCallStackFactory.h"
36
37using namespace JSC;
38
39namespace Inspector {
40
41#if !LOG_DISABLED
42static bool sLogToSystemConsole = true;
43#else
44static bool sLogToSystemConsole = false;
45#endif
46
47bool JSGlobalObjectConsoleClient::logToSystemConsole()
48{
49 return sLogToSystemConsole;
50}
51
52void JSGlobalObjectConsoleClient::setLogToSystemConsole(bool shouldLog)
53{
54 sLogToSystemConsole = shouldLog;
55}
56
57JSGlobalObjectConsoleClient::JSGlobalObjectConsoleClient(InspectorConsoleAgent* consoleAgent)
58 : ConsoleClient()
59 , m_consoleAgent(consoleAgent)
60{
61}
62
63void JSGlobalObjectConsoleClient::messageWithTypeAndLevel(MessageType type, MessageLevel level, JSC::JSGlobalObject* globalObject, Ref<ScriptArguments>&& arguments)
64{
65 if (JSGlobalObjectConsoleClient::logToSystemConsole())
66 ConsoleClient::printConsoleMessageWithArguments(MessageSource::ConsoleAPI, type, level, globalObject, arguments.copyRef());
67
68 String message;
69 arguments->getFirstArgumentAsString(message);
70 m_consoleAgent->addMessageToConsole(makeUnique<ConsoleMessage>(MessageSource::ConsoleAPI, type, level, message, WTFMove(arguments), globalObject));
71
72 if (type == MessageType::Assert) {
73 if (m_debuggerAgent)
74 m_debuggerAgent->handleConsoleAssert(message);
75 }
76}
77
78void JSGlobalObjectConsoleClient::count(JSGlobalObject* globalObject, const String& label)
79{
80 m_consoleAgent->count(globalObject, label);
81}
82
83void JSGlobalObjectConsoleClient::countReset(JSGlobalObject* globalObject, const String& label)
84{
85 m_consoleAgent->countReset(globalObject, label);
86}
87
88void JSGlobalObjectConsoleClient::profile(JSC::JSGlobalObject*, const String& title)
89{
90 if (!m_consoleAgent->enabled())
91 return;
92
93 // Allow duplicate unnamed profiles. Disallow duplicate named profiles.
94 if (!title.isEmpty()) {
95 for (auto& existingTitle : m_profiles) {
96 if (existingTitle == title) {
97 // FIXME: Send an enum to the frontend for localization?
98 String warning = title.isEmpty() ? "Unnamed Profile already exists"_s : makeString("Profile \"", title, "\" already exists");
99 m_consoleAgent->addMessageToConsole(makeUnique<ConsoleMessage>(MessageSource::ConsoleAPI, MessageType::Profile, MessageLevel::Warning, warning));
100 return;
101 }
102 }
103 }
104
105 m_profiles.append(title);
106 startConsoleProfile();
107}
108
109void JSGlobalObjectConsoleClient::profileEnd(JSC::JSGlobalObject*, const String& title)
110{
111 if (!m_consoleAgent->enabled())
112 return;
113
114 // Stop profiles in reverse order. If the title is empty, then stop the last profile.
115 // Otherwise, match the title of the profile to stop.
116 for (ptrdiff_t i = m_profiles.size() - 1; i >= 0; --i) {
117 if (title.isEmpty() || m_profiles[i] == title) {
118 m_profiles.remove(i);
119 if (m_profiles.isEmpty())
120 stopConsoleProfile();
121 return;
122 }
123 }
124
125 // FIXME: Send an enum to the frontend for localization?
126 String warning = title.isEmpty() ? "No profiles exist"_s : makeString("Profile \"", title, "\" does not exist");
127 m_consoleAgent->addMessageToConsole(makeUnique<ConsoleMessage>(MessageSource::ConsoleAPI, MessageType::ProfileEnd, MessageLevel::Warning, warning));
128}
129
130void JSGlobalObjectConsoleClient::startConsoleProfile()
131{
132 ErrorString ignored;
133
134 if (m_debuggerAgent) {
135 m_profileRestoreBreakpointActiveValue = m_debuggerAgent->breakpointsActive();
136 m_debuggerAgent->setBreakpointsActive(ignored, false);
137 }
138
139 if (m_scriptProfilerAgent) {
140 const bool includeSamples = true;
141 m_scriptProfilerAgent->startTracking(ignored, &includeSamples);
142 }
143}
144
145void JSGlobalObjectConsoleClient::stopConsoleProfile()
146{
147 ErrorString ignored;
148
149 if (m_scriptProfilerAgent)
150 m_scriptProfilerAgent->stopTracking(ignored);
151
152 if (m_debuggerAgent)
153 m_debuggerAgent->setBreakpointsActive(ignored, m_profileRestoreBreakpointActiveValue);
154}
155
156void JSGlobalObjectConsoleClient::takeHeapSnapshot(JSC::JSGlobalObject*, const String& title)
157{
158 m_consoleAgent->takeHeapSnapshot(title);
159}
160
161void JSGlobalObjectConsoleClient::time(JSGlobalObject* globalObject, const String& label)
162{
163 m_consoleAgent->startTiming(globalObject, label);
164}
165
166void JSGlobalObjectConsoleClient::timeLog(JSGlobalObject* globalObject, const String& label, Ref<ScriptArguments>&& arguments)
167{
168 m_consoleAgent->logTiming(globalObject, label, WTFMove(arguments));
169}
170
171void JSGlobalObjectConsoleClient::timeEnd(JSGlobalObject* globalObject, const String& label)
172{
173 m_consoleAgent->stopTiming(globalObject, label);
174}
175
176void JSGlobalObjectConsoleClient::timeStamp(JSGlobalObject*, Ref<ScriptArguments>&&)
177{
178 // FIXME: JSContext inspection needs a timeline.
179 warnUnimplemented("console.timeStamp"_s);
180}
181
182void JSGlobalObjectConsoleClient::record(JSGlobalObject*, Ref<ScriptArguments>&&) { }
183void JSGlobalObjectConsoleClient::recordEnd(JSGlobalObject*, Ref<ScriptArguments>&&) { }
184
185void JSGlobalObjectConsoleClient::screenshot(JSGlobalObject*, Ref<ScriptArguments>&&)
186{
187 warnUnimplemented("console.screenshot"_s);
188}
189
190void JSGlobalObjectConsoleClient::warnUnimplemented(const String& method)
191{
192 String message = method + " is currently ignored in JavaScript context inspection.";
193 m_consoleAgent->addMessageToConsole(makeUnique<ConsoleMessage>(MessageSource::ConsoleAPI, MessageType::Log, MessageLevel::Warning, message));
194}
195
196} // namespace Inspector
197