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