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.debug.ui;
|
|
6 |
|
|
7 |
import org.chromium.debug.core.ChromiumDebugPlugin;
|
|
8 |
import org.chromium.debug.core.model.DebugElementImpl;
|
|
9 |
import org.chromium.debug.core.model.StackFrame;
|
|
10 |
import org.chromium.debug.core.model.Variable;
|
|
11 |
import org.chromium.sdk.CallFrame;
|
|
12 |
import org.chromium.sdk.JsVariable;
|
|
13 |
import org.eclipse.core.runtime.Status;
|
|
14 |
import org.eclipse.debug.core.DebugException;
|
|
15 |
import org.eclipse.debug.core.model.IDebugElement;
|
|
16 |
import org.eclipse.debug.core.model.IValue;
|
|
17 |
import org.eclipse.debug.core.model.IWatchExpressionDelegate;
|
|
18 |
import org.eclipse.debug.core.model.IWatchExpressionListener;
|
|
19 |
import org.eclipse.debug.core.model.IWatchExpressionResult;
|
|
20 |
|
|
21 |
/**
|
|
22 |
* Performs the Watch expression evaluation while debugging Chromium JavaScript.
|
|
23 |
*/
|
|
24 |
public class JsWatchExpressionDelegate implements IWatchExpressionDelegate {
|
|
25 |
|
|
26 |
private static final String[] EMPTY_STRINGS = new String[0];
|
|
27 |
|
|
28 |
private static final class GoodWatchExpressionResult implements IWatchExpressionResult {
|
|
29 |
|
|
30 |
private final Variable variable;
|
|
31 |
|
|
32 |
private final String expression;
|
|
33 |
|
|
34 |
private IValue value;
|
|
35 |
|
|
36 |
private DebugException exception;
|
|
37 |
|
|
38 |
private GoodWatchExpressionResult(Variable variable, String expression) {
|
|
39 |
this.variable = variable;
|
|
40 |
this.expression = expression;
|
|
41 |
}
|
|
42 |
|
|
43 |
public String[] getErrorMessages() {
|
|
44 |
return exception == null
|
|
45 |
? EMPTY_STRINGS
|
|
46 |
: new String[] { exception.getStatus().getMessage() };
|
|
47 |
}
|
|
48 |
|
|
49 |
public DebugException getException() {
|
|
50 |
getValue();
|
|
51 |
return exception;
|
|
52 |
}
|
|
53 |
|
|
54 |
public String getExpressionText() {
|
|
55 |
return expression;
|
|
56 |
}
|
|
57 |
|
|
58 |
public synchronized IValue getValue() {
|
|
59 |
if (value == null && exception == null) {
|
|
60 |
try {
|
|
61 |
value = variable.getValue();
|
|
62 |
} catch (DebugException e) {
|
|
63 |
this.exception = e;
|
|
64 |
}
|
|
65 |
}
|
|
66 |
return value;
|
|
67 |
}
|
|
68 |
|
|
69 |
public boolean hasErrors() {
|
|
70 |
getValue();
|
|
71 |
return exception != null;
|
|
72 |
}
|
|
73 |
}
|
|
74 |
|
|
75 |
private static final class BadWatchExpressionResult implements IWatchExpressionResult {
|
|
76 |
|
|
77 |
private final DebugException exception;
|
|
78 |
|
|
79 |
private final String expressionText;
|
|
80 |
|
|
81 |
private BadWatchExpressionResult(DebugException exception, String expressionText) {
|
|
82 |
this.exception = exception;
|
|
83 |
this.expressionText = expressionText;
|
|
84 |
}
|
|
85 |
|
|
86 |
public String[] getErrorMessages() {
|
|
87 |
return new String[] { exception.getStatus().getMessage() };
|
|
88 |
}
|
|
89 |
|
|
90 |
public DebugException getException() {
|
|
91 |
return exception;
|
|
92 |
}
|
|
93 |
|
|
94 |
public String getExpressionText() {
|
|
95 |
return expressionText;
|
|
96 |
}
|
|
97 |
|
|
98 |
public IValue getValue() {
|
|
99 |
return null;
|
|
100 |
}
|
|
101 |
|
|
102 |
public boolean hasErrors() {
|
|
103 |
return true;
|
|
104 |
}
|
|
105 |
}
|
|
106 |
|
|
107 |
public void evaluateExpression(final String expression, final IDebugElement context,
|
|
108 |
final IWatchExpressionListener listener) {
|
|
109 |
final DebugElementImpl contextImpl = (DebugElementImpl) context;
|
|
110 |
if (!contextImpl.getDebugTarget().isSuspended()) {
|
|
111 |
// can only evaluate while suspended. Notify empty result.
|
|
112 |
listener.watchEvaluationFinished(new IWatchExpressionResult() {
|
|
113 |
|
|
114 |
public String[] getErrorMessages() {
|
|
115 |
return EMPTY_STRINGS;
|
|
116 |
}
|
|
117 |
|
|
118 |
public DebugException getException() {
|
|
119 |
return null;
|
|
120 |
}
|
|
121 |
|
|
122 |
public String getExpressionText() {
|
|
123 |
return expression;
|
|
124 |
}
|
|
125 |
|
|
126 |
public IValue getValue() {
|
|
127 |
return null;
|
|
128 |
}
|
|
129 |
|
|
130 |
public boolean hasErrors() {
|
|
131 |
return false;
|
|
132 |
}
|
|
133 |
});
|
|
134 |
return;
|
|
135 |
}
|
|
136 |
if (!(contextImpl instanceof StackFrame)) {
|
|
137 |
listener.watchEvaluationFinished(new BadWatchExpressionResult(
|
|
138 |
new DebugException(
|
|
139 |
new Status(Status.ERROR, ChromiumDebugUIPlugin.PLUGIN_ID, "Bad debug context")), //$NON-NLS-1$
|
|
140 |
expression));
|
|
141 |
return;
|
|
142 |
}
|
|
143 |
StackFrame stackFrame = (StackFrame) contextImpl;
|
|
144 |
final CallFrame frame = stackFrame.getCallFrame();
|
|
145 |
frame.evaluateAsync(expression, new CallFrame.EvaluateCallback() {
|
|
146 |
public void success(JsVariable variable) {
|
|
147 |
final Variable var = new Variable(contextImpl.getDebugTarget(), variable, false);
|
|
148 |
listener.watchEvaluationFinished(new GoodWatchExpressionResult(var, expression));
|
|
149 |
}
|
|
150 |
|
|
151 |
public void failure(String message) {
|
|
152 |
listener.watchEvaluationFinished(new BadWatchExpressionResult(new DebugException(
|
|
153 |
createErrorStatus(message == null
|
|
154 |
? Messages.JsWatchExpressionDelegate_ErrorEvaluatingExpression
|
|
155 |
: message, null)), expression));
|
|
156 |
return;
|
|
157 |
}
|
|
158 |
},
|
|
159 |
null);
|
|
160 |
}
|
|
161 |
|
|
162 |
private static Status createErrorStatus(String message, Exception e) {
|
|
163 |
return new Status(Status.ERROR, ChromiumDebugPlugin.PLUGIN_ID, message, e);
|
|
164 |
}
|
|
165 |
|
|
166 |
}
|