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 |
||
8 |
import org.chromium.sdk.JsVariable; |
|
9 |
import org.chromium.sdk.JsValue.Type; |
|
10 |
||
11 |
/** |
|
12 |
* A generic implementation of the JsVariable interface. |
|
13 |
*/ |
|
14 |
public class JsVariableImpl implements JsVariable { |
|
15 |
||
16 |
/** |
|
17 |
* The variable value data as reported by the JavaScript VM (is used to |
|
18 |
* construct the variable value.) |
|
19 |
*/ |
|
20 |
private final ValueMirror valueData; |
|
21 |
||
52
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
22 |
/** The context this variable belongs in. */ |
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
23 |
private final InternalContext context; |
2 | 24 |
|
25 |
/** The fully qualified name of this variable. */ |
|
26 |
private final String variableFqn; |
|
27 |
||
28 |
private final NameDecorator nameDecorator; |
|
29 |
||
30 |
/** The lazily constructed value of this variable. */ |
|
31 |
private final JsValueImpl value; |
|
32 |
||
33 |
/** Variable name. */ |
|
34 |
private final String rawName; |
|
35 |
||
36 |
/** |
|
52
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
37 |
* Constructs a variable contained in the given context with the given |
2 | 38 |
* value mirror. |
39 |
* |
|
52
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
40 |
* @param context that owns this variable |
2 | 41 |
* @param valueData value data for this variable |
42 |
*/ |
|
52
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
43 |
JsVariableImpl(InternalContext context, ValueMirror valueData, String name) { |
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
44 |
this(context, valueData, name, null, NameDecorator.NOOP); |
2 | 45 |
} |
46 |
||
47 |
/** |
|
52
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
48 |
* Constructs a variable contained in the given context with the given |
2 | 49 |
* value mirror. |
50 |
* |
|
52
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
51 |
* @param context that owns this variable |
2 | 52 |
* @param valueData for this variable |
53 |
* @param variableFqn the fully qualified name of this variable |
|
54 |
*/ |
|
52
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
55 |
JsVariableImpl(InternalContext context, ValueMirror valueData, String name, String variableFqn, |
2 | 56 |
NameDecorator nameDecorator) { |
52
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
57 |
this.context = context; |
2 | 58 |
this.valueData = valueData; |
59 |
this.rawName = name; |
|
60 |
this.variableFqn = variableFqn; |
|
61 |
this.nameDecorator = nameDecorator; |
|
62 |
||
63 |
Type type = this.valueData.getType(); |
|
64 |
switch (type) { |
|
65 |
case TYPE_FUNCTION: |
|
52
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
66 |
this.value = new JsFunctionImpl(context, this.variableFqn, this.valueData); |
2 | 67 |
break; |
68 |
case TYPE_ERROR: |
|
69 |
case TYPE_OBJECT: |
|
52
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
70 |
this.value = new JsObjectImpl(context, this.variableFqn, this.valueData); |
2 | 71 |
break; |
72 |
case TYPE_ARRAY: |
|
52
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
73 |
this.value = new JsArrayImpl(context, this.variableFqn, this.valueData); |
2 | 74 |
break; |
75 |
default: |
|
76 |
this.value = new JsValueImpl(this.valueData); |
|
77 |
} |
|
78 |
} |
|
79 |
||
80 |
/** |
|
81 |
* @return a [probably compound] JsValue corresponding to this variable. |
|
82 |
* {@code null} if there was an error lazy-loading the value data. |
|
83 |
*/ |
|
84 |
public JsValueImpl getValue() { |
|
85 |
return value; |
|
86 |
} |
|
87 |
||
88 |
public String getName() { |
|
89 |
return nameDecorator.decorateVarName(rawName); |
|
90 |
} |
|
91 |
||
92 |
public String getRawName() { |
|
93 |
return this.rawName; |
|
94 |
} |
|
95 |
||
96 |
public boolean isMutable() { |
|
97 |
return false; // TODO(apavlov): fix once V8 supports it |
|
98 |
} |
|
99 |
||
100 |
public boolean isReadable() { |
|
101 |
// TODO(apavlov): implement once the readability metadata are available |
|
102 |
return true; |
|
103 |
} |
|
104 |
||
105 |
public synchronized void setValue(String newValue, SetValueCallback callback) { |
|
106 |
// TODO(apavlov): currently V8 does not support it |
|
107 |
if (!isMutable()) { |
|
108 |
throw new UnsupportedOperationException(); |
|
109 |
} |
|
110 |
} |
|
111 |
||
112 |
@Override |
|
113 |
public String toString() { |
|
114 |
return new StringBuilder() |
|
115 |
.append("[JsVariable: name=") |
|
116 |
.append(getName()) |
|
117 |
.append(",value=") |
|
118 |
.append(getValue()) |
|
119 |
.append(']') |
|
120 |
.toString(); |
|
121 |
} |
|
122 |
||
123 |
/** |
|
52
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
124 |
* Returns the context owning this variable. |
2 | 125 |
*/ |
52
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
126 |
protected InternalContext getInternalContext() { |
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
127 |
return context; |
2 | 128 |
} |
129 |
||
130 |
public ValueMirror getMirror() { |
|
131 |
return valueData; |
|
132 |
} |
|
133 |
||
134 |
public String getFullyQualifiedName() { |
|
135 |
return variableFqn != null |
|
136 |
? variableFqn |
|
137 |
: getName(); |
|
138 |
} |
|
139 |
||
140 |
static abstract class NameDecorator { |
|
141 |
static final NameDecorator NOOP = new NameDecorator() { |
|
142 |
@Override |
|
143 |
String decorateVarName(String rawName) { |
|
144 |
return rawName; |
|
145 |
} |
|
146 |
@Override |
|
147 |
String buildAccessSuffix(String rawName) { |
|
148 |
return "." + rawName; |
|
149 |
} |
|
150 |
}; |
|
151 |
abstract String decorateVarName(String rawName); |
|
152 |
abstract String buildAccessSuffix(String rawName); |
|
153 |
} |
|
154 |
} |