1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "config.h"
6 #include "core/inspector/ConsoleMessage.h"
7
8 #include "bindings/core/v8/ScriptCallStackFactory.h"
9 #include "bindings/core/v8/ScriptValue.h"
10 #include "core/inspector/ScriptArguments.h"
11 #include "wtf/CurrentTime.h"
12 #include "wtf/PassOwnPtr.h"
13
14 namespace blink {
15
ConsoleMessage(MessageSource source,MessageLevel level,const String & message,const String & url,unsigned lineNumber,unsigned columnNumber)16 ConsoleMessage::ConsoleMessage(MessageSource source,
17 MessageLevel level,
18 const String& message,
19 const String& url,
20 unsigned lineNumber,
21 unsigned columnNumber)
22 : m_source(source)
23 , m_level(level)
24 , m_type(LogMessageType)
25 , m_message(message)
26 , m_scriptId(0)
27 , m_url(url)
28 , m_lineNumber(lineNumber)
29 , m_columnNumber(columnNumber)
30 , m_requestIdentifier(0)
31 , m_timestamp(WTF::currentTime())
32 , m_workerProxy(nullptr)
33 {
34 }
35
~ConsoleMessage()36 ConsoleMessage::~ConsoleMessage()
37 {
38 }
39
type() const40 MessageType ConsoleMessage::type() const
41 {
42 return m_type;
43 }
44
setType(MessageType type)45 void ConsoleMessage::setType(MessageType type)
46 {
47 m_type = type;
48 }
49
scriptId() const50 int ConsoleMessage::scriptId() const
51 {
52 return m_scriptId;
53 }
54
setScriptId(int scriptId)55 void ConsoleMessage::setScriptId(int scriptId)
56 {
57 m_scriptId = scriptId;
58 }
59
url() const60 const String& ConsoleMessage::url() const
61 {
62 return m_url;
63 }
64
setURL(const String & url)65 void ConsoleMessage::setURL(const String& url)
66 {
67 m_url = url;
68 }
69
lineNumber() const70 unsigned ConsoleMessage::lineNumber() const
71 {
72 return m_lineNumber;
73 }
74
setLineNumber(unsigned lineNumber)75 void ConsoleMessage::setLineNumber(unsigned lineNumber)
76 {
77 m_lineNumber = lineNumber;
78 }
79
callStack() const80 PassRefPtrWillBeRawPtr<ScriptCallStack> ConsoleMessage::callStack() const
81 {
82 return m_callStack;
83 }
84
setCallStack(PassRefPtrWillBeRawPtr<ScriptCallStack> callStack)85 void ConsoleMessage::setCallStack(PassRefPtrWillBeRawPtr<ScriptCallStack> callStack)
86 {
87 m_callStack = callStack;
88 }
89
scriptState() const90 ScriptState* ConsoleMessage::scriptState() const
91 {
92 if (m_scriptState)
93 return m_scriptState->get();
94 return nullptr;
95 }
96
setScriptState(ScriptState * scriptState)97 void ConsoleMessage::setScriptState(ScriptState* scriptState)
98 {
99 if (m_scriptState)
100 m_scriptState->clear();
101
102 if (scriptState)
103 m_scriptState = adoptPtr(new ScriptStateProtectingContext(scriptState));
104 else
105 m_scriptState.clear();
106 }
107
scriptArguments() const108 PassRefPtrWillBeRawPtr<ScriptArguments> ConsoleMessage::scriptArguments() const
109 {
110 return m_scriptArguments;
111 }
112
setScriptArguments(PassRefPtrWillBeRawPtr<ScriptArguments> scriptArguments)113 void ConsoleMessage::setScriptArguments(PassRefPtrWillBeRawPtr<ScriptArguments> scriptArguments)
114 {
115 m_scriptArguments = scriptArguments;
116 }
117
requestIdentifier() const118 unsigned long ConsoleMessage::requestIdentifier() const
119 {
120 return m_requestIdentifier;
121 }
122
setRequestIdentifier(unsigned long requestIdentifier)123 void ConsoleMessage::setRequestIdentifier(unsigned long requestIdentifier)
124 {
125 m_requestIdentifier = requestIdentifier;
126 }
127
timestamp() const128 double ConsoleMessage::timestamp() const
129 {
130 return m_timestamp;
131 }
132
setTimestamp(double timestamp)133 void ConsoleMessage::setTimestamp(double timestamp)
134 {
135 m_timestamp = timestamp;
136 }
137
source() const138 MessageSource ConsoleMessage::source() const
139 {
140 return m_source;
141 }
142
level() const143 MessageLevel ConsoleMessage::level() const
144 {
145 return m_level;
146 }
147
message() const148 const String& ConsoleMessage::message() const
149 {
150 return m_message;
151 }
152
columnNumber() const153 unsigned ConsoleMessage::columnNumber() const
154 {
155 return m_columnNumber;
156 }
157
frameWindowDiscarded(LocalDOMWindow * window)158 void ConsoleMessage::frameWindowDiscarded(LocalDOMWindow* window)
159 {
160 if (scriptState() && scriptState()->domWindow() == window)
161 setScriptState(nullptr);
162
163 if (!m_scriptArguments)
164 return;
165 if (m_scriptArguments->scriptState()->domWindow() != window)
166 return;
167 if (!m_message)
168 m_message = "<message collected>";
169 m_scriptArguments.clear();
170 }
171
argumentCount()172 unsigned ConsoleMessage::argumentCount()
173 {
174 if (m_scriptArguments)
175 return m_scriptArguments->argumentCount();
176 return 0;
177 }
178
collectCallStack()179 void ConsoleMessage::collectCallStack()
180 {
181 if (m_type == EndGroupMessageType)
182 return;
183
184 if (!m_callStack || m_source == ConsoleAPIMessageSource)
185 m_callStack = createScriptCallStackForConsole(ScriptCallStack::maxCallStackSizeToCapture, true);
186
187 if (m_callStack && m_callStack->size() && !m_scriptId) {
188 const ScriptCallFrame& frame = m_callStack->at(0);
189 m_url = frame.sourceURL();
190 m_lineNumber = frame.lineNumber();
191 m_columnNumber = frame.columnNumber();
192 return;
193 }
194
195 m_callStack.clear();
196 }
197
trace(Visitor * visitor)198 void ConsoleMessage::trace(Visitor* visitor)
199 {
200 visitor->trace(m_callStack);
201 visitor->trace(m_scriptArguments);
202 }
203
204 } // namespace blink
205