OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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 "bindings/core/v8/V8ConsoleBase.h" | |
6 | |
7 #include "bindings/core/v8/ExceptionState.h" | |
8 #include "bindings/core/v8/ScriptCallStack.h" | |
9 #include "bindings/core/v8/ScriptState.h" | |
10 #include "core/frame/Deprecation.h" | |
11 #include "core/inspector/ConsoleMessage.h" | |
12 #include "core/inspector/InspectorConsoleInstrumentation.h" | |
13 #include "core/inspector/InspectorTraceEvents.h" | |
14 #include "core/inspector/ScriptArguments.h" | |
15 | |
16 namespace blink { | |
17 | |
18 static String toString(v8::Local<v8::Value> value) | |
19 { | |
20 if (value.IsEmpty() || !value->IsString()) | |
21 return String(); | |
22 return v8StringToWebCoreString<String>(value.As<v8::String>(), DoNotExternal ize); | |
23 } | |
24 | |
25 static void internalAddMessage(MessageType type, MessageLevel level, const v8::F unctionCallbackInfo<v8::Value>& info, bool allowEmptyArguments, int firstArg) | |
26 { | |
27 if (!allowEmptyArguments && !info.Length()) | |
28 return; | |
29 ScriptState* scriptState = ScriptState::current(info.GetIsolate()); | |
30 RawPtr<ScriptArguments> scriptArguments(ScriptArguments::create(scriptState, info, firstArg)); | |
31 if (scriptState && !scriptState->contextIsValid()) | |
32 scriptArguments.clear(); | |
33 String message; | |
34 if (scriptArguments) | |
35 scriptArguments->getFirstArgumentAsString(message); | |
36 | |
37 ConsoleMessage* consoleMessage = ConsoleMessage::create(ConsoleAPIMessageSou rce, level, message); | |
38 consoleMessage->setType(type); | |
39 consoleMessage->setScriptState(scriptState); | |
40 consoleMessage->setScriptArguments(scriptArguments.release()); | |
41 consoleMessage->setCallStack(ScriptCallStack::captureForConsole()); | |
42 | |
43 ConsoleBase* console = V8ConsoleBase::toImplWithTypeCheck(info.GetIsolate(), info.Data()); | |
44 if (!console) | |
45 return; | |
46 console->reportMessageToConsole(consoleMessage); | |
47 } | |
48 | |
49 static void debugFunction(const v8::FunctionCallbackInfo<v8::Value>& info) | |
50 { | |
51 internalAddMessage(LogMessageType, DebugMessageLevel, info, false, 0); | |
52 } | |
53 | |
54 void V8ConsoleBase::debugAttributeGetterCustom(const v8::FunctionCallbackInfo<v8 ::Value>& info) | |
55 { | |
56 info.GetReturnValue().Set(v8::Function::New(info.GetIsolate(), debugFunction , info.Holder())); | |
dgozman
2016/04/06 15:51:33
Is it fine to create new function for each call? W
kozy
2016/04/06 17:45:20
I've compared perfomance: https://docs.google.com/
| |
57 } | |
58 | |
59 static void errorFunction(const v8::FunctionCallbackInfo<v8::Value>& info) | |
60 { | |
61 internalAddMessage(LogMessageType, ErrorMessageLevel, info, false, 0); | |
62 } | |
63 | |
64 void V8ConsoleBase::errorAttributeGetterCustom(const v8::FunctionCallbackInfo<v8 ::Value>& info) | |
65 { | |
66 info.GetReturnValue().Set(v8::Function::New(info.GetIsolate(), errorFunction , info.Holder())); | |
67 } | |
68 | |
69 static void infoFunction(const v8::FunctionCallbackInfo<v8::Value>& info) | |
70 { | |
71 internalAddMessage(LogMessageType, InfoMessageLevel, info, false, 0); | |
72 } | |
73 | |
74 void V8ConsoleBase::infoAttributeGetterCustom(const v8::FunctionCallbackInfo<v8: :Value>& info) | |
75 { | |
76 info.GetReturnValue().Set(v8::Function::New(info.GetIsolate(), infoFunction, info.Holder())); | |
77 } | |
78 | |
79 static void logFunction(const v8::FunctionCallbackInfo<v8::Value>& info) | |
80 { | |
81 internalAddMessage(LogMessageType, LogMessageLevel, info, false, 0); | |
82 } | |
83 | |
84 void V8ConsoleBase::logAttributeGetterCustom(const v8::FunctionCallbackInfo<v8:: Value>& info) | |
85 { | |
86 info.GetReturnValue().Set(v8::Function::New(info.GetIsolate(), logFunction, info.Holder())); | |
87 } | |
88 | |
89 static void warnFunction(const v8::FunctionCallbackInfo<v8::Value>& info) | |
90 { | |
91 internalAddMessage(LogMessageType, WarningMessageLevel, info, false, 0); | |
92 } | |
93 | |
94 void V8ConsoleBase::warnAttributeGetterCustom(const v8::FunctionCallbackInfo<v8: :Value>& info) | |
95 { | |
96 info.GetReturnValue().Set(v8::Function::New(info.GetIsolate(), warnFunction, info.Holder())); | |
97 } | |
98 | |
99 static void dirFunction(const v8::FunctionCallbackInfo<v8::Value>& info) | |
100 { | |
101 internalAddMessage(DirMessageType, LogMessageLevel, info, false, 0); | |
102 } | |
103 | |
104 void V8ConsoleBase::dirAttributeGetterCustom(const v8::FunctionCallbackInfo<v8:: Value>& info) | |
105 { | |
106 info.GetReturnValue().Set(v8::Function::New(info.GetIsolate(), dirFunction, info.Holder())); | |
107 } | |
108 | |
109 static void dirxmlFunction(const v8::FunctionCallbackInfo<v8::Value>& info) | |
110 { | |
111 internalAddMessage(DirXMLMessageType, LogMessageLevel, info, false, 0); | |
112 } | |
113 | |
114 void V8ConsoleBase::dirxmlAttributeGetterCustom(const v8::FunctionCallbackInfo<v 8::Value>& info) | |
115 { | |
116 info.GetReturnValue().Set(v8::Function::New(info.GetIsolate(), dirxmlFunctio n, info.Holder())); | |
117 } | |
118 | |
119 static void tableFunction(const v8::FunctionCallbackInfo<v8::Value>& info) | |
120 { | |
121 internalAddMessage(TableMessageType, LogMessageLevel, info, false, 0); | |
122 } | |
123 | |
124 void V8ConsoleBase::tableAttributeGetterCustom(const v8::FunctionCallbackInfo<v8 ::Value>& info) | |
125 { | |
126 info.GetReturnValue().Set(v8::Function::New(info.GetIsolate(), tableFunction , info.Holder())); | |
127 } | |
128 | |
129 static void traceFunction(const v8::FunctionCallbackInfo<v8::Value>& info) | |
130 { | |
131 internalAddMessage(TraceMessageType, LogMessageLevel, info, true, 0); | |
132 } | |
133 | |
134 void V8ConsoleBase::traceAttributeGetterCustom(const v8::FunctionCallbackInfo<v8 ::Value>& info) | |
135 { | |
136 info.GetReturnValue().Set(v8::Function::New(info.GetIsolate(), traceFunction , info.Holder())); | |
137 } | |
138 | |
139 static void groupFunction(const v8::FunctionCallbackInfo<v8::Value>& info) | |
140 { | |
141 internalAddMessage(StartGroupMessageType, LogMessageLevel, info, true, 0); | |
142 } | |
143 | |
144 void V8ConsoleBase::groupAttributeGetterCustom(const v8::FunctionCallbackInfo<v8 ::Value>& info) | |
145 { | |
146 info.GetReturnValue().Set(v8::Function::New(info.GetIsolate(), groupFunction , info.Holder())); | |
147 } | |
148 | |
149 static void groupCollapsedFunction(const v8::FunctionCallbackInfo<v8::Value>& in fo) | |
150 { | |
151 internalAddMessage(StartGroupCollapsedMessageType, LogMessageLevel, info, tr ue, 0); | |
152 } | |
153 | |
154 void V8ConsoleBase::groupCollapsedAttributeGetterCustom(const v8::FunctionCallba ckInfo<v8::Value>& info) | |
155 { | |
156 info.GetReturnValue().Set(v8::Function::New(info.GetIsolate(), groupCollapse dFunction, info.Holder())); | |
157 } | |
158 | |
159 static void groupEndFunction(const v8::FunctionCallbackInfo<v8::Value>& info) | |
160 { | |
161 internalAddMessage(EndGroupMessageType, LogMessageLevel, info, true, 0); | |
162 } | |
163 | |
164 void V8ConsoleBase::groupEndAttributeGetterCustom(const v8::FunctionCallbackInfo <v8::Value>& info) | |
165 { | |
166 info.GetReturnValue().Set(v8::Function::New(info.GetIsolate(), groupEndFunct ion, info.Holder())); | |
167 } | |
168 | |
169 static void clearFunction(const v8::FunctionCallbackInfo<v8::Value>& info) | |
170 { | |
171 internalAddMessage(ClearMessageType, LogMessageLevel, info, true, 0); | |
172 } | |
173 | |
174 void V8ConsoleBase::clearAttributeGetterCustom(const v8::FunctionCallbackInfo<v8 ::Value>& info) | |
175 { | |
176 info.GetReturnValue().Set(v8::Function::New(info.GetIsolate(), clearFunction , info.Holder())); | |
177 } | |
178 | |
179 static void countFunction(const v8::FunctionCallbackInfo<v8::Value>& info) | |
180 { | |
181 ConsoleBase* console = V8ConsoleBase::toImplWithTypeCheck(info.GetIsolate(), info.Data()); | |
182 if (!console) | |
183 return; | |
184 ScriptState* scriptState = ScriptState::current(info.GetIsolate()); | |
185 RawPtr<ScriptArguments> scriptArguments(ScriptArguments::create(scriptState, info, 0)); | |
186 console->count(scriptState, scriptArguments.release()); | |
187 } | |
188 | |
189 void V8ConsoleBase::countAttributeGetterCustom(const v8::FunctionCallbackInfo<v8 ::Value>& info) | |
190 { | |
191 info.GetReturnValue().Set(v8::Function::New(info.GetIsolate(), countFunction , info.Holder())); | |
192 } | |
193 | |
194 static void assertFunction(const v8::FunctionCallbackInfo<v8::Value>& info) | |
195 { | |
196 ExceptionState exceptionState(ExceptionState::ExecutionContext, "assert", "C onsoleBase", info.Holder(), info.GetIsolate()); | |
197 bool condition = false; | |
198 if (info.Length() > 0) { | |
199 condition = toBoolean(info.GetIsolate(), info[0], exceptionState); | |
200 if (exceptionState.throwIfNeeded()) | |
201 return; | |
202 } | |
203 if (condition) | |
204 return; | |
205 internalAddMessage(AssertMessageType, ErrorMessageLevel, info, true, 1); | |
206 } | |
207 | |
208 void V8ConsoleBase::assertAttributeGetterCustom(const v8::FunctionCallbackInfo<v 8::Value>& info) | |
209 { | |
210 info.GetReturnValue().Set(v8::Function::New(info.GetIsolate(), assertFunctio n, info.Holder())); | |
211 } | |
212 | |
213 static void markTimelineFunction(const v8::FunctionCallbackInfo<v8::Value>& info ) | |
214 { | |
215 Deprecation::countDeprecationIfNotPrivateScript(info.GetIsolate(), currentEx ecutionContext(info.GetIsolate()), UseCounter::ConsoleMarkTimeline); | |
216 String title = info.Length() > 0 ? toString(info[0]) : String(); | |
217 TRACE_EVENT_INSTANT1("devtools.timeline", "TimeStamp", TRACE_EVENT_SCOPE_THR EAD, "data", InspectorTimeStampEvent::data(currentExecutionContext(info.GetIsola te()), title)); | |
218 } | |
219 | |
220 void V8ConsoleBase::markTimelineAttributeGetterCustom(const v8::FunctionCallback Info<v8::Value>& info) | |
221 { | |
222 info.GetReturnValue().Set(v8::Function::New(info.GetIsolate(), markTimelineF unction, info.Holder())); | |
223 } | |
224 | |
225 static void profileFunction(const v8::FunctionCallbackInfo<v8::Value>& info) | |
226 { | |
227 ConsoleBase* console = V8ConsoleBase::toImplWithTypeCheck(info.GetIsolate(), info.Data()); | |
228 if (!console) | |
229 return; | |
230 String title = info.Length() > 0 ? toString(info[0]) : String(); | |
231 InspectorInstrumentation::consoleProfile(currentExecutionContext(info.GetIso late()), title); | |
232 } | |
233 | |
234 void V8ConsoleBase::profileAttributeGetterCustom(const v8::FunctionCallbackInfo< v8::Value>& info) | |
235 { | |
236 info.GetReturnValue().Set(v8::Function::New(info.GetIsolate(), profileFuncti on, info.Holder())); | |
237 } | |
238 | |
239 static void profileEndFunction(const v8::FunctionCallbackInfo<v8::Value>& info) | |
240 { | |
241 ConsoleBase* console = V8ConsoleBase::toImplWithTypeCheck(info.GetIsolate(), info.Data()); | |
242 if (!console) | |
243 return; | |
244 String title = info.Length() > 0 ? toString(info[0]) : String(); | |
245 InspectorInstrumentation::consoleProfileEnd(currentExecutionContext(info.Get Isolate()), title); | |
246 } | |
247 | |
248 void V8ConsoleBase::profileEndAttributeGetterCustom(const v8::FunctionCallbackIn fo<v8::Value>& info) | |
249 { | |
250 info.GetReturnValue().Set(v8::Function::New(info.GetIsolate(), profileEndFun ction, info.Holder())); | |
251 } | |
252 | |
253 static String formatTimelineTitle(const String& title) | |
254 { | |
255 return String::format("Timeline '%s'", title.utf8().data()); | |
256 } | |
257 | |
258 static void timelineFunction(const v8::FunctionCallbackInfo<v8::Value>& info) | |
259 { | |
260 Deprecation::countDeprecationIfNotPrivateScript(info.GetIsolate(), currentEx ecutionContext(info.GetIsolate()), UseCounter::ConsoleTimeline); | |
261 ConsoleBase* console = V8ConsoleBase::toImplWithTypeCheck(info.GetIsolate(), info.Data()); | |
262 if (!console) | |
263 return; | |
264 String title = info.Length() > 0 ? toString(info[0]) : String(); | |
265 TRACE_EVENT_COPY_ASYNC_BEGIN0("blink.console", formatTimelineTitle(title).ut f8().data(), console); | |
266 } | |
267 | |
268 void V8ConsoleBase::timelineAttributeGetterCustom(const v8::FunctionCallbackInfo <v8::Value>& info) | |
269 { | |
270 info.GetReturnValue().Set(v8::Function::New(info.GetIsolate(), timelineFunct ion, info.Holder())); | |
271 } | |
272 | |
273 static void timelineEndFunction(const v8::FunctionCallbackInfo<v8::Value>& info) | |
274 { | |
275 Deprecation::countDeprecationIfNotPrivateScript(info.GetIsolate(), currentEx ecutionContext(info.GetIsolate()), UseCounter::ConsoleTimelineEnd); | |
276 ConsoleBase* console = V8ConsoleBase::toImplWithTypeCheck(info.GetIsolate(), info.Data()); | |
277 if (!console) | |
278 return; | |
279 String title = info.Length() > 0 ? toString(info[0]) : String(); | |
280 TRACE_EVENT_COPY_ASYNC_END0("blink.console", formatTimelineTitle(title).utf8 ().data(), console); | |
281 } | |
282 | |
283 void V8ConsoleBase::timelineEndAttributeGetterCustom(const v8::FunctionCallbackI nfo<v8::Value>& info) | |
284 { | |
285 info.GetReturnValue().Set(v8::Function::New(info.GetIsolate(), timelineEndFu nction, info.Holder())); | |
286 } | |
287 | |
288 static void timeFunction(const v8::FunctionCallbackInfo<v8::Value>& info) | |
289 { | |
290 ConsoleBase* console = V8ConsoleBase::toImplWithTypeCheck(info.GetIsolate(), info.Data()); | |
291 if (!console) | |
292 return; | |
293 String title = info.Length() > 0 ? toString(info[0]) : String(); | |
294 console->time(title); | |
295 } | |
296 | |
297 void V8ConsoleBase::timeAttributeGetterCustom(const v8::FunctionCallbackInfo<v8: :Value>& info) | |
298 { | |
299 info.GetReturnValue().Set(v8::Function::New(info.GetIsolate(), timeFunction, info.Holder())); | |
300 } | |
301 | |
302 static void timeEndFunction(const v8::FunctionCallbackInfo<v8::Value>& info) | |
303 { | |
304 ConsoleBase* console = V8ConsoleBase::toImplWithTypeCheck(info.GetIsolate(), info.Data()); | |
305 if (!console) | |
306 return; | |
307 String title = info.Length() > 0 ? toString(info[0]) : String(); | |
308 ScriptState* scriptState = ScriptState::current(info.GetIsolate()); | |
309 console->timeEnd(scriptState, title); | |
310 } | |
311 | |
312 void V8ConsoleBase::timeEndAttributeGetterCustom(const v8::FunctionCallbackInfo< v8::Value>& info) | |
313 { | |
314 info.GetReturnValue().Set(v8::Function::New(info.GetIsolate(), timeEndFuncti on, info.Holder())); | |
315 } | |
316 | |
317 static void timeStampFunction(const v8::FunctionCallbackInfo<v8::Value>& info) | |
318 { | |
319 String title = info.Length() > 0 ? toString(info[0]) : String(); | |
320 TRACE_EVENT_INSTANT1("devtools.timeline", "TimeStamp", TRACE_EVENT_SCOPE_THR EAD, "data", InspectorTimeStampEvent::data(currentExecutionContext(info.GetIsola te()), title)); | |
321 } | |
322 | |
323 void V8ConsoleBase::timeStampAttributeGetterCustom(const v8::FunctionCallbackInf o<v8::Value>& info) | |
324 { | |
325 info.GetReturnValue().Set(v8::Function::New(info.GetIsolate(), timeStampFunc tion, info.Holder())); | |
326 } | |
327 | |
328 } // namespace blink | |
OLD | NEW |