ConsoleMessage.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * Copyright (C) 2007, 2008 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 Computer, 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. #include "config.h"
  31. #if ENABLE(INSPECTOR)
  32. #include "ConsoleMessage.h"
  33. #include "Console.h"
  34. #include "IdentifiersFactory.h"
  35. #include "InjectedScript.h"
  36. #include "InjectedScriptManager.h"
  37. #include "InspectorFrontend.h"
  38. #include "InspectorValues.h"
  39. #include "ScriptArguments.h"
  40. #include "ScriptCallFrame.h"
  41. #include "ScriptCallStack.h"
  42. #include "ScriptCallStackFactory.h"
  43. #include "ScriptValue.h"
  44. #include <wtf/MainThread.h>
  45. namespace WebCore {
  46. ConsoleMessage::ConsoleMessage(bool canGenerateCallStack, MessageSource source, MessageType type, MessageLevel level, const String& message, unsigned long requestIdentifier)
  47. : m_source(source)
  48. , m_type(type)
  49. , m_level(level)
  50. , m_message(message)
  51. , m_url()
  52. , m_line(0)
  53. , m_column(0)
  54. , m_repeatCount(1)
  55. , m_requestId(IdentifiersFactory::requestId(requestIdentifier))
  56. {
  57. autogenerateMetadata(canGenerateCallStack);
  58. }
  59. ConsoleMessage::ConsoleMessage(bool canGenerateCallStack, MessageSource source, MessageType type, MessageLevel level, const String& message, const String& url, unsigned line, unsigned column, ScriptState* state, unsigned long requestIdentifier)
  60. : m_source(source)
  61. , m_type(type)
  62. , m_level(level)
  63. , m_message(message)
  64. , m_url(url)
  65. , m_line(line)
  66. , m_column(column)
  67. , m_repeatCount(1)
  68. , m_requestId(IdentifiersFactory::requestId(requestIdentifier))
  69. {
  70. autogenerateMetadata(canGenerateCallStack, state);
  71. }
  72. ConsoleMessage::ConsoleMessage(bool, MessageSource source, MessageType type, MessageLevel level, const String& message, PassRefPtr<ScriptCallStack> callStack, unsigned long requestIdentifier)
  73. : m_source(source)
  74. , m_type(type)
  75. , m_level(level)
  76. , m_message(message)
  77. , m_arguments(0)
  78. , m_line(0)
  79. , m_column(0)
  80. , m_repeatCount(1)
  81. , m_requestId(IdentifiersFactory::requestId(requestIdentifier))
  82. {
  83. if (callStack && callStack->size()) {
  84. const ScriptCallFrame& frame = callStack->at(0);
  85. m_url = frame.sourceURL();
  86. m_line = frame.lineNumber();
  87. m_column = frame.columnNumber();
  88. }
  89. m_callStack = callStack;
  90. }
  91. ConsoleMessage::ConsoleMessage(bool canGenerateCallStack, MessageSource source, MessageType type, MessageLevel level, const String& message, PassRefPtr<ScriptArguments> arguments, ScriptState* state, unsigned long requestIdentifier)
  92. : m_source(source)
  93. , m_type(type)
  94. , m_level(level)
  95. , m_message(message)
  96. , m_arguments(arguments)
  97. , m_url()
  98. , m_line(0)
  99. , m_column(0)
  100. , m_repeatCount(1)
  101. , m_requestId(IdentifiersFactory::requestId(requestIdentifier))
  102. {
  103. autogenerateMetadata(canGenerateCallStack, state);
  104. }
  105. ConsoleMessage::~ConsoleMessage()
  106. {
  107. }
  108. void ConsoleMessage::autogenerateMetadata(bool canGenerateCallStack, ScriptState* state)
  109. {
  110. if (m_type == EndGroupMessageType)
  111. return;
  112. if (state)
  113. m_callStack = createScriptCallStackForConsole(state);
  114. else if (canGenerateCallStack)
  115. m_callStack = createScriptCallStack(ScriptCallStack::maxCallStackSizeToCapture, true);
  116. else
  117. return;
  118. if (m_callStack && m_callStack->size()) {
  119. const ScriptCallFrame& frame = m_callStack->at(0);
  120. m_url = frame.sourceURL();
  121. m_line = frame.lineNumber();
  122. m_column = frame.columnNumber();
  123. return;
  124. }
  125. m_callStack.clear();
  126. }
  127. // Keep in sync with inspector/front-end/ConsoleView.js
  128. static TypeBuilder::Console::ConsoleMessage::Source::Enum messageSourceValue(MessageSource source)
  129. {
  130. switch (source) {
  131. case XMLMessageSource: return TypeBuilder::Console::ConsoleMessage::Source::Xml;
  132. case JSMessageSource: return TypeBuilder::Console::ConsoleMessage::Source::Javascript;
  133. case NetworkMessageSource: return TypeBuilder::Console::ConsoleMessage::Source::Network;
  134. case ConsoleAPIMessageSource: return TypeBuilder::Console::ConsoleMessage::Source::Console_api;
  135. case StorageMessageSource: return TypeBuilder::Console::ConsoleMessage::Source::Storage;
  136. case AppCacheMessageSource: return TypeBuilder::Console::ConsoleMessage::Source::Appcache;
  137. case RenderingMessageSource: return TypeBuilder::Console::ConsoleMessage::Source::Rendering;
  138. case CSSMessageSource: return TypeBuilder::Console::ConsoleMessage::Source::Css;
  139. case SecurityMessageSource: return TypeBuilder::Console::ConsoleMessage::Source::Security;
  140. case OtherMessageSource: return TypeBuilder::Console::ConsoleMessage::Source::Other;
  141. }
  142. return TypeBuilder::Console::ConsoleMessage::Source::Other;
  143. }
  144. static TypeBuilder::Console::ConsoleMessage::Type::Enum messageTypeValue(MessageType type)
  145. {
  146. switch (type) {
  147. case LogMessageType: return TypeBuilder::Console::ConsoleMessage::Type::Log;
  148. case ClearMessageType: return TypeBuilder::Console::ConsoleMessage::Type::Clear;
  149. case DirMessageType: return TypeBuilder::Console::ConsoleMessage::Type::Dir;
  150. case DirXMLMessageType: return TypeBuilder::Console::ConsoleMessage::Type::Dirxml;
  151. case TableMessageType: return TypeBuilder::Console::ConsoleMessage::Type::Table;
  152. case TraceMessageType: return TypeBuilder::Console::ConsoleMessage::Type::Trace;
  153. case StartGroupMessageType: return TypeBuilder::Console::ConsoleMessage::Type::StartGroup;
  154. case StartGroupCollapsedMessageType: return TypeBuilder::Console::ConsoleMessage::Type::StartGroupCollapsed;
  155. case EndGroupMessageType: return TypeBuilder::Console::ConsoleMessage::Type::EndGroup;
  156. case AssertMessageType: return TypeBuilder::Console::ConsoleMessage::Type::Assert;
  157. case TimingMessageType: return TypeBuilder::Console::ConsoleMessage::Type::Timing;
  158. case ProfileMessageType: return TypeBuilder::Console::ConsoleMessage::Type::Profile;
  159. case ProfileEndMessageType: return TypeBuilder::Console::ConsoleMessage::Type::ProfileEnd;
  160. }
  161. return TypeBuilder::Console::ConsoleMessage::Type::Log;
  162. }
  163. static TypeBuilder::Console::ConsoleMessage::Level::Enum messageLevelValue(MessageLevel level)
  164. {
  165. switch (level) {
  166. case DebugMessageLevel: return TypeBuilder::Console::ConsoleMessage::Level::Debug;
  167. case LogMessageLevel: return TypeBuilder::Console::ConsoleMessage::Level::Log;
  168. case WarningMessageLevel: return TypeBuilder::Console::ConsoleMessage::Level::Warning;
  169. case ErrorMessageLevel: return TypeBuilder::Console::ConsoleMessage::Level::Error;
  170. }
  171. return TypeBuilder::Console::ConsoleMessage::Level::Log;
  172. }
  173. void ConsoleMessage::addToFrontend(InspectorFrontend::Console* frontend, InjectedScriptManager* injectedScriptManager, bool generatePreview)
  174. {
  175. RefPtr<TypeBuilder::Console::ConsoleMessage> jsonObj = TypeBuilder::Console::ConsoleMessage::create()
  176. .setSource(messageSourceValue(m_source))
  177. .setLevel(messageLevelValue(m_level))
  178. .setText(m_message);
  179. // FIXME: only send out type for ConsoleAPI source messages.
  180. jsonObj->setType(messageTypeValue(m_type));
  181. jsonObj->setLine(static_cast<int>(m_line));
  182. jsonObj->setColumn(static_cast<int>(m_column));
  183. jsonObj->setUrl(m_url);
  184. jsonObj->setRepeatCount(static_cast<int>(m_repeatCount));
  185. if (m_source == NetworkMessageSource && !m_requestId.isEmpty())
  186. jsonObj->setNetworkRequestId(m_requestId);
  187. if (m_arguments && m_arguments->argumentCount()) {
  188. InjectedScript injectedScript = injectedScriptManager->injectedScriptFor(m_arguments->globalState());
  189. if (!injectedScript.hasNoValue()) {
  190. RefPtr<TypeBuilder::Array<TypeBuilder::Runtime::RemoteObject> > jsonArgs = TypeBuilder::Array<TypeBuilder::Runtime::RemoteObject>::create();
  191. if (m_type == TableMessageType && generatePreview && m_arguments->argumentCount()) {
  192. ScriptValue table = m_arguments->argumentAt(0);
  193. ScriptValue columns = m_arguments->argumentCount() > 1 ? m_arguments->argumentAt(1) : ScriptValue();
  194. RefPtr<TypeBuilder::Runtime::RemoteObject> inspectorValue = injectedScript.wrapTable(table, columns);
  195. if (!inspectorValue) {
  196. ASSERT_NOT_REACHED();
  197. return;
  198. }
  199. jsonArgs->addItem(inspectorValue);
  200. } else {
  201. for (unsigned i = 0; i < m_arguments->argumentCount(); ++i) {
  202. RefPtr<TypeBuilder::Runtime::RemoteObject> inspectorValue = injectedScript.wrapObject(m_arguments->argumentAt(i), "console", generatePreview);
  203. if (!inspectorValue) {
  204. ASSERT_NOT_REACHED();
  205. return;
  206. }
  207. jsonArgs->addItem(inspectorValue);
  208. }
  209. }
  210. jsonObj->setParameters(jsonArgs);
  211. }
  212. }
  213. if (m_callStack)
  214. jsonObj->setStackTrace(m_callStack->buildInspectorArray());
  215. frontend->messageAdded(jsonObj);
  216. }
  217. void ConsoleMessage::updateRepeatCountInConsole(InspectorFrontend::Console* frontend)
  218. {
  219. frontend->messageRepeatCountUpdated(m_repeatCount);
  220. }
  221. bool ConsoleMessage::isEqual(ConsoleMessage* msg) const
  222. {
  223. if (m_arguments) {
  224. if (!m_arguments->isEqual(msg->m_arguments.get()))
  225. return false;
  226. // Never treat objects as equal - their properties might change over time.
  227. for (size_t i = 0; i < m_arguments->argumentCount(); ++i) {
  228. if (m_arguments->argumentAt(i).isObject())
  229. return false;
  230. }
  231. } else if (msg->m_arguments)
  232. return false;
  233. if (m_callStack) {
  234. if (!m_callStack->isEqual(msg->m_callStack.get()))
  235. return false;
  236. } else if (msg->m_callStack)
  237. return false;
  238. return msg->m_source == m_source
  239. && msg->m_type == m_type
  240. && msg->m_level == m_level
  241. && msg->m_message == m_message
  242. && msg->m_line == m_line
  243. && msg->m_column == m_column
  244. && msg->m_url == m_url
  245. && msg->m_requestId == m_requestId;
  246. }
  247. void ConsoleMessage::windowCleared(DOMWindow* window)
  248. {
  249. if (!m_arguments)
  250. return;
  251. if (domWindowFromScriptState(m_arguments->globalState()) != window)
  252. return;
  253. if (!m_message)
  254. m_message = "<message collected>";
  255. m_arguments.clear();
  256. }
  257. unsigned ConsoleMessage::argumentCount()
  258. {
  259. if (m_arguments)
  260. return m_arguments->argumentCount();
  261. return 0;
  262. }
  263. } // namespace WebCore
  264. #endif // ENABLE(INSPECTOR)