| 1 | /* |
| 2 | * Copyright (C) 2007, 2008, 2014, 2015 Apple Inc. All rights reserved. |
| 3 | * Copyright (C) 2008 Matt Lilek <webkit@mattlilek.com> |
| 4 | * Copyright (C) 2009, 2010 Google Inc. All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * 3. Neither the name of Apple Inc. ("Apple") nor the names of |
| 16 | * its contributors may be used to endorse or promote products derived |
| 17 | * from this software without specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
| 20 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 21 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 22 | * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
| 23 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 25 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 26 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
| 30 | |
| 31 | #include "config.h" |
| 32 | #include "ConsoleMessage.h" |
| 33 | |
| 34 | #include "IdentifiersFactory.h" |
| 35 | #include "InjectedScript.h" |
| 36 | #include "InjectedScriptManager.h" |
| 37 | #include "InspectorFrontendDispatchers.h" |
| 38 | #include "ScriptArguments.h" |
| 39 | #include "ScriptCallFrame.h" |
| 40 | #include "ScriptCallStack.h" |
| 41 | #include "ScriptCallStackFactory.h" |
| 42 | |
| 43 | namespace Inspector { |
| 44 | |
| 45 | ConsoleMessage::ConsoleMessage(MessageSource source, MessageType type, MessageLevel level, const String& message, unsigned long requestIdentifier) |
| 46 | : m_source(source) |
| 47 | , m_type(type) |
| 48 | , m_level(level) |
| 49 | , m_message(message) |
| 50 | , m_url() |
| 51 | , m_requestId(IdentifiersFactory::requestId(requestIdentifier)) |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | ConsoleMessage::ConsoleMessage(MessageSource source, MessageType type, MessageLevel level, const String& message, const String& url, unsigned line, unsigned column, JSC::ExecState* state, unsigned long requestIdentifier) |
| 56 | : m_source(source) |
| 57 | , m_type(type) |
| 58 | , m_level(level) |
| 59 | , m_message(message) |
| 60 | , m_url(url) |
| 61 | , m_line(line) |
| 62 | , m_column(column) |
| 63 | , m_requestId(IdentifiersFactory::requestId(requestIdentifier)) |
| 64 | { |
| 65 | autogenerateMetadata(state); |
| 66 | } |
| 67 | |
| 68 | ConsoleMessage::ConsoleMessage(MessageSource source, MessageType type, MessageLevel level, const String& message, Ref<ScriptCallStack>&& callStack, unsigned long requestIdentifier) |
| 69 | : m_source(source) |
| 70 | , m_type(type) |
| 71 | , m_level(level) |
| 72 | , m_message(message) |
| 73 | , m_callStack(WTFMove(callStack)) |
| 74 | , m_url() |
| 75 | , m_requestId(IdentifiersFactory::requestId(requestIdentifier)) |
| 76 | { |
| 77 | const ScriptCallFrame* frame = m_callStack ? m_callStack->firstNonNativeCallFrame() : nullptr; |
| 78 | if (frame) { |
| 79 | m_url = frame->sourceURL(); |
| 80 | m_line = frame->lineNumber(); |
| 81 | m_column = frame->columnNumber(); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | ConsoleMessage::ConsoleMessage(MessageSource source, MessageType type, MessageLevel level, const String& message, Ref<ScriptArguments>&& arguments, JSC::ExecState* state, unsigned long requestIdentifier) |
| 86 | : m_source(source) |
| 87 | , m_type(type) |
| 88 | , m_level(level) |
| 89 | , m_message(message) |
| 90 | , m_arguments(WTFMove(arguments)) |
| 91 | , m_url() |
| 92 | , m_requestId(IdentifiersFactory::requestId(requestIdentifier)) |
| 93 | { |
| 94 | autogenerateMetadata(state); |
| 95 | } |
| 96 | |
| 97 | ConsoleMessage::ConsoleMessage(MessageSource source, MessageType type, MessageLevel level, Vector<JSONLogValue>&& messages, JSC::ExecState* state, unsigned long requestIdentifier) |
| 98 | : m_source(source) |
| 99 | , m_type(type) |
| 100 | , m_level(level) |
| 101 | , m_url() |
| 102 | , m_scriptState(state) |
| 103 | , m_requestId(IdentifiersFactory::requestId(requestIdentifier)) |
| 104 | { |
| 105 | if (!messages.size()) |
| 106 | return; |
| 107 | |
| 108 | m_jsonLogValues.reserveInitialCapacity(messages.size()); |
| 109 | |
| 110 | StringBuilder builder; |
| 111 | for (auto& message : messages) { |
| 112 | switch (message.type) { |
| 113 | case JSONLogValue::Type::String: |
| 114 | builder.append(message.value); |
| 115 | break; |
| 116 | case JSONLogValue::Type::JSON: |
| 117 | if (builder.length()) { |
| 118 | m_jsonLogValues.append({ JSONLogValue::Type::String, JSON::Value::create(builder.toString())->toJSONString() }); |
| 119 | builder.resize(0); |
| 120 | } |
| 121 | |
| 122 | m_jsonLogValues.append(message); |
| 123 | break; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | if (builder.length()) |
| 128 | m_jsonLogValues.append({ JSONLogValue::Type::String, JSON::Value::create(builder.toString())->toJSONString() }); |
| 129 | |
| 130 | if (m_jsonLogValues.size()) |
| 131 | m_message = m_jsonLogValues[0].value; |
| 132 | } |
| 133 | |
| 134 | ConsoleMessage::~ConsoleMessage() |
| 135 | { |
| 136 | } |
| 137 | |
| 138 | void ConsoleMessage::autogenerateMetadata(JSC::ExecState* state) |
| 139 | { |
| 140 | if (!state) |
| 141 | return; |
| 142 | |
| 143 | if (m_type == MessageType::EndGroup) |
| 144 | return; |
| 145 | |
| 146 | // FIXME: Should this really be using "for console" in the generic ConsoleMessage autogeneration? This can skip the first frame. |
| 147 | m_callStack = createScriptCallStackForConsole(state); |
| 148 | |
| 149 | if (const ScriptCallFrame* frame = m_callStack->firstNonNativeCallFrame()) { |
| 150 | m_url = frame->sourceURL(); |
| 151 | m_line = frame->lineNumber(); |
| 152 | m_column = frame->columnNumber(); |
| 153 | return; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | static Protocol::Console::ChannelSource messageSourceValue(MessageSource source) |
| 158 | { |
| 159 | switch (source) { |
| 160 | case MessageSource::XML: return Protocol::Console::ChannelSource::XML; |
| 161 | case MessageSource::JS: return Protocol::Console::ChannelSource::JavaScript; |
| 162 | case MessageSource::Network: return Protocol::Console::ChannelSource::Network; |
| 163 | case MessageSource::ConsoleAPI: return Protocol::Console::ChannelSource::ConsoleAPI; |
| 164 | case MessageSource::Storage: return Protocol::Console::ChannelSource::Storage; |
| 165 | case MessageSource::AppCache: return Protocol::Console::ChannelSource::Appcache; |
| 166 | case MessageSource::Rendering: return Protocol::Console::ChannelSource::Rendering; |
| 167 | case MessageSource::CSS: return Protocol::Console::ChannelSource::CSS; |
| 168 | case MessageSource::Security: return Protocol::Console::ChannelSource::Security; |
| 169 | case MessageSource::ContentBlocker: return Protocol::Console::ChannelSource::ContentBlocker; |
| 170 | case MessageSource::Other: return Protocol::Console::ChannelSource::Other; |
| 171 | case MessageSource::Media: return Protocol::Console::ChannelSource::Media; |
| 172 | case MessageSource::WebRTC: return Protocol::Console::ChannelSource::WebRTC; |
| 173 | case MessageSource::MediaSource: return Protocol::Console::ChannelSource::MediaSource; |
| 174 | } |
| 175 | return Protocol::Console::ChannelSource::Other; |
| 176 | } |
| 177 | |
| 178 | static Protocol::Console::ConsoleMessage::Type messageTypeValue(MessageType type) |
| 179 | { |
| 180 | switch (type) { |
| 181 | case MessageType::Log: return Protocol::Console::ConsoleMessage::Type::Log; |
| 182 | case MessageType::Clear: return Protocol::Console::ConsoleMessage::Type::Clear; |
| 183 | case MessageType::Dir: return Protocol::Console::ConsoleMessage::Type::Dir; |
| 184 | case MessageType::DirXML: return Protocol::Console::ConsoleMessage::Type::DirXML; |
| 185 | case MessageType::Table: return Protocol::Console::ConsoleMessage::Type::Table; |
| 186 | case MessageType::Trace: return Protocol::Console::ConsoleMessage::Type::Trace; |
| 187 | case MessageType::StartGroup: return Protocol::Console::ConsoleMessage::Type::StartGroup; |
| 188 | case MessageType::StartGroupCollapsed: return Protocol::Console::ConsoleMessage::Type::StartGroupCollapsed; |
| 189 | case MessageType::EndGroup: return Protocol::Console::ConsoleMessage::Type::EndGroup; |
| 190 | case MessageType::Assert: return Protocol::Console::ConsoleMessage::Type::Assert; |
| 191 | case MessageType::Timing: return Protocol::Console::ConsoleMessage::Type::Timing; |
| 192 | case MessageType::Profile: return Protocol::Console::ConsoleMessage::Type::Profile; |
| 193 | case MessageType::ProfileEnd: return Protocol::Console::ConsoleMessage::Type::ProfileEnd; |
| 194 | case MessageType::Image: return Protocol::Console::ConsoleMessage::Type::Image; |
| 195 | } |
| 196 | return Protocol::Console::ConsoleMessage::Type::Log; |
| 197 | } |
| 198 | |
| 199 | static Protocol::Console::ConsoleMessage::Level messageLevelValue(MessageLevel level) |
| 200 | { |
| 201 | switch (level) { |
| 202 | case MessageLevel::Log: return Protocol::Console::ConsoleMessage::Level::Log; |
| 203 | case MessageLevel::Info: return Protocol::Console::ConsoleMessage::Level::Info; |
| 204 | case MessageLevel::Warning: return Protocol::Console::ConsoleMessage::Level::Warning; |
| 205 | case MessageLevel::Error: return Protocol::Console::ConsoleMessage::Level::Error; |
| 206 | case MessageLevel::Debug: return Protocol::Console::ConsoleMessage::Level::Debug; |
| 207 | } |
| 208 | return Protocol::Console::ConsoleMessage::Level::Log; |
| 209 | } |
| 210 | |
| 211 | void ConsoleMessage::addToFrontend(ConsoleFrontendDispatcher& consoleFrontendDispatcher, InjectedScriptManager& injectedScriptManager, bool generatePreview) |
| 212 | { |
| 213 | auto messageObject = Protocol::Console::ConsoleMessage::create() |
| 214 | .setSource(messageSourceValue(m_source)) |
| 215 | .setLevel(messageLevelValue(m_level)) |
| 216 | .setText(m_message) |
| 217 | .release(); |
| 218 | |
| 219 | // FIXME: only send out type for ConsoleAPI source messages. |
| 220 | messageObject->setType(messageTypeValue(m_type)); |
| 221 | messageObject->setLine(static_cast<int>(m_line)); |
| 222 | messageObject->setColumn(static_cast<int>(m_column)); |
| 223 | messageObject->setUrl(m_url); |
| 224 | messageObject->setRepeatCount(static_cast<int>(m_repeatCount)); |
| 225 | |
| 226 | if (m_source == MessageSource::Network && !m_requestId.isEmpty()) |
| 227 | messageObject->setNetworkRequestId(m_requestId); |
| 228 | |
| 229 | if ((m_arguments && m_arguments->argumentCount()) || m_jsonLogValues.size()) { |
| 230 | InjectedScript injectedScript = injectedScriptManager.injectedScriptFor(scriptState()); |
| 231 | if (!injectedScript.hasNoValue()) { |
| 232 | auto argumentsObject = JSON::ArrayOf<Protocol::Runtime::RemoteObject>::create(); |
| 233 | if (m_arguments && m_arguments->argumentCount()) { |
| 234 | if (m_type == MessageType::Table && generatePreview && m_arguments->argumentCount()) { |
| 235 | auto table = m_arguments->argumentAt(0); |
| 236 | auto columns = m_arguments->argumentCount() > 1 ? m_arguments->argumentAt(1) : JSC::JSValue(); |
| 237 | auto inspectorValue = injectedScript.wrapTable(table, columns); |
| 238 | if (!inspectorValue) { |
| 239 | ASSERT_NOT_REACHED(); |
| 240 | return; |
| 241 | } |
| 242 | argumentsObject->addItem(WTFMove(inspectorValue)); |
| 243 | if (m_arguments->argumentCount() > 1) |
| 244 | argumentsObject->addItem(injectedScript.wrapObject(columns, "console"_s , true)); |
| 245 | } else { |
| 246 | for (unsigned i = 0; i < m_arguments->argumentCount(); ++i) { |
| 247 | auto inspectorValue = injectedScript.wrapObject(m_arguments->argumentAt(i), "console"_s , generatePreview); |
| 248 | if (!inspectorValue) { |
| 249 | ASSERT_NOT_REACHED(); |
| 250 | return; |
| 251 | } |
| 252 | argumentsObject->addItem(WTFMove(inspectorValue)); |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | if (m_jsonLogValues.size()) { |
| 258 | for (auto& message : m_jsonLogValues) { |
| 259 | if (message.value.isEmpty()) |
| 260 | continue; |
| 261 | auto inspectorValue = injectedScript.wrapJSONString(message.value, "console"_s , generatePreview); |
| 262 | if (!inspectorValue) |
| 263 | continue; |
| 264 | |
| 265 | argumentsObject->addItem(WTFMove(inspectorValue)); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | if (argumentsObject->length()) |
| 270 | messageObject->setParameters(WTFMove(argumentsObject)); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | if (m_callStack) |
| 275 | messageObject->setStackTrace(m_callStack->buildInspectorArray()); |
| 276 | |
| 277 | consoleFrontendDispatcher.messageAdded(WTFMove(messageObject)); |
| 278 | } |
| 279 | |
| 280 | void ConsoleMessage::updateRepeatCountInConsole(ConsoleFrontendDispatcher& consoleFrontendDispatcher) |
| 281 | { |
| 282 | consoleFrontendDispatcher.messageRepeatCountUpdated(m_repeatCount); |
| 283 | } |
| 284 | |
| 285 | bool ConsoleMessage::isEqual(ConsoleMessage* msg) const |
| 286 | { |
| 287 | if (m_arguments) { |
| 288 | if (!msg->m_arguments || !m_arguments->isEqual(*msg->m_arguments)) |
| 289 | return false; |
| 290 | |
| 291 | // Never treat objects as equal - their properties might change over time. |
| 292 | for (size_t i = 0; i < m_arguments->argumentCount(); ++i) { |
| 293 | if (m_arguments->argumentAt(i).isObject()) |
| 294 | return false; |
| 295 | } |
| 296 | } else if (msg->m_arguments) |
| 297 | return false; |
| 298 | |
| 299 | if (m_callStack) { |
| 300 | if (!m_callStack->isEqual(msg->m_callStack.get())) |
| 301 | return false; |
| 302 | } else if (msg->m_callStack) |
| 303 | return false; |
| 304 | |
| 305 | if (m_jsonLogValues.size() || msg->m_jsonLogValues.size()) |
| 306 | return false; |
| 307 | |
| 308 | return msg->m_source == m_source |
| 309 | && msg->m_type == m_type |
| 310 | && msg->m_level == m_level |
| 311 | && msg->m_message == m_message |
| 312 | && msg->m_line == m_line |
| 313 | && msg->m_column == m_column |
| 314 | && msg->m_url == m_url |
| 315 | && msg->m_requestId == m_requestId; |
| 316 | } |
| 317 | |
| 318 | void ConsoleMessage::clear() |
| 319 | { |
| 320 | if (!m_message) |
| 321 | m_message = "<message collected>"_s ; |
| 322 | |
| 323 | if (m_arguments) |
| 324 | m_arguments = nullptr; |
| 325 | } |
| 326 | |
| 327 | JSC::ExecState* ConsoleMessage::scriptState() const |
| 328 | { |
| 329 | if (m_arguments) |
| 330 | return m_arguments->globalState(); |
| 331 | |
| 332 | if (m_scriptState) |
| 333 | return m_scriptState; |
| 334 | |
| 335 | return nullptr; |
| 336 | } |
| 337 | |
| 338 | unsigned ConsoleMessage::argumentCount() const |
| 339 | { |
| 340 | if (m_arguments) |
| 341 | return m_arguments->argumentCount(); |
| 342 | |
| 343 | return 0; |
| 344 | } |
| 345 | |
| 346 | } // namespace Inspector |
| 347 | |