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 | |
37 | using namespace JSC; |
38 | |
39 | namespace Inspector { |
40 | |
41 | #if !LOG_DISABLED |
42 | static bool sLogToSystemConsole = true; |
43 | #else |
44 | static bool sLogToSystemConsole = false; |
45 | #endif |
46 | |
47 | bool JSGlobalObjectConsoleClient::logToSystemConsole() |
48 | { |
49 | return sLogToSystemConsole; |
50 | } |
51 | |
52 | void JSGlobalObjectConsoleClient::setLogToSystemConsole(bool shouldLog) |
53 | { |
54 | sLogToSystemConsole = shouldLog; |
55 | } |
56 | |
57 | JSGlobalObjectConsoleClient::JSGlobalObjectConsoleClient(InspectorConsoleAgent* consoleAgent) |
58 | : ConsoleClient() |
59 | , m_consoleAgent(consoleAgent) |
60 | { |
61 | } |
62 | |
63 | void 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 | |
73 | void JSGlobalObjectConsoleClient::count(ExecState* exec, const String& label) |
74 | { |
75 | m_consoleAgent->count(exec, label); |
76 | } |
77 | |
78 | void JSGlobalObjectConsoleClient::countReset(ExecState* exec, const String& label) |
79 | { |
80 | m_consoleAgent->countReset(exec, label); |
81 | } |
82 | |
83 | void JSGlobalObjectConsoleClient::profile(JSC::ExecState*, const String& title) |
84 | { |
85 | if (!m_consoleAgent->enabled()) |
86 | return; |
87 | |
88 | // Allow duplicate unnamed profiles. Disallow duplicate named profiles. |
89 | if (!title.isEmpty()) { |
90 | for (auto& existingTitle : m_profiles) { |
91 | if (existingTitle == title) { |
92 | // FIXME: Send an enum to the frontend for localization? |
93 | String warning = title.isEmpty() ? "Unnamed Profile already exists"_s : makeString("Profile \"" , title, "\" already exists" ); |
94 | m_consoleAgent->addMessageToConsole(std::make_unique<ConsoleMessage>(MessageSource::ConsoleAPI, MessageType::Profile, MessageLevel::Warning, warning)); |
95 | return; |
96 | } |
97 | } |
98 | } |
99 | |
100 | m_profiles.append(title); |
101 | startConsoleProfile(); |
102 | } |
103 | |
104 | void JSGlobalObjectConsoleClient::profileEnd(JSC::ExecState*, const String& title) |
105 | { |
106 | if (!m_consoleAgent->enabled()) |
107 | return; |
108 | |
109 | // Stop profiles in reverse order. If the title is empty, then stop the last profile. |
110 | // Otherwise, match the title of the profile to stop. |
111 | for (ptrdiff_t i = m_profiles.size() - 1; i >= 0; --i) { |
112 | if (title.isEmpty() || m_profiles[i] == title) { |
113 | m_profiles.remove(i); |
114 | if (m_profiles.isEmpty()) |
115 | stopConsoleProfile(); |
116 | return; |
117 | } |
118 | } |
119 | |
120 | // FIXME: Send an enum to the frontend for localization? |
121 | String warning = title.isEmpty() ? "No profiles exist"_s : makeString("Profile \"" , title, "\" does not exist" ); |
122 | m_consoleAgent->addMessageToConsole(std::make_unique<ConsoleMessage>(MessageSource::ConsoleAPI, MessageType::ProfileEnd, MessageLevel::Warning, warning)); |
123 | } |
124 | |
125 | void JSGlobalObjectConsoleClient::startConsoleProfile() |
126 | { |
127 | ErrorString unused; |
128 | |
129 | if (m_debuggerAgent) { |
130 | m_profileRestoreBreakpointActiveValue = m_debuggerAgent->breakpointsActive(); |
131 | m_debuggerAgent->setBreakpointsActive(unused, false); |
132 | } |
133 | |
134 | if (m_scriptProfilerAgent) { |
135 | const bool includeSamples = true; |
136 | m_scriptProfilerAgent->startTracking(unused, &includeSamples); |
137 | } |
138 | } |
139 | |
140 | void JSGlobalObjectConsoleClient::stopConsoleProfile() |
141 | { |
142 | ErrorString unused; |
143 | |
144 | if (m_scriptProfilerAgent) |
145 | m_scriptProfilerAgent->stopTracking(unused); |
146 | |
147 | if (m_debuggerAgent) |
148 | m_debuggerAgent->setBreakpointsActive(unused, m_profileRestoreBreakpointActiveValue); |
149 | } |
150 | |
151 | void JSGlobalObjectConsoleClient::takeHeapSnapshot(JSC::ExecState*, const String& title) |
152 | { |
153 | m_consoleAgent->takeHeapSnapshot(title); |
154 | } |
155 | |
156 | void JSGlobalObjectConsoleClient::time(ExecState* exec, const String& label) |
157 | { |
158 | m_consoleAgent->startTiming(exec, label); |
159 | } |
160 | |
161 | void JSGlobalObjectConsoleClient::timeLog(ExecState* exec, const String& label, Ref<ScriptArguments>&& arguments) |
162 | { |
163 | m_consoleAgent->logTiming(exec, label, WTFMove(arguments)); |
164 | } |
165 | |
166 | void JSGlobalObjectConsoleClient::timeEnd(ExecState* exec, const String& label) |
167 | { |
168 | m_consoleAgent->stopTiming(exec, label); |
169 | } |
170 | |
171 | void JSGlobalObjectConsoleClient::timeStamp(ExecState*, Ref<ScriptArguments>&&) |
172 | { |
173 | // FIXME: JSContext inspection needs a timeline. |
174 | warnUnimplemented("console.timeStamp"_s ); |
175 | } |
176 | |
177 | void JSGlobalObjectConsoleClient::record(ExecState*, Ref<ScriptArguments>&&) { } |
178 | void JSGlobalObjectConsoleClient::recordEnd(ExecState*, Ref<ScriptArguments>&&) { } |
179 | |
180 | void JSGlobalObjectConsoleClient::screenshot(ExecState*, Ref<ScriptArguments>&&) |
181 | { |
182 | warnUnimplemented("console.screenshot"_s ); |
183 | } |
184 | |
185 | void JSGlobalObjectConsoleClient::warnUnimplemented(const String& method) |
186 | { |
187 | String message = method + " is currently ignored in JavaScript context inspection." ; |
188 | m_consoleAgent->addMessageToConsole(std::make_unique<ConsoleMessage>(MessageSource::ConsoleAPI, MessageType::Log, MessageLevel::Warning, message)); |
189 | } |
190 | |
191 | } // namespace Inspector |
192 | |