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 
31 #include "config.h"
32 #include "ConsoleMessage.h"
33 
34 #if ENABLE(INSPECTOR)
35 
36 #include "Console.h"
37 #include "InjectedScript.h"
38 #include "InjectedScriptManager.h"
39 #include "InspectorFrontend.h"
40 #include "InspectorValues.h"
41 #include "ScriptArguments.h"
42 #include "ScriptCallStack.h"
43 #include "ScriptValue.h"
44 
45 namespace WebCore {
46 
ConsoleMessage(MessageSource s,MessageType t,MessageLevel l,const String & m,unsigned li,const String & u)47 ConsoleMessage::ConsoleMessage(MessageSource s, MessageType t, MessageLevel l, const String& m, unsigned li, const String& u)
48     : m_source(s)
49     , m_type(t)
50     , m_level(l)
51     , m_message(m)
52     , m_line(li)
53     , m_url(u)
54     , m_repeatCount(1)
55     , m_requestId(0)
56 {
57 }
58 
ConsoleMessage(MessageSource s,MessageType t,MessageLevel l,const String & m,PassRefPtr<ScriptArguments> arguments,PassRefPtr<ScriptCallStack> callStack)59 ConsoleMessage::ConsoleMessage(MessageSource s, MessageType t, MessageLevel l, const String& m, PassRefPtr<ScriptArguments> arguments, PassRefPtr<ScriptCallStack> callStack)
60     : m_source(s)
61     , m_type(t)
62     , m_level(l)
63     , m_message(m)
64     , m_arguments(arguments)
65     , m_callStack(callStack)
66     , m_line(0)
67     , m_url()
68     , m_repeatCount(1)
69     , m_requestId(0)
70 {
71 }
72 
ConsoleMessage(MessageSource s,MessageType t,MessageLevel l,const String & m,const String & responseUrl,unsigned long identifier)73 ConsoleMessage::ConsoleMessage(MessageSource s, MessageType t, MessageLevel l, const String& m, const String& responseUrl, unsigned long identifier)
74     : m_source(s)
75     , m_type(t)
76     , m_level(l)
77     , m_message(m)
78     , m_line(0)
79     , m_url(responseUrl)
80     , m_repeatCount(1)
81     , m_requestId(identifier)
82 {
83 }
84 
~ConsoleMessage()85 ConsoleMessage::~ConsoleMessage()
86 {
87 }
88 
89 // Keep in sync with inspector/front-end/ConsoleView.js
messageSourceValue(MessageSource source)90 static String messageSourceValue(MessageSource source)
91 {
92     switch (source) {
93     case HTMLMessageSource: return "html";
94     case XMLMessageSource: return "xml";
95     case JSMessageSource: return "javascript";
96     case CSSMessageSource: return "css";
97     case OtherMessageSource: return "other";
98     }
99     return "other";
100 }
101 
messageTypeValue(MessageType type)102 static String messageTypeValue(MessageType type)
103 {
104     switch (type) {
105     case LogMessageType: return "log";
106     case ObjectMessageType: return "other";
107     case TraceMessageType: return "trace";
108     case StartGroupMessageType: return "startGroup";
109     case StartGroupCollapsedMessageType: return "startGroupCollapsed";
110     case EndGroupMessageType: return "endGroup";
111     case AssertMessageType: return "assert";
112     case UncaughtExceptionMessageType: return "uncaughtException";
113     case NetworkErrorMessageType: return "networkError";
114     }
115     return "other";
116 }
117 
messageLevelValue(MessageLevel level)118 static String messageLevelValue(MessageLevel level)
119 {
120     switch (level) {
121     case TipMessageLevel: return "tip";
122     case LogMessageLevel: return "log";
123     case WarningMessageLevel: return "warning";
124     case ErrorMessageLevel: return "error";
125     case DebugMessageLevel: return "debug";
126     }
127     return "log";
128 }
129 
addToFrontend(InspectorFrontend::Console * frontend,InjectedScriptManager * injectedScriptManager)130 void ConsoleMessage::addToFrontend(InspectorFrontend::Console* frontend, InjectedScriptManager* injectedScriptManager)
131 {
132     RefPtr<InspectorObject> jsonObj = InspectorObject::create();
133     jsonObj->setString("source", messageSourceValue(m_source));
134     jsonObj->setString("type", messageTypeValue(m_type));
135     jsonObj->setString("level", messageLevelValue(m_level));
136     jsonObj->setNumber("line", static_cast<int>(m_line));
137     jsonObj->setString("url", m_url);
138     jsonObj->setNumber("repeatCount", static_cast<int>(m_repeatCount));
139     jsonObj->setString("text", m_message);
140     if (m_type == NetworkErrorMessageType)
141         jsonObj->setNumber("networkIdentifier", m_requestId);
142     if (m_arguments && m_arguments->argumentCount()) {
143         InjectedScript injectedScript = injectedScriptManager->injectedScriptFor(m_arguments->globalState());
144         if (!injectedScript.hasNoValue()) {
145             RefPtr<InspectorArray> jsonArgs = InspectorArray::create();
146             for (unsigned i = 0; i < m_arguments->argumentCount(); ++i) {
147                 RefPtr<InspectorValue> inspectorValue = injectedScript.wrapObject(m_arguments->argumentAt(i), "console");
148                 if (!inspectorValue) {
149                     ASSERT_NOT_REACHED();
150                     return;
151                 }
152                 jsonArgs->pushValue(inspectorValue);
153             }
154             jsonObj->setArray("parameters", jsonArgs);
155         }
156     }
157     if (m_callStack)
158         jsonObj->setArray("stackTrace", m_callStack->buildInspectorArray());
159     frontend->messageAdded(jsonObj);
160 }
161 
updateRepeatCountInConsole(InspectorFrontend::Console * frontend)162 void ConsoleMessage::updateRepeatCountInConsole(InspectorFrontend::Console* frontend)
163 {
164     frontend->messageRepeatCountUpdated(m_repeatCount);
165 }
166 
isEqual(ConsoleMessage * msg) const167 bool ConsoleMessage::isEqual(ConsoleMessage* msg) const
168 {
169     if (m_arguments) {
170         if (!m_arguments->isEqual(msg->m_arguments.get()))
171             return false;
172     } else if (msg->m_arguments)
173         return false;
174 
175     if (m_callStack) {
176         if (!m_callStack->isEqual(msg->m_callStack.get()))
177             return false;
178     } else if (msg->m_callStack)
179         return false;
180 
181     return msg->m_source == m_source
182         && msg->m_type == m_type
183         && msg->m_level == m_level
184         && msg->m_message == m_message
185         && msg->m_line == m_line
186         && msg->m_url == m_url
187         && msg->m_requestId == m_requestId;
188 }
189 
190 } // namespace WebCore
191 
192 #endif // ENABLE(INSPECTOR)
193