|
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.symbian.tools.tmw.debug.internal.model; |
|
6 |
|
7 import org.chromium.debug.core.model.DebugElementImpl; |
|
8 import org.chromium.debug.core.model.EvaluateContext; |
|
9 import org.chromium.debug.core.model.Variable; |
|
10 import org.chromium.sdk.JsEvaluateContext; |
|
11 import org.chromium.sdk.JsVariable; |
|
12 import org.eclipse.core.runtime.Status; |
|
13 import org.eclipse.debug.core.DebugException; |
|
14 import org.eclipse.debug.core.model.IDebugElement; |
|
15 import org.eclipse.debug.core.model.IValue; |
|
16 import org.eclipse.debug.core.model.IWatchExpressionDelegate; |
|
17 import org.eclipse.debug.core.model.IWatchExpressionListener; |
|
18 import org.eclipse.debug.core.model.IWatchExpressionResult; |
|
19 import org.symbian.tools.tmw.debug.internal.Activator; |
|
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 |
|
137 final EvaluateContext evaluateContext = |
|
138 (EvaluateContext) contextImpl.getAdapter(EvaluateContext.class); |
|
139 if (evaluateContext == null) { |
|
140 listener.watchEvaluationFinished(new BadWatchExpressionResult( |
|
141 new DebugException( |
|
142 new Status(Status.ERROR, Activator.PLUGIN_ID, "Bad debug context")), //$NON-NLS-1$ |
|
143 expression)); |
|
144 return; |
|
145 } |
|
146 |
|
147 evaluateContext.getJsEvaluateContext().evaluateAsync( |
|
148 expression, |
|
149 new JsEvaluateContext.EvaluateCallback() { |
|
150 public void success(JsVariable variable) { |
|
151 final Variable var = new Variable(contextImpl.getDebugTarget(), variable, false); |
|
152 listener.watchEvaluationFinished(new GoodWatchExpressionResult(var, expression)); |
|
153 } |
|
154 |
|
155 public void failure(String message) { |
|
156 listener.watchEvaluationFinished(new BadWatchExpressionResult(new DebugException( |
|
157 createErrorStatus(message == null |
|
158 ? "Error evaluating expression" |
|
159 : message, null)), expression)); |
|
160 return; |
|
161 } |
|
162 }, |
|
163 null); |
|
164 } |
|
165 |
|
166 private static Status createErrorStatus(String message, Exception e) { |
|
167 return new Status(Status.ERROR, Activator.PLUGIN_ID, message, e); |
|
168 } |
|
169 |
|
170 } |