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.Collections;
|
|
8 |
|
|
9 |
import org.chromium.sdk.Breakpoint;
|
|
10 |
import org.chromium.sdk.DebugEventListener;
|
|
11 |
import org.chromium.sdk.InvalidContextException;
|
|
12 |
import org.chromium.sdk.SyncCallback;
|
|
13 |
import org.chromium.sdk.Version;
|
|
14 |
import org.chromium.sdk.JavascriptVm.ScriptsCallback;
|
|
15 |
import org.chromium.sdk.JavascriptVm.SuspendCallback;
|
|
16 |
import org.chromium.sdk.internal.InternalContext.ContextDismissedCheckedException;
|
|
17 |
import org.chromium.sdk.internal.protocol.CommandResponse;
|
|
18 |
import org.chromium.sdk.internal.protocol.SuccessCommandResponse;
|
|
19 |
import org.chromium.sdk.internal.tools.v8.BreakpointManager;
|
|
20 |
import org.chromium.sdk.internal.tools.v8.DefaultResponseHandler;
|
|
21 |
import org.chromium.sdk.internal.tools.v8.V8BlockingCallback;
|
|
22 |
import org.chromium.sdk.internal.tools.v8.V8CommandOutput;
|
|
23 |
import org.chromium.sdk.internal.tools.v8.V8CommandProcessor;
|
|
24 |
import org.chromium.sdk.internal.tools.v8.V8Helper;
|
|
25 |
import org.chromium.sdk.internal.tools.v8.V8ProtocolUtil;
|
|
26 |
import org.chromium.sdk.internal.tools.v8.V8CommandProcessor.V8HandlerCallback;
|
|
27 |
import org.chromium.sdk.internal.tools.v8.request.ContextlessDebuggerMessage;
|
|
28 |
import org.chromium.sdk.internal.tools.v8.request.DebuggerMessageFactory;
|
|
29 |
|
|
30 |
/**
|
|
31 |
* A default, thread-safe implementation of the JsDebugContext interface.
|
|
32 |
*/
|
|
33 |
public class DebugSession {
|
|
34 |
|
|
35 |
/** The script manager for the associated tab. */
|
|
36 |
private final ScriptManager scriptManager;
|
|
37 |
|
|
38 |
private final V8CommandProcessor v8CommandProcessor;
|
|
39 |
|
|
40 |
/** A helper for performing complex V8-related actions. */
|
|
41 |
private final V8Helper v8Helper = new V8Helper(this);
|
|
42 |
|
|
43 |
private final ContextBuilder contextBuilder;
|
|
44 |
|
|
45 |
/** Our manager. */
|
|
46 |
private DebugSessionManager sessionManager;
|
|
47 |
|
|
48 |
/** Context owns breakpoint manager. */
|
|
49 |
private final BreakpointManager breakpointManager;
|
|
50 |
|
|
51 |
private final ScriptLoader scriptLoader = new ScriptLoader();
|
|
52 |
|
|
53 |
private final DefaultResponseHandler defaultResponseHandler;
|
|
54 |
|
|
55 |
public DebugSession(DebugSessionManager sessionManager, V8ContextFilter contextFilter,
|
|
56 |
V8CommandOutput v8CommandOutput) {
|
|
57 |
this.scriptManager = new ScriptManager(contextFilter);
|
|
58 |
this.sessionManager = sessionManager;
|
|
59 |
this.breakpointManager = new BreakpointManager(this);
|
|
60 |
|
|
61 |
this.defaultResponseHandler = new DefaultResponseHandler(this);
|
|
62 |
this.v8CommandProcessor = new V8CommandProcessor(v8CommandOutput, defaultResponseHandler);
|
|
63 |
this.contextBuilder = new ContextBuilder(this);
|
|
64 |
}
|
|
65 |
|
|
66 |
public ScriptManager getScriptManager() {
|
|
67 |
return scriptManager;
|
|
68 |
}
|
|
69 |
|
|
70 |
public V8CommandProcessor getV8CommandProcessor() {
|
|
71 |
return v8CommandProcessor;
|
|
72 |
}
|
|
73 |
|
|
74 |
public DebugSessionManager getSessionManager() {
|
|
75 |
return sessionManager;
|
|
76 |
}
|
|
77 |
|
|
78 |
public void onDebuggerDetached() {
|
|
79 |
getSessionManager().onDebuggerDetached();
|
|
80 |
getScriptManager().reset();
|
|
81 |
contextBuilder.forceCancelContext();
|
|
82 |
}
|
|
83 |
|
|
84 |
/**
|
|
85 |
* Sends V8 command messages, but only those which doesn't depend on context.
|
|
86 |
* Use {@code InternalContext} if you need to send context-specific commands.
|
|
87 |
*/
|
|
88 |
public void sendMessageAsync(ContextlessDebuggerMessage message, boolean isImmediate,
|
|
89 |
V8CommandProcessor.V8HandlerCallback commandCallback, SyncCallback syncCallback) {
|
|
90 |
v8CommandProcessor.sendV8CommandAsync(message, isImmediate,
|
|
91 |
commandCallback, syncCallback);
|
|
92 |
}
|
|
93 |
|
|
94 |
/**
|
|
95 |
* Gets invoked when a navigation event is reported by the browser tab.
|
|
96 |
*/
|
|
97 |
public void navigated() {
|
|
98 |
getScriptManager().reset();
|
|
99 |
}
|
|
100 |
|
|
101 |
/**
|
|
102 |
* @return the DebugEventListener associated with this context
|
|
103 |
*/
|
|
104 |
public DebugEventListener getDebugEventListener() {
|
|
105 |
return getSessionManager().getDebugEventListener();
|
|
106 |
}
|
|
107 |
|
|
108 |
public BreakpointManager getBreakpointManager() {
|
|
109 |
return breakpointManager;
|
|
110 |
}
|
|
111 |
|
|
112 |
public ScriptLoader getScriptLoader() {
|
|
113 |
return scriptLoader;
|
|
114 |
}
|
|
115 |
|
|
116 |
public V8Helper getV8Helper() {
|
|
117 |
return v8Helper;
|
|
118 |
}
|
|
119 |
|
|
120 |
public ContextBuilder getContextBuilder() {
|
|
121 |
return contextBuilder;
|
|
122 |
}
|
|
123 |
|
|
124 |
public void suspend(final SuspendCallback suspendCallback) {
|
|
125 |
V8CommandProcessor.V8HandlerCallback v8Callback = new V8CommandProcessor.V8HandlerCallback() {
|
|
126 |
public void failure(String message) {
|
|
127 |
if (suspendCallback != null) {
|
|
128 |
suspendCallback.failure(new Exception(message));
|
|
129 |
}
|
|
130 |
}
|
|
131 |
public void messageReceived(CommandResponse response) {
|
|
132 |
SuccessCommandResponse successResponse = response.asSuccess();
|
|
133 |
if (successResponse == null) {
|
|
134 |
if (suspendCallback != null) {
|
|
135 |
suspendCallback.failure(new Exception("Unsuccessful command"));
|
|
136 |
}
|
|
137 |
return;
|
|
138 |
}
|
|
139 |
if (suspendCallback != null) {
|
|
140 |
suspendCallback.success();
|
|
141 |
}
|
|
142 |
|
|
143 |
ContextBuilder.ExpectingBreakEventStep step1 = contextBuilder.buildNewContextWhenIdle();
|
|
144 |
if (step1 == null) {
|
|
145 |
return;
|
|
146 |
}
|
|
147 |
ContextBuilder.ExpectingBacktraceStep step2 =
|
|
148 |
step1.setContextState(Collections.<Breakpoint>emptyList(), null);
|
|
149 |
defaultResponseHandler.getBreakpointProcessor().processNextStep(step2);
|
|
150 |
}
|
|
151 |
};
|
|
152 |
sendMessageAsync(DebuggerMessageFactory.suspend(), true, v8Callback, null);
|
|
153 |
}
|
|
154 |
|
|
155 |
public class ScriptLoader {
|
|
156 |
|
|
157 |
/** Whether the initial script loading has completed. */
|
|
158 |
private volatile boolean doneInitialScriptLoad = false;
|
|
159 |
|
|
160 |
/**
|
|
161 |
* Loads all scripts from the remote if necessary, and feeds them into the
|
|
162 |
* callback provided (if any).
|
|
163 |
*
|
|
164 |
* @param callback nullable callback to invoke when the scripts are ready
|
|
165 |
*/
|
|
166 |
public void loadAllScripts(final ScriptsCallback callback, SyncCallback syncCallback) {
|
|
167 |
if (!doneInitialScriptLoad) {
|
|
168 |
this.doneInitialScriptLoad = true;
|
|
169 |
// Not loaded the scripts initially, do full load.
|
|
170 |
v8Helper.reloadAllScriptsAsync(new V8HandlerCallback() {
|
|
171 |
public void messageReceived(CommandResponse response) {
|
|
172 |
if (callback != null) {
|
|
173 |
SuccessCommandResponse successResponse = response.asSuccess();
|
|
174 |
if (successResponse != null) {
|
|
175 |
callback.success(getScriptManager().allScripts());
|
|
176 |
} else {
|
|
177 |
callback.failure(response.asFailure().getMessage());
|
|
178 |
}
|
|
179 |
}
|
|
180 |
}
|
|
181 |
|
|
182 |
public void failure(String message) {
|
|
183 |
if (callback != null) {
|
|
184 |
callback.failure(message);
|
|
185 |
}
|
|
186 |
}
|
|
187 |
}, syncCallback);
|
|
188 |
} else {
|
|
189 |
try {
|
|
190 |
if (callback != null) {
|
|
191 |
callback.success(getScriptManager().allScripts());
|
|
192 |
}
|
|
193 |
} finally {
|
|
194 |
if (syncCallback != null) {
|
|
195 |
syncCallback.callbackDone(null);
|
|
196 |
}
|
|
197 |
}
|
|
198 |
}
|
|
199 |
}
|
|
200 |
}
|
|
201 |
|
|
202 |
/**
|
|
203 |
* Checks version of V8 and check if it in running state.
|
|
204 |
*/
|
|
205 |
public void startCommunication() {
|
|
206 |
V8BlockingCallback<Void> callback = new V8BlockingCallback<Void>() {
|
|
207 |
@Override
|
|
208 |
public Void messageReceived(CommandResponse response) {
|
|
209 |
SuccessCommandResponse successResponse = response.asSuccess();
|
|
210 |
if (successResponse == null) {
|
|
211 |
return null;
|
|
212 |
}
|
|
213 |
Version vmVersion = V8ProtocolUtil.parseVersionResponse(successResponse);
|
|
214 |
|
|
215 |
if (V8VersionMilestones.isRunningAccurate(vmVersion)) {
|
|
216 |
Boolean running = successResponse.running();
|
|
217 |
if (running == Boolean.FALSE) {
|
|
218 |
ContextBuilder.ExpectingBreakEventStep step1 = contextBuilder.buildNewContextWhenIdle();
|
|
219 |
// If step is not null -- we are already in process of building a context.
|
|
220 |
if (step1 != null) {
|
|
221 |
ContextBuilder.ExpectingBacktraceStep step2 =
|
|
222 |
step1.setContextState(Collections.<Breakpoint>emptyList(), null);
|
|
223 |
|
|
224 |
defaultResponseHandler.getBreakpointProcessor().processNextStep(step2);
|
|
225 |
}
|
|
226 |
}
|
|
227 |
}
|
|
228 |
return null;
|
|
229 |
}
|
|
230 |
|
|
231 |
@Override
|
|
232 |
protected Void handleSuccessfulResponse(SuccessCommandResponse response) {
|
|
233 |
throw new UnsupportedOperationException();
|
|
234 |
}
|
|
235 |
};
|
|
236 |
|
|
237 |
V8Helper.callV8Sync(this.v8CommandProcessor, DebuggerMessageFactory.version(), callback);
|
|
238 |
}
|
|
239 |
|
|
240 |
public void maybeRethrowContextException(ContextDismissedCheckedException e) {
|
|
241 |
// TODO(peter.rybin): make some kind of option out of this
|
|
242 |
final boolean strictPolicy = true;
|
|
243 |
if (strictPolicy) {
|
|
244 |
throw new InvalidContextException(e);
|
|
245 |
}
|
|
246 |
}
|
|
247 |
}
|