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 |
|
|
24 |
private final CallFrameImpl callFrame;
|
|
25 |
|
|
26 |
private final String parentFqn;
|
|
27 |
|
|
28 |
/**
|
|
29 |
* This constructor implies the lazy resolution of object properties.
|
|
30 |
*
|
|
31 |
* @param callFrame where this instance belongs in
|
|
32 |
* @param parentFqn the fully qualified name of the object parent
|
|
33 |
* @param valueState the value data from the JS VM
|
|
34 |
*/
|
|
35 |
JsObjectImpl(CallFrameImpl callFrame, String parentFqn, ValueMirror valueState) {
|
|
36 |
super(valueState);
|
|
37 |
this.callFrame = callFrame;
|
|
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 |
|
|
99 |
protected CallFrameImpl getCallFrame() {
|
|
100 |
return callFrame;
|
|
101 |
}
|
|
102 |
|
|
103 |
Subproperties getSubpropertiesHelper() {
|
|
104 |
return subproperties;
|
|
105 |
}
|
|
106 |
|
|
107 |
protected SubpropertiesMirror getSubpropertiesMirror() {
|
|
108 |
ValueLoader valueLoader = callFrame.getInternalContext().getValueLoader();
|
|
109 |
|
|
110 |
return valueLoader.loadSubpropertiesInMirror(getMirror()).getSubpropertiesMirror();
|
|
111 |
}
|
|
112 |
|
|
113 |
abstract class Subproperties {
|
|
114 |
private List<JsVariableImpl> properties = null;
|
|
115 |
private Map<String, JsVariableImpl> propertyMap = null;
|
|
116 |
|
|
117 |
/**
|
|
118 |
* Calls to this method must be synchronized on propertyLock.
|
|
119 |
*/
|
|
120 |
private Map<String, JsVariableImpl> ensurePropertyMap() {
|
|
121 |
if (propertyMap == null) {
|
|
122 |
List<JsVariableImpl> propertiesList = getPropertiesLazily();
|
|
123 |
Map<String, JsVariableImpl> map =
|
|
124 |
new HashMap<String, JsVariableImpl>(propertiesList.size() * 2, 0.75f);
|
|
125 |
for (JsVariableImpl prop : propertiesList) {
|
|
126 |
map.put(prop.getName(), prop);
|
|
127 |
}
|
|
128 |
propertyMap = Collections.unmodifiableMap(map);
|
|
129 |
}
|
|
130 |
return propertyMap;
|
|
131 |
}
|
|
132 |
|
|
133 |
|
|
134 |
private List<JsVariableImpl> createPropertiesFromMirror(List<ValueMirror> mirrorProperties,
|
|
135 |
List<? extends PropertyReference> propertyRefs) throws MethodIsBlockingException {
|
|
136 |
// TODO(peter.rybin) Maybe assert that context is valid here
|
|
137 |
|
|
138 |
List<JsVariableImpl> result = new ArrayList<JsVariableImpl>(mirrorProperties.size());
|
|
139 |
for (int i = 0; i < mirrorProperties.size(); i++) {
|
|
140 |
ValueMirror mirror = mirrorProperties.get(i);
|
|
141 |
String varName = propertyRefs.get(i).getName();
|
|
142 |
String fqn = getFullyQualifiedName(varName);
|
|
143 |
if (fqn == null) {
|
|
144 |
continue;
|
|
145 |
}
|
|
146 |
result.add(new JsVariableImpl(callFrame, mirror, varName, fqn,
|
|
147 |
getNameDecorator()));
|
|
148 |
}
|
|
149 |
return result;
|
|
150 |
}
|
|
151 |
|
|
152 |
private String getFullyQualifiedName(String propName) {
|
|
153 |
if (propName.startsWith(".")) {
|
|
154 |
// ".arguments" is not legal
|
|
155 |
return null;
|
|
156 |
}
|
|
157 |
return parentFqn + getNameDecorator().buildAccessSuffix(propName);
|
|
158 |
}
|
|
159 |
|
|
160 |
JsVariableImpl getProperty(String propertyName) {
|
|
161 |
return ensurePropertyMap().get(propertyName);
|
|
162 |
}
|
|
163 |
|
|
164 |
List<JsVariableImpl> getPropertiesLazily() throws MethodIsBlockingException {
|
|
165 |
synchronized (this) {
|
|
166 |
if (properties == null) {
|
|
167 |
|
|
168 |
List<? extends PropertyReference> propertyRefs = getPropertyRefs(getSubpropertiesMirror());
|
|
169 |
ValueLoader valueLoader = callFrame.getInternalContext().getValueLoader();
|
|
170 |
List<ValueMirror> subMirrors = valueLoader.getOrLoadValueFromRefs(propertyRefs);
|
|
171 |
|
|
172 |
List<JsVariableImpl> wrappedProperties = createPropertiesFromMirror(subMirrors,
|
|
173 |
propertyRefs);
|
|
174 |
properties = Collections.unmodifiableList(wrappedProperties);
|
|
175 |
}
|
|
176 |
return properties;
|
|
177 |
}
|
|
178 |
}
|
|
179 |
|
|
180 |
abstract JsVariableImpl.NameDecorator getNameDecorator();
|
|
181 |
abstract List<? extends PropertyReference> getPropertyRefs(
|
|
182 |
SubpropertiesMirror subpropertiesMirror);
|
|
183 |
}
|
|
184 |
|
|
185 |
private final Subproperties subproperties = new Subproperties() {
|
|
186 |
@Override
|
|
187 |
JsVariableImpl.NameDecorator getNameDecorator() {
|
|
188 |
return getChildPropertyNameDecorator();
|
|
189 |
}
|
|
190 |
@Override
|
|
191 |
List<? extends PropertyReference> getPropertyRefs(SubpropertiesMirror subpropertiesMirror) {
|
|
192 |
return subpropertiesMirror.getProperties();
|
|
193 |
}
|
|
194 |
};
|
|
195 |
|
|
196 |
private final Subproperties internalProperties = new Subproperties() {
|
|
197 |
@Override
|
|
198 |
JsVariableImpl.NameDecorator getNameDecorator() {
|
|
199 |
return JsVariableImpl.NameDecorator.NOOP;
|
|
200 |
}
|
|
201 |
@Override
|
|
202 |
List<? extends PropertyReference> getPropertyRefs(SubpropertiesMirror subpropertiesMirror) {
|
|
203 |
return subpropertiesMirror.getInternalProperties();
|
|
204 |
}
|
|
205 |
};
|
|
206 |
}
|