author | Eugene Ostroukhov <eugeneo@symbian.org> |
Thu, 18 Mar 2010 11:56:59 -0700 | |
changeset 276 | f2f4a1259de8 |
parent 52 | f577ea64429e |
child 355 | 8726e95bcbba |
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; |
|
6 |
||
7 |
import java.util.ArrayList; |
|
8 |
import java.util.Collection; |
|
9 |
import java.util.Collections; |
|
10 |
import java.util.HashMap; |
|
11 |
import java.util.List; |
|
12 |
import java.util.Map; |
|
13 |
||
14 |
import org.chromium.sdk.JsFunction; |
|
15 |
import org.chromium.sdk.JsObject; |
|
16 |
import org.chromium.sdk.JsVariable; |
|
17 |
import org.chromium.sdk.internal.tools.v8.MethodIsBlockingException; |
|
18 |
||
19 |
/** |
|
20 |
* A generic implementation of the JsObject interface. |
|
21 |
*/ |
|
22 |
class JsObjectImpl extends JsValueImpl implements JsObject { |
|
23 |
||
52
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
24 |
private final InternalContext context; |
2 | 25 |
|
276
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
52
diff
changeset
|
26 |
/** |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
52
diff
changeset
|
27 |
* Fully qualified name of variable holding this object. |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
52
diff
changeset
|
28 |
*/ |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
52
diff
changeset
|
29 |
private final String variableFqn; |
2 | 30 |
|
31 |
/** |
|
32 |
* This constructor implies the lazy resolution of object properties. |
|
33 |
* |
|
52
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
34 |
* @param context where this instance belongs in |
276
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
52
diff
changeset
|
35 |
* @param variableFqn the fully qualified name of the variable holding this object |
2 | 36 |
* @param valueState the value data from the JS VM |
37 |
*/ |
|
276
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
52
diff
changeset
|
38 |
JsObjectImpl(InternalContext context, String variableFqn, ValueMirror valueState) { |
2 | 39 |
super(valueState); |
52
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
40 |
this.context = context; |
276
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
52
diff
changeset
|
41 |
this.variableFqn = variableFqn; |
2 | 42 |
} |
43 |
||
44 |
public Collection<JsVariableImpl> getProperties() throws MethodIsBlockingException { |
|
45 |
return subproperties.getPropertiesLazily(); |
|
46 |
} |
|
47 |
||
48 |
public Collection<JsVariableImpl> getInternalProperties() throws MethodIsBlockingException { |
|
49 |
return internalProperties.getPropertiesLazily(); |
|
50 |
} |
|
51 |
||
52 |
public String getRefId() { |
|
53 |
int ref = getMirror().getRef(); |
|
54 |
if (ref < 0) { |
|
55 |
// Negative handle means that it's transient. We don't expose it. |
|
56 |
return null; |
|
57 |
} else { |
|
58 |
return String.valueOf(ref); |
|
59 |
} |
|
60 |
} |
|
61 |
||
62 |
@Override |
|
63 |
public String toString() { |
|
64 |
StringBuilder result = new StringBuilder(); |
|
65 |
result.append("[JsObject: type=").append(getType()); |
|
66 |
try { |
|
67 |
for (JsVariable prop : getProperties()) { |
|
68 |
result.append(',').append(prop); |
|
69 |
} |
|
70 |
} catch (MethodIsBlockingException e) { |
|
71 |
return "[JsObject: Exception in retrieving data]"; |
|
72 |
} |
|
73 |
result.append(']'); |
|
74 |
return result.toString(); |
|
75 |
} |
|
76 |
||
77 |
@Override |
|
78 |
public JsObjectImpl asObject() { |
|
79 |
return this; |
|
80 |
} |
|
81 |
||
82 |
public JsArrayImpl asArray() { |
|
83 |
return null; |
|
84 |
} |
|
85 |
||
86 |
public JsFunction asFunction() { |
|
87 |
return null; |
|
88 |
} |
|
89 |
||
90 |
public JsVariable getProperty(String name) { |
|
91 |
return subproperties.getProperty(name); |
|
92 |
} |
|
93 |
||
94 |
public String getClassName() { |
|
95 |
return getMirror().getClassName(); |
|
96 |
} |
|
97 |
||
52
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
98 |
protected InternalContext getInternalContext() { |
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
99 |
return context; |
2 | 100 |
} |
101 |
||
102 |
Subproperties getSubpropertiesHelper() { |
|
103 |
return subproperties; |
|
104 |
} |
|
105 |
||
106 |
protected SubpropertiesMirror getSubpropertiesMirror() { |
|
52
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
107 |
return context.getValueLoader().loadSubpropertiesInMirror(getMirror()).getSubpropertiesMirror(); |
2 | 108 |
} |
109 |
||
110 |
abstract class Subproperties { |
|
111 |
private List<JsVariableImpl> properties = null; |
|
112 |
private Map<String, JsVariableImpl> propertyMap = null; |
|
113 |
||
114 |
/** |
|
115 |
* Calls to this method must be synchronized on propertyLock. |
|
116 |
*/ |
|
117 |
private Map<String, JsVariableImpl> ensurePropertyMap() { |
|
118 |
if (propertyMap == null) { |
|
119 |
List<JsVariableImpl> propertiesList = getPropertiesLazily(); |
|
120 |
Map<String, JsVariableImpl> map = |
|
121 |
new HashMap<String, JsVariableImpl>(propertiesList.size() * 2, 0.75f); |
|
122 |
for (JsVariableImpl prop : propertiesList) { |
|
123 |
map.put(prop.getName(), prop); |
|
124 |
} |
|
125 |
propertyMap = Collections.unmodifiableMap(map); |
|
126 |
} |
|
127 |
return propertyMap; |
|
128 |
} |
|
129 |
||
130 |
||
131 |
private List<JsVariableImpl> createPropertiesFromMirror(List<ValueMirror> mirrorProperties, |
|
132 |
List<? extends PropertyReference> propertyRefs) throws MethodIsBlockingException { |
|
133 |
// TODO(peter.rybin) Maybe assert that context is valid here |
|
134 |
||
135 |
List<JsVariableImpl> result = new ArrayList<JsVariableImpl>(mirrorProperties.size()); |
|
136 |
for (int i = 0; i < mirrorProperties.size(); i++) { |
|
137 |
ValueMirror mirror = mirrorProperties.get(i); |
|
276
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
52
diff
changeset
|
138 |
Object varName = propertyRefs.get(i).getName(); |
2 | 139 |
String fqn = getFullyQualifiedName(varName); |
140 |
if (fqn == null) { |
|
141 |
continue; |
|
142 |
} |
|
276
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
52
diff
changeset
|
143 |
String decoratedName = JsVariableImpl.NameDecorator.decorateVarName(varName); |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
52
diff
changeset
|
144 |
result.add(new JsVariableImpl(context, mirror, varName, decoratedName, fqn)); |
2 | 145 |
} |
146 |
return result; |
|
147 |
} |
|
148 |
||
276
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
52
diff
changeset
|
149 |
private String getFullyQualifiedName(Object propName) { |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
52
diff
changeset
|
150 |
if (variableFqn == null) { |
2 | 151 |
return null; |
152 |
} |
|
276
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
52
diff
changeset
|
153 |
if (propName instanceof String) { |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
52
diff
changeset
|
154 |
String propNameStr = (String) propName; |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
52
diff
changeset
|
155 |
if (propNameStr.startsWith(".")) { |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
52
diff
changeset
|
156 |
// ".arguments" is not legal |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
52
diff
changeset
|
157 |
return null; |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
52
diff
changeset
|
158 |
} |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
52
diff
changeset
|
159 |
} |
f2f4a1259de8
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
52
diff
changeset
|
160 |
return variableFqn + JsVariableImpl.NameDecorator.buildAccessSuffix(propName); |
2 | 161 |
} |
162 |
||
163 |
JsVariableImpl getProperty(String propertyName) { |
|
164 |
return ensurePropertyMap().get(propertyName); |
|
165 |
} |
|
166 |
||
167 |
List<JsVariableImpl> getPropertiesLazily() throws MethodIsBlockingException { |
|
168 |
synchronized (this) { |
|
169 |
if (properties == null) { |
|
170 |
||
171 |
List<? extends PropertyReference> propertyRefs = getPropertyRefs(getSubpropertiesMirror()); |
|
52
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
172 |
ValueLoader valueLoader = context.getValueLoader(); |
2 | 173 |
List<ValueMirror> subMirrors = valueLoader.getOrLoadValueFromRefs(propertyRefs); |
174 |
||
175 |
List<JsVariableImpl> wrappedProperties = createPropertiesFromMirror(subMirrors, |
|
176 |
propertyRefs); |
|
177 |
properties = Collections.unmodifiableList(wrappedProperties); |
|
178 |
} |
|
179 |
return properties; |
|
180 |
} |
|
181 |
} |
|
182 |
||
183 |
abstract List<? extends PropertyReference> getPropertyRefs( |
|
184 |
SubpropertiesMirror subpropertiesMirror); |
|
185 |
} |
|
186 |
||
187 |
private final Subproperties subproperties = new Subproperties() { |
|
188 |
@Override |
|
189 |
List<? extends PropertyReference> getPropertyRefs(SubpropertiesMirror subpropertiesMirror) { |
|
190 |
return subpropertiesMirror.getProperties(); |
|
191 |
} |
|
192 |
}; |
|
193 |
||
194 |
private final Subproperties internalProperties = new Subproperties() { |
|
195 |
@Override |
|
196 |
List<? extends PropertyReference> getPropertyRefs(SubpropertiesMirror subpropertiesMirror) { |
|
197 |
return subpropertiesMirror.getInternalProperties(); |
|
198 |
} |
|
199 |
}; |
|
200 |
} |