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