author | Eugene Ostroukhov <eugeneo@symbian.org> |
Mon, 07 Jun 2010 16:51:19 -0700 | |
changeset 355 | 8726e95bcbba |
parent 276 | f2f4a1259de8 |
permissions | -rw-r--r-- |
2 | 1 |
// Copyright (c) 2009 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 |
package org.chromium.sdk.internal.tools.v8; |
|
6 |
||
7 |
import java.util.ArrayList; |
|
8 |
import java.util.List; |
|
9 |
||
10 |
import org.chromium.sdk.Script; |
|
11 |
import org.chromium.sdk.Version; |
|
12 |
import org.chromium.sdk.Script.Type; |
|
13 |
import org.chromium.sdk.internal.DataWithRef; |
|
14 |
import org.chromium.sdk.internal.JsonUtil; |
|
15 |
import org.chromium.sdk.internal.PropertyReference; |
|
16 |
import org.chromium.sdk.internal.PropertyType; |
|
17 |
import org.chromium.sdk.internal.V8ContextFilter; |
|
18 |
import org.chromium.sdk.internal.protocol.AfterCompileBody; |
|
19 |
import org.chromium.sdk.internal.protocol.BacktraceCommandBody; |
|
20 |
import org.chromium.sdk.internal.protocol.BreakEventBody; |
|
21 |
import org.chromium.sdk.internal.protocol.BreakpointBody; |
|
355
8726e95bcbba
Initial commit of updated Chrome Java SDK
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
276
diff
changeset
|
22 |
import org.chromium.sdk.internal.protocol.ChangeLiveBody; |
2 | 23 |
import org.chromium.sdk.internal.protocol.CommandResponse; |
24 |
import org.chromium.sdk.internal.protocol.CommandResponseBody; |
|
25 |
import org.chromium.sdk.internal.protocol.EventNotification; |
|
26 |
import org.chromium.sdk.internal.protocol.EventNotificationBody; |
|
27 |
import org.chromium.sdk.internal.protocol.FailedCommandResponse; |
|
28 |
import org.chromium.sdk.internal.protocol.FrameObject; |
|
29 |
import org.chromium.sdk.internal.protocol.IncomingMessage; |
|
355
8726e95bcbba
Initial commit of updated Chrome Java SDK
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
276
diff
changeset
|
30 |
import org.chromium.sdk.internal.protocol.ListBreakpointsBody; |
2 | 31 |
import org.chromium.sdk.internal.protocol.ScopeBody; |
32 |
import org.chromium.sdk.internal.protocol.ScopeRef; |
|
33 |
import org.chromium.sdk.internal.protocol.SuccessCommandResponse; |
|
34 |
import org.chromium.sdk.internal.protocol.VersionBody; |
|
355
8726e95bcbba
Initial commit of updated Chrome Java SDK
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
276
diff
changeset
|
35 |
import org.chromium.sdk.internal.protocol.data.BreakpointInfo; |
2 | 36 |
import org.chromium.sdk.internal.protocol.data.ContextData; |
37 |
import org.chromium.sdk.internal.protocol.data.ContextHandle; |
|
38 |
import org.chromium.sdk.internal.protocol.data.FunctionValueHandle; |
|
39 |
import org.chromium.sdk.internal.protocol.data.ObjectValueHandle; |
|
40 |
import org.chromium.sdk.internal.protocol.data.PropertyObject; |
|
41 |
import org.chromium.sdk.internal.protocol.data.PropertyWithRef; |
|
42 |
import org.chromium.sdk.internal.protocol.data.PropertyWithValue; |
|
43 |
import org.chromium.sdk.internal.protocol.data.RefWithDisplayData; |
|
44 |
import org.chromium.sdk.internal.protocol.data.ScriptHandle; |
|
45 |
import org.chromium.sdk.internal.protocol.data.SomeHandle; |
|
46 |
import org.chromium.sdk.internal.protocol.data.SomeRef; |
|
47 |
import org.chromium.sdk.internal.protocol.data.SomeSerialized; |
|
48 |
import org.chromium.sdk.internal.protocol.data.ValueHandle; |
|
49 |
import org.chromium.sdk.internal.protocolparser.JsonProtocolModelParseException; |
|
50 |
import org.chromium.sdk.internal.protocolparser.JsonProtocolParseException; |
|
51 |
import org.chromium.sdk.internal.protocolparser.dynamicimpl.JsonProtocolParser; |
|
52 |
import org.chromium.sdk.internal.tools.v8.request.ScriptsMessage; |
|
53 |
import org.json.simple.JSONObject; |
|
54 |
||
55 |
/** |
|
56 |
* A utility class to process V8 debugger messages. |
|
57 |
*/ |
|
58 |
public class V8ProtocolUtil { |
|
59 |
||
60 |
/** |
|
61 |
* Computes a script type given a V8 Long type value |
|
62 |
* |
|
63 |
* @param typeNumber a type designator from a V8 JSON response |
|
64 |
* @return a type corresponding to {@code typeNumber} or {@code null} if |
|
65 |
* {@code typeNumber == null} |
|
66 |
*/ |
|
67 |
public static Script.Type getScriptType(Long typeNumber) { |
|
68 |
if (typeNumber == null) { |
|
69 |
return null; |
|
70 |
} |
|
71 |
switch (typeNumber.intValue()) { |
|
72 |
case ScriptsMessage.SCRIPTS_NORMAL: |
|
73 |
return Type.NORMAL; |
|
74 |
case ScriptsMessage.SCRIPTS_NATIVE: |
|
75 |
return Type.NATIVE; |
|
76 |
case ScriptsMessage.SCRIPTS_EXTENSION: |
|
77 |
return Type.EXTENSION; |
|
78 |
default: |
|
79 |
throw new IllegalArgumentException("unknown script type: " + typeNumber); |
|
80 |
} |
|
81 |
} |
|
82 |
||
83 |
/** |
|
84 |
* Returns the value of "ref" field in object corresponding to the fieldName |
|
85 |
* in parent. |
|
86 |
* |
|
87 |
* @param parent to get the object from |
|
88 |
* @param fieldName of the object to get the "ref" from |
|
89 |
* @return ref value or null if fieldName or "ref" not found |
|
90 |
*/ |
|
91 |
public static Long getObjectRef(SomeRef child) { |
|
92 |
if (child == null) { |
|
93 |
return null; |
|
94 |
} |
|
95 |
return child.ref(); |
|
96 |
} |
|
97 |
||
98 |
||
99 |
/** |
|
100 |
* Constructs {@code PropertyReference}s from the specified object, be it in |
|
101 |
* the "original" or "inlineRefs" format. |
|
102 |
* |
|
103 |
* @param handle to get property references from |
|
104 |
* @return an array of PropertyReferences |
|
105 |
*/ |
|
106 |
public static List<? extends PropertyReference> extractObjectProperties( |
|
107 |
ObjectValueHandle handle) { |
|
108 |
List<PropertyObject> props = handle.properties(); |
|
109 |
int propsLen = props.size(); |
|
110 |
List<PropertyReference> objProps = new ArrayList<PropertyReference>(propsLen); |
|
111 |
for (int i = 0; i < propsLen; i++) { |
|
112 |
PropertyObject prop = props.get(i); |
|
113 |
putMirror(objProps, prop, PropertyNameGetter.SUBPROPERTY); |
|
114 |
} |
|
115 |
||
116 |
return objProps; |
|
117 |
} |
|
118 |
public static List<? extends PropertyReference> extractObjectInternalProperties( |
|
119 |
ObjectValueHandle handle) { |
|
120 |
List<PropertyReference> objProps = new ArrayList<PropertyReference>(3); |
|
121 |
SomeRef protoObject = handle.protoObject(); |
|
122 |
RefWithDisplayData protoObjectRef = protoObject.asWithDisplayData(); |
|
123 |
if (protoObjectRef != null) { |
|
124 |
putMirror(objProps, protoObjectRef, PropertyNameGetter.PROTO_OBJECT); |
|
125 |
} |
|
126 |
SomeRef constructorFunction = handle.constructorFunction(); |
|
127 |
RefWithDisplayData constructorFunctionRef = constructorFunction.asWithDisplayData(); |
|
128 |
if (constructorFunctionRef != null) { |
|
129 |
putMirror(objProps, constructorFunctionRef, PropertyNameGetter.CONSTRUCTOR_FUNCTION); |
|
130 |
} |
|
131 |
return objProps; |
|
132 |
} |
|
133 |
||
134 |
static <OBJ> void putMirror(List<PropertyReference> refs, OBJ propertyObject, |
|
135 |
V8ProtocolUtil.PropertyNameGetter<OBJ> nameGetter) { |
|
136 |
PropertyReference propertyRef = V8ProtocolUtil.extractProperty(propertyObject, nameGetter); |
|
137 |
if (propertyRef != null) { |
|
138 |
refs.add(propertyRef); |
|
139 |
} |
|
140 |
} |
|
141 |
||
142 |
/** |
|
143 |
* Constructs one {@code PropertyReference} from the specified object, be it in |
|
144 |
* the "original" or "inlineRefs" format. |
|
145 |
* |
|
146 |
* @param prop json object |
|
147 |
* @param valuePropertyName name of value property in this prop object, might be null |
|
148 |
* @return PropertyReference or null if we ignore this property |
|
149 |
*/ |
|
150 |
public static <OBJ> PropertyReference extractProperty(OBJ prop, |
|
151 |
PropertyNameGetter<OBJ> nameGetter) { |
|
276
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
152 |
Object name = nameGetter.getName(prop); |
2 | 153 |
if (name == null) { |
154 |
return null; |
|
155 |
} |
|
156 |
||
157 |
if (isInternalProperty(name)) { |
|
158 |
return null; |
|
159 |
} |
|
160 |
||
161 |
DataWithRef propValue = nameGetter.getRef(prop); |
|
162 |
||
163 |
Long propType = nameGetter.getPropertyType(prop); |
|
164 |
// propType is NORMAL by default |
|
165 |
int propTypeValue = propType != null |
|
166 |
? propType.intValue() |
|
167 |
: PropertyType.NORMAL.value; |
|
168 |
if (propTypeValue == PropertyType.FIELD.value || |
|
169 |
propTypeValue == PropertyType.CONSTANT_FUNCTION.value || |
|
170 |
propTypeValue == PropertyType.CALLBACKS.value || |
|
171 |
propTypeValue == PropertyType.NORMAL.value) { |
|
172 |
return new PropertyReference(name, propValue); |
|
173 |
} |
|
174 |
return null; |
|
175 |
} |
|
176 |
||
177 |
static abstract class PropertyNameGetter<OBJ> { |
|
178 |
static class SimpleNameGetter extends PropertyNameGetter<RefWithDisplayData> { |
|
179 |
private final String name; |
|
180 |
SimpleNameGetter(String name) { |
|
181 |
this.name = name; |
|
182 |
} |
|
183 |
@Override |
|
184 |
String getName(RefWithDisplayData ref) { |
|
185 |
return name; |
|
186 |
} |
|
187 |
@Override |
|
188 |
DataWithRef getRef(RefWithDisplayData prop) { |
|
189 |
return DataWithRef.fromSomeRef(prop.getSuper()); |
|
190 |
} |
|
191 |
@Override |
|
192 |
Long getPropertyType(RefWithDisplayData prop) { |
|
193 |
return null; |
|
194 |
} |
|
195 |
} |
|
196 |
||
197 |
static final PropertyNameGetter<PropertyObject> LOCAL = new SubpropertyNameGetter() { |
|
198 |
@Override |
|
276
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
199 |
Object getName(PropertyObject ref) { |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
200 |
Object name = super.getName(ref); |
2 | 201 |
if (V8ProtocolUtil.isInternalProperty(name)) { |
202 |
return null; |
|
203 |
} |
|
204 |
return name; |
|
205 |
} |
|
206 |
}; |
|
207 |
/** The name of the "this" object to report as a variable name. */ |
|
208 |
static final PropertyNameGetter<RefWithDisplayData> THIS = new SimpleNameGetter("this"); |
|
209 |
static final PropertyNameGetter<RefWithDisplayData> PROTO_OBJECT = |
|
210 |
new SimpleNameGetter("__proto__"); |
|
211 |
static final PropertyNameGetter<RefWithDisplayData> CONSTRUCTOR_FUNCTION = |
|
212 |
new SimpleNameGetter("constructor"); |
|
213 |
||
214 |
static final PropertyNameGetter<PropertyObject> SUBPROPERTY = new SubpropertyNameGetter(); |
|
215 |
static class SubpropertyNameGetter extends PropertyNameGetter<PropertyObject> { |
|
216 |
@Override |
|
276
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
217 |
Object getName(PropertyObject ref) { |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
218 |
return ref.name(); |
2 | 219 |
} |
220 |
@Override |
|
221 |
DataWithRef getRef(PropertyObject prop) { |
|
222 |
PropertyWithValue asPropertyWithValue = prop.asPropertyWithValue(); |
|
223 |
if (asPropertyWithValue != null) { |
|
276
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
224 |
return DataWithRef.fromSomeRef(asPropertyWithValue.getValue()); |
2 | 225 |
} else { |
226 |
return DataWithRef.fromLong(prop.asPropertyWithRef().ref()); |
|
227 |
} |
|
228 |
} |
|
229 |
@Override |
|
230 |
Long getPropertyType(PropertyObject prop) { |
|
231 |
PropertyWithRef asPropertyWithRef = prop.asPropertyWithRef(); |
|
232 |
if (asPropertyWithRef == null) { |
|
233 |
return null; |
|
234 |
} |
|
235 |
return asPropertyWithRef.propertyType(); |
|
236 |
} |
|
237 |
} |
|
238 |
||
239 |
abstract DataWithRef getRef(OBJ prop); |
|
240 |
||
241 |
/** |
|
242 |
* @return property name or null if we should skip this property |
|
243 |
*/ |
|
276
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
244 |
abstract Object getName(OBJ ref); |
2 | 245 |
abstract Long getPropertyType(OBJ prop); |
246 |
} |
|
247 |
||
248 |
/** |
|
249 |
* @param propertyName the property name to check |
|
250 |
* @return whether the given property name corresponds to an internal V8 |
|
251 |
* property |
|
252 |
*/ |
|
276
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
253 |
public static boolean isInternalProperty(Object propertyName) { |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
254 |
if (propertyName instanceof String == false) { |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
255 |
return false; |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
256 |
} |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
257 |
String propertyNameStr = (String) propertyName; |
2 | 258 |
// Chrome can return properties like ".arguments". They should be ignored. |
276
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
259 |
return propertyNameStr.length() == 0 || propertyNameStr.startsWith("."); |
2 | 260 |
} |
261 |
||
262 |
/** |
|
263 |
* Gets a function name from the given function handle. |
|
264 |
* |
|
265 |
* @param functionObject the function handle |
|
266 |
* @return the actual of inferred function name. Will handle {@code null} or |
|
267 |
* unnamed functions |
|
268 |
*/ |
|
269 |
public static String getFunctionName(JSONObject functionObject) { |
|
270 |
if (functionObject == null) { |
|
271 |
return "<unknown>"; |
|
272 |
} else { |
|
273 |
String name = getNameOrInferred(functionObject, V8Protocol.LOCAL_NAME); |
|
274 |
if (isNullOrEmpty(name)) { |
|
275 |
return "(anonymous function)"; |
|
276 |
} else { |
|
277 |
return name; |
|
278 |
} |
|
279 |
} |
|
280 |
} |
|
281 |
||
282 |
/** |
|
283 |
* Gets a script id from a script response. |
|
284 |
* |
|
285 |
* @param scriptObject to get the "id" value from |
|
286 |
* @return the script id |
|
287 |
*/ |
|
288 |
public static Long getScriptIdFromResponse(ScriptHandle scriptObject) { |
|
289 |
return scriptObject.id(); |
|
290 |
} |
|
291 |
||
292 |
/** |
|
293 |
* Determines if a {@code script} is valid in the current debug context. |
|
294 |
* Returns {@code null} if it is not, otherwise returns {@code script}. |
|
295 |
* |
|
296 |
* @param script to check and, possibly, modify |
|
297 |
* @param refs from the corresponding V8 response |
|
298 |
* @return script with a non-null name if the script is valid, {@code null} |
|
299 |
* otherwise |
|
300 |
*/ |
|
301 |
public static ScriptHandle validScript(ScriptHandle script, List<SomeHandle> refs, |
|
302 |
V8ContextFilter contextFilter) { |
|
303 |
Long contextRef = V8ProtocolUtil.getObjectRef(script.context()); |
|
304 |
for (int i = 0, size = refs.size(); i < size; i++) { |
|
305 |
SomeHandle ref = refs.get(i); |
|
306 |
if (ref.handle() != contextRef.longValue()) { |
|
307 |
continue; |
|
308 |
} |
|
309 |
ContextHandle contextHandle; |
|
310 |
try { |
|
311 |
contextHandle = ref.asContextHandle(); |
|
312 |
} catch (JsonProtocolParseException e) { |
|
313 |
throw new RuntimeException(e); |
|
314 |
} |
|
315 |
if (!contextFilter.isContextOurs(contextHandle)) { |
|
316 |
return null; |
|
317 |
} |
|
318 |
return script; |
|
319 |
} |
|
320 |
return null; // good context not found |
|
321 |
} |
|
322 |
||
323 |
public static Version parseVersionResponse(SuccessCommandResponse versionResponse) { |
|
324 |
VersionBody body; |
|
325 |
try { |
|
326 |
body = versionResponse.getBody().asVersionBody(); |
|
327 |
} catch (JsonProtocolParseException e) { |
|
328 |
throw new RuntimeException(e); |
|
329 |
} |
|
330 |
String versionString = body.getV8Version(); |
|
331 |
if (versionString == null) { |
|
332 |
return null; |
|
333 |
} |
|
334 |
return Version.parseString(versionString); |
|
335 |
} |
|
336 |
||
337 |
private static String getNameOrInferred(JSONObject obj, V8Protocol nameProperty) { |
|
338 |
String name = JsonUtil.getAsString(obj, nameProperty); |
|
339 |
if (isNullOrEmpty(name)) { |
|
340 |
name = JsonUtil.getAsString(obj, V8Protocol.INFERRED_NAME); |
|
341 |
} |
|
342 |
return name; |
|
343 |
} |
|
344 |
||
345 |
private static boolean isNullOrEmpty(String value) { |
|
346 |
return value == null || value.length() == 0; |
|
347 |
} |
|
348 |
||
349 |
public static JsonProtocolParser getV8Parser() { |
|
350 |
return parser; |
|
351 |
} |
|
352 |
||
353 |
private static final JsonProtocolParser parser; |
|
354 |
static { |
|
355 |
try { |
|
356 |
// TODO(peter.rybin): change to ParserHolder. |
|
357 |
parser = new JsonProtocolParser(new Class<?>[] { |
|
358 |
IncomingMessage.class, |
|
359 |
EventNotification.class, |
|
360 |
SuccessCommandResponse.class, |
|
361 |
FailedCommandResponse.class, |
|
362 |
CommandResponse.class, |
|
363 |
BreakEventBody.class, |
|
364 |
EventNotificationBody.class, |
|
365 |
CommandResponseBody.class, |
|
366 |
BacktraceCommandBody.class, |
|
367 |
FrameObject.class, |
|
368 |
BreakpointBody.class, |
|
369 |
ScopeBody.class, |
|
370 |
ScopeRef.class, |
|
371 |
VersionBody.class, |
|
372 |
AfterCompileBody.class, |
|
355
8726e95bcbba
Initial commit of updated Chrome Java SDK
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
276
diff
changeset
|
373 |
ChangeLiveBody.class, |
8726e95bcbba
Initial commit of updated Chrome Java SDK
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
276
diff
changeset
|
374 |
ListBreakpointsBody.class, |
2 | 375 |
|
376 |
SomeHandle.class, |
|
377 |
ScriptHandle.class, |
|
378 |
ValueHandle.class, |
|
379 |
RefWithDisplayData.class, |
|
380 |
PropertyObject.class, |
|
381 |
PropertyWithRef.class, |
|
382 |
PropertyWithValue.class, |
|
383 |
ObjectValueHandle.class, |
|
384 |
FunctionValueHandle.class, |
|
385 |
SomeRef.class, |
|
386 |
SomeSerialized.class, |
|
387 |
ContextHandle.class, |
|
388 |
ContextData.class, |
|
355
8726e95bcbba
Initial commit of updated Chrome Java SDK
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
276
diff
changeset
|
389 |
BreakpointInfo.class, |
2 | 390 |
}); |
391 |
} catch (JsonProtocolModelParseException e) { |
|
392 |
throw new RuntimeException(e); |
|
393 |
} |
|
394 |
} |
|
395 |
} |