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::ExecState* exec, Ref<ScriptArguments>&& arguments)
64{
65 if (JSGlobalObjectConsoleClient::logToSystemConsole())
66 ConsoleClient::printConsoleMessageWithArguments(MessageSource::ConsoleAPI, type, level, exec, arguments.copyRef());
67
68 String message;
69 arguments->getFirstArgumentAsString(message);
70 m_consoleAgent->addMessageToConsole(std::make_unique<ConsoleMessage>(MessageSource::ConsoleAPI, type, level, message, WTFMove(arguments), exec));
71}
72
73void JSGlobalObjectConsoleClient::count(ExecState* exec, Ref<ScriptArguments>&& arguments)
74{
75 m_consoleAgent->count(exec, WTFMove(arguments));
76}
77
78void JSGlobalObjectConsoleClient::profile(JSC::ExecState*, const String& title)
79{
80 if (!m_consoleAgent->enabled())
81 return;
82
83 // Allow duplicate unnamed profiles. Disallow duplicate named profiles.
84 if (!title.isEmpty()) {
85 for (auto& existingTitle : m_profiles) {
86 if (existingTitle == title) {
87 // FIXME: Send an enum to the frontend for localization?
88 String warning = title.isEmpty() ? "Unnamed Profile already exists"_s : makeString("Profile \"", title, "\" already exists");
89 m_consoleAgent->addMessageToConsole(std::make_unique<ConsoleMessage>(MessageSource::ConsoleAPI, MessageType::Profile, MessageLevel::Warning, warning));
90 return;
91 }
92 }
93 }
94
95 m_profiles.append(title);
96 startConsoleProfile();
97}
98
99void JSGlobalObjectConsoleClient::profileEnd(JSC::ExecState*, const String& title)
100{
101 if (!m_consoleAgent->enabled())
102 return;
103
104 // Stop profiles in reverse order. If the title is empty, then stop the last profile.
105 // Otherwise, match the title of the profile to stop.
106 for (ptrdiff_t i = m_profiles.size() - 1; i >= 0; --i) {
107 if (title.isEmpty() || m_profiles[i] == title) {
108 m_profiles.remove(i);
109 if (m_profiles.isEmpty())
110 stopConsoleProfile();
111 return;
112 }
113 }
114
115 // FIXME: Send an enum to the frontend for localization?
116 String warning = title.isEmpty() ? "No profiles exist"_s : makeString("Profile \"", title, "\" does not exist");
117 m_consoleAgent->addMessageToConsole(std::make_unique<ConsoleMessage>(MessageSource::ConsoleAPI, MessageType::ProfileEnd, MessageLevel::Warning, warning));
118}
119
120void JSGlobalObjectConsoleClient::startConsoleProfile()
121{
122 ErrorString unused;
123
124 if (m_debuggerAgent) {
125 m_profileRestoreBreakpointActiveValue = m_debuggerAgent->breakpointsActive();
126 m_debuggerAgent->setBreakpointsActive(unused, false);
127 }
128
129 if (m_scriptProfilerAgent) {
130 const bool includeSamples = true;
131 m_scriptProfilerAgent->startTracking(unused, &includeSamples);
132 }
133}
134
135void JSGlobalObjectConsoleClient::stopConsoleProfile()
136{
137 ErrorString unused;
138
139 if (m_scriptProfilerAgent)
140 m_scriptProfilerAgent->stopTracking(unused);
141
142 if (m_debuggerAgent)
143 m_debuggerAgent->setBreakpointsActive(unused, m_profileRestoreBreakpointActiveValue);
144}
145
146void JSGlobalObjectConsoleClient::takeHeapSnapshot(JSC::ExecState*, const String& title)
147{
148 m_consoleAgent->takeHeapSnapshot(title);
149}
150
151void JSGlobalObjectConsoleClient::time(ExecState*, const String& title)
152{
153 m_consoleAgent->startTiming(title);
154}
155
156void JSGlobalObjectConsoleClient::timeEnd(ExecState* exec, const String& title)
157{
158 m_consoleAgent->stopTiming(title, createScriptCallStackForConsole(exec, 1));
159}
160
161void JSGlobalObjectConsoleClient::timeStamp(ExecState*, Ref<ScriptArguments>&&)
162{
163 // FIXME: JSContext inspection needs a timeline.
164 warnUnimplemented("console.timeStamp"_s);
165}
166
167void JSGlobalObjectConsoleClient::record(ExecState*, Ref<ScriptArguments>&&) { }
168void JSGlobalObjectConsoleClient::recordEnd(ExecState*, Ref<ScriptArguments>&&) { }
169
170void JSGlobalObjectConsoleClient::screenshot(ExecState*, Ref<ScriptArguments>&&)
171{
172 warnUnimplemented("console.screenshot"_s);
173}
174
175void JSGlobalObjectConsoleClient::warnUnimplemented(const String& method)
176{
177 String message = method + " is currently ignored in JavaScript context inspection.";
178 m_consoleAgent->addMessageToConsole(std::make_unique<ConsoleMessage>(MessageSource::ConsoleAPI, MessageType::Log, MessageLevel::Warning, message));
179}
180
181} // namespace Inspector
182